Sha256: aae9afcd39e1b62e5970ad45dc7ca03b46814301f32e5e51e2d1262370c751e1

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

module Responders
  module ControllerMethod
    # Adds the given responders to the current controller's responder, allowing you to cherry-pick
    # which responders you want per controller.
    #
    #   class InvitationsController < ApplicationController
    #     responders :flash, :http_cache
    #   end
    #
    # Takes symbols and strings and translates them to VariableResponder (eg. :flash becomes FlashResponder).
    # Also allows passing in the responders modules in directly, so you could do:
    #
    #    responders FlashResponder, HttpCacheResponder
    #
    # Or a mix of both methods:
    #
    #    responders :flash, MyCustomResponder
    #
    def responders(*responders)
      self.responder = responders.inject(Class.new(responder)) do |klass, responder|
        responder = case responder
          when Module
            responder
          when String, Symbol
            Responders.const_get("#{responder.to_s.camelize}Responder")
          else
            raise "responder has to be a string, a symbol or a module"
          end

        klass.send(:include, responder)
        klass
      end
    end
  end
end

ActiveSupport.on_load(:action_controller) do
  ActionController::Base.extend Responders::ControllerMethod
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
responders-2.4.1 lib/responders/controller_method.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/responders-2.4.0/lib/responders/controller_method.rb
responders-2.4.0 lib/responders/controller_method.rb
responders-2.3.0 lib/responders/controller_method.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/responders-2.2.0/lib/responders/controller_method.rb
responders-2.2.0 lib/responders/controller_method.rb
responders-2.1.2 lib/responders/controller_method.rb
responders-2.1.1 lib/responders/controller_method.rb