diff --git a/CHANGELOG b/CHANGELOG index 0485886..bf82538 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ [2008-08-22] - - fixed bug with attribute cache not clearing on write for date and time columns + - fixed bug with attribute cache not clearing on write for date and time columns [reported by Sylvestre Mergulhão] + - return Date object for date column attribute reader when assigned string as per normal Rails behaviour + - attribute reader returns same object type when assigned Date or Time object as per normal Rails behaviour [2008-08-07] - modified matcher option value parsing to allow same value types as validation method diff --git a/lib/validates_timeliness/validations.rb b/lib/validates_timeliness/validations.rb index 88e6407..b09fea3 100644 --- a/lib/validates_timeliness/validations.rb +++ b/lib/validates_timeliness/validations.rb @@ -34,7 +34,7 @@ module ValidatesTimeliness def parse_date_time(raw_value, type, strict=true) return nil if raw_value.blank? - return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date) + return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date) time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict) raise if time_array.nil? @@ -43,7 +43,9 @@ module ValidatesTimeliness time_array[0..2] = 2000, 1, 1 if type == :time # Date.new enforces days per month, unlike Time - Date.new(*time_array[0..2]) unless type == :time + date = Date.new(*time_array[0..2]) unless type == :time + + return date if type == :date # Create time object which checks time part, and return time object make_time(time_array) diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index 2efc579..55519b8 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -34,11 +34,26 @@ describe ValidatesTimeliness::AttributeMethods do @person.birth_date_and_time_before_type_cast.should be_kind_of(Time) end - it "should return Time object using attribute read method when written with string" do + it "should return Time object for datetime attribute read method when assigned Time object" do + @person.birth_date_and_time = Time.now + @person.birth_date_and_time.should be_kind_of(Time) + end + + it "should return Time object for datetime attribute read method when assigned string" do @person.birth_date_and_time = "2000-01-01 02:03:04" @person.birth_date_and_time.should be_kind_of(Time) end + it "should return Date object for date attribute read method when assigned Date object" do + @person.birth_date = Date.today + @person.birth_date.should be_kind_of(Date) + end + + it "should return Date object for date attribute read method when assigned string" do + @person.birth_date = '2000-01-01' + @person.birth_date.should be_kind_of(Date) + end + it "should return nil when time is invalid" do @person.birth_date_and_time = "2000-01-32 02:03:04" @person.birth_date_and_time.should be_nil