85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
# File 'lib/layered/resource/routing.rb', line 85
def layered_resources(resource_name, resource: nil, controller: nil, namespace: nil, only: RESOURCE_ACTIONS, except: nil, **options, &block)
if @scope.resource_scope?
parent = @scope[:scope_level_resource]
return send(:with_scope_level, :nested) do
scope(path: parent.nested_scope) do
saved_as = @scope.frame[:as]
@scope.frame[:as] = nil
begin
layered_resources(resource_name,
resource: resource, controller: controller, namespace: namespace,
only: only, except: except, **options, &block)
ensure
@scope.frame[:as] = saved_as
end
end
end
end
namespace = namespace.to_s.presence
resource_class_name = resource ||
(namespace ? "#{namespace}::#{resource_name.to_s.classify}Resource" : "#{resource_name.to_s.classify}Resource")
route_key = resource_name.to_s
singular_key = resource_name.to_s.singularize
raw_scope_path = @scope[:path].to_s
parent_params = raw_scope_path.scan(/:([a-zA-Z_]\w*)/).flatten.map(&:to_sym)
segments = raw_scope_path.split("/")
parent_collection_keys = {}
accumulated_param_prefix = []
segments.each_with_index do |seg, i|
next unless seg.start_with?(":")
param = seg.delete_prefix(":").to_sym
parent_collection_keys[param] = [*accumulated_param_prefix, segments[i - 1]].compact.join("_") if i > 0
accumulated_param_prefix << param.to_s.delete_suffix("_id")
end
parent_collection_segments = segments.each_with_index
.select { |seg, i| i > 0 && seg.start_with?(":") }
.map { |_, i| segments[i - 1] }
= segments.reject { |s| s.empty? || s.start_with?(":") || parent_collection_segments.include?(s) }
parent_prefix = parent_params.map { |p| p.to_s.delete_suffix("_id") }.join("_")
as_prefix = [.join("_").presence, parent_prefix.presence].compact.join("_")
as_base = [as_prefix.presence, route_key].compact.join("_")
as_singular = [as_prefix.presence, singular_key].compact.join("_")
user_as = @scope[:as].to_s
if user_as.present? && user_as != as_prefix
warn "[layered-resource-rails] layered_resources :#{resource_name} ignores the " \
"surrounding `as: #{user_as.inspect}` — it derives route-helper names from the " \
"path instead, so your helpers are named `#{as_base}_path`, `new_#{as_singular}_path`, " \
"etc. Drop the `as:` to silence this (the `path:`/scope segments already namespace " \
"the helpers)."
end
as_to_pass = as_base
singular_as_to_pass = as_singular
controller_override = controller
controller = if controller
controller.to_s
elsif namespace && "#{namespace}::ResourcesController".safe_constantize
@scope[:module] ? "/#{namespace.underscore}/resources" : "#{namespace.underscore}/resources"
elsif @scope[:module]
"/layered/resource/resources"
else
"layered/resource/resources"
end
actions = Array(only).map(&:to_sym)
actions -= Array(except).map(&:to_sym) if except
if (actions & %i[new create]).any? && !actions.include?(:index)
raise ArgumentError,
"layered_resources :#{resource_name} includes :new or :create without :index. " \
"The form actions require a collection route; add :index to only:."
end
if actions.include?(:new) && !actions.include?(:create)
raise ArgumentError,
"layered_resources :#{resource_name} includes :new without :create. " \
"The new form posts to the collection route; add :create to only:."
end
if actions.include?(:edit) && !actions.include?(:update)
raise ArgumentError,
"layered_resources :#{resource_name} includes :edit without :update. " \
"The edit form patches the member route; add :update to only:."
end
if actions.include?(:update) && !actions.include?(:index)
raise ArgumentError,
"layered_resources :#{resource_name} includes :update without :index. " \
"Update redirects to the collection route; add :index to only:."
end
if actions.include?(:destroy) && !actions.include?(:index)
raise ArgumentError,
"layered_resources :#{resource_name} includes :destroy without :index. " \
"Destroy redirects to the collection route; add :index to only:."
end
custom_member = []
custom_collection = []
if block
unless controller_override
raise ArgumentError,
"layered_resources :#{resource_name} declared a block of custom actions " \
"but no controller: override. Generate one with " \
"`rails g layered:resource:controller #{resource_name}` and pass " \
"controller: \"#{resource_name}\"."
end
builder = CustomActionsBuilder.new
builder.instance_eval(&block)
custom_member = builder.member_actions
custom_collection = builder.collection_actions
if custom_collection.any? { |a| a[:action] == :new } && actions.include?(:new)
raise ArgumentError,
"layered_resources :#{resource_name} declares collection :new, " \
"which collides with the built-in /#{route_key}/new route. " \
"Rename it or pass `except: [:new]`."
end
if custom_member.any? { |a| a[:action] == :edit } && actions.include?(:edit)
raise ArgumentError,
"layered_resources :#{resource_name} declares member :edit, " \
"which collides with the built-in /#{route_key}/:id/edit route. " \
"Rename it or pass `except: [:edit]`."
end
end
Layered::Resource::Routing.register(as_base, resource_class_name,
actions: actions,
routes: @set,
parent_params: parent_params,
parent_collection_keys: parent_collection_keys,
resource_name: route_key,
member_actions: custom_member.map { |a| a[:action] },
collection_actions: custom_collection.map { |a| a[:action] })
route_defaults = (options[:defaults] || {}).merge(
_layered_resource_route_key: as_base
)
options = options.except(:defaults, :as)
saved_scope_as = @scope.frame[:as]
@scope.frame[:as] = nil
begin
if actions.include?(:index)
get route_key, to: "#{controller}#index",
as: as_to_pass.to_sym,
defaults: route_defaults, **options
end
if actions.include?(:new)
get "#{route_key}/new", to: "#{controller}#new",
as: :"new_#{singular_as_to_pass}",
defaults: route_defaults, **options
end
if actions.include?(:create)
post route_key, to: "#{controller}#create",
as: nil,
defaults: route_defaults, **options
end
custom_collection.each do |route|
public_send(route[:verb], "#{route_key}/#{route[:action]}",
to: "#{controller}##{route[:action]}",
as: :"#{route[:action]}_#{as_to_pass}",
defaults: route_defaults, **options)
end
if actions.include?(:edit)
get "#{route_key}/:id/edit", to: "#{controller}#edit",
as: :"edit_#{singular_as_to_pass}",
defaults: route_defaults, **options
end
member_named = false
if actions.include?(:show)
get "#{route_key}/:id", to: "#{controller}#show",
as: singular_as_to_pass.to_sym,
defaults: route_defaults, **options
member_named = true
end
if actions.include?(:update)
update_opts = { to: "#{controller}#update", defaults: route_defaults, **options }
update_opts[:as] = member_named ? nil : singular_as_to_pass.to_sym
patch "#{route_key}/:id", **update_opts
member_named = true
end
if actions.include?(:destroy)
destroy_opts = { to: "#{controller}#destroy", defaults: route_defaults, **options }
destroy_opts[:as] = member_named ? nil : singular_as_to_pass.to_sym
delete "#{route_key}/:id", **destroy_opts
end
custom_member.each do |route|
public_send(route[:verb], "#{route_key}/:id/#{route[:action]}",
to: "#{controller}##{route[:action]}",
as: :"#{route[:action]}_#{singular_as_to_pass}",
defaults: route_defaults, **options)
end
ensure
@scope.frame[:as] = saved_scope_as
end
end
|