Sha256: c9d14cebafc159c7c74708a607eefb7eda0b8afc9666be57c02970a15d4ab869

Contents?: true

Size: 1.99 KB

Versions: 9

Compression:

Stored size: 1.99 KB

Contents

# Simpified version of callbacks
# Does not support if
class Jets::Controller
  module Callbacks
    extend ActiveSupport::Concern
    included do
      class_attribute :before_actions, default: []
      class_attribute :after_actions, default: []

      class << self
        def before_action(meth, options={})
          self.before_actions += [[meth, options]]
        end

        def prepend_before_action(meth, options={})
          self.before_actions = [[meth, options]] + self.before_actions
        end

        def skip_before_action(meth)
          self.before_actions = self.before_actions.reject { |el| el.first.to_s == meth.to_s }
        end

        alias_method :append_before_action, :before_action

        def after_action(meth, options={})
          self.after_actions += [[meth, options]]
        end

        def prepend_after_action(meth, options={})
          self.after_actions = [[meth, options]] + self.after_actions
        end

        def skip_after_action(meth)
          self.after_actions = self.after_actions.reject { |el| el.first.to_s == meth.to_s }
        end

        alias_method :append_after_action, :after_action
      end
    end # included

    # Instance Methods
    # define run_before_actions and run_after_actions
    # returns true if all actions were run, false if break_if condition yielded `true`
    [:before, :after].each do |type|
      define_method "run_#{type}_actions" do |break_if: nil|
        called_method = @meth.to_sym
        callbacks = self.class.send("#{type}_actions")
        callbacks.each do |array|
          callback, options = array

          except = options[:except]
          next if except && except.include?(called_method)

          only = options[:only]
          if only
            send(callback) if only.include?(called_method)
          else
            send(callback)
          end
          @last_callback_name = callback

          return false if !break_if.nil? && break_if.call
        end
        true
      end
    end
  end # ClassOptions
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
jets_bb_fork-0.0.4 lib/jets/controller/callbacks.rb
jets-2.0.4 lib/jets/controller/callbacks.rb
jets-2.0.3 lib/jets/controller/callbacks.rb
jets-2.0.1 lib/jets/controller/callbacks.rb
jets-2.0.0 lib/jets/controller/callbacks.rb
jets-1.9.32 lib/jets/controller/callbacks.rb
jets-1.9.31 lib/jets/controller/callbacks.rb
jets-1.9.30 lib/jets/controller/callbacks.rb
jets-1.9.29 lib/jets/controller/callbacks.rb