From 012b1e3281115b1b3fd5a13a0058e18c13a0b46f Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 15 Nov 2019 10:18:37 -0800 Subject: [PATCH] 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 #37616 --- actionpack/test/controller/filters_test.rb | 25 +++++++++++++++++++++ guides/source/action_controller_overview.md | 6 +++++ 2 files changed, 31 insertions(+) diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 40443a9397..4d288d0543 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -546,6 +546,31 @@ class FilterTest < ActionController::TestCase end end + def test_around_action_can_use_yield_inline_with_passed_action + controller = Class.new(ActionController::Base) do + around_action do |c, a| + c.values << "before" + a.call + c.values << "after" + end + + def index + values << "action" + render inline: "index" + end + + def values + @values ||= [] + end + end.new + + assert_nothing_raised do + test_process(controller, "index") + end + + assert_equal ["before", "action", "after"], controller.values + end + def test_after_actions_are_not_run_if_around_action_does_not_yield controller = NonYieldingAroundFilterController.new test_process(controller, "index") diff --git a/guides/source/action_controller_overview.md b/guides/source/action_controller_overview.md index b3c9ddf8ee..281b6314fe 100644 --- a/guides/source/action_controller_overview.md +++ b/guides/source/action_controller_overview.md @@ -753,6 +753,12 @@ end 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. +Specifically for `around_action`, the block also yields in the `action`: + +```ruby +around_action { |_controller, action| time(&action) } +``` + 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: ```ruby