human_routes 0.0.3 → 0.0.6
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 +55 -5
- data/human_routes.gemspec +2 -0
- data/lib/human_routes/context.rb +120 -86
- 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: 599c1d4f7022f37dd7a8c87f89e01480c95ea8f04d89137034139d87beabd8a5
|
4
|
+
data.tar.gz: e26db1816cc2b383938b5f2adf7e5d6c1955c8d25996c0913a2bb7def1c8f61e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af48d012d17e2c1525a524c68e14e6a72f8626fd4804d9a9252b923850547bf39eefcfe54966410ef5fe1619f3c510d92cd0271691e9e29c43ad6c4a02193849
|
7
|
+
data.tar.gz: 789d18466d195a4afb4490567a38ad8c72d0f489b9485ffbb3a9c3cbcd2b09f63a58f49a5968b366ea07a28fca126ca191b663f5d1a43768c1d20b4fd08371c7
|
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
|
@@ -127,6 +131,52 @@ Rails.application.routes.draw do
|
|
127
131
|
end
|
128
132
|
```
|
129
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
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
For nested paths, you can use `:parent`:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
Rails.application.routes.draw do
|
166
|
+
route :posts do
|
167
|
+
all
|
168
|
+
end
|
169
|
+
|
170
|
+
route :comments, parent: "posts/:post_id" do
|
171
|
+
remove #=> /posts/:post_id/comments/:id/remove
|
172
|
+
list #=> /posts/:post_id/comments
|
173
|
+
|
174
|
+
# or
|
175
|
+
all
|
176
|
+
end
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
130
180
|
## Development
|
131
181
|
|
132
182
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
@@ -142,7 +192,7 @@ git commits and tags, and push the `.gem` file to
|
|
142
192
|
## Contributing
|
143
193
|
|
144
194
|
Bug reports and pull requests are welcome on GitHub at
|
145
|
-
https://github.com/fnando/human_routes
|
195
|
+
<https://github.com/fnando/human_routes>. This project is intended to be a safe,
|
146
196
|
welcoming space for collaboration, and contributors are expected to adhere to
|
147
197
|
the
|
148
198
|
[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,12 +2,13 @@
|
|
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 = {}
|
11
12
|
end
|
12
13
|
|
13
14
|
def controller_name
|
@@ -33,25 +34,19 @@ module HumanRoutes
|
|
33
34
|
args: args
|
34
35
|
)
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
via: :post,
|
50
|
-
controller: controller,
|
51
|
-
action: :create,
|
52
|
-
as: ""
|
53
|
-
}.merge(options)
|
54
|
-
]
|
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)
|
55
50
|
end
|
56
51
|
|
57
52
|
def update(*args)
|
@@ -61,25 +56,19 @@ module HumanRoutes
|
|
61
56
|
args: args
|
62
57
|
)
|
63
58
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
via: :post,
|
78
|
-
controller: controller,
|
79
|
-
action: :update,
|
80
|
-
as: ""
|
81
|
-
}.merge(options)
|
82
|
-
]
|
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)
|
83
72
|
end
|
84
73
|
|
85
74
|
def remove(*args)
|
@@ -89,25 +78,19 @@ module HumanRoutes
|
|
89
78
|
args: args
|
90
79
|
)
|
91
80
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
via: :post,
|
106
|
-
controller: controller,
|
107
|
-
action: :destroy,
|
108
|
-
as: ""
|
109
|
-
}.merge(options)
|
110
|
-
]
|
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)
|
111
94
|
end
|
112
95
|
|
113
96
|
def list(*args)
|
@@ -117,33 +100,28 @@ module HumanRoutes
|
|
117
100
|
args: args
|
118
101
|
)
|
119
102
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
as: name
|
127
|
-
}.merge(options)
|
128
|
-
]
|
103
|
+
match path, {
|
104
|
+
via: :get,
|
105
|
+
controller: controller,
|
106
|
+
action: :index,
|
107
|
+
as: name
|
108
|
+
}.merge(options)
|
129
109
|
end
|
130
110
|
|
131
111
|
def show(*args)
|
132
112
|
path, name, options = extract_route_args(
|
133
113
|
segment: :show,
|
134
114
|
default_name: singular_controller_name,
|
135
|
-
args: args
|
115
|
+
args: args,
|
116
|
+
bare: true
|
136
117
|
)
|
137
118
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
as: name
|
145
|
-
}.merge(options)
|
146
|
-
]
|
119
|
+
match path, {
|
120
|
+
via: :get,
|
121
|
+
controller: controller,
|
122
|
+
action: :show,
|
123
|
+
as: name
|
124
|
+
}.merge(options)
|
147
125
|
end
|
148
126
|
|
149
127
|
def all
|
@@ -154,12 +132,68 @@ module HumanRoutes
|
|
154
132
|
list unless controller_name == controller_name.singularize
|
155
133
|
end
|
156
134
|
|
157
|
-
|
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
|
+
)
|
158
176
|
route_options = args.extract_options!
|
159
|
-
route_options = default_options
|
177
|
+
route_options = default_options
|
178
|
+
.merge(bare: bare)
|
179
|
+
.merge(options)
|
180
|
+
.merge(route_options)
|
181
|
+
|
160
182
|
path = args.first || path_for(segment, route_options)
|
183
|
+
|
184
|
+
path = [
|
185
|
+
route_options[:parent].to_s.split("/"),
|
186
|
+
path.to_s.split("/")
|
187
|
+
].flatten.compact
|
188
|
+
|
189
|
+
path = path.map do |s|
|
190
|
+
s.start_with?(":") ? s : s.dasherize
|
191
|
+
end.join("/")
|
192
|
+
|
161
193
|
name = route_options.delete(:as) { default_name.underscore.tr("/", "_") }
|
162
194
|
|
195
|
+
route_options.delete(:bare)
|
196
|
+
|
163
197
|
[path, name, route_options]
|
164
198
|
end
|
165
199
|
|
@@ -173,21 +207,21 @@ module HumanRoutes
|
|
173
207
|
param = options.fetch(:param, :id)
|
174
208
|
|
175
209
|
segments = if resource?
|
176
|
-
resource_segments(segment, param)
|
210
|
+
resource_segments(segment, param, options)
|
177
211
|
else
|
178
|
-
resources_segments(segment, param)
|
212
|
+
resources_segments(segment, param, options)
|
179
213
|
end
|
180
214
|
|
181
215
|
segments.compact.join("/")
|
182
216
|
end
|
183
217
|
|
184
|
-
private def resource_segments(segment, _param)
|
218
|
+
private def resource_segments(segment, _param, options)
|
185
219
|
segments = [controller_name]
|
186
|
-
segments << segment unless
|
220
|
+
segments << segment unless options[:bare]
|
187
221
|
segments
|
188
222
|
end
|
189
223
|
|
190
|
-
private def resources_segments(segment, param)
|
224
|
+
private def resources_segments(segment, param, _options)
|
191
225
|
case segment
|
192
226
|
when :list
|
193
227
|
[controller_name]
|
@@ -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.6
|
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-07-18 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.17
|
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
|