human_routes 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cfd4e06db3b929750903be57fb3a372463a25d176b79b61b8a6bf80093a099b
4
- data.tar.gz: ef6be79a9800bf8aea869c9b6f0f80c29e74479c5e652e2f1650065fd6c67728
3
+ metadata.gz: 750706caf0cfc1bbd3580eaf6c3103c73bc2e0f02c190c5103c7883f0749a958
4
+ data.tar.gz: d6cf027de35c5b1c9a3905550a606bb26585307b2b6fd97a5224975d804c4e8c
5
5
  SHA512:
6
- metadata.gz: 0e383792de96ae4117d47411b77e92cdfb2f4f8e60e28706cab505ebda709df97df543d9718a6c2575d71943cd7f88867916df01d64f7faca0c34126b5e0b6d9
7
- data.tar.gz: db912bd53f9433f05823e8bedf9da0f285b00e9263df598bbf49524311d322283861a1b6d1585226ea2030baf83a18e79e761d4f9f033508647401206b711dc9
6
+ metadata.gz: c00bc6a43435013e188b97913862a37cd5dc665fd5fab540d4ee64808f997c81967e0437b9a85cbc7726a638cbeecd58b033940e851f23b590935295ceb7f5f8
7
+ data.tar.gz: dd0df5f668857a9906c74ad87df9f9518f8320df74c961a548d2a00abc5f2dd0277851168fca14aed51a1a135a6fef0366511d9127a58bf083afd2ec42aba94f
data/README.md CHANGED
@@ -111,6 +111,19 @@ Rails.application.routes.draw do
111
111
  route "admin/reports", name: "reports" do
112
112
  all
113
113
  end
114
+
115
+ # Singular routes also are detected and generated accordingly.
116
+ # This will generate the following routes:
117
+ #
118
+ # GET /profile profile_path
119
+ # POST /profile/new
120
+ # GET /profile/edit edit_profile_path
121
+ # POST /profile/edit
122
+ # GET /profile/remove remove_profile_path
123
+ # POST /profile/remove
124
+ route "profile" do
125
+ all
126
+ end
114
127
  end
115
128
  ```
116
129
 
@@ -18,13 +18,17 @@ module HumanRoutes
18
18
  @singular_controller_name ||= controller_name.singularize
19
19
  end
20
20
 
21
+ def resource?
22
+ controller_name == singular_controller_name
23
+ end
24
+
21
25
  def routes
22
26
  @routes ||= []
23
27
  end
24
28
 
25
29
  def create(*args)
26
30
  path, name, options = extract_route_args(
27
- default_path: "#{controller_name}/new",
31
+ segment: :new,
28
32
  default_name: "new_#{singular_controller_name}",
29
33
  args: args
30
34
  )
@@ -52,7 +56,7 @@ module HumanRoutes
52
56
 
53
57
  def update(*args)
54
58
  path, name, options = extract_route_args(
55
- default_path: "#{controller_name}/:id/edit",
59
+ segment: :edit,
56
60
  default_name: "edit_#{singular_controller_name}",
57
61
  args: args
58
62
  )
@@ -80,7 +84,7 @@ module HumanRoutes
80
84
 
81
85
  def remove(*args)
82
86
  path, name, options = extract_route_args(
83
- default_path: "#{controller_name}/:id/remove",
87
+ segment: :remove,
84
88
  default_name: "remove_#{singular_controller_name}",
85
89
  args: args
86
90
  )
@@ -108,7 +112,7 @@ module HumanRoutes
108
112
 
109
113
  def list(*args)
110
114
  path, name, options = extract_route_args(
111
- default_path: controller_name,
115
+ segment: :list,
112
116
  default_name: controller_name,
113
117
  args: args
114
118
  )
@@ -126,7 +130,7 @@ module HumanRoutes
126
130
 
127
131
  def show(*args)
128
132
  path, name, options = extract_route_args(
129
- default_path: "#{controller_name}/:id",
133
+ segment: :show,
130
134
  default_name: singular_controller_name,
131
135
  args: args
132
136
  )
@@ -146,15 +150,15 @@ module HumanRoutes
146
150
  create
147
151
  update
148
152
  remove
149
- list
150
153
  show
154
+ list unless controller_name == controller_name.singularize
151
155
  end
152
156
 
153
- private def extract_route_args(default_path:, default_name:, args:)
157
+ private def extract_route_args(segment:, default_name:, args:)
154
158
  route_options = args.extract_options!
155
159
  route_options = default_options.merge(options).merge(route_options)
156
- path = args.first || default_path
157
- name = route_options.delete(:as) || default_name
160
+ path = args.first || path_for(segment, route_options)
161
+ name = route_options.delete(:as) { default_name.underscore.tr("/", "_") }
158
162
 
159
163
  [path, name, route_options]
160
164
  end
@@ -164,5 +168,36 @@ module HumanRoutes
164
168
  format: false
165
169
  }
166
170
  end
171
+
172
+ private def path_for(segment, options)
173
+ param = options.fetch(:param, :id)
174
+
175
+ segments = if resource?
176
+ resource_segments(segment, param)
177
+ else
178
+ resources_segments(segment, param)
179
+ end
180
+
181
+ segments.compact.join("/")
182
+ end
183
+
184
+ private def resource_segments(segment, _param)
185
+ segments = [controller_name]
186
+ segments << segment unless segment == :show
187
+ segments
188
+ end
189
+
190
+ private def resources_segments(segment, param)
191
+ case segment
192
+ when :list
193
+ [controller_name]
194
+ when :new
195
+ [controller_name, segment]
196
+ when :show
197
+ [controller_name, ":#{param}"]
198
+ else
199
+ [controller_name, ":#{param}", segment]
200
+ end
201
+ end
167
202
  end
168
203
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanRoutes
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira