human_routes 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6619b6b97a5cb0145a8d7094e78b9a60977de870747daab57ead72a4616b35a7
4
- data.tar.gz: 9ce9e153c6662da207f112f4eb35bca3959afa80993d10be771d23768db38590
3
+ metadata.gz: b141f9a399978068f28f1756d0a575a893aaeb48d998ce3aecb641bb0648fb57
4
+ data.tar.gz: cf69b4906f02fad6d251ec3210e902961f27cea5e9f1e1757a947d8aca34ae9f
5
5
  SHA512:
6
- metadata.gz: 99529e4c6c0b3eaea6557cc8caa3eac0c25c68fc17e4404ebd1a825e02ac556a69307d0e769488164b7e35797b0099c72f6a7e9b54d359702f861b5b6aad0a64
7
- data.tar.gz: 21d11d2ea7e321637278de2488bdcf55c733d0e2684cfe3e6b1f46b02fcdaf6f2b16dc156fdc3eca637fe88cd6f6798efaef07028a767ce469fc284ac2ffff2f
6
+ metadata.gz: 4023dccdbcfbd1be66d10124df81e4b1f75fb3daf354d309ce391b90fd4762701e33a28baa18a930bd0a56617729b656d020fad65d045828691f3e4d95913ac1
7
+ data.tar.gz: 4f4708fe090019fff8a630aa403b76a66d38480feccb26856cfb21559b3cda61a7fdced5d077cd896bdd0aaf48c9044d1d5c0bd0115bf149b119c29facd3c680
data/.rubocop.yml CHANGED
@@ -4,6 +4,7 @@ inherit_gem:
4
4
 
5
5
  AllCops:
6
6
  TargetRubyVersion: 2.7
7
+ NewCops: enable
7
8
 
8
9
  Metrics:
9
10
  Enabled: false
data/README.md CHANGED
@@ -120,6 +120,7 @@ Rails.application.routes.draw do
120
120
  # This will generate the following routes:
121
121
  #
122
122
  # GET /profile profile_path
123
+ # GET /profile/new new_profile_path
123
124
  # POST /profile/new
124
125
  # GET /profile/edit edit_profile_path
125
126
  # POST /profile/edit
@@ -128,6 +129,21 @@ Rails.application.routes.draw do
128
129
  route "profile" do
129
130
  all
130
131
  end
132
+
133
+ # You can use `resource: true` when you want a plural route but need a
134
+ # singular resource.
135
+ #
136
+ # GET /settings settings_path
137
+ # GET /settings/new new_settings_path
138
+ # POST /settings/new
139
+ # GET /settings/edit edit_settings_path
140
+ # POST /settings/edit
141
+ # GET /settings/remove remove_settings_path
142
+ # POST /settings/remove
143
+ #
144
+ route "settings", resource: true do
145
+ all
146
+ end
131
147
  end
132
148
  ```
133
149
 
@@ -159,6 +175,39 @@ Rails.application.routes.draw do
159
175
  end
160
176
  ```
161
177
 
178
+ For nested paths, you can use `:prefix`:
179
+
180
+ ```ruby
181
+ Rails.application.routes.draw do
182
+ route :posts do
183
+ all
184
+ end
185
+
186
+ route :comments, prefix: "posts/:post_id" do
187
+ remove #=> /posts/:post_id/comments/:id/remove
188
+ list #=> /posts/:post_id/comments
189
+
190
+ # or
191
+ all
192
+ end
193
+ end
194
+ ```
195
+
196
+ If you need to change the url path, but point to a different controller, then
197
+ use `:path_name`:
198
+
199
+ ```ruby
200
+ Rails.application.routes.draw do
201
+ route :blogs do
202
+ all
203
+ end
204
+
205
+ route :blog_comments, path: "blogs/:blog_id", path_name: "comments" do
206
+ all
207
+ end
208
+ end
209
+ ```
210
+
162
211
  ## Development
163
212
 
164
213
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
@@ -16,11 +16,19 @@ module HumanRoutes
16
16
  end
17
17
 
18
18
  def singular_controller_name
19
- @singular_controller_name ||= controller_name.singularize
19
+ @singular_controller_name ||= if options[:resource]
20
+ controller_name
21
+ else
22
+ controller_name.singularize
23
+ end
20
24
  end
21
25
 
22
26
  def resource?
23
- controller_name == singular_controller_name
27
+ options[:resource] || controller_name == singular_controller_name
28
+ end
29
+
30
+ def path_name
31
+ options[:path_name] || controller_name
24
32
  end
25
33
 
26
34
  def routes
@@ -129,7 +137,7 @@ module HumanRoutes
129
137
  update
130
138
  remove
131
139
  show
132
- list unless controller_name == controller_name.singularize
140
+ list unless resource?
133
141
  end
134
142
 
135
143
  def get(action, *args)
@@ -180,9 +188,25 @@ module HumanRoutes
180
188
  .merge(route_options)
181
189
 
182
190
  path = args.first || path_for(segment, route_options)
183
- path = path.to_s.dasherize
191
+
192
+ path = [
193
+ route_options[:prefix].to_s.split("/"),
194
+ path.to_s.split("/")
195
+ ].flatten.compact
196
+
197
+ path = path.map do |s|
198
+ s.start_with?(":") ? s : s.dasherize
199
+ end.join("/")
200
+
184
201
  name = route_options.delete(:as) { default_name.underscore.tr("/", "_") }
185
202
 
203
+ %i[
204
+ bare
205
+ prefix
206
+ path_name
207
+ resource
208
+ ].each {|key| route_options.delete(key) }
209
+
186
210
  [path, name, route_options]
187
211
  end
188
212
 
@@ -205,7 +229,7 @@ module HumanRoutes
205
229
  end
206
230
 
207
231
  private def resource_segments(segment, _param, options)
208
- segments = [controller_name]
232
+ segments = [path_name]
209
233
  segments << segment unless options[:bare]
210
234
  segments
211
235
  end
@@ -213,13 +237,13 @@ module HumanRoutes
213
237
  private def resources_segments(segment, param, _options)
214
238
  case segment
215
239
  when :list
216
- [controller_name]
240
+ [path_name]
217
241
  when :new
218
- [controller_name, segment]
242
+ [path_name, segment]
219
243
  when :show
220
- [controller_name, ":#{param}"]
244
+ [path_name, ":#{param}"]
221
245
  else
222
- [controller_name, ":#{param}", segment]
246
+ [path_name, ":#{param}", segment]
223
247
  end
224
248
  end
225
249
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanRoutes
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.7"
5
5
  end
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
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-27 00:00:00.000000000 Z
11
+ date: 2022-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
167
  - !ruby/object:Gem::Version
168
168
  version: '0'
169
169
  requirements: []
170
- rubygems_version: 3.2.32
170
+ rubygems_version: 3.3.17
171
171
  signing_key:
172
172
  specification_version: 4
173
173
  summary: I say no to REST for client-facing urls.