diff --git a/lib/validates_timeliness/extensions/date_time_select.rb b/lib/validates_timeliness/extensions/date_time_select.rb index 1f07cde..83eed35 100644 --- a/lib/validates_timeliness/extensions/date_time_select.rb +++ b/lib/validates_timeliness/extensions/date_time_select.rb @@ -13,28 +13,27 @@ module ValidatesTimeliness alias_method_chain :value, :timeliness end - module InstanceMethods - - class TimelinessDateTime - attr_accessor :year, :month, :day, :hour, :min, :sec - - def initialize(year, month, day, hour, min, sec) - @year, @month, @day, @hour, @min, @sec = year, month, day, hour, min, sec - end - - # adapted from activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 36 (3.0.7) - def change(options) - TimelinessDateTime.new( - options[:year] || year, - options[:month] || month, - options[:day] || day, - options[:hour] || hour, - options[:min] || (options[:hour] ? 0 : min), - options[:sec] || ((options[:hour] || options[:min]) ? 0 : sec) - ) - end + class TimelinessDateTime + attr_accessor :year, :month, :day, :hour, :min, :sec + + def initialize(year, month, day, hour, min, sec) + @year, @month, @day, @hour, @min, @sec = year, month, day, hour, min, sec end + # adapted from activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 36 (3.0.7) + def change(options) + TimelinessDateTime.new( + options[:year] || year, + options[:month] || month, + options[:day] || day, + options[:hour] || hour, + options[:min] || (options[:hour] ? 0 : min), + options[:sec] || ((options[:hour] || options[:min]) ? 0 : sec) + ) + end + end + + module InstanceMethods def datetime_selector_with_timeliness(*args) @timeliness_date_or_time_tag = true datetime_selector_without_timeliness(*args) diff --git a/spec/validates_timeliness/extensions/date_time_select_spec.rb b/spec/validates_timeliness/extensions/date_time_select_spec.rb index a873815..dcfe15f 100644 --- a/spec/validates_timeliness/extensions/date_time_select_spec.rb +++ b/spec/validates_timeliness/extensions/date_time_select_spec.rb @@ -68,7 +68,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_date(3i)" => '29', } person.birth_date = nil - @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + @output = date_select(:person, :birth_date, :include_blank => true) should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29) end @@ -79,42 +79,40 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do "birth_date(3i)" => '29', } person.birth_date = "2009-03-01" - @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + @output = date_select(:person, :birth_date, :include_blank => true) should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29) end it "should select attribute values from object if no params" do person.birth_date = "2009-01-02" - @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + @output = date_select(:person, :birth_date, :include_blank => true) should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2) end it "should select attribute values if params does not contain attribute params" do person.birth_date = "2009-01-02" @params["person"] = { } - @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + @output = date_select(:person, :birth_date, :include_blank => true) should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2) end it "should not select values when attribute value is nil and has no param values" do person.birth_date = nil - @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true) + @output = date_select(:person, :birth_date, :include_blank => true) should_not_have_datetime_selected(:birth_time, :year, :month, :day) end - - it "should support discarding of the day part" do - # this doesn't make sense for birthdays, but it does for credit card expirations, for example + + it "should allow the day part to be discarded" do @params["person"] = { - "birth_date(1i)" => 2009, - "birth_date(2i)" => 2, + "birth_date(1i)" => '2009', + "birth_date(2i)" => '2', } - person.birth_date = nil - @output = date_select(:person, :birth_date, :discard_day => true) + + @output = date_select(:person, :birth_date, :include_blank => true, :discard_day => true) should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February') - should_not_have_datetime_selected(:birth_date, :day) + should_not_have_datetime_selected(:birth_time, :day) @output.should have_tag("input[id=person_birth_date_3i][type=hidden][value='1']") end - end describe "time_select" do