diff --git a/README.md b/README.md index d0f88b5f..e85b8d52 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,30 @@ end Remember that commands are invoked by a controller [before action filter](https://guides.rubyonrails.org/action_controller_overview.html#filters). That means controller rendering from inside a command halts the standard request cycle. +### Commands Lifecycle + +By default, commands are run before existing `before_action` callbacks (as +`TurboBoost::Commands::Controller` is automatically included in `ActionController::Base`). You +can configure this behavior to have more control over when commands are run: +```ruby +# config/initializers/turbo_boost_commands.rb +TurboBoost::Commands.config.tap do |config| + config[:run_commands_before_controller_callbacks] = false +end +``` + +Then, you'll need to include `TurboBoost::Commands::Controller` manually where you want it: + +```ruby +class ApplicationController < ActionController::Base + before_action :action1 + before_action :action2 + before_action :action3 + + include TurboBoost::Commands::Controller +end +``` + ### Broadcasting Turbo Streams You can also broadcast Turbo Streams to subscribed users from a command. diff --git a/lib/turbo_boost/commands/engine.rb b/lib/turbo_boost/commands/engine.rb index 450740e5..9c48c9f7 100644 --- a/lib/turbo_boost/commands/engine.rb +++ b/lib/turbo_boost/commands/engine.rb @@ -25,12 +25,19 @@ class Engine < ::Rails::Engine config.turbo_boost_commands[:apply_client_state_overrides] = false config.turbo_boost_commands[:apply_server_state_overrides] = false + # Choose whether we want to run commands before existing `before_action` + # callacks + config.turbo_boost_commands[:run_commands_before_controller_callbacks] = true + initializer "turbo_boost_commands.configuration" do Mime::Type.register "text/vnd.turbo-boost.html", :turbo_boost ActiveSupport.on_load :action_controller_base do # `self` is ActionController::Base - include TurboBoost::Commands::Controller + unless TurboBoost::Commands.config[:run_commands_before_controller_callbacks] == false + include TurboBoost::Commands::Controller + end + helper TurboBoost::Commands::ApplicationHelper end