Skip to content
This repository
Browse code

Reset attributes should not report changes.

When resetting an attribute, you expect it to return to the state it was
before any changes. Namely, this fixes this unexpected behavior:

~~~ruby
model.name = "Bob"
model.reset_name!
model.name_changed? #=> true
~~~
  • Loading branch information...
commit cf7ab6056adfd1d2feb98444d82f89adcb6e5533 1 parent 0181c2d
Renato Mascarenhas authored December 01, 2012
4  activemodel/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,9 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   `[attribute]_changed?` now returns `false` after a call to `reset_[attribute]!`
  4
+
  5
+    *Renato Mascarenhas*
  6
+
3 7
 *   Observers was extracted from Active Model as `rails-observers` gem.
4 8
 
5 9
     *Rafael Mendonça França*
5  activemodel/lib/active_model/dirty.rb
@@ -174,7 +174,10 @@ def attribute_will_change!(attr)
174 174
 
175 175
       # Handle <tt>reset_*!</tt> for +method_missing+.
176 176
       def reset_attribute!(attr)
177  
-        __send__("#{attr}=", changed_attributes[attr]) if attribute_changed?(attr)
  177
+        if attribute_changed?(attr)
  178
+          __send__("#{attr}=", changed_attributes[attr])
  179
+          changed_attributes.delete(attr)
  180
+        end
178 181
       end
179 182
   end
180 183
 end
3  activemodel/test/cases/dirty_test.rb
@@ -78,7 +78,7 @@ def save
78 78
     @model.name = "Bob"
79 79
     @model.reset_name!
80 80
     assert_nil @model.name
81  
-    #assert !@model.name_changed #Doesn't work yet
  81
+    assert !@model.name_changed?
82 82
   end
83 83
 
84 84
   test "setting color to same value should not result in change being recorded" do
@@ -114,5 +114,4 @@ def save
114 114
     assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
115 115
     assert_equal @model.name_was, "Otto"
116 116
   end
117  
-
118 117
 end

0 notes on commit cf7ab60

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