Sha256: 9ddb611769bf9ce2a22c8859bb1bd521b374a01465376bc3d1d57b1a0f91a296

Contents?: true

Size: 1.23 KB

Versions: 183

Compression:

Stored size: 1.23 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(:[], self.format.to_sym)
        self.on_success = handler(controller, result, :success)
        self.on_failure = handler(controller, result, :failure)
      end
    end

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

    def to_format
      super and return if !(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

183 entries across 183 versions & 3 rubygems

Version Path
spree_core-3.2.9 lib/spree/responder.rb
spree_core-3.1.14 lib/spree/responder.rb
spree_core-3.3.6 lib/spree/responder.rb
spree_core-3.2.8 lib/spree/responder.rb
spree_core-3.1.13 lib/spree/responder.rb
spree_core-3.3.5 lib/spree/responder.rb
spree_core-3.3.4 lib/spree/responder.rb
spree_core-3.2.7 lib/spree/responder.rb
spree_core-3.1.12 lib/spree/responder.rb
solidus_core-1.2.3 lib/spree/responder.rb
solidus_core-1.1.4 lib/spree/responder.rb
solidus_core-1.0.7 lib/spree/responder.rb
spree_core-3.3.3 lib/spree/responder.rb
spree_core-3.2.6 lib/spree/responder.rb
spree_core-3.1.11 lib/spree/responder.rb
spree_core-3.1.10 lib/spree/responder.rb
spree_core-3.3.2 lib/spree/responder.rb
spree_core-3.2.5 lib/spree/responder.rb
spree_core-3.1.9 lib/spree/responder.rb
spree_core-3.3.1 lib/spree/responder.rb