sinja 1.2.0.pre3 → 1.2.1

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
  SHA1:
3
- metadata.gz: 82d0d656750a406ce8ac8f8e3fc227dcd9123970
4
- data.tar.gz: 87b665b88f0abc9097b935ed59a648d5fdbca4e7
3
+ metadata.gz: 2baae47c1aa32d45265b3726e8a57163bf1d381e
4
+ data.tar.gz: 2e3de4a123865207d3a9bc6090563fa979a8ee52
5
5
  SHA512:
6
- metadata.gz: 14557eda5449b7249797c693497f7bf686c0dd4b040e78fdd77e868df6b3b21c794ca7709f4cd37a0cc40ace6edc2e26f424156a35b4227be9b9190873339b1d
7
- data.tar.gz: d9a3e61434e0733425045e110a494bd25f4c57176ffb5ba6e374c8b45a883f1ea42704d77f3426ff73e5ae035ebcb2ed4e6830be6185e24151c47628e52dde83
6
+ metadata.gz: 6fcfb53cdc190885a45594927e6fdb335c2fe44152884fe7051d3420f6431a7d7762215c924aea8526fd9486deb664a58486b3858d2b17f77e072f6d9b131f63
7
+ data.tar.gz: 952f451d6e3f18c992015e14d82fde782e866cdd078b06bbff30a00944b1c4a623a49c673fc971630f51ad943be7aebe59c3f50e900dd7f002ad7432720f4afc
data/README.md CHANGED
@@ -47,6 +47,7 @@ the {json:api} specification is).
47
47
  - [`resource`](#resource)
48
48
  - [`index {..}` => Array](#index---array)
49
49
  - [`show {|id| ..}` => Object](#show-id---object)
50
+ - [`show {..}` => Object](#show---object)
50
51
  - [`show_many {|ids| ..}` => Array](#show_many-ids---array)
51
52
  - [`create {|attr, id| ..}` => id, Object?](#create-attr-id---id-object)
52
53
  - [`create {|attr| ..}` => id, Object](#create-attr---id-object)
@@ -260,12 +261,10 @@ class App < Sinatra::Base
260
261
  end
261
262
  end
262
263
 
263
- show do |id|
264
- book = find(id)
265
- next unless book
266
- headers 'X-ISBN'=>book.isbn
267
- last_modified book.updated_at
268
- next book, include: %w[author]
264
+ show do
265
+ headers 'X-ISBN'=>resource.isbn
266
+ last_modified resource.updated_at
267
+ next resource, include: %w[author]
269
268
  end
270
269
 
271
270
  has_one :author do
@@ -482,8 +481,8 @@ Much of Sinja's advanced functionality (e.g. updating and destroying resources,
482
481
  relationship routes) is dependent upon its ability to locate the corresponding
483
482
  resource for a request. To enable these features, define an ordinary helper
484
483
  method named `find` in your resource definition that takes a single ID argument
485
- and returns the corresponding object. You can, of course, use this helper
486
- method elsewhere in your application, such as in your `show` action helper.
484
+ and returns the corresponding object. Once defined, a `resource` object will
485
+ be made available in any action helpers that operate on a single resource.
487
486
 
488
487
  ```ruby
489
488
  resource :posts do
@@ -493,8 +492,8 @@ resource :posts do
493
492
  end
494
493
  end
495
494
 
496
- show do |id|
497
- next find(id), include: 'comments'
495
+ show do
496
+ next resource, include: 'comments'
498
497
  end
499
498
  end
500
499
  ```
@@ -514,7 +513,7 @@ end
514
513
 
515
514
  * How do I control access to the resource locator?
516
515
 
517
- You don't. Instead, control access to the action helpers that use it:
516
+ You don't. Instead, control access to the action helpers that use it: `show`,
518
517
  `update`, `destroy`, and all of the relationship action helpers such as
519
518
  `pluck` and `fetch`.
520
519
 
@@ -525,7 +524,7 @@ end
525
524
 
526
525
  As a bit of syntactic sugar, if you define a `find` helper and subsequently
527
526
  call `show` without a block, Sinja will generate a `show` action helper that
528
- delegates to `find`.
527
+ simply returns `resource`.
529
528
 
530
529
  ### Action Helpers
531
530
 
@@ -560,8 +559,15 @@ Return an array of zero or more objects to serialize on the response.
560
559
 
561
560
  ##### `show {|id| ..}` => Object
562
561
 
563
- Take an ID and return the corresponding object (or `nil` if not found) to
564
- serialize on the response.
562
+ Without a resource locator: Take an ID and return the corresponding object (or
563
+ `nil` if not found) to serialize on the response. (Note that only one or the
564
+ other `show` action helpers is allowed in any given resource block.)
565
+
566
+ ##### `show {..}` => Object
567
+
568
+ With a resource locator: Return the `resource` object to serialize on the
569
+ response. (Note that only one or the other `show` action helpers is allowed in
570
+ any given resource block.)
565
571
 
566
572
  ##### `show_many {|ids| ..}` => Array
567
573
 
@@ -840,7 +846,7 @@ show do |id|
840
846
  exclude = []
841
847
  exclude << 'secrets' unless role?(:admin)
842
848
 
843
- next find(id), exclude: exclude
849
+ next resource, exclude: exclude
844
850
  end
845
851
  ```
846
852
 
@@ -1370,12 +1376,12 @@ constraints on the join table.
1370
1376
 
1371
1377
  ### Coalesced Find Requests
1372
1378
 
1373
- If your {json:api} client coalesces find requests, the `show` action helper will
1374
- be invoked once for each ID in the `:id` filter, and the resulting collection
1375
- will be serialized on the response. Both query parameter syntaxes for arrays
1376
- are supported: `?filter[id]=1,2` and `?filter[id][]=1&filter[id][]=2`. If any
1377
- ID is not found (i.e. `show` returns `nil`), the route will halt with HTTP
1378
- status 404.
1379
+ If your {json:api} client coalesces find requests, the resource locator (or
1380
+ `show` action helper) will be invoked once for each ID in the `:id` filter, and
1381
+ the resulting collection will be serialized on the response. Both query
1382
+ parameter syntaxes for arrays are supported: `?filter[id]=1,2` and
1383
+ `?filter[id][]=1&filter[id][]=2`. If any ID is not found (i.e. `show` returns
1384
+ `nil`), the route will halt with HTTP status 404.
1379
1385
 
1380
1386
  Optionally, to reduce round trips to the database, you may define a "special"
1381
1387
  `show_many` action helper that takes an array of IDs to show. It does not take
@@ -46,8 +46,8 @@ CommentController = proc do
46
46
  end
47
47
  end
48
48
 
49
- show do |id|
50
- next find(id), include: 'author'
49
+ show do
50
+ next resource, include: 'author'
51
51
  end
52
52
 
53
53
  create(roles: :logged_in) do |attr|
@@ -59,8 +59,8 @@ PostController = proc do
59
59
  end
60
60
  end
61
61
 
62
- show do |slug|
63
- next find(slug), include: %w[author comments tags]
62
+ show do
63
+ next resource, include: %w[author comments tags]
64
64
  end
65
65
 
66
66
  show_many do |slugs|
@@ -58,19 +58,19 @@ TagController = proc do
58
58
  end
59
59
 
60
60
  replace(roles: :logged_in) do |rios|
61
- add_remove(:posts, rios) do |post|
61
+ add_remove(:posts, rios, :to_s) do |post|
62
62
  role?(:superuser) || post.author == current_user
63
63
  end
64
64
  end
65
65
 
66
66
  merge(roles: :logged_in) do |rios|
67
- add_missing(:posts, rios) do |post|
67
+ add_missing(:posts, rios, :to_s) do |post|
68
68
  role?(:superuser) || post.author == current_user
69
69
  end
70
70
  end
71
71
 
72
72
  subtract(roles: :logged_in) do |rios|
73
- remove_present(:posts, rios) do |post|
73
+ remove_present(:posts, rios, :to_s) do |post|
74
74
  role?(:superuser) || post.author == current_user
75
75
  end
76
76
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ require 'sinatra'
2
3
  require 'sinja'
3
4
 
4
5
  module Sinatra
@@ -48,7 +48,7 @@ module Sinja
48
48
  end
49
49
  end
50
50
 
51
- before %r{/(?<id>[^/]+)(?:/.*)?} do |id|
51
+ before %r{/(?<id>[^/]+)(?:/.+)?} do |id|
52
52
  self.resource =
53
53
  if env.key?('sinja.resource')
54
54
  env['sinja.resource']
@@ -28,7 +28,7 @@ module Sinja
28
28
  return unless block ||=
29
29
  case !method_defined?(action) && action
30
30
  when :show
31
- proc { |id| find(id) } if method_defined?(:find)
31
+ proc { resource } if method_defined?(:find)
32
32
  end
33
33
 
34
34
  # TODO: Move this to a constant or configurable?
@@ -18,18 +18,22 @@ module Sinja
18
18
  ids = ids.split(',') if String === ids
19
19
  ids = [*ids].tap(&:uniq!)
20
20
 
21
- resources, opts = [], {}
22
- if respond_to?(:show_many)
23
- resources, opts = show_many(ids)
24
- raise NotFoundError, "Resource(s) not found" \
25
- unless ids.length == resources.length
26
- else
27
- ids.each do |id|
28
- tmp, opts = show(id)
29
- raise NotFoundError, "Resource '#{id}' not found" unless tmp
30
- resources << tmp
21
+ resources, opts =
22
+ if respond_to?(:show_many)
23
+ show_many(ids)
24
+ else
25
+ finder =
26
+ if respond_to?(:find)
27
+ method(:find)
28
+ else
29
+ proc { |id| show(id).first }
30
+ end
31
+
32
+ [ids.map!(&finder).tap(&:compact!), {}]
31
33
  end
32
- end
34
+
35
+ raise NotFoundError, "Resource(s) not found" \
36
+ unless ids.length == resources.length
33
37
 
34
38
  serialize_models(resources, opts)
35
39
  end
@@ -52,9 +56,7 @@ module Sinja
52
56
  transaction do
53
57
  id, self.resource, opts =
54
58
  begin
55
- args = [attributes]
56
- args << data[:id] if data.key?(:id)
57
- create(*args)
59
+ create(*[attributes].tap { |a| a << data[:id] if data.key?(:id) })
58
60
  rescue ArgumentError
59
61
  if data.key?(:id)
60
62
  raise ForbiddenError, 'Client-generated ID not supported'
@@ -85,7 +87,7 @@ module Sinja
85
87
  end
86
88
 
87
89
  app.get '/:id', :qparams=>%i[include fields], :actions=>:show do |id|
88
- tmp, opts = show(id)
90
+ tmp, opts = show(*[].tap { |a| a << id unless respond_to?(:find) })
89
91
  raise NotFoundError, "Resource '#{id}' not found" unless tmp
90
92
  serialize_model(tmp, opts)
91
93
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Sinja
3
- VERSION = '1.2.0.pre3'
3
+ VERSION = '1.2.1'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinja
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.pre3
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Pastore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-15 00:00:00.000000000 Z
11
+ date: 2016-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -265,9 +265,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
265
265
  version: 2.3.0
266
266
  required_rubygems_version: !ruby/object:Gem::Requirement
267
267
  requirements:
268
- - - ">"
268
+ - - ">="
269
269
  - !ruby/object:Gem::Version
270
- version: 1.3.1
270
+ version: '0'
271
271
  requirements: []
272
272
  rubyforge_project:
273
273
  rubygems_version: 2.6.8