human_routes 0.0.6 → 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: 599c1d4f7022f37dd7a8c87f89e01480c95ea8f04d89137034139d87beabd8a5
4
- data.tar.gz: e26db1816cc2b383938b5f2adf7e5d6c1955c8d25996c0913a2bb7def1c8f61e
3
+ metadata.gz: b141f9a399978068f28f1756d0a575a893aaeb48d998ce3aecb641bb0648fb57
4
+ data.tar.gz: cf69b4906f02fad6d251ec3210e902961f27cea5e9f1e1757a947d8aca34ae9f
5
5
  SHA512:
6
- metadata.gz: af48d012d17e2c1525a524c68e14e6a72f8626fd4804d9a9252b923850547bf39eefcfe54966410ef5fe1619f3c510d92cd0271691e9e29c43ad6c4a02193849
7
- data.tar.gz: 789d18466d195a4afb4490567a38ad8c72d0f489b9485ffbb3a9c3cbcd2b09f63a58f49a5968b366ea07a28fca126ca191b663f5d1a43768c1d20b4fd08371c7
6
+ metadata.gz: 4023dccdbcfbd1be66d10124df81e4b1f75fb3daf354d309ce391b90fd4762701e33a28baa18a930bd0a56617729b656d020fad65d045828691f3e4d95913ac1
7
+ data.tar.gz: 4f4708fe090019fff8a630aa403b76a66d38480feccb26856cfb21559b3cda61a7fdced5d077cd896bdd0aaf48c9044d1d5c0bd0115bf149b119c29facd3c680
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,7 +175,7 @@ Rails.application.routes.draw do
159
175
  end
160
176
  ```
161
177
 
162
- For nested paths, you can use `:parent`:
178
+ For nested paths, you can use `:prefix`:
163
179
 
164
180
  ```ruby
165
181
  Rails.application.routes.draw do
@@ -167,7 +183,7 @@ Rails.application.routes.draw do
167
183
  all
168
184
  end
169
185
 
170
- route :comments, parent: "posts/:post_id" do
186
+ route :comments, prefix: "posts/:post_id" do
171
187
  remove #=> /posts/:post_id/comments/:id/remove
172
188
  list #=> /posts/:post_id/comments
173
189
 
@@ -177,6 +193,21 @@ Rails.application.routes.draw do
177
193
  end
178
194
  ```
179
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
+
180
211
  ## Development
181
212
 
182
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)
@@ -182,7 +190,7 @@ module HumanRoutes
182
190
  path = args.first || path_for(segment, route_options)
183
191
 
184
192
  path = [
185
- route_options[:parent].to_s.split("/"),
193
+ route_options[:prefix].to_s.split("/"),
186
194
  path.to_s.split("/")
187
195
  ].flatten.compact
188
196
 
@@ -192,7 +200,12 @@ module HumanRoutes
192
200
 
193
201
  name = route_options.delete(:as) { default_name.underscore.tr("/", "_") }
194
202
 
195
- route_options.delete(:bare)
203
+ %i[
204
+ bare
205
+ prefix
206
+ path_name
207
+ resource
208
+ ].each {|key| route_options.delete(key) }
196
209
 
197
210
  [path, name, route_options]
198
211
  end
@@ -216,7 +229,7 @@ module HumanRoutes
216
229
  end
217
230
 
218
231
  private def resource_segments(segment, _param, options)
219
- segments = [controller_name]
232
+ segments = [path_name]
220
233
  segments << segment unless options[:bare]
221
234
  segments
222
235
  end
@@ -224,13 +237,13 @@ module HumanRoutes
224
237
  private def resources_segments(segment, param, _options)
225
238
  case segment
226
239
  when :list
227
- [controller_name]
240
+ [path_name]
228
241
  when :new
229
- [controller_name, segment]
242
+ [path_name, segment]
230
243
  when :show
231
- [controller_name, ":#{param}"]
244
+ [path_name, ":#{param}"]
232
245
  else
233
- [controller_name, ":#{param}", segment]
246
+ [path_name, ":#{param}", segment]
234
247
  end
235
248
  end
236
249
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanRoutes
4
- VERSION = "0.0.6"
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.6
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: 2022-07-18 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