Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to choose when commands are run #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mansakondo
Copy link

@mansakondo mansakondo commented Apr 15, 2023

Summary

By default, commands are run before existing before_action callbacks (as TurboBoost::Commands::Controller is automatically included in ActionController::Base). This PR allows configuring this behavior to have more control over when commands are run:

# config/initializers/turbo_boost_commands.rb
TurboBoost::Commands.config.tap do |config|
  config[:run_commands_before_controller_callbacks] = false
end

Then, we can include TurboBoost::Commands::Controller manually where we want it:

class ApplicationController < ActionController::Base
  before_action :action1
  before_action :action2
  before_action :action3

  include TurboBoost::Commands::Controller
end

@mansakondo mansakondo changed the title Add ability to choose when commands are runned Add the ability to choose when commands are run Apr 16, 2023
@hopsoft
Copy link
Owner

hopsoft commented Jun 23, 2023

I like this idea, but can't you achieve the same behavior by simply moving where/when you include TurboBoost::Commands::Controller? I don't think the config option is needed.

Another approach would be to use prepend_before_action in your controller(s) for any callbacks you'd like to run before TurboBoost does. Thoughts?

@mansakondo
Copy link
Author

mansakondo commented Jun 27, 2023

Thanks for taking the time to look at this.

The config option is not needed if you're ok with not including the module by default, which would require users to include it manually. Otherwise, the config option would allow users to prevent the default behavior (TurboBoost::Commands::Controller included automatically) if needed.

Using prevent_before_action instead of before_action works too, but I was hoping to find a solution that would not require users to make this change as it will be more confusing, because the actions will need to be declared in the reversed order.

Before:

class ApplicationController < ActionController::Base
  before_action :action1
  before_action :action2
  before_action :action3
end

After:

class ApplicationController < ActionController::Base
  prepend_before_action :action3
  prepend_before_action :action2
  prepend_before_action :action1
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants