Skip to content
This repository
Browse code

Dirty: treat two changes resulting in the original value as being unc…

…hanged.

[#798 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit ad562c58eabfb8b44cb8ac9e87b87a7f998325fd 1 parent f277e1d
Tom Lea authored August 11, 2008 jeremy committed August 27, 2008
5  activerecord/lib/active_record/dirty.rb
@@ -123,7 +123,10 @@ def write_attribute_with_dirty(attr, value)
123 123
         attr = attr.to_s
124 124
 
125 125
         # The attribute already has an unsaved change.
126  
-        unless changed_attributes.include?(attr)
  126
+        if changed_attributes.include?(attr)
  127
+          old = changed_attributes[attr]
  128
+          changed_attributes.delete(attr) unless field_changed?(attr, old, value)
  129
+        else
127 130
           old = clone_attribute_value(:read_attribute, attr)
128 131
           changed_attributes[attr] = old if field_changed?(attr, old, value)
129 132
         end
36  activerecord/test/cases/dirty_test.rb
@@ -191,6 +191,42 @@ def test_reload_should_clear_changed_attributes
191 191
     assert !pirate.changed?
192 192
   end
193 193
 
  194
+  def test_reverted_changes_are_not_dirty
  195
+    phrase = "shiver me timbers"
  196
+    pirate = Pirate.create!(:catchphrase => phrase)
  197
+    pirate.catchphrase = "*hic*"
  198
+    assert pirate.changed?
  199
+    pirate.catchphrase = phrase
  200
+    assert !pirate.changed?
  201
+  end
  202
+
  203
+  def test_reverted_changes_are_not_dirty_after_multiple_changes
  204
+    phrase = "shiver me timbers"
  205
+    pirate = Pirate.create!(:catchphrase => phrase)
  206
+    10.times do |i|
  207
+      pirate.catchphrase = "*hic*" * i
  208
+      assert pirate.changed?
  209
+    end
  210
+    assert pirate.changed?
  211
+    pirate.catchphrase = phrase
  212
+    assert !pirate.changed?
  213
+  end
  214
+
  215
+
  216
+  def test_reverted_changes_are_not_dirty_going_from_nil_to_value_and_back
  217
+    pirate = Pirate.create!(:catchphrase => "Yar!")
  218
+
  219
+    pirate.parrot_id = 1
  220
+    assert pirate.changed?
  221
+    assert pirate.parrot_id_changed?
  222
+    assert !pirate.catchphrase_changed?
  223
+
  224
+    pirate.parrot_id = nil
  225
+    assert !pirate.changed?
  226
+    assert !pirate.parrot_id_changed?
  227
+    assert !pirate.catchphrase_changed?
  228
+  end
  229
+
194 230
   def test_save_should_store_serialized_attributes_even_with_partial_updates
195 231
     with_partial_updates(Topic) do
196 232
       topic = Topic.create!(:content => {:a => "a"})

0 notes on commit ad562c5

Please sign in to comment.
Something went wrong with that request. Please try again.