mc9
mc9

Reputation: 6341

Rails around_action includes before, and after callbacks?

Does around_action in ApplicationController::Base include before_action and after_action? I understand that around_action wraps around the specified action, but would like to know if it also wraps around the before, and after callbacks associated with that action.

For instance, let's look at a code modified from Rails documentation:

class ChangesController < ApplicationController
  before_action :some_callback, only: show
  around_action :wrap_in_transaction, only: :show

  ...

  private

  def wrap_in_transaction
    yield unless true
  end
end

Will some_callback ever be executed?

Upvotes: 3

Views: 3175

Answers (1)

Brad Werth
Brad Werth

Reputation: 17647

Yes, some_callback will be executed. These methods are completely unaware of one another, and will be performed in the order they are written.

It may interest you to see the code that rails uses to do this. You can find it at https://github.com/rails/rails/blob/cdaab2c479c819b04cc72a97c52b804832365cef/actionpack/lib/abstract_controller/callbacks.rb#L180. You will notice that they both call the _insert_callbacks method (https://github.com/rails/rails/blob/cdaab2c479c819b04cc72a97c52b804832365cef/actionpack/lib/abstract_controller/callbacks.rb#L87).

Also, why not just try it using console output or something? This type of thing should be very easy to verify with a quick trial (which is, I suspect, the reason behind this question's downvote).

Upvotes: 4

Related Questions