aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2017-04-14 10:27:47 -0700
committerGitHub <noreply@github.com>2017-04-14 10:27:47 -0700
commite447c8c80a3f99a29286661eb47762535c249181 (patch)
tree7532907156d21f973f2187745e9c5996cdf0469d /activerecord
parent851b7f866e13518d900407c78dcd6eb477afad06 (diff)
parentb5eb3215a68f94bb8cb20739366232c415744b83 (diff)
downloadrails-e447c8c80a3f99a29286661eb47762535c249181.tar.gz
rails-e447c8c80a3f99a29286661eb47762535c249181.tar.bz2
rails-e447c8c80a3f99a29286661eb47762535c249181.zip
Merge pull request #28661 from bogdanvlviv/fix-dirty-attributes-if-override-attr_accessor
Fix inconsistency with changed attributes when overriding AR attribute reader
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb4
-rw-r--r--activerecord/test/cases/dirty_test.rb19
3 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 9e3b686bbb..ffe588b3c5 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Fix inconsistency with changed attributes when overriding AR attribute reader.
+
+ *bogdanvlviv*
+
* When calling the dynamic fixture accessor method with no arguments it now returns all fixtures of this type.
Previously this method always returned an empty array.
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index bd5003d63a..63978cfb3e 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -328,6 +328,10 @@ module ActiveRecord
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
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index f9eccfbda1..15d7a32e21 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -671,6 +671,25 @@ class DirtyTest < ActiveRecord::TestCase
assert binary.changed?
end
+ test "changes is correct if override attribute reader" do
+ pirate = Pirate.create!(catchphrase: "arrrr")
+ def pirate.catchphrase
+ super.upcase
+ end
+
+ 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 "attribute_changed? doesn't compute in-place changes for unrelated attributes" do
test_type_class = Class.new(ActiveRecord::Type::Value) do
define_method(:changed_in_place?) do |*|