Skip to content

Commit

Permalink
Avoid empty Arrays in ActiveSupport::Callbacks
Browse files Browse the repository at this point in the history
ActiveSupport::Callbacks often ended up with empty Arrays: on callbacks
without a conditional, and on callback sequences which had no before
and/or after callbacks.
  • Loading branch information
jhawthorn committed Oct 21, 2023
1 parent 6c5a042 commit 578cdf8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions activesupport/lib/active_support/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ def check_conditionals(conditionals)
end

def conditions_lambdas
@if.map { |c| CallTemplate.build(c, self).make_lambda } +
conditions =
@if.map { |c| CallTemplate.build(c, self).make_lambda } +
@unless.map { |c| CallTemplate.build(c, self).inverted_lambda }
conditions.empty? ? EMPTY_ARRAY : conditions
end
end

Expand Down Expand Up @@ -517,16 +519,18 @@ def initialize(nested = nil, call_template = nil, user_conditions = nil)
@call_template = call_template
@user_conditions = user_conditions

@before = []
@after = []
@before = nil
@after = nil
end

def before(before)
@before ||= []
@before.unshift(before)
self
end

def after(after)
@after ||= []
@after.push(after)
self
end
Expand All @@ -550,11 +554,11 @@ def expand_call_template(arg, block)
end

def invoke_before(arg)
@before.each { |b| b.call(arg) }
@before&.each { |b| b.call(arg) }
end

def invoke_after(arg)
@after.each { |a| a.call(arg) }
@after&.each { |a| a.call(arg) }
end
end

Expand Down

0 comments on commit 578cdf8

Please sign in to comment.