Skip to content

Commit

Permalink
Reset attributes should not report changes.
Browse files Browse the repository at this point in the history
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
Renato Mascarenhas committed Dec 1, 2012
1 parent 0181c2d commit cf7ab60
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##

* `[attribute]_changed?` now returns `false` after a call to `reset_[attribute]!`

*Renato Mascarenhas*

* Observers was extracted from Active Model as `rails-observers` gem.

*Rafael Mendonça França*
Expand Down
5 changes: 4 additions & 1 deletion activemodel/lib/active_model/dirty.rb
Expand Up @@ -174,7 +174,10 @@ def attribute_will_change!(attr)

# Handle <tt>reset_*!</tt> for +method_missing+.
def reset_attribute!(attr)
__send__("#{attr}=", changed_attributes[attr]) if attribute_changed?(attr)
if attribute_changed?(attr)
__send__("#{attr}=", changed_attributes[attr])
changed_attributes.delete(attr)
end
end
end
end
3 changes: 1 addition & 2 deletions activemodel/test/cases/dirty_test.rb
Expand Up @@ -78,7 +78,7 @@ def save
@model.name = "Bob"
@model.reset_name!
assert_nil @model.name
#assert !@model.name_changed #Doesn't work yet
assert !@model.name_changed?
end

test "setting color to same value should not result in change being recorded" do
Expand Down Expand Up @@ -114,5 +114,4 @@ def save
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
assert_equal @model.name_was, "Otto"
end

end

0 comments on commit cf7ab60

Please sign in to comment.