Skip to content

Commit

Permalink
Dirty: treat two changes resulting in the original value as being unc…
Browse files Browse the repository at this point in the history
…hanged.

[#798 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
tomlea authored and jeremy committed Aug 28, 2008
1 parent e21ed3e commit a3a3067
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/dirty.rb
Expand Up @@ -123,7 +123,10 @@ def write_attribute_with_dirty(attr, value)
attr = attr.to_s

# The attribute already has an unsaved change.
unless changed_attributes.include?(attr)
if changed_attributes.include?(attr)
old = changed_attributes[attr]
changed_attributes.delete(attr) unless field_changed?(attr, old, value)
else
old = clone_attribute_value(:read_attribute, attr)
changed_attributes[attr] = old if field_changed?(attr, old, value)
end
Expand Down
36 changes: 36 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Expand Up @@ -191,6 +191,42 @@ def test_reload_should_clear_changed_attributes
assert !pirate.changed?
end

def test_reverted_changes_are_not_dirty
phrase = "shiver me timbers"
pirate = Pirate.create!(:catchphrase => phrase)
pirate.catchphrase = "*hic*"
assert pirate.changed?
pirate.catchphrase = phrase
assert !pirate.changed?
end

def test_reverted_changes_are_not_dirty_after_multiple_changes
phrase = "shiver me timbers"
pirate = Pirate.create!(:catchphrase => phrase)
10.times do |i|
pirate.catchphrase = "*hic*" * i
assert pirate.changed?
end
assert pirate.changed?
pirate.catchphrase = phrase
assert !pirate.changed?
end


def test_reverted_changes_are_not_dirty_going_from_nil_to_value_and_back
pirate = Pirate.create!(:catchphrase => "Yar!")

pirate.parrot_id = 1
assert pirate.changed?
assert pirate.parrot_id_changed?
assert !pirate.catchphrase_changed?

pirate.parrot_id = nil
assert !pirate.changed?
assert !pirate.parrot_id_changed?
assert !pirate.catchphrase_changed?
end

def test_save_should_store_serialized_attributes_even_with_partial_updates
with_partial_updates(Topic) do
topic = Topic.create!(:content => {:a => "a"})
Expand Down

0 comments on commit a3a3067

Please sign in to comment.