From 83d6cc4815debe2086073387ecbf38c0f47cfec5 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 14 Apr 2017 10:46:07 -0700 Subject: Move around AR::Dirty and fix _attribute method We already have a _read_attribute method that can get the value we need from the model. Lets define that method in AM::Dirty and use the existing one from AR::Dirty rather than introducing a new method. --- activemodel/lib/active_model/attribute_methods.rb | 4 ++++ activemodel/lib/active_model/dirty.rb | 12 ++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 166c6ac21f..b5c0b43b61 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -472,5 +472,9 @@ module ActiveModel def missing_attribute(attr_name, stack) raise ActiveModel::MissingAttributeError, "missing attribute: #{attr_name}", stack end + + def _read_attribute(attr) + __send__(attr) + end end end diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 37b51ad354..dc81f74779 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -179,13 +179,13 @@ module ActiveModel # Handles *_changed? for +method_missing+. def attribute_changed?(attr, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) # :nodoc: !!changes_include?(attr) && - (to == OPTION_NOT_GIVEN || to == _attributes(attr)) && + (to == OPTION_NOT_GIVEN || to == _read_attribute(attr)) && (from == OPTION_NOT_GIVEN || from == changed_attributes[attr]) end # Handles *_was for +method_missing+. def attribute_was(attr) # :nodoc: - attribute_changed?(attr) ? changed_attributes[attr] : _attributes(attr) + attribute_changed?(attr) ? changed_attributes[attr] : _read_attribute(attr) end # Handles *_previously_changed? for +method_missing+. @@ -226,7 +226,7 @@ module ActiveModel # Handles *_change for +method_missing+. def attribute_change(attr) - [changed_attributes[attr], _attributes(attr)] if attribute_changed?(attr) + [changed_attributes[attr], _read_attribute(attr)] if attribute_changed?(attr) end # Handles *_previous_change for +method_missing+. @@ -239,7 +239,7 @@ module ActiveModel return if attribute_changed?(attr) begin - value = _attributes(attr) + value = _read_attribute(attr) value = value.duplicable? ? value.clone : value rescue TypeError, NoMethodError end @@ -268,9 +268,5 @@ module ActiveModel def clear_attribute_changes(attributes) # :doc: attributes_changed_by_setter.except!(*attributes) end - - def _attributes(attr) - __send__(attr) - end end end -- cgit v1.2.3