From 37bf3f495f436408916bf00b4909dcc5c13c91a4 Mon Sep 17 00:00:00 2001 From: Carlos Chitty Date: Tue, 10 Jun 2025 12:37:23 -0400 Subject: [PATCH] Refactor preference access to use define_method instead of method_missing Replaces dynamic method handling with explicit getters and setters to avoid recursion issues and improve clarity. --- app/models/spree/preferences/configuration.rb | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/app/models/spree/preferences/configuration.rb b/app/models/spree/preferences/configuration.rb index d028dc41cd..464047d390 100644 --- a/app/models/spree/preferences/configuration.rb +++ b/app/models/spree/preferences/configuration.rb @@ -27,6 +27,20 @@ module Spree class Configuration include Spree::Preferences::Preferable + class << self + def preference(name, type, *args) + super + + define_method(name) do + get_preference(name) + end + + define_method("#{name}=") do |value| + set_preference(name, value) + end + end + end + def configure yield(self) if block_given? end @@ -56,24 +70,6 @@ module Spree set_preference args[0], args[1] end - - def respond_to_missing?(method_name, include_private = false) - name = method_name.to_s.chomp('=') - has_preference?(name) || super - end - - def method_missing(method, *args) - name = method.to_s.gsub('=', '') - if has_preference? name - if method.to_s =~ /=$/ - set_preference(name, args.first) - else - get_preference name - end - else - super - end - end end end end