From 190e9a79b2f504a48a3a8ec10e36f140dc6fda36 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 2 Apr 2026 13:01:16 +1100 Subject: [PATCH] Move respond_with definition to own module and avoid monkey-patching --- app/controllers/application_controller.rb | 1 + app/controllers/concerns/respond_with.rb | 47 +++++++++++++++++++ .../core/controller_helpers/respond_with.rb | 47 ------------------- 3 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 app/controllers/concerns/respond_with.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c31e748a30..4434cbe0c7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -12,6 +12,7 @@ class ApplicationController < ActionController::Base include CablecarResponses include Pagy::Backend include RequestTimeouts + include RespondWith self.responder = ApplicationResponder respond_to :html diff --git a/app/controllers/concerns/respond_with.rb b/app/controllers/concerns/respond_with.rb new file mode 100644 index 0000000000..8f1259f6a2 --- /dev/null +++ b/app/controllers/concerns/respond_with.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module RespondWith + extend ActiveSupport::Concern + + def respond_with(*resources, &) + if self.class.mimes_for_respond_to.empty? + raise "In order to use respond_with, first you need to declare the formats your " \ + "controller responds to in the class level" + end + + return unless (collector = retrieve_collector_from_mimes(&)) + + options = resources.size == 1 ? {} : resources.extract_options! + + # Fix spree issues #3531 and #2210 (patch provided by leiyangyou) + if (defined_response = collector.response) && + !ApplicationController.spree_responders[self.class.to_s.to_sym] + .try(:[], action_name.to_sym) + + if action = options.delete(:action) + render(action:) + else + defined_response.call + end + else + # The action name is needed for processing + options[:action_name] = action_name.to_sym + # If responder is not specified then pass in Spree::Responder + (options.delete(:responder) || Spree::Responder).call(self, resources, options) + end + end + + private + + def retrieve_collector_from_mimes(mimes = nil, &block) + mimes ||= collect_mimes_from_class_level + collector = Collector.new(mimes, request.variant) + block.call(collector) if block_given? + format = collector.negotiate_format(request) + + raise ActionController::UnknownFormat unless format + + _process_format(format) + collector + end +end diff --git a/lib/spree/core/controller_helpers/respond_with.rb b/lib/spree/core/controller_helpers/respond_with.rb index 716586f34a..4cfe53ca8c 100644 --- a/lib/spree/core/controller_helpers/respond_with.rb +++ b/lib/spree/core/controller_helpers/respond_with.rb @@ -1,52 +1,5 @@ # frozen_string_literal: true -require 'spree/responder' - -module ActionController - class Base - def respond_with(*resources, &) - if self.class.mimes_for_respond_to.empty? - raise "In order to use respond_with, first you need to declare the formats your " \ - "controller responds to in the class level" - end - - return unless (collector = retrieve_collector_from_mimes(&)) - - options = resources.size == 1 ? {} : resources.extract_options! - - # Fix spree issues #3531 and #2210 (patch provided by leiyangyou) - if (defined_response = collector.response) && - !ApplicationController.spree_responders[self.class.to_s.to_sym].try(:[], - action_name.to_sym) - if action = options.delete(:action) - render(action:) - else - defined_response.call - end - else - # The action name is needed for processing - options[:action_name] = action_name.to_sym - # If responder is not specified then pass in Spree::Responder - (options.delete(:responder) || Spree::Responder).call(self, resources, options) - end - end - - private - - def retrieve_collector_from_mimes(mimes = nil, &block) - mimes ||= collect_mimes_from_class_level - collector = Collector.new(mimes, request.variant) - block.call(collector) if block_given? - format = collector.negotiate_format(request) - - raise ActionController::UnknownFormat unless format - - _process_format(format) - collector - end - end -end - module Spree module Core module ControllerHelpers