From 81519dec1add5ee5d93cc24239283ca56379eb9a Mon Sep 17 00:00:00 2001 From: Chris Salzberg Date: Thu, 3 Feb 2022 17:17:03 +0900 Subject: [PATCH] Use different namespace for proxy calls --- activemodel/CHANGELOG.md | 6 +++++ .../lib/active_model/attribute_methods.rb | 2 +- activemodel/test/cases/attributes_test.rb | 27 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index eb42932381..e0f2a05698 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,9 @@ +* Use different cache namespace for proxy calls + Models can currently have different attribute bodies for the same method + names, leading to conflicts. Adding a new namespace `:active_model_proxy` + fixes the issue. + + *Chris Salzberg* Please check [7-0-stable](https://github.com/rails/rails/blob/7-0-stable/activemodel/CHANGELOG.md) for previous changes. diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index a07e98f5e4..57ec31c043 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -319,7 +319,7 @@ module ActiveModel if respond_to?(generate_method, true) send(generate_method, attr_name.to_s, owner: owner) else - define_proxy_call(owner, method_name, matcher.target, matcher.parameters, attr_name.to_s, namespace: :active_model) + define_proxy_call(owner, method_name, matcher.target, matcher.parameters, attr_name.to_s, namespace: :active_model_proxy) end end end diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb index 387005f56c..6cc126eb68 100644 --- a/activemodel/test/cases/attributes_test.rb +++ b/activemodel/test/cases/attributes_test.rb @@ -24,6 +24,33 @@ module ActiveModel attribute :string_field, default: "default string" end + class ModelWithGeneratedAttributeMethods + include ActiveModel::Attributes + + attribute :foo + end + + class ModelWithProxiedAttributeMethods + include ActiveModel::AttributeMethods + + attribute_method_suffix "=" + + define_attribute_method(:foo) + + def attribute=(_, _) + end + end + + test "models that proxy attributes do not conflict with models with generated methods" do + ModelWithGeneratedAttributeMethods.new + + model = ModelWithProxiedAttributeMethods.new + + assert_nothing_raised do + model.foo = "foo" + end + end + test "properties assignment" do data = ModelForAttributesTest.new( integer_field: "2.3",