From 941055c16e2066c3978f105e5e051f63c7b018bb Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 16 May 2018 09:27:25 +1000 Subject: [PATCH] Restore usage of DateTime substitute value object Remove initialize override. Not sure why what was added. --- .../extensions/date_time_select.rb | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/validates_timeliness/extensions/date_time_select.rb b/lib/validates_timeliness/extensions/date_time_select.rb index 6ef9bca..ff5fe13 100644 --- a/lib/validates_timeliness/extensions/date_time_select.rb +++ b/lib/validates_timeliness/extensions/date_time_select.rb @@ -11,9 +11,23 @@ module ValidatesTimeliness :year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6 }.freeze - def initialize(object_name, method_name, template_object, options, html_options) - @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup - @template_object, @options, @html_options = template_object, options, html_options + class DateTimeValue + attr_accessor :year, :month, :day, :hour, :min, :sec + + def initialize(year:, month:, day: nil, hour: nil, min: nil, sec: nil) + @year, @month, @day, @hour, @min, @sec = year, month, day, hour, min, sec + end + + def change(options) + self.class.new( + year: options.fetch(:year, year), + month: options.fetch(:month, month), + day: options.fetch(:day, day), + hour: options.fetch(:hour, hour), + min: options.fetch(:min) { options[:hour] ? 0 : min }, + sec: options.fetch(:sec) { options[:hour] || options[:min] ? 0 : sec } + ) + end end # Splat args to support Rails 5.0 which expects object, and 5.2 which doesn't @@ -29,7 +43,7 @@ module ValidatesTimeliness values[POSITION.key(position.to_i)] = value.to_i end - values + DateTimeValue.new(values) end end end