belt 0.1.10 → 0.1.11

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: c48b3087bb87bb86b2111cc243cf57d889e8f3d94fb25f1b023e5ba3dabf4973
4
- data.tar.gz: 53b43e092412aaab0a0fae0e531d7d7ffb07c7a69e19c3e7e7480cf19753184f
3
+ metadata.gz: 68099232d7da2991436a263de28721bfc4e15822116f11a42cd2433588293b59
4
+ data.tar.gz: ba091a54897cfd88b02e2ddf48750ca8a47ca3f4c298b7b4371be799e835550e
5
5
  SHA512:
6
- metadata.gz: 2443a472879d551084bd9f6a72b468e6dab20105ae84b3d906cbc33d42c8af6cc32229468300b00f9788a19c324b747f6cd0872d7d652c8d17e96a20cd2c95a6
7
- data.tar.gz: a6dd1cc9483792736335058a41b983df0380d8d48ee180b47e3a619a92d7d0a47fcd33af811cff20d7c451d1a783a8ccc49c3934dae60155c7aa6d039b8b7f27
6
+ metadata.gz: 8e3d77c55c4297f2978486d9d0755ce45fda7f4a8886c372af0b211952502e5f5fde50c0d8881d4d5db39be4c3747a469825dfbd1b5d6f9be1120d441116600a
7
+ data.tar.gz: 7226f0ce500b1d19a7bccf60f9f087573998f3589ae7b41dceba7f35289a71e7e4ea3250269b96b18ef823acff06bbd67916f8d39c1ecb025ce97fbb8288019e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.1.11
6
+
7
+ ### Route DSL
8
+
9
+ - `resources` / `resource` honor `scope path:` (and nested path scopes).
10
+ Example: `scope path: "admin", auth: :cognito { resources :users }` → `/admin/users…` with Cognito auth.
11
+ - Nested `scope path:` segments stack (`a` then `b` → `a/b`) instead of replacing.
12
+ - `scope controller:` is applied to `resources` / `resource` the same way as verb routes.
13
+ - Controller inference for multi-segment resource paths joins segments (`/admin/users` → `admin/users`), matching nested-resource convention.
14
+
5
15
  ## 0.1.10
6
16
 
7
17
  ### Frontend env map
@@ -14,7 +14,11 @@ module Belt
14
14
  non_param = segments.reject { |s| s.start_with?(':', '{') }
15
15
  return gateway.name if non_param.empty?
16
16
 
17
- return non_param.map { |s| s.gsub('-', '_') }.join('/') if route.resource? && nested_resource?(segments)
17
+ # Nested resources (/posts/{id}/comments) and scoped resources (/admin/users):
18
+ # join non-param segments → posts/comments, admin/users
19
+ if route.resource? && non_param.length > 1
20
+ return non_param.map { |s| s.gsub('-', '_') }.join('/')
21
+ end
18
22
 
19
23
  # For non-resource routes with a single segment (e.g., post '/signup' in :onboarding),
20
24
  # the segment is the action name, not the controller. Use the gateway name as controller.
@@ -174,20 +174,22 @@ module Belt
174
174
  resource_name = name.to_s
175
175
  singular = singularize(resource_name)
176
176
  param_name = options[:param] || "#{singular}_id"
177
+ base_path = resource_base_path(resource_name, options)
178
+ options = options.except(:path_prefix)
177
179
  options = auto_infer_tables(resource_name, options)
178
180
  resource_options = options.merge(route_type: :resources)
179
181
  actions = determine_actions(options)
180
182
 
181
- add_route(:get, "/#{resource_name}", resource_options) if actions.include?(:index)
182
- add_route(:post, "/#{resource_name}", resource_options) if actions.include?(:create)
183
- add_route(:get, "/#{resource_name}/{#{param_name}}", resource_options) if actions.include?(:show)
184
- add_route(:put, "/#{resource_name}/{#{param_name}}", resource_options) if actions.include?(:update)
185
- add_route(:delete, "/#{resource_name}/{#{param_name}}", resource_options) if actions.include?(:destroy)
183
+ add_route(:get, base_path, resource_options) if actions.include?(:index)
184
+ add_route(:post, base_path, resource_options) if actions.include?(:create)
185
+ add_route(:get, "#{base_path}/{#{param_name}}", resource_options) if actions.include?(:show)
186
+ add_route(:put, "#{base_path}/{#{param_name}}", resource_options) if actions.include?(:update)
187
+ add_route(:delete, "#{base_path}/{#{param_name}}", resource_options) if actions.include?(:destroy)
186
188
 
