public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Dirty: treat two changes resulting in the original value as being unchanged.

[#798 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Tom Lea (author)
Mon Aug 11 06:12:53 -0700 2008
jeremy (committer)
Wed Aug 27 23:20:36 -0700 2008
commit  a3a3067adbd578ad9d145f5a148816ec0460a987
tree    66bf724ac9cc65473476fbd4305a51fc080fe773
parent  e21ed3e45429cf4a7ee4f5b6b550f457f0c4c313
...
123
124
125
126
 
 
 
 
127
128
129
...
123
124
125
 
126
127
128
129
130
131
132
0
@@ -123,7 +123,10 @@ module ActiveRecord
0
         attr = attr.to_s
0
 
0
         # The attribute already has an unsaved change.
0
-        unless changed_attributes.include?(attr)
0
+        if changed_attributes.include?(attr)
0
+          old = changed_attributes[attr]
0
+          changed_attributes.delete(attr) unless field_changed?(attr, old, value)
0
+        else
0
           old = clone_attribute_value(:read_attribute, attr)
0
           changed_attributes[attr] = old if field_changed?(attr, old, value)
0
         end
...
191
192
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
195
196
...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
0
@@ -191,6 +191,42 @@ class DirtyTest < ActiveRecord::TestCase
0
     assert !pirate.changed?
0
   end
0
 
0
+  def test_reverted_changes_are_not_dirty
0
+    phrase = "shiver me timbers"
0
+    pirate = Pirate.create!(:catchphrase => phrase)
0
+    pirate.catchphrase = "*hic*"
0
+    assert pirate.changed?
0
+    pirate.catchphrase = phrase
0
+    assert !pirate.changed?
0
+  end
0
+
0
+  def test_reverted_changes_are_not_dirty_after_multiple_changes
0
+    phrase = "shiver me timbers"
0
+    pirate = Pirate.create!(:catchphrase => phrase)
0
+    10.times do |i|
0
+      pirate.catchphrase = "*hic*" * i
0
+      assert pirate.changed?
0
+    end
0
+    assert pirate.changed?
0
+    pirate.catchphrase = phrase
0
+    assert !pirate.changed?
0
+  end
0
+
0
+
0
+  def test_reverted_changes_are_not_dirty_going_from_nil_to_value_and_back
0
+    pirate = Pirate.create!(:catchphrase => "Yar!")
0
+
0
+    pirate.parrot_id = 1
0
+    assert pirate.changed?
0
+    assert pirate.parrot_id_changed?
0
+    assert !pirate.catchphrase_changed?
0
+
0
+    pirate.parrot_id = nil
0
+    assert !pirate.changed?
0
+    assert !pirate.parrot_id_changed?
0
+    assert !pirate.catchphrase_changed?
0
+  end
0
+
0
   def test_save_should_store_serialized_attributes_even_with_partial_updates
0
     with_partial_updates(Topic) do
0
       topic = Topic.create!(:content => {:a => "a"})

Comments