1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
~~~
This commit is contained in:
Renato Mascarenhas 2012-12-01 15:29:49 -02:00
parent 0181c2da97
commit cf7ab6056a
3 changed files with 9 additions and 3 deletions

View file

@ -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*

View file

@ -174,7 +174,10 @@ module ActiveModel
# 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

View file

@ -78,7 +78,7 @@ class DirtyTest < ActiveModel::TestCase
@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
@ -114,5 +114,4 @@ class DirtyTest < ActiveModel::TestCase
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
assert_equal @model.name_was, "Otto"
end
end