Skip to content

Commit

Permalink
Allow retrieving current route name
Browse files Browse the repository at this point in the history
This is useful for generating path or URL for current route with
different parameters, while still reusing configured route definitions.
  • Loading branch information
janko committed Dec 22, 2023
1 parent 4db0ae5 commit fb0041a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rodauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def route(name=feature_name, default=name.to_s.tr('_', '-'), &block)

define_method(handle_meth) do
request.is send(route_meth) do
@route = name
check_csrf if check_csrf?
_around_rodauth do
before_rodauth
Expand Down
1 change: 1 addition & 0 deletions lib/rodauth/features/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def auth_class_eval(&block)

attr_reader :scope
attr_reader :account
attr_reader :route

def initialize(scope)
@scope = scope
Expand Down
20 changes: 20 additions & 0 deletions spec/rodauth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@
['http://www.example.com/auth/login?a%5B%5D=b&a%5B%5D=c', 'http://www.example.com/auth/login?a[]=b&a[]=c'].must_include page.text
end

it "should set current route" do
rodauth do
enable :login
login_additional_form_tags { "<span id=\"current-route\">#{route}</span>" }
end
roda do |r|
r.rodauth
r.root { "Current route: #{rodauth.route.inspect}" }
end

visit '/login'
page.find("#current-route").text.must_equal "login"

click_on 'Login'
page.find("#current-route").text.must_equal "login"

visit '/'
page.text.must_equal "Current route: nil"
end

it "should support disabling routes" do
rodauth do
enable :create_account, :internal_request
Expand Down

0 comments on commit fb0041a

Please sign in to comment.