mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user