Sha256: f9b6a064e61eb97d10ebe7456a22781931b97b4fb6315da69d467d7e8d03f306

Contents?: true

Size: 1.8 KB

Versions: 52

Compression:

Stored size: 1.8 KB

Contents

require 'spree/responder'

module ActionController
  class Base
    def respond_with(*resources, &block)
      if Spree::BaseController.spree_responders.keys.include?(self.class.to_s.to_sym)
        # Checkout AS Array#extract_options! and original respond_with
        # implementation for a better picture of this hack
        if resources.last.is_a? Hash
          resources.last[:action_name] = action_name.to_sym
        else
          resources.push action_name: action_name.to_sym
        end
      end

      super
    end
  end
end

module Spree
  module Core
    module ControllerHelpers
      module RespondWith
        extend ActiveSupport::Concern

        included do
          cattr_accessor :spree_responders
          self.spree_responders = {}
          self.responder = Spree::Responder
        end

        module ClassMethods
          def clear_overrides!
            self.spree_responders = {}
          end

          def respond_override(options = {})
            unless options.blank?
              action_name = options.keys.first
              action_value = options.values.first

              if action_name.blank? || action_value.blank?
                raise ArgumentError, "invalid values supplied #{options.inspect}"
              end

              format_name = action_value.keys.first
              format_value = action_value.values.first

              if format_name.blank? || format_value.blank?
                raise ArgumentError, "invalid values supplied #{options.inspect}"
              end

              if format_value.is_a?(Proc)
                options = { action_name.to_sym => { format_name.to_sym => { success: format_value } } }
              end

              spree_responders.deep_merge!(name.to_sym => options)
            end
          end
        end
      end
    end
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
spree_core-3.4.6 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.6 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.5 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.4 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.3 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.2 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.1 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.0 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.5.0 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.6.0.rc1 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.5.0.rc2 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.5.0.rc1 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.4.5 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.4.4 lib/spree/core/controller_helpers/respond_with.rb
spree_core-3.4.3 lib/spree/core/controller_helpers/respond_with.rb
solidus_core-2.3.1 lib/spree/core/controller_helpers/respond_with.rb
solidus_core-2.2.2 lib/spree/core/controller_helpers/respond_with.rb
solidus_core-2.1.1 lib/spree/core/controller_helpers/respond_with.rb
solidus_core-2.0.3 lib/spree/core/controller_helpers/respond_with.rb
solidus_core-1.4.2 lib/spree/core/controller_helpers/respond_with.rb