187
189
  return unless block_given?
188
190
 
189
- collection_prefix = "/#{resource_name}"
190
- member_prefix = "/#{resource_name}/{#{param_name}}"
191
+ collection_prefix = base_path
192
+ member_prefix = "#{base_path}/{#{param_name}}"
191
193
  resource_tables = Array(options[:tables] || [])
192
194
  inherited_tables = (@default_tables + resource_tables).uniq
193
195
  inherited_auth = options[:auth] || @default_auth
@@ -199,17 +201,25 @@ module Belt
199
201
 
200
202
  def resource(name, options = {})
201
203
  resource_name = name.to_s
204
+ base_path = resource_base_path(resource_name, options)
205
+ options = options.except(:path_prefix)
202
206
  actions = determine_actions(options, default: %i[show update destroy])
203
207
  resource_options = options.merge(route_type: :resource)
204
208
 
205
- add_route(:get, "/#{resource_name}", resource_options) if actions.include?(:show)
206
- add_route(:put, "/#{resource_name}", resource_options) if actions.include?(:update)
207
- add_route(:delete, "/#{resource_name}", resource_options) if actions.include?(:destroy)
208
- add_route(:post, "/#{resource_name}", resource_options) if actions.include?(:create)
209
+ add_route(:get, base_path, resource_options) if actions.include?(:show)
210
+ add_route(:put, base_path, resource_options) if actions.include?(:update)
211
+ add_route(:delete, base_path, resource_options) if actions.include?(:destroy)
212
+ add_route(:post, base_path, resource_options) if actions.include?(:create)
209
213
  end
210
214
 
211
215
  private
212
216
 
217
+ # Builds "/users" or "/admin/users" when path_prefix is set (from scope path:).
218
+ def resource_base_path(resource_name, options)
219
+ prefix = options[:path_prefix].to_s.gsub(%r{^/|/$}, '')
220
+ prefix.empty? ? "/#{resource_name}" : "/#{prefix}/#{resource_name}"
221
+ end
222
+
213
223
  def add_route(method, path, options = {})
214
224
  lambda_to_use = options[:lambda] || @current_lambda_context || @default_lambda
215
225
  route_tables = Array(options[:tables] || [])
@@ -308,7 +318,11 @@ module Belt
308
318
  previous_tables = @scope_tables
309
319
  previous_controller = @scope_controller
310
320
 
311
- @scope_prefix = options[:path] || @scope_prefix
321
+ # Nest path segments (Rails-style): scope path: "a" { scope path: "b" } → "a/b"
322
+ if options.key?(:path)
323
+ segment = options[:path].to_s.gsub(%r{^/|/$}, '')
324
+ @scope_prefix = @scope_prefix.to_s.empty? ? segment : "#{@scope_prefix}/#{segment}"
325
+ end
312
326
  @scope_module = options[:module] || @scope_module
313
327
  @scope_auth = options[:auth] || @scope_auth
314
328
  @scope_tables = (@scope_tables + Array(options[:tables] || [])).uniq
@@ -340,11 +354,13 @@ module Belt
340
354
 
341
355
  def resources(name, options = {}, &)
342
356
  options = apply_scope_options(options)
357
+ options = options.merge(path_prefix: @scope_prefix) unless @scope_prefix.to_s.empty?
343
358
  @gateway.resources(name, options, &)
344
359
  end
345
360
 
346
361
  def resource(name, options = {})
347
362
  options = apply_scope_options(options)
363
+ options = options.merge(path_prefix: @scope_prefix) unless @scope_prefix.to_s.empty?
348
364
  @gateway.resource(name, options)
349
365
  end
350
366
 
@@ -400,6 +416,7 @@ module Belt
400
416
  result = options.dup
401
417
  result[:auth] ||= @scope_auth if @scope_auth
402
418
  result[:lambda] ||= @scope_module if @scope_module
419
+ result[:controller] ||= @scope_controller if @scope_controller
403
420
  result[:tables] = (@scope_tables + Array(result[:tables] || [])).uniq if @scope_tables.any? || result[:tables]
404
421
  result
405
422
  end
data/lib/belt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Belt
4
- VERSION = '0.1.10'
4
+ VERSION = '0.1.11'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: belt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stowzilla