Skip to content

Commit b721c62

Browse files
authored
Merge pull request rails#54646 from Edouard-chin/ec-current-attribute-fix
Fix CurrentAttributes not setting an attribute's default:
2 parents b63a4a1 + e34b6f5 commit b721c62

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

activesupport/lib/active_support/current_attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def method_added(name)
187187
super
188188
return if name == :initialize
189189
return unless public_method_defined?(name)
190-
return if respond_to?(name, true)
190+
return if singleton_class.method_defined?(name) || singleton_class.private_method_defined?(name)
191191
Delegation.generate(singleton_class, [name], to: :instance, as: self, nilable: false)
192192
end
193193
end

activesupport/test/current_attributes_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,18 @@ class InvalidAttributeNames < ActiveSupport::CurrentAttributes
262262
end
263263
end
264264
end
265+
266+
test "method_added hook doesn't reach the instance. Fix for #54646" do
267+
current = Class.new(ActiveSupport::CurrentAttributes) do
268+
def self.name
269+
"MyCurrent"
270+
end
271+
272+
def foo; end # Sets the cache because of a `method_added` hook
273+
274+
attribute :bar, default: {}
275+
end
276+
277+
assert_instance_of(Hash, current.bar)
278+
end
265279
end

0 commit comments

Comments
 (0)