fixed dirty attributes when old value incorrectly read and added more examples for dirty attributes

This commit is contained in:
Adam Meehan 2008-10-28 19:54:52 +11:00
parent 8f950d5ac6
commit ced61929da
3 changed files with 42 additions and 8 deletions

View File

@ -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-08-25] [2008-08-25]
- fixed bug for non-timezone write method not updating changed attributes hash [reported by Sylvestre Mergulhão] - fixed bug for non-timezone write method not updating changed attributes hash [reported by Sylvestre Mergulhão]

View File

@ -71,8 +71,7 @@ module ValidatesTimeliness
def write_date_time_attribute(attr_name, value) def write_date_time_attribute(attr_name, value)
attr_name = attr_name.to_s attr_name = attr_name.to_s
column = column_for_attribute(attr_name) 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) new = self.class.parse_date_time(value, column.type)
if self.class.send(:create_time_zone_conversion_attribute?, attr_name, column) if self.class.send(:create_time_zone_conversion_attribute?, attr_name, column)

View File

@ -92,13 +92,45 @@ describe ValidatesTimeliness::AttributeMethods do
@person.birth_date_and_time_changed?.should be_true @person.birth_date_and_time_changed?.should be_true
end end
it "should show changes for time attribute as nil to Time object" do 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" time_string = "2000-01-01 02:03:04"
@person.birth_date_and_time = time_string @person.birth_date_and_time = time_string
time = @person.birth_date_and_time time = @person.birth_date_and_time
@person.changes.should == {"birth_date_and_time" => [nil, time]} @person.changes.should == {"birth_date_and_time" => [nil, time]}
end 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 else
it "should return time object from database in default timezone" do it "should return time object from database in default timezone" do