From 2cd4abcc876bdbe3adc2cbbfeee9712fc2bc0601 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Tue, 9 Jan 2024 16:34:07 -0500 Subject: [PATCH] Simplify `CurrentAttribute.instance` delegation Follow-up to [#50676][] Instead of relying on code generation, call a corresponding [delegate][] method on the `.singleton_class`. [#50676]: https://github.com/rails/rails/pull/50676 [delegate]: https://edgeapi.rubyonrails.org/classes/Module.html#method-i-delegate Co-authored-by: Jean Boussier --- .../lib/active_support/current_attributes.rb | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/activesupport/lib/active_support/current_attributes.rb b/activesupport/lib/active_support/current_attributes.rb index a7b1e051137..9c592a7e2df 100644 --- a/activesupport/lib/active_support/current_attributes.rb +++ b/activesupport/lib/active_support/current_attributes.rb @@ -125,22 +125,7 @@ module ActiveSupport end end - ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner| - names.each do |name| - owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch| - batch << - "def #{name}" << - "instance.#{name}" << - "end" - end - owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch| - batch << - "def #{name}=(value)" << - "instance.#{name} = value" << - "end" - end - end - end + singleton_class.delegate(*names.flat_map { |name| [name, "#{name}="] }, to: :instance, as: self) end # Calls this callback before #reset is called on the instance. Used for resetting external collaborators that depend on current values.