Skip to content

Commit c2b083d

Browse files
authored
Merge pull request rails#43335 from jhawthorn/faster_conditional_callbacks
Avoid instance_exec for controller callbacks
2 parents 7b6d308 + b302448 commit c2b083d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

actionpack/lib/abstract_controller/callbacks.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ module Callbacks
3535
skip_after_callbacks_if_terminated: true
3636
end
3737

38+
class ActionFilter
39+
def initialize(actions)
40+
@actions = Array(actions).map(&:to_s).to_set
41+
end
42+
43+
def match?(controller)
44+
@actions.include?(controller.action_name)
45+
end
46+
47+
alias after match?
48+
alias before match?
49+
alias around match?
50+
end
51+
3852
module ClassMethods
3953
# If +:only+ or +:except+ are used, convert the options into the
4054
# +:if+ and +:unless+ options of ActiveSupport::Callbacks.
@@ -62,8 +76,7 @@ def _normalize_callback_options(options)
6276

6377
def _normalize_callback_option(options, from, to) # :nodoc:
6478
if from = options.delete(from)
65-
_from = Array(from).map(&:to_s).to_set
66-
from = proc { |c| _from.include? c.action_name }
79+
from = ActionFilter.new(from)
6780
options[to] = Array(options[to]).unshift(from)
6881
end
6982
end

0 commit comments

Comments
 (0)