Skip to content

Commit 578cdf8

Browse files
committed
Avoid empty Arrays in ActiveSupport::Callbacks
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.
1 parent 6c5a042 commit 578cdf8

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

activesupport/lib/active_support/callbacks.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ def check_conditionals(conditionals)
323323
end
324324

325325
def conditions_lambdas
326-
@if.map { |c| CallTemplate.build(c, self).make_lambda } +
326+
conditions =
327+
@if.map { |c| CallTemplate.build(c, self).make_lambda } +
327328
@unless.map { |c| CallTemplate.build(c, self).inverted_lambda }
329+
conditions.empty? ? EMPTY_ARRAY : conditions
328330
end
329331
end
330332

@@ -517,16 +519,18 @@ def initialize(nested = nil, call_template = nil, user_conditions = nil)
517519
@call_template = call_template
518520
@user_conditions = user_conditions
519521

520-
@before = []
521-
@after = []
522+
@before = nil
523+
@after = nil
522524
end
523525

524526
def before(before)
527+
@before ||= []
525528
@before.unshift(before)
526529
self
527530
end
528531

529532
def after(after)
533+
@after ||= []
530534
@after.push(after)
531535
self
532536
end
@@ -550,11 +554,11 @@ def expand_call_template(arg, block)
550554
end
551555

552556
def invoke_before(arg)
553-
@before.each { |b| b.call(arg) }
557+
@before&.each { |b| b.call(arg) }
554558
end
555559

556560
def invoke_after(arg)
557-
@after.each { |a| a.call(arg) }
561+
@after&.each { |a| a.call(arg) }
558562
end
559563
end
560564

0 commit comments

Comments
 (0)