aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-08-16 23:09:33 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2014-08-16 23:09:33 -0700
commitd20270612cd9be3eed910171934a6b3463dbead7 (patch)
treeb7d1a8154839de05eedb015e6d11e2b83c15e010 /activemodel/lib
parent88d27ae9181151f448f39298adce9d1b7401a376 (diff)
parent008f3da3835e47f719ba6820703ba404ff363640 (diff)
downloadrails-d20270612cd9be3eed910171934a6b3463dbead7.tar.gz
rails-d20270612cd9be3eed910171934a6b3463dbead7.tar.bz2
rails-d20270612cd9be3eed910171934a6b3463dbead7.zip
Merge pull request #16189 from sgrif/sg-attribute-was-in-place
Implement `_was` and `changes` for in-place mutations of AR attributes
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/dirty.rb18
1 files changed, 16 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index d11243c4c0..ca04f48c1c 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -219,7 +219,7 @@ module ActiveModel
rescue TypeError, NoMethodError
end
- changed_attributes[attr] = value
+ set_attribute_was(attr, value)
end
# Handle <tt>reset_*!</tt> for +method_missing+.
@@ -233,8 +233,22 @@ module ActiveModel
def restore_attribute!(attr)
if attribute_changed?(attr)
__send__("#{attr}=", changed_attributes[attr])
- changed_attributes.delete(attr)
+ clear_attribute_changes([attr])
end
end
+
+ # This is necessary because `changed_attributes` might be overridden in
+ # other implemntations (e.g. in `ActiveRecord`)
+ alias_method :attributes_changed_by_setter, :changed_attributes # :nodoc:
+
+ # Force an attribute to have a particular "before" value
+ def set_attribute_was(attr, old_value)
+ attributes_changed_by_setter[attr] = old_value
+ end
+
+ # Remove changes information for the provided attributes.
+ def clear_attribute_changes(attributes)
+ attributes_changed_by_setter.except!(*attributes)
+ end
end
end