Add respond_to_missing? to classes defining method_missing to improve respond_to? behavior

This commit is contained in:
Carlos Chitty
2025-06-10 11:56:30 -04:00
parent 4f3887fcc8
commit f909bb2c30
4 changed files with 14 additions and 7 deletions

View File

@@ -273,13 +273,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

@@ -57,6 +57,11 @@ 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