Skip to content

Commit 012b1e3

Browse files
committed
Add guide for inline around_action
This commit adds a test to ensure the behavior of inline `around_action` calls, as well as a change to the guides to call out this alternate use of `around_action`. Closes rails#37616
1 parent b674f04 commit 012b1e3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

actionpack/test/controller/filters_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,31 @@ def test_non_yielding_around_actions_do_not_raise
546546
end
547547
end
548548

549+
def test_around_action_can_use_yield_inline_with_passed_action
550+
controller = Class.new(ActionController::Base) do
551+
around_action do |c, a|
552+
c.values << "before"
553+
a.call
554+
c.values << "after"
555+
end
556+
557+
def index
558+
values << "action"
559+
render inline: "index"
560+
end
561+
562+
def values
563+
@values ||= []
564+
end
565+
end.new
566+
567+
assert_nothing_raised do
568+
test_process(controller, "index")
569+
end
570+
571+
assert_equal ["before", "action", "after"], controller.values
572+
end
573+
549574
def test_after_actions_are_not_run_if_around_action_does_not_yield
550575
controller = NonYieldingAroundFilterController.new
551576
test_process(controller, "index")

guides/source/action_controller_overview.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,12 @@ end
753753

754754
Note that the filter in this case uses `send` because the `logged_in?` method is private and the filter does not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.
755755

756+
Specifically for `around_action`, the block also yields in the `action`:
757+
758+
```ruby
759+
around_action { |_controller, action| time(&action) }
760+
```
761+
756762
The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex and cannot be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
757763

758764
```ruby

0 commit comments

Comments
 (0)