diff --git a/CHANGELOG b/CHANGELOG index 578a35c..3f42c3f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +[2008-10-28] + - fixed bug when dirty attributes not reflecting change when attribute changed from time value to nil [reported by Brad (pvjq)] + [2008-09-24] - refactored attribute write method definitions diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index 526f88b..becbe2f 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -71,8 +71,7 @@ module ValidatesTimeliness def write_date_time_attribute(attr_name, value) attr_name = attr_name.to_s column = column_for_attribute(attr_name) - - old = read_attribute('#{attr_name}') if defined?(ActiveRecord::Dirty) + old = read_attribute(attr_name) if defined?(ActiveRecord::Dirty) new = self.class.parse_date_time(value, column.type) if self.class.send(:create_time_zone_conversion_attribute?, attr_name, column) diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index 0828832..58d6a0b 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -92,13 +92,45 @@ describe ValidatesTimeliness::AttributeMethods do @person.birth_date_and_time_changed?.should be_true end - it "should show changes for time attribute as nil to Time object" do - time_string = "2000-01-01 02:03:04" - @person.birth_date_and_time = time_string - time = @person.birth_date_and_time - @person.changes.should == {"birth_date_and_time" => [nil, time]} - end + describe "dirty attributes" do + it "should show changes when time attribute changed from nil to Time object" do + time_string = "2000-01-01 02:03:04" + @person.birth_date_and_time = time_string + time = @person.birth_date_and_time + @person.changes.should == {"birth_date_and_time" => [nil, time]} + end + + it "should show changes when time attribute changed from Time object to nil" do + time_string = "2020-01-01 02:03:04" + @person.birth_date_and_time = time_string + @person.save false + @person.reload + time = @person.birth_date_and_time + @person.birth_date_and_time = nil + @person.changes.should == {"birth_date_and_time" => [time, nil]} + end + + it "should show no changes when assigned same value as Time object" do + time_string = "2020-01-01 02:03:04" + @person.birth_date_and_time = time_string + @person.save false + @person.reload + time = @person.birth_date_and_time + @person.birth_date_and_time = time + @person.changes.should == {} + end + + it "should show no changes when assigned same value as time string" do + time_string = "2020-01-01 02:03:04" + @person.birth_date_and_time = time_string + @person.save false + @person.reload + @person.birth_date_and_time = time_string + @person.changes.should == {} + end + + end else it "should return time object from database in default timezone" do