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:13:41 -0700 2008
commit  ad562c58eabfb8b44cb8ac9e87b87a7f998325fd
tree    de532e0f56ea86382b12d637f32a7b4480c84c28
parent  f277e1d8fddfa417104c6fe095c15559f0c8713d
...
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