Skip to content

Commit 92c5e8c

Browse files
committed
Add ability to choose when commands are runned
1 parent 2f11f21 commit 92c5e8c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,30 @@ end
457457
Remember that commands are invoked by a controller [before action filter](https://guides.rubyonrails.org/action_controller_overview.html#filters).
458458
That means controller rendering from inside a command halts the standard request cycle.
459459

460+
### Commands Lifecycle
461+
462+
By default, commands are runned before existing `before_action` callbacks (as
463+
`TurboBoost::Commands::Controller` is automatically included in `ActionController::Base`). You
464+
can configure this behavior to have more control over when commands are runned:
465+
```ruby
466+
# config/initializers/turbo_boost_commands.rb
467+
TurboBoost::Commands.config.tap do |config|
468+
config[:run_commands_before_controller_callbacks] = false
469+
end
470+
```
471+
472+
Then, you'll need to include `TurboBoost::Commands::Controller` manually where you want it:
473+
474+
```ruby
475+
class ApplicationController < ActionController::Base
476+
before_action :action1
477+
before_action :action2
478+
before_action :action3
479+
480+
include TurboBoost::Commands::Controller
481+
end
482+
```
483+
460484
### Broadcasting Turbo Streams
461485

462486
You can also broadcast Turbo Streams to subscribed users from a command.

lib/turbo_boost/commands/engine.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ class Engine < ::Rails::Engine
2525
config.turbo_boost_commands[:apply_client_state_overrides] = false
2626
config.turbo_boost_commands[:apply_server_state_overrides] = false
2727

28+
# Choose whether we want to run commands before existing `before_action`
29+
# callacks
30+
config.turbo_boost_commands[:run_commands_before_controller_callbacks] = true
31+
2832
initializer "turbo_boost_commands.configuration" do
2933
Mime::Type.register "text/vnd.turbo-boost.html", :turbo_boost
3034

3135
ActiveSupport.on_load :action_controller_base do
3236
# `self` is ActionController::Base
33-
include TurboBoost::Commands::Controller
37+
unless TurboBoost::Commands.config[:run_commands_before_controller_callbacks] == false
38+
include TurboBoost::Commands::Controller
39+
end
40+
3441
helper TurboBoost::Commands::ApplicationHelper
3542
end
3643

0 commit comments

Comments
 (0)