Skip to content

Commit 2cd4abc

Browse files
seanpdoylebyroot
andcommitted
Simplify CurrentAttribute.instance delegation
Follow-up to [rails#50676][] Instead of relying on code generation, call a corresponding [delegate][] method on the `.singleton_class`. [rails#50676]: rails#50676 [delegate]: https://edgeapi.rubyonrails.org/classes/Module.html#method-i-delegate Co-authored-by: Jean Boussier <[email protected]>
1 parent ba2603a commit 2cd4abc

File tree

1 file changed

+1
-16
lines changed

1 file changed

+1
-16
lines changed

activesupport/lib/active_support/current_attributes.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,7 @@ def attribute(*names)
125125
end
126126
end
127127

128-
ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner|
129-
names.each do |name|
130-
owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch|
131-
batch <<
132-
"def #{name}" <<
133-
"instance.#{name}" <<
134-
"end"
135-
end
136-
owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch|
137-
batch <<
138-
"def #{name}=(value)" <<
139-
"instance.#{name} = value" <<
140-
"end"
141-
end
142-
end
143-
end
128+
singleton_class.delegate(*names.flat_map { |name| [name, "#{name}="] }, to: :instance, as: self)
144129
end
145130

146131
# Calls this callback before #reset is called on the instance. Used for resetting external collaborators that depend on current values.

0 commit comments

Comments
 (0)