From 5345cf0342a30970c57fa2e8d89df67b11626e9c Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 10 Dec 2019 13:39:08 +0100 Subject: [PATCH] Updated date and time comparison --- app/models/repository_date_range_value.rb | 6 +++--- app/models/repository_date_time_range_value.rb | 6 +++--- .../repository_date_time_range_value_base.rb | 14 +++----------- app/models/repository_date_time_value.rb | 7 ++++--- app/models/repository_date_time_value_base.rb | 3 +-- app/models/repository_date_value.rb | 7 ++++--- app/models/repository_time_range_value.rb | 6 +++--- app/models/repository_time_value.rb | 7 ++++--- 8 files changed, 25 insertions(+), 31 deletions(-) diff --git a/app/models/repository_date_range_value.rb b/app/models/repository_date_range_value.rb index ada4f3d6e..3195cb257 100644 --- a/app/models/repository_date_range_value.rb +++ b/app/models/repository_date_range_value.rb @@ -4,11 +4,11 @@ class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase def data_changed?(new_data) st = Time.zone.parse(new_data[:start_time]) et = Time.zone.parse(new_data[:end_time]) - formatted != formatted(new_dates: [st, et]) + st.to_date != start_time.to_date || et.to_date != end_time.to_date end - def formatted(new_dates: nil) - super(:full_date, new_dates: new_dates) + def formatted + super(:full_date) end def self.new_with_payload(payload, attributes) diff --git a/app/models/repository_date_time_range_value.rb b/app/models/repository_date_time_range_value.rb index b92c848de..73358fa3b 100644 --- a/app/models/repository_date_time_range_value.rb +++ b/app/models/repository_date_time_range_value.rb @@ -4,11 +4,11 @@ class RepositoryDateTimeRangeValue < RepositoryDateTimeRangeValueBase def data_changed?(new_data) st = Time.zone.parse(new_data[:start_time]) et = Time.zone.parse(new_data[:end_time]) - formatted != formatted(new_dates: [st, et]) + st.to_i != start_time.to_i || et.to_i != end_time.to_i end - def formatted(new_dates: nil) - super(:full_with_comma, new_dates: new_dates) + def formatted + super(:full_with_comma) end def self.new_with_payload(payload, attributes) diff --git a/app/models/repository_date_time_range_value_base.rb b/app/models/repository_date_time_range_value_base.rb index 0678ad845..3be90358f 100644 --- a/app/models/repository_date_time_range_value_base.rb +++ b/app/models/repository_date_time_range_value_base.rb @@ -30,18 +30,10 @@ class RepositoryDateTimeRangeValueBase < ApplicationRecord private - def formatted(format, new_dates: nil) - if new_dates&.any? - st = new_dates[0] - et = new_dates[1] - else - st = start_time - et = end_time - end - + def formatted(format) [ - I18n.l(st, format: format), - I18n.l(et, format: format) + I18n.l(start_time, format: format), + I18n.l(end_time, format: format) ].compact.join(' - ') end end diff --git a/app/models/repository_date_time_value.rb b/app/models/repository_date_time_value.rb index fc7826e22..f1e42f7ec 100644 --- a/app/models/repository_date_time_value.rb +++ b/app/models/repository_date_time_value.rb @@ -2,11 +2,12 @@ class RepositoryDateTimeValue < RepositoryDateTimeValueBase def data_changed?(new_data) - formatted != formatted(new_date: new_data) + new_time = Time.zone.parse(new_data) + new_time.to_i != data.to_i end - def formatted(new_date: nil) - super(:full_with_comma, new_date: new_date) + def formatted + super(:full_with_comma) end def self.new_with_payload(payload, attributes) diff --git a/app/models/repository_date_time_value_base.rb b/app/models/repository_date_time_value_base.rb index d93ac0159..305f1e219 100644 --- a/app/models/repository_date_time_value_base.rb +++ b/app/models/repository_date_time_value_base.rb @@ -25,8 +25,7 @@ class RepositoryDateTimeValueBase < ApplicationRecord private - def formatted(format, new_date: nil) - d = new_date ? Time.zone.parse(new_date) : data + def formatted(format) I18n.l(d, format: format) end end diff --git a/app/models/repository_date_value.rb b/app/models/repository_date_value.rb index ea72ea217..3e9eb30fb 100644 --- a/app/models/repository_date_value.rb +++ b/app/models/repository_date_value.rb @@ -2,11 +2,12 @@ class RepositoryDateValue < RepositoryDateTimeValueBase def data_changed?(new_data) - formatted != formatted(new_date: new_data) + new_time = Time.zone.parse(new_data) + new_time.to_date != data.to_date end - def formatted(new_date: nil) - super(:full_date, new_date: new_date) + def formatted + super(:full_date) end def self.new_with_payload(payload, attributes) diff --git a/app/models/repository_time_range_value.rb b/app/models/repository_time_range_value.rb index 8a56e4a7e..16a8b25f8 100644 --- a/app/models/repository_time_range_value.rb +++ b/app/models/repository_time_range_value.rb @@ -4,11 +4,11 @@ class RepositoryTimeRangeValue < RepositoryDateTimeRangeValueBase def data_changed?(new_data) st = Time.zone.parse(new_data[:start_time]) et = Time.zone.parse(new_data[:end_time]) - formatted != formatted(new_dates: [st, et]) + st.hour != start_time.hour || et.hour != end_time.hour || st.min != start_time.min || et.min != end_time.min end - def formatted(new_dates: nil) - super(:time, new_dates: new_dates) + def formatted + super(:time) end def self.new_with_payload(payload, attributes) diff --git a/app/models/repository_time_value.rb b/app/models/repository_time_value.rb index 9df2dacea..835621622 100644 --- a/app/models/repository_time_value.rb +++ b/app/models/repository_time_value.rb @@ -2,11 +2,12 @@ class RepositoryTimeValue < RepositoryDateTimeValueBase def data_changed?(new_data) - formatted != formatted(new_date: new_data) + new_time = Time.zone.parse(new_data) + new_time.min != data.min || new_time.hour != data.hour end - def formatted(new_date: nil) - super(:time, new_date: new_date) + def formatted + super(:time) end def self.new_with_payload(payload, attributes)