Sha256: 3da117a76460948a2104f254cfc03b739bfef1c982d5edfe5b4c82e159a25269

Contents?: true

Size: 1.22 KB

Versions: 39

Compression:

Stored size: 1.22 KB

Contents

module Spree
  class Responder < ::ActionController::Responder #:nodoc:
    attr_accessor :on_success, :on_failure

    def initialize(controller, resources, options = {})
      super

      class_name = controller.class.name.to_sym
      action_name = options.delete(:action_name)

      if result = Spree::BaseController.spree_responders[class_name].try(:[], action_name).try(:[], format.to_sym)
        self.on_success = handler(controller, result, :success)
        self.on_failure = handler(controller, result, :failure)
      end
    end

    def to_html
      super && return unless on_success || on_failure
      has_errors? ? controller.instance_exec(&on_failure) : controller.instance_exec(&on_success)
    end

    def to_format
      super && return unless on_success || on_failure
      has_errors? ? controller.instance_exec(&on_failure) : controller.instance_exec(&on_success)
    end

    private

    def handler(controller, result, status)
      return result if result.respond_to? :call

      case result
      when Hash
        if result[status].is_a? Symbol
          controller.method(result[status])
        else
          result[status]
        end
      when Symbol
        controller.method(result)
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
spree_core-3.7.14.1 lib/spree/responder.rb
spree_core-3.7.14 lib/spree/responder.rb
spree_core-3.7.13 lib/spree/responder.rb
spree_core-3.7.12 lib/spree/responder.rb
spree_core-3.7.11 lib/spree/responder.rb
spree_core-3.4.6 lib/spree/responder.rb
spree_core-3.7.10 lib/spree/responder.rb
spree_core-3.7.9 lib/spree/responder.rb
spree_core-3.7.8 lib/spree/responder.rb
spree_core-3.7.7 lib/spree/responder.rb
spree_core-3.7.6 lib/spree/responder.rb
spree_core-3.7.5 lib/spree/responder.rb
spree_core-3.7.4 lib/spree/responder.rb
spree_core-3.7.3 lib/spree/responder.rb
spree_core-3.7.2 lib/spree/responder.rb
spree_core-3.7.1 lib/spree/responder.rb
spree_core-3.7.0 lib/spree/responder.rb
spree_core-3.7.0.rc3 lib/spree/responder.rb
spree_core-3.7.0.rc2 lib/spree/responder.rb
spree_core-3.7.0.rc1 lib/spree/responder.rb