diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-07-12 18:30:49 -0600 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2014-08-16 23:08:41 -0700 |
commit | 877ea784e4cd0d539bdfbd15839ae3d28169b156 (patch) | |
tree | fe249c2d53d6cd7062b131b6157b3d00bbb7a683 /activerecord/lib/active_record/attribute_methods | |
parent | 88d27ae9181151f448f39298adce9d1b7401a376 (diff) | |
download | rails-877ea784e4cd0d539bdfbd15839ae3d28169b156.tar.gz rails-877ea784e4cd0d539bdfbd15839ae3d28169b156.tar.bz2 rails-877ea784e4cd0d539bdfbd15839ae3d28169b156.zip |
Implement `_was` and `changes` for in-place mutations of AR attributes
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index b58295a106..d3f4e51c33 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -51,14 +51,6 @@ module ActiveRecord super | changed_in_place end - def attribute_changed?(attr_name, options = {}) - result = super - # We can't change "from" something in place. Only setters can define - # "from" and "to" - result ||= changed_in_place?(attr_name) unless options.key?(:from) - result - end - def changes_applied super store_original_raw_attributes @@ -69,12 +61,16 @@ module ActiveRecord original_raw_attributes.clear end + def changed_attributes + super.reverse_merge(attributes_changed_in_place).freeze + end + private def calculate_changes_from_defaults @changed_attributes = nil self.class.column_defaults.each do |attr, orig_value| - changed_attributes[attr] = orig_value if _field_changed?(attr, orig_value) + set_attribute_was(attr, orig_value) if _field_changed?(attr, orig_value) end end @@ -100,9 +96,9 @@ module ActiveRecord def save_changed_attribute(attr, old_value) if attribute_changed?(attr) - changed_attributes.delete(attr) unless _field_changed?(attr, old_value) + clear_attribute_changes(attr) unless _field_changed?(attr, old_value) else - changed_attributes[attr] = old_value if _field_changed?(attr, old_value) + set_attribute_was(attr, old_value) if _field_changed?(attr, old_value) end end @@ -132,6 +128,13 @@ module ActiveRecord @attributes[attr].changed_from?(old_value) end + def attributes_changed_in_place + changed_in_place.each_with_object({}) do |attr_name, h| + orig = @attributes[attr_name].original_value + h[attr_name] = orig + end + end + def changed_in_place self.class.attribute_names.select do |attr_name| changed_in_place?(attr_name) |