aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/abstract/callbacks.rb
blob: fd7e5ebfdab0c8760d0c35f8b66dc0445417a23d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module AbstractController
  module Callbacks
    extend ActiveSupport::Concern

    include ActiveSupport::NewCallbacks

    included do
      define_callbacks :process_action, "response_body"
    end

    def process_action(method_name)
      _run_process_action_callbacks(method_name) do
        super
      end
    end

    module ClassMethods
      def _normalize_callback_options(options)
        if only = options[:only]
          only = Array(only).map {|o| "action_name == '#{o}'"}.join(" || ")
          options[:per_key] = {:if => only}
        end
        if except = options[:except]
          except = Array(except).map {|e| "action_name == '#{e}'"}.join(" || ")
          options[:per_key] = {:unless => except}
        end
      end

      def skip_filter(*names, &blk)
        skip_before_filter(*names, &blk)
        skip_after_filter(*names, &blk)
        skip_around_filter(*names, &blk)
      end

      def _insert_callbacks(names, block)
        options = names.last.is_a?(Hash) ? names.pop : {}
        _normalize_callback_options(options)
        names.push(block) if block
        names.each do |name|
          yield name, options
        end
      end

      [:before, :after, :around].each do |filter|
        class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
          def #{filter}_filter(*names, &blk)
            _insert_callbacks(names, blk) do |name, options|
              set_callback(:process_action, :#{filter}, name, options)
            end
          end

          def prepend_#{filter}_filter(*names, &blk)
            _insert_callbacks(names, blk) do |name, options|
              set_callback(:process_action, :#{filter}, name, options.merge(:prepend => true))
            end
          end

          def skip_#{filter}_filter(*names, &blk)
            _insert_callbacks(names, blk) do |name, options|
              skip_callback(:process_action, :#{filter}, name, options)
            end
          end

          alias_method :append_#{filter}_filter, :#{filter}_filter
        RUBY_EVAL
      end
    end
  end
end