Sha256: 0b44c3d9e8a6c1df929a7bb867515186d9ec3e68476871bff892a895a481e770

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

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_base) do
  ActionController::Base.extend Responders::ControllerMethod
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/responders-3.1.1/lib/responders/controller_method.rb
responders-3.1.1 lib/responders/controller_method.rb
responders-3.1.0 lib/responders/controller_method.rb