Sha256: 96ed105369e3c97fc69bda85c8b8265fe81ea1b42488f28ca2a700ce667dd20c

Contents?: true

Size: 1.87 KB

Versions: 25

Compression:

Stored size: 1.87 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  class Controller
    module Callbacks

      extend ActiveSupport::Concern

      include ActiveSupport::Callbacks

      included do
        define_callbacks :action, skip_after_callbacks_if_terminated: true
      end

      module ClassMethods
        def _normalize_callback_options(options)
          _normalize_callback_option(options, :only, :if)
          _normalize_callback_option(options, :except, :unless)
        end

        def _normalize_callback_option(options, from, to)
          if from = options[from]
            _from = Array(from).map(&:to_s).to_set
            from = proc { |c| _from.include?(c.action_name) }
            options[to] = Array(options[to]).unshift(from)
          end
        end

        def _insert_callbacks(callbacks, block = nil)
          options = callbacks.extract_options!
          _normalize_callback_options(options)
          callbacks.push(block) if block
          callbacks.each do |callback|
            yield callback, options
          end
        end

        [:before, :after, :around].each do |callback|
          define_method "#{callback}_action" do |*names, &blk|
            _insert_callbacks(names, blk) do |name, options|
              set_callback(:action, callback, name, options)
            end
          end

          define_method "prepend_#{callback}_action" do |*names, &blk|
            _insert_callbacks(names, blk) do |name, options|
              set_callback(:action, callback, name, options.merge(prepend: true))
            end
          end

          define_method "skip_#{callback}_action" do |*names|
            _insert_callbacks(names) do |name, options|
              skip_callback(:action, callback, name, options)
            end
          end

          alias_method :"append_#{callback}_action", :"#{callback}_action"
        end
      end

    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
stealth-1.1.6 lib/stealth/controller/callbacks.rb
stealth-1.1.5 lib/stealth/controller/callbacks.rb
stealth-1.1.4 lib/stealth/controller/callbacks.rb
stealth-1.1.3 lib/stealth/controller/callbacks.rb
stealth-1.1.2 lib/stealth/controller/callbacks.rb
stealth-1.1.1 lib/stealth/controller/callbacks.rb
stealth-1.1.0 lib/stealth/controller/callbacks.rb
stealth-1.1.0.rc3 lib/stealth/controller/callbacks.rb
stealth-1.1.0.rc2 lib/stealth/controller/callbacks.rb
stealth-1.1.0.rc1 lib/stealth/controller/callbacks.rb
stealth-1.0.4 lib/stealth/controller/callbacks.rb
stealth-1.0.3 lib/stealth/controller/callbacks.rb
stealth-1.0.2 lib/stealth/controller/callbacks.rb
stealth-1.0.1 lib/stealth/controller/callbacks.rb
stealth-1.0.0 lib/stealth/controller/callbacks.rb
stealth-1.0.0.rc1 lib/stealth/controller/callbacks.rb
stealth-1.0.0.pre2 lib/stealth/controller/callbacks.rb
stealth-1.0.0.pre1 lib/stealth/controller/callbacks.rb
stealth-0.10.6 lib/stealth/controller/callbacks.rb
stealth-0.10.5 lib/stealth/controller/callbacks.rb