Use different namespace for proxy calls

This commit is contained in:
Chris Salzberg 2022-02-03 17:17:03 +09:00 committed by Jean Boussier
parent 11b125ae95
commit 81519dec1a
3 changed files with 34 additions and 1 deletions

View File

@ -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.

View File

@ -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

View File

@ -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",