Skip to content

Commit

Permalink
Enable per-action controller callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Nov 26, 2023
1 parent 117a85a commit aefb127
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/rodauth/rails/feature/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Callbacks
private

def _around_rodauth
rails_controller_instance.instance_variable_set(:@_action_name, rails_path_parameters[:action])

rails_controller_around { super }
end

Expand Down
22 changes: 15 additions & 7 deletions test/integration/callbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ class CallbacksTest < IntegrationTest
visit "/login"

assert_match "login-form", page.html
assert_equal 200, page.status_code
assert_equal "true", page.response_headers["X-Before-Action"]
assert_equal "true", page.response_headers["X-After-Action"]
assert_equal "true", page.response_headers["X-Before-Around-Action"]
assert_equal "true", page.response_headers["X-After-Around-Action"]
assert_equal 200, page.status_code
assert_equal "true", page.response_headers["X-Before-Action"]
assert_equal "true", page.response_headers["X-After-Action"]
assert_equal "true", page.response_headers["X-Before-Around-Action"]
assert_equal "true", page.response_headers["X-After-Around-Action"]
end

test "runs callbacks for specific actions" do
visit "/create-account"
assert_equal "true", page.response_headers["X-Before-Specific-Action"]

visit "/login"
assert_nil page.response_headers["X-Before-Specific-Action"]
end

test "handles rendering in callback chain" do
visit "/login?early_return=true&fail=true"

assert_equal "early return", page.html
assert_equal 201, page.status_code
assert_equal "true", page.response_headers["X-Before-Action"]
assert_equal 201, page.status_code
assert_equal "true", page.response_headers["X-Before-Action"]
end
end
5 changes: 5 additions & 0 deletions test/rails_app/app/controllers/rodauth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class RodauthController < ApplicationController
before_action :before_route
after_action :after_route
around_action :around_route
before_action :before_specific_route, only: [:create_account]

rescue_from NotImplementedError do
render plain: "rescued response", status: 500
Expand All @@ -27,6 +28,10 @@ def around_route
response.headers["X-After-Around-Action"] = "true"
end

def before_specific_route
response.header["X-Before-Specific-Action"] = "true"
end

def some_method
"controller method"
end
Expand Down

0 comments on commit aefb127

Please sign in to comment.