Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move around AR::Dirty and fix _attribute method #28760

Merged
merged 1 commit into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activemodel/lib/active_model/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,5 +472,9 @@ def matched_attribute_method(method_name)
def missing_attribute(attr_name, stack)
raise ActiveModel::MissingAttributeError, "missing attribute: #{attr_name}", stack
end

def _read_attribute(attr)
__send__(attr)
end
end
end
12 changes: 4 additions & 8 deletions activemodel/lib/active_model/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ def changed_attributes
# Handles <tt>*_changed?</tt> 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 <tt>*_was</tt> 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 <tt>*_previously_changed?</tt> for +method_missing+.
Expand Down Expand Up @@ -226,7 +226,7 @@ def clear_changes_information # :doc:

# Handles <tt>*_change</tt> 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 <tt>*_previous_change</tt> for +method_missing+.
Expand All @@ -239,7 +239,7 @@ def attribute_will_change!(attr)
return if attribute_changed?(attr)

begin
value = _attributes(attr)
value = _read_attribute(attr)
value = value.duplicable? ? value.clone : value
rescue TypeError, NoMethodError
end
Expand Down Expand Up @@ -268,9 +268,5 @@ def set_attribute_was(attr, old_value)
def clear_attribute_changes(attributes) # :doc:
attributes_changed_by_setter.except!(*attributes)
end

def _attributes(attr)
__send__(attr)
end
end
end
4 changes: 0 additions & 4 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,6 @@ def cache_changed_attributes
def clear_changed_attributes_cache
remove_instance_variable(:@cached_changed_attributes) if defined?(@cached_changed_attributes)
end

def _attributes(attr)
_read_attribute(attr)
end
end
end
end
22 changes: 22 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,28 @@ def test_datetime_attribute_doesnt_change_if_zone_is_modified_in_string
assert binary.changed?
end

test "changes is correct for subclass" do
foo = Class.new(Pirate) do
def catchphrase
super.upcase
end
end

pirate = foo.create!(catchphrase: "arrrr")

new_catchphrase = "arrrr matey!"

pirate.catchphrase = new_catchphrase
assert pirate.catchphrase_changed?

expected_changes = {
"catchphrase" => ["arrrr", new_catchphrase]
}

assert_equal new_catchphrase.upcase, pirate.catchphrase
assert_equal expected_changes, pirate.changes
end

test "changes is correct if override attribute reader" do
pirate = Pirate.create!(catchphrase: "arrrr")
def pirate.catchphrase
Expand Down