Skip to content

Commit

Permalink
Use different namespace for proxy calls
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama authored and byroot committed Feb 3, 2022
1 parent 11b125a commit 81519de
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions 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.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -319,7 +319,7 @@ def define_attribute_method(attr_name, _owner: generated_attribute_methods)
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
Expand Down
27 changes: 27 additions & 0 deletions activemodel/test/cases/attributes_test.rb
Expand Up @@ -24,6 +24,33 @@ class GrandchildModelForAttributesTest < ChildModelForAttributesTest
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",
Expand Down

0 comments on commit 81519de

Please sign in to comment.