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 +4 -4
- data/README.md +27 -21
- data/demo-app/classes/comment.rb +2 -2
- data/demo-app/classes/post.rb +2 -2
- data/demo-app/classes/tag.rb +3 -3
- data/lib/sinatra/jsonapi.rb +1 -0
- data/lib/sinja.rb +1 -1
- data/lib/sinja/resource.rb +1 -1
- data/lib/sinja/resource_routes.rb +17 -15
- data/lib/sinja/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2baae47c1aa32d45265b3726e8a57163bf1d381e
|
4
|
+
data.tar.gz: 2e3de4a123865207d3a9bc6090563fa979a8ee52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
264
|
-
|
265
|
-
|
266
|
-
|
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.
|
486
|
-
|
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
|
497
|
-
next
|
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
|
-
|
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
|
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
|
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
|
1374
|
-
be invoked once for each ID in the `:id` filter, and
|
1375
|
-
will be serialized on the response. Both query
|
1376
|
-
are supported: `?filter[id]=1,2` and
|
1377
|
-
ID is not found (i.e. `show` returns
|
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
|
data/demo-app/classes/comment.rb
CHANGED
data/demo-app/classes/post.rb
CHANGED
data/demo-app/classes/tag.rb
CHANGED
@@ -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
|
data/lib/sinatra/jsonapi.rb
CHANGED
data/lib/sinja.rb
CHANGED
data/lib/sinja/resource.rb
CHANGED
@@ -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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/sinja/version.rb
CHANGED
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.
|
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-
|
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:
|
270
|
+
version: '0'
|
271
271
|
requirements: []
|
272
272
|
rubyforge_project:
|
273
273
|
rubygems_version: 2.6.8
|