Merge pull request #13372 from chitty/cch/add-respond-to-missing

Add `respond_to_missing?` and replace `method_missing` with explicit preference methods
This commit is contained in:
Gaetan Craig-Riou
2025-06-19 18:47:08 +10:00
committed by GitHub
4 changed files with 23 additions and 20 deletions

View File

@@ -225,13 +225,6 @@ Style/MapToHash:
- 'lib/reporting/reports/enterprise_fee_summary/fee_summary.rb'
- 'lib/tasks/sample_data/user_factory.rb'
# Offense count: 3
Style/MissingRespondToMissing:
Exclude:
- 'app/helpers/application_helper.rb'
- 'app/models/spree/gateway.rb'
- 'app/models/spree/preferences/configuration.rb'
# Offense count: 38
Style/OpenStructUse:
Exclude:

View File

@@ -45,6 +45,11 @@ module ApplicationHelper
form_for(name, *(args << options.merge(builder: AngularFormBuilder)), &)
end
def respond_to_missing?(method_name, include_private = false)
(method_name.to_s.end_with?('_path',
'_url') && spree.respond_to?(method_name, include_private)) || super
end
# Pass URL helper calls on to spree where applicable so that we don't need to use
# spree.foo_path in any view rendered from non-spree-namespaced controllers.
def method_missing(method, *args, &)

View File

@@ -30,6 +30,10 @@ module Spree
preferences.transform_keys(&:to_sym)
end
def respond_to_missing?(method_name, include_private = false)
@provider.respond_to?(method_name, include_private) || super
end
def method_missing(method, *args)
if @provider.nil? || !@provider.respond_to?(method)
super

View File

@@ -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,19 +70,6 @@ module Spree
set_preference args[0], args[1]
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