Skip to content

Commit 4ea5604

Browse files
committed
Fix routing with the nopassword route helper
1 parent 5d8321e commit 4ea5604

4 files changed

Lines changed: 57 additions & 10 deletions

File tree

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add the route to your `config/routes.rb`:
2525
nopassword EmailAuthenticationsController
2626
```
2727

28-
Restart the development server and head to `http://localhost:3000/email_authentication/new`.
28+
Restart the development server and head to `http://localhost:3000/email_authentications/new`.
2929

3030
## How It Works
3131

@@ -148,17 +148,21 @@ class SessionsController < ApplicationController
148148
end
149149
```
150150

151-
Then define your own routes:
151+
Then use `nopassword` with your controller — the routes come with the concern:
152152

153153
```ruby
154154
# config/routes.rb
155-
resource :session, only: [:new, :create, :destroy] do
156-
get ":id", action: :show, on: :collection
157-
patch ":id", action: :update, on: :collection
158-
end
155+
nopassword SessionsController # generates /sessions routes
156+
```
157+
158+
The routes are derived from your controller name. To customize the path:
159+
160+
```ruby
161+
# config/routes.rb
162+
nopassword SessionsController, path: "login" # generates /login routes
159163
```
160164

161-
Or skip the concern entirely and use the models directly with your own views:
165+
Or skip the concern entirely and use the models directly with your own views and routes:
162166

163167
```ruby
164168
class SessionsController < ApplicationController

app/controllers/concerns/nopassword/email_authentication.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module EmailAuthentication
1818
included do
1919
include Routable
2020

21-
routes.draw do |controller|
22-
resources :email_authentications, controller:, except: :destroy do
21+
routes.draw do |controller, resource_name, **options|
22+
resources resource_name, controller:, except: :destroy, **options do
2323
collection do
2424
delete :destroy
2525
end

lib/extensions/action_dispatch/routing/mapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Routing
33
class Mapper
44
def nopassword(controller_class, *, **, &)
55
if controller_class.respond_to?(:routes) and controller_class.routes.routable?
6-
instance_exec(controller_class.controller_path, *, **, &controller_class.routes)
6+
instance_exec(controller_class.controller_path, controller_class.controller_name, *, **, &controller_class.routes)
77
else
88
raise ArgumentError, "controller_class must respond to :routes"
99
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "nopassword routes" do
4+
describe "path option" do
5+
before do
6+
Rails.application.routes.draw do
7+
nopassword NoPassword::EmailAuthenticationsController, path: "login"
8+
end
9+
end
10+
11+
after do
12+
Rails.application.reload_routes!
13+
end
14+
15+
it "generates routes with custom path" do
16+
expect(get: "/login/new").to route_to(controller: "nopassword/email_authentications", action: "new")
17+
expect(post: "/login").to route_to(controller: "nopassword/email_authentications", action: "create")
18+
expect(get: "/login/abc123").to route_to(controller: "nopassword/email_authentications", action: "show", id: "abc123")
19+
expect(patch: "/login/abc123").to route_to(controller: "nopassword/email_authentications", action: "update", id: "abc123")
20+
expect(delete: "/login").to route_to(controller: "nopassword/email_authentications", action: "destroy")
21+
end
22+
end
23+
24+
describe "default routes" do
25+
before do
26+
Rails.application.routes.draw do
27+
nopassword NoPassword::EmailAuthenticationsController
28+
end
29+
end
30+
31+
after do
32+
Rails.application.reload_routes!
33+
end
34+
35+
it "generates routes based on controller name" do
36+
expect(get: "/email_authentications/new").to route_to(controller: "nopassword/email_authentications", action: "new")
37+
expect(post: "/email_authentications").to route_to(controller: "nopassword/email_authentications", action: "create")
38+
expect(get: "/email_authentications/abc123").to route_to(controller: "nopassword/email_authentications", action: "show", id: "abc123")
39+
expect(patch: "/email_authentications/abc123").to route_to(controller: "nopassword/email_authentications", action: "update", id: "abc123")
40+
expect(delete: "/email_authentications").to route_to(controller: "nopassword/email_authentications", action: "destroy")
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)