human_routes 0.0.1 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +3 -0
- data/.rubocop.yml +2 -4
- data/README.md +57 -5
- data/human_routes.gemspec +2 -0
- data/lib/human_routes/context.rb +155 -91
- data/lib/human_routes/extensions.rb +1 -5
- data/lib/human_routes/version.rb +1 -1
- metadata +9 -8
- data/.tool-versions +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c9732c44671356336d93d93f66551dc7ac4f05e430b0abe73cbff5d4aa32fe9
|
4
|
+
data.tar.gz: ec8df768c2dfbdea30fd6e020545fc9df04669da1236c1266877a4046e987589
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c66820fb3a6224c242ce241f7d9256946e96d4c27e855b7b5ef5548e7a8ad2c91b036faf6897c2442bb0d33ca6b5889d6e865de8dca5ba85430713f36afc32f
|
7
|
+
data.tar.gz: 594bbc22352cd07cc313768f7768c308f1f965763d4ed6da2d773ef2b7132e76aba914c95f8fc6413a4335467e2f7ec6e590d30df777601be4d11daaec8bf496
|
data/.github/FUNDING.yml
ADDED
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -16,11 +16,15 @@ gem "human_routes"
|
|
16
16
|
|
17
17
|
And then execute:
|
18
18
|
|
19
|
-
|
19
|
+
```bash
|
20
|
+
bundle install
|
21
|
+
```
|
20
22
|
|
21
23
|
Or install it yourself as:
|
22
24
|
|
23
|
-
|
25
|
+
```bash
|
26
|
+
gem install human_routes
|
27
|
+
```
|
24
28
|
|
25
29
|
## Usage
|
26
30
|
|
@@ -36,7 +40,7 @@ end
|
|
36
40
|
|
37
41
|
This will generate a few routes different routes, as you can see below:
|
38
42
|
|
39
|
-
```
|
43
|
+
```console
|
40
44
|
$ rails routes
|
41
45
|
Prefix Verb URI Pattern Controller#Action
|
42
46
|
new_signup GET /signup/new signup#new
|
@@ -64,7 +68,7 @@ The above could added in one line with `route(:blogs) { all }`.
|
|
64
68
|
|
65
69
|
This will generate the following routes:
|
66
70
|
|
67
|
-
```
|
71
|
+
```console
|
68
72
|
$ rails routes
|
69
73
|
Prefix Verb URI Pattern Controller#Action
|
70
74
|
new_blog GET /blogs/new blogs#new
|
@@ -104,6 +108,54 @@ Rails.application.routes.draw do
|
|
104
108
|
route :pages, module: "customer" do
|
105
109
|
all
|
106
110
|
end
|
111
|
+
|
112
|
+
# Additionally, you can use `:name` to give a different name to
|
113
|
+
# namespaced controllers. This way routes can be generated using a shallow
|
114
|
+
# path instead of the usual `admin/reports`.
|
115
|
+
route "admin/reports", name: "reports" do
|
116
|
+
all
|
117
|
+
end
|
118
|
+
|
119
|
+
# Singular routes also are detected and generated accordingly.
|
120
|
+
# This will generate the following routes:
|
121
|
+
#
|
122
|
+
# GET /profile profile_path
|
123
|
+
# POST /profile/new
|
124
|
+
# GET /profile/edit edit_profile_path
|
125
|
+
# POST /profile/edit
|
126
|
+
# GET /profile/remove remove_profile_path
|
127
|
+
# POST /profile/remove
|
128
|
+
route "profile" do
|
129
|
+
all
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
Sometimes you want to create routes without the action (e.g. `new` or `edit`);
|
135
|
+
in this case, you can use `bare: true`.
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
Rails.application.routes.draw do
|
139
|
+
# This will generate the following routes:
|
140
|
+
#
|
141
|
+
# GET /login new_login_path
|
142
|
+
# POST /login
|
143
|
+
route :login do
|
144
|
+
create bare: true
|
145
|
+
end
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
You may want to add another paths not covered by the default helpers. In that
|
150
|
+
case, you can use `get` and `post`.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
Rails.application.routes.draw do
|
154
|
+
route :login do
|
155
|
+
create as: "login", bare: true
|
156
|
+
get :verify_email #=> /login/verify-email
|
157
|
+
get :check_inbox #=> /login/check-inbox
|
158
|
+
end
|
107
159
|
end
|
108
160
|
```
|
109
161
|
|
@@ -122,7 +174,7 @@ git commits and tags, and push the `.gem` file to
|
|
122
174
|
## Contributing
|
123
175
|
|
124
176
|
Bug reports and pull requests are welcome on GitHub at
|
125
|
-
https://github.com/fnando/human_routes
|
177
|
+
<https://github.com/fnando/human_routes>. This project is intended to be a safe,
|
126
178
|
welcoming space for collaboration, and contributors are expected to adhere to
|
127
179
|
the
|
128
180
|
[code of conduct](https://github.com/fnando/human_routes/blob/master/CODE_OF_CONDUCT.md).
|
data/human_routes.gemspec
CHANGED
@@ -7,6 +7,8 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.version = HumanRoutes::VERSION
|
8
8
|
spec.authors = ["Nando Vieira"]
|
9
9
|
spec.email = ["me@fnando.com"]
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
11
|
+
spec.metadata = {"rubygems_mfa_required" => "true"}
|
10
12
|
|
11
13
|
spec.summary = "I say no to REST for client-facing urls."
|
12
14
|
spec.description = spec.summary
|
data/lib/human_routes/context.rb
CHANGED
@@ -2,16 +2,25 @@
|
|
2
2
|
|
3
3
|
module HumanRoutes
|
4
4
|
class Context
|
5
|
-
attr_reader :controller
|
6
|
-
attr_reader :options
|
5
|
+
attr_reader :controller, :options, :router, :named_routes
|
7
6
|
|
8
|
-
def initialize(controller, options = {})
|
7
|
+
def initialize(router, controller, options = {})
|
8
|
+
@router = router
|
9
9
|
@controller = controller
|
10
10
|
@options = options
|
11
|
+
@named_routes = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def controller_name
|
15
|
+
@controller_name ||= options.delete(:name) { controller.to_s }
|
11
16
|
end
|
12
17
|
|
13
18
|
def singular_controller_name
|
14
|
-
@singular_controller_name ||=
|
19
|
+
@singular_controller_name ||= controller_name.singularize
|
20
|
+
end
|
21
|
+
|
22
|
+
def resource?
|
23
|
+
controller_name == singular_controller_name
|
15
24
|
end
|
16
25
|
|
17
26
|
def routes
|
@@ -20,137 +29,161 @@ module HumanRoutes
|
|
20
29
|
|
21
30
|
def create(*args)
|
22
31
|
path, name, options = extract_route_args(
|
23
|
-
|
32
|
+
segment: :new,
|
24
33
|
default_name: "new_#{singular_controller_name}",
|
25
34
|
args: args
|
26
35
|
)
|
27
36
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
via: :post,
|
42
|
-
controller: controller,
|
43
|
-
action: :create,
|
44
|
-
as: ""
|
45
|
-
}.merge(options)
|
46
|
-
]
|
37
|
+
match path, {
|
38
|
+
via: :get,
|
39
|
+
controller: controller,
|
40
|
+
action: :new,
|
41
|
+
as: name
|
42
|
+
}.merge(options)
|
43
|
+
|
44
|
+
match path, {
|
45
|
+
via: :post,
|
46
|
+
controller: controller,
|
47
|
+
action: :create,
|
48
|
+
as: ""
|
49
|
+
}.merge(options)
|
47
50
|
end
|
48
51
|
|
49
52
|
def update(*args)
|
50
53
|
path, name, options = extract_route_args(
|
51
|
-
|
54
|
+
segment: :edit,
|
52
55
|
default_name: "edit_#{singular_controller_name}",
|
53
56
|
args: args
|
54
57
|
)
|
55
58
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
via: :post,
|
70
|
-
controller: controller,
|
71
|
-
action: :update,
|
72
|
-
as: ""
|
73
|
-
}.merge(options)
|
74
|
-
]
|
59
|
+
match path, {
|
60
|
+
via: :get,
|
61
|
+
controller: controller,
|
62
|
+
action: :edit,
|
63
|
+
as: name
|
64
|
+
}.merge(options)
|
65
|
+
|
66
|
+
match path, {
|
67
|
+
via: :post,
|
68
|
+
controller: controller,
|
69
|
+
action: :update,
|
70
|
+
as: ""
|
71
|
+
}.merge(options)
|
75
72
|
end
|
76
73
|
|
77
74
|
def remove(*args)
|
78
75
|
path, name, options = extract_route_args(
|
79
|
-
|
76
|
+
segment: :remove,
|
80
77
|
default_name: "remove_#{singular_controller_name}",
|
81
78
|
args: args
|
82
79
|
)
|
83
80
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
via: :post,
|
98
|
-
controller: controller,
|
99
|
-
action: :destroy,
|
100
|
-
as: ""
|
101
|
-
}.merge(options)
|
102
|
-
]
|
81
|
+
match path, {
|
82
|
+
via: :get,
|
83
|
+
controller: controller,
|
84
|
+
action: :remove,
|
85
|
+
as: name
|
86
|
+
}.merge(options)
|
87
|
+
|
88
|
+
match path, {
|
89
|
+
via: :post,
|
90
|
+
controller: controller,
|
91
|
+
action: :destroy,
|
92
|
+
as: ""
|
93
|
+
}.merge(options)
|
103
94
|
end
|
104
95
|
|
105
96
|
def list(*args)
|
106
97
|
path, name, options = extract_route_args(
|
107
|
-
|
108
|
-
default_name:
|
98
|
+
segment: :list,
|
99
|
+
default_name: controller_name,
|
109
100
|
args: args
|
110
101
|
)
|
111
102
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
as: name
|
119
|
-
}.merge(options)
|
120
|
-
]
|
103
|
+
match path, {
|
104
|
+
via: :get,
|
105
|
+
controller: controller,
|
106
|
+
action: :index,
|
107
|
+
as: name
|
108
|
+
}.merge(options)
|
121
109
|
end
|
122
110
|
|
123
111
|
def show(*args)
|
124
112
|
path, name, options = extract_route_args(
|
125
|
-
|
113
|
+
segment: :show,
|
126
114
|
default_name: singular_controller_name,
|
127
|
-
args: args
|
115
|
+
args: args,
|
116
|
+
bare: true
|
128
117
|
)
|
129
118
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
as: name
|
137
|
-
}.merge(options)
|
138
|
-
]
|
119
|
+
match path, {
|
120
|
+
via: :get,
|
121
|
+
controller: controller,
|
122
|
+
action: :show,
|
123
|
+
as: name
|
124
|
+
}.merge(options)
|
139
125
|
end
|
140
126
|
|
141
127
|
def all
|
142
128
|
create
|
143
129
|
update
|
144
130
|
remove
|
145
|
-
list
|
146
131
|
show
|
132
|
+
list unless controller_name == controller_name.singularize
|
147
133
|
end
|
148
134
|
|
149
|
-
|
135
|
+
def get(action, *args)
|
136
|
+
path, name, options = extract_route_args(
|
137
|
+
segment: action,
|
138
|
+
default_name: action.to_s,
|
139
|
+
args: args
|
140
|
+
)
|
141
|
+
|
142
|
+
match path, {
|
143
|
+
via: :get,
|
144
|
+
controller: controller,
|
145
|
+
action: action,
|
146
|
+
as: name
|
147
|
+
}.merge(options)
|
148
|
+
end
|
149
|
+
|
150
|
+
def post(action, *args)
|
151
|
+
path, name, options = extract_route_args(
|
152
|
+
segment: action,
|
153
|
+
default_name: action.to_s,
|
154
|
+
args: args
|
155
|
+
)
|
156
|
+
|
157
|
+
match path, {
|
158
|
+
via: :post,
|
159
|
+
controller: controller,
|
160
|
+
action: action,
|
161
|
+
as: named_routes[path] == name ? "" : name
|
162
|
+
}.merge(options)
|
163
|
+
end
|
164
|
+
|
165
|
+
private def match(path, options)
|
166
|
+
named_routes[path] = options[:as] unless options[:as].empty?
|
167
|
+
router.match(path, options)
|
168
|
+
end
|
169
|
+
|
170
|
+
private def extract_route_args(
|
171
|
+
segment:,
|
172
|
+
default_name:,
|
173
|
+
args:,
|
174
|
+
bare: false
|
175
|
+
)
|
150
176
|
route_options = args.extract_options!
|
151
|
-
route_options = default_options
|
152
|
-
|
153
|
-
|
177
|
+
route_options = default_options
|
178
|
+
.merge(bare: bare)
|
179
|
+
.merge(options)
|
180
|
+
.merge(route_options)
|
181
|
+
|
182
|
+
path = args.first || path_for(segment, route_options)
|
183
|
+
path = path.to_s.dasherize
|
184
|
+
name = route_options.delete(:as) { default_name.underscore.tr("/", "_") }
|
185
|
+
|
186
|
+
route_options.delete(:bare)
|
154
187
|
|
155
188
|
[path, name, route_options]
|
156
189
|
end
|
@@ -160,5 +193,36 @@ module HumanRoutes
|
|
160
193
|
format: false
|
161
194
|
}
|
162
195
|
end
|
196
|
+
|
197
|
+
private def path_for(segment, options)
|
198
|
+
param = options.fetch(:param, :id)
|
199
|
+
|
200
|
+
segments = if resource?
|
201
|
+
resource_segments(segment, param, options)
|
202
|
+
else
|
203
|
+
resources_segments(segment, param, options)
|
204
|
+
end
|
205
|
+
|
206
|
+
segments.compact.join("/")
|
207
|
+
end
|
208
|
+
|
209
|
+
private def resource_segments(segment, _param, options)
|
210
|
+
segments = [controller_name]
|
211
|
+
segments << segment unless options[:bare]
|
212
|
+
segments
|
213
|
+
end
|
214
|
+
|
215
|
+
private def resources_segments(segment, param, _options)
|
216
|
+
case segment
|
217
|
+
when :list
|
218
|
+
[controller_name]
|
219
|
+
when :new
|
220
|
+
[controller_name, segment]
|
221
|
+
when :show
|
222
|
+
[controller_name, ":#{param}"]
|
223
|
+
else
|
224
|
+
[controller_name, ":#{param}", segment]
|
225
|
+
end
|
226
|
+
end
|
163
227
|
end
|
164
228
|
end
|
@@ -3,12 +3,8 @@
|
|
3
3
|
module HumanRoutes
|
4
4
|
module Extensions
|
5
5
|
def route(controller, options = {}, &block)
|
6
|
-
context = Context.new(controller, options)
|
6
|
+
context = Context.new(self, controller, options)
|
7
7
|
context.instance_eval(&block)
|
8
|
-
|
9
|
-
context.routes.each do |route|
|
10
|
-
match(*route)
|
11
|
-
end
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
data/lib/human_routes/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: human_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -129,9 +129,9 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
+
- ".github/FUNDING.yml"
|
132
133
|
- ".gitignore"
|
133
134
|
- ".rubocop.yml"
|
134
|
-
- ".tool-versions"
|
135
135
|
- CODE_OF_CONDUCT.md
|
136
136
|
- Gemfile
|
137
137
|
- LICENSE.txt
|
@@ -148,10 +148,11 @@ homepage: https://github.com/fnando/human_routes
|
|
148
148
|
licenses:
|
149
149
|
- MIT
|
150
150
|
metadata:
|
151
|
+
rubygems_mfa_required: 'true'
|
151
152
|
homepage_uri: https://github.com/fnando/human_routes
|
152
153
|
source_code_uri: https://github.com/fnando/human_routes
|
153
154
|
changelog_uri: https://github.com/fnando/human_routes
|
154
|
-
post_install_message:
|
155
|
+
post_install_message:
|
155
156
|
rdoc_options: []
|
156
157
|
require_paths:
|
157
158
|
- lib
|
@@ -159,15 +160,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
160
|
requirements:
|
160
161
|
- - ">="
|
161
162
|
- !ruby/object:Gem::Version
|
162
|
-
version:
|
163
|
+
version: 2.7.0
|
163
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
165
|
requirements:
|
165
166
|
- - ">="
|
166
167
|
- !ruby/object:Gem::Version
|
167
168
|
version: '0'
|
168
169
|
requirements: []
|
169
|
-
rubygems_version: 3.
|
170
|
-
signing_key:
|
170
|
+
rubygems_version: 3.3.3
|
171
|
+
signing_key:
|
171
172
|
specification_version: 4
|
172
173
|
summary: I say no to REST for client-facing urls.
|
173
174
|
test_files: []
|
data/.tool-versions
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
nodejs 12.16.1
|