From f23d2a0c2d3cc2c8f94fe8d6819a3066f69368c9 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Mon, 25 Aug 2008 14:30:36 +1000 Subject: [PATCH] fixed non-timezone write method not updating changed attributes hash --- lib/validates_timeliness/attribute_methods.rb | 9 +++++-- spec/attribute_methods_spec.rb | 26 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index ecd7ae2..ca13b76 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -104,7 +104,7 @@ module ValidatesTimeliness def define_write_method_for_time_zone_conversion(attr_name) method_body = <<-EOV def #{attr_name}=(time) - old = read_attribute('#{attr_name}') if defined?(ActiveRecord::Dirty) + old = read_attribute('#{attr_name}') @attributes['#{attr_name}'] = time unless time.acts_like?(:time) time = self.class.parse_date_time(time, :datetime) @@ -146,7 +146,12 @@ module ValidatesTimeliness def define_write_method_for_dates_and_times(attr_name, type) method_body = <<-EOV def #{attr_name}=(value) - @attributes_cache['#{attr_name}'] = self.class.parse_date_time(value, :#{type}) + old = read_attribute('#{attr_name}') if defined?(ActiveRecord::Dirty) + new = self.class.parse_date_time(value, :#{type}) + @attributes_cache['#{attr_name}'] = new + if defined?(ActiveRecord::Dirty) && !changed_attributes.include?('#{attr_name}') && old != new + changed_attributes['#{attr_name}'] = (old.duplicable? ? old.clone : old) + end @attributes['#{attr_name}'] = value end EOV diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index 55519b8..0828832 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -113,18 +113,28 @@ describe ValidatesTimeliness::AttributeMethods do end - it "should return same time object on repeat reads" do + it "should return same time object on repeat reads on existing object" do Time.zone = 'Melbourne' unless RAILS_VER < '2.1' time_string = "2000-01-01 09:00:00" @person = Person.new @person.birth_date_and_time = time_string - @person.save + @person.save! @person.reload time = @person.birth_date_and_time @person.birth_date_and_time.should == time end - it "should return correct value after new value assigned" do + it "should return same date object on repeat reads on existing object" do + date_string = Date.today + @person = Person.new + @person.birth_date = date_string + @person.save! + @person.reload + date = @person.birth_date + @person.birth_date.should == date + end + + it "should return correct date value after new value assigned" do today = Date.today tomorrow = Date.today + 1.day @person = Person.new @@ -134,4 +144,14 @@ describe ValidatesTimeliness::AttributeMethods do @person.birth_date.should == tomorrow end + it "should update date attribute on existing object" do + today = Date.today + tomorrow = Date.today + 1.day + @person = Person.create(:birth_date => today) + @person.birth_date = tomorrow + @person.save! + @person.reload + @person.birth_date.should == tomorrow + end + end