ActiveSupport callbacks - after being executed even if a before callback aborts

I’ve noticed something that I’m really surprised I hadn’t spotted before but I wanted to just run this behaviour past anyone with any knowledge of the ActiveSupport callback system.

Hopefully the pseudo code below explains this situation more easily than I can describe it.

define_callbacks :event

before_event { throw :abort }
before_event { do_something  } # this won't run
after_event { do_something_else } # this WILL run

run_callbacks(:event) do 
  # this won't run

Basically, if a before callback halts the chain, additional before callbacks aren’t executed, the block provided to run_callbacks is not executed but the after callbacks ARE executed.

Looking at the source it does seem like this is the intended behaviour because the invoke_after doesn’t check whether the before chain was halted.

Having said that, I don’t think any of this is very logical.

1 Like