jsonapi-materializer 1.0.0.rc5 → 1.0.0.rc6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -59
- data/lib/jsonapi/materializer.rb +0 -1
- data/lib/jsonapi/materializer/collection.rb +1 -2
- data/lib/jsonapi/materializer/collection_spec.rb +1 -2
- data/lib/jsonapi/materializer/resource.rb +8 -36
- data/lib/jsonapi/materializer/resource/attribute.rb +0 -18
- data/lib/jsonapi/materializer/resource/relation.rb +0 -18
- data/lib/jsonapi/materializer/resource_spec.rb +1 -1
- data/lib/jsonapi/materializer/version.rb +1 -1
- metadata +2 -3
- data/lib/jsonapi/materializer/context.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93a0db9fffecd96a8bcf6cad1c1ac91770f7c0d06879fa8db306b6fcc7018ef3
|
4
|
+
data.tar.gz: 32327fa9622fe5bd18684e5d97796be5aee9a2335041fd64a3a694de40e5d5d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b87be37188db64b0494162becd98e1ee33b96138c026171f8ee3f6b859bad5b3b8a80ff2a56f3d23e07411876dac6a22e91607c8583ade664664550bedc3173d
|
7
|
+
data.tar.gz: 27cdfd03895418eda4a92e5f453bba042e169796d6916e4dbd046d83c0066dd6fc46011eaa52053fda7d32fda61d24e8491a0ba1615aa093ba69488234d3983f
|
data/README.md
CHANGED
@@ -126,65 +126,6 @@ There is *nothing* specific about rails for this library, it can be used in any
|
|
126
126
|
0. A place to store the configuration at boot (rails initializers)
|
127
127
|
|
128
128
|
|
129
|
-
### policies (aka pundit)
|
130
|
-
|
131
|
-
If you're using some sort of policy logic like pundit you'll have the ability to pass it as a context to the materializer:
|
132
|
-
|
133
|
-
``` ruby
|
134
|
-
class AccountsController < ApplicationController
|
135
|
-
def show
|
136
|
-
context = {
|
137
|
-
:policy => policy
|
138
|
-
}
|
139
|
-
render(
|
140
|
-
:json => AccountMaterializer::Resource.new(:object => object, :context => context)
|
141
|
-
)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
```
|
145
|
-
|
146
|
-
And now the use of that context object:
|
147
|
-
|
148
|
-
``` ruby
|
149
|
-
class AccountMaterializer
|
150
|
-
include(JSONAPI::Materializer::Resource)
|
151
|
-
|
152
|
-
type(:accounts)
|
153
|
-
|
154
|
-
has_many(:reviews, :class_name => "ReviewMaterializer")
|
155
|
-
|
156
|
-
has(:name, :visible => :readable_attribute?)
|
157
|
-
|
158
|
-
private def readable_attribute?(attribute)
|
159
|
-
context.policy.read_attribute?(attribute.from)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
```
|
163
|
-
|
164
|
-
You'll notice that context is an object, not a hash, when referenced on the materializer. That's because we give you the ability to enforce the context for saftey:
|
165
|
-
|
166
|
-
``` ruby
|
167
|
-
|
168
|
-
class AccountMaterializer
|
169
|
-
include(JSONAPI::Materializer::Resource)
|
170
|
-
|
171
|
-
type(:accounts)
|
172
|
-
|
173
|
-
has_many(:reviews, :class_name => "ReviewMaterializer")
|
174
|
-
|
175
|
-
has(:name, :visible => :readable_attribute?)
|
176
|
-
|
177
|
-
context.validates_presence_of(:policy)
|
178
|
-
|
179
|
-
private def readable_attribute?(attribute)
|
180
|
-
context.policy.read_attribute?(attribute.from)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
```
|
184
|
-
|
185
|
-
These are just aliases for ActiveModel::Validations.
|
186
|
-
|
187
|
-
|
188
129
|
### Sister Projects
|
189
130
|
|
190
131
|
I'm already using jsonapi-materializer. and it's sister project [jsonapi-realizer](https://github.com/krainboltgreene/jsonapi-realizer.rb) in a new gem of mine that allows services to be discoverable: [jsonapi-home](https://github.com/krainboltgreene/jsonapi-home.rb).
|
data/lib/jsonapi/materializer.rb
CHANGED
@@ -10,7 +10,6 @@ module JSONAPI
|
|
10
10
|
attr_writer(:selects)
|
11
11
|
attr_writer(:includes)
|
12
12
|
attr_writer(:pagination)
|
13
|
-
attr_accessor(:context)
|
14
13
|
|
15
14
|
delegate(:first_page?, :to => :object)
|
16
15
|
delegate(:prev_page, :to => :object)
|
@@ -34,7 +33,7 @@ module JSONAPI
|
|
34
33
|
end
|
35
34
|
|
36
35
|
private def materializers
|
37
|
-
@materializers ||= object.map {|subobject| self.class.parent.new(:object => subobject, :selects => selects, :includes => includes
|
36
|
+
@materializers ||= object.map {|subobject| self.class.parent.new(:object => subobject, :selects => selects, :includes => includes)}
|
38
37
|
end
|
39
38
|
|
40
39
|
private def links_pagination
|
@@ -2,8 +2,7 @@ require("spec_helper")
|
|
2
2
|
|
3
3
|
RSpec.describe(JSONAPI::Materializer::Collection) do
|
4
4
|
let(:described_class) {ArticleMaterializer::Collection}
|
5
|
-
let(:
|
6
|
-
let(:collection) {described_class.new(:object => object, :includes => [["comments"], ["author"]], :context => {:policy => policy.new})}
|
5
|
+
let(:collection) {described_class.new(:object => object, :includes => [["comments"], ["author"]])}
|
7
6
|
|
8
7
|
describe("#as_json") do
|
9
8
|
subject {collection.as_json.deep_stringify_keys}
|
@@ -19,18 +19,6 @@ module JSONAPI
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
unless const_defined?("Context")
|
23
|
-
self::Context = Class.new do
|
24
|
-
include(JSONAPI::Materializer::Context)
|
25
|
-
|
26
|
-
def initialize(**keyword_arguments)
|
27
|
-
keyword_arguments.keys.each(&singleton_class.method(:attr_accessor))
|
28
|
-
|
29
|
-
super(**keyword_arguments)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
22
|
validates_presence_of(:object, allow_blank: true)
|
35
23
|
|
36
24
|
origin(JSONAPI::Materializer.configuration.default_origin)
|
@@ -42,7 +30,6 @@ module JSONAPI
|
|
42
30
|
attr_accessor(:object)
|
43
31
|
attr_writer(:selects)
|
44
32
|
attr_writer(:includes)
|
45
|
-
attr_writer(:context)
|
46
33
|
attr_reader(:raw)
|
47
34
|
|
48
35
|
def initialize(**keyword_arguments)
|
@@ -50,7 +37,6 @@ module JSONAPI
|
|
50
37
|
|
51
38
|
@raw = keyword_arguments
|
52
39
|
|
53
|
-
context.validate!
|
54
40
|
validate!
|
55
41
|
end
|
56
42
|
|
@@ -70,12 +56,9 @@ module JSONAPI
|
|
70
56
|
|
71
57
|
private def exposed(mapping)
|
72
58
|
if selects.any?
|
73
|
-
mapping.
|
74
|
-
select {|_, value| value.visible?(self)}.
|
75
|
-
slice(*selects.dig(type))
|
59
|
+
mapping.slice(*selects.dig(type))
|
76
60
|
else
|
77
|
-
mapping
|
78
|
-
select {|_, value| value.visible?(self)}
|
61
|
+
mapping
|
79
62
|
end
|
80
63
|
end
|
81
64
|
|
@@ -135,10 +118,6 @@ module JSONAPI
|
|
135
118
|
@includes || []
|
136
119
|
end
|
137
120
|
|
138
|
-
def context
|
139
|
-
self.class.const_get("Context").new(**@context || {})
|
140
|
-
end
|
141
|
-
|
142
121
|
private def included
|
143
122
|
@included ||= includes.flat_map do |path|
|
144
123
|
path.reduce(self) do |subject, key|
|
@@ -172,41 +151,34 @@ module JSONAPI
|
|
172
151
|
@type = value.to_sym
|
173
152
|
end
|
174
153
|
|
175
|
-
def has(name, from: name
|
154
|
+
def has(name, from: name)
|
176
155
|
@attributes[name] = Attribute.new(
|
177
156
|
:owner => self,
|
178
157
|
:name => name,
|
179
|
-
:from => from
|
180
|
-
:visible => visible
|
158
|
+
:from => from
|
181
159
|
)
|
182
160
|
end
|
183
161
|
|
184
|
-
def has_one(name, from: name, class_name
|
162
|
+
def has_one(name, from: name, class_name:)
|
185
163
|
@relations[name] = Relation.new(
|
186
164
|
:owner => self,
|
187
165
|
:type => :one,
|
188
166
|
:name => name,
|
189
167
|
:from => from,
|
190
|
-
:class_name => class_name
|
191
|
-
:visible => visible
|
168
|
+
:class_name => class_name
|
192
169
|
)
|
193
170
|
end
|
194
171
|
|
195
|
-
def has_many(name, from: name, class_name
|
172
|
+
def has_many(name, from: name, class_name:)
|
196
173
|
@relations[name] = Relation.new(
|
197
174
|
:owner => self,
|
198
175
|
:type => :many,
|
199
176
|
:name => name,
|
200
177
|
:from => from,
|
201
|
-
:class_name => class_name
|
202
|
-
:visible => visible
|
178
|
+
:class_name => class_name
|
203
179
|
)
|
204
180
|
end
|
205
181
|
|
206
|
-
def context
|
207
|
-
const_get("Context")
|
208
|
-
end
|
209
|
-
|
210
182
|
def configuration
|
211
183
|
@configuration ||= Configuration.new(
|
212
184
|
:owner => self,
|
@@ -7,12 +7,10 @@ module JSONAPI
|
|
7
7
|
attr_accessor(:owner)
|
8
8
|
attr_accessor(:name)
|
9
9
|
attr_accessor(:from)
|
10
|
-
attr_accessor(:visible)
|
11
10
|
|
12
11
|
validates_presence_of(:owner)
|
13
12
|
validates_presence_of(:name)
|
14
13
|
validates_presence_of(:from)
|
15
|
-
validate(:visible_callable)
|
16
14
|
|
17
15
|
def initialize(**keyword_arguments)
|
18
16
|
super(**keyword_arguments)
|
@@ -24,25 +22,9 @@ module JSONAPI
|
|
24
22
|
subject.object.public_send(from)
|
25
23
|
end
|
26
24
|
|
27
|
-
def visible?(subject)
|
28
|
-
return visible if [true, false].include?(visible)
|
29
|
-
return subject.send(visible, :attribute, self) if visible.is_a?(Symbol)
|
30
|
-
return visible.call(:attribute, self) if visible.respond_to?(:call)
|
31
|
-
|
32
|
-
true
|
33
|
-
end
|
34
|
-
|
35
25
|
private def materializer_class
|
36
26
|
class_name.constantize
|
37
27
|
end
|
38
|
-
|
39
|
-
private def visible_callable
|
40
|
-
return if [true, false].include?(visible)
|
41
|
-
return if visible.is_a?(Symbol)
|
42
|
-
return if visible.respond_to?(:call)
|
43
|
-
|
44
|
-
errors.add(:visible, "not callable or boolean")
|
45
|
-
end
|
46
28
|
end
|
47
29
|
end
|
48
30
|
end
|
@@ -9,14 +9,12 @@ module JSONAPI
|
|
9
9
|
attr_accessor(:type)
|
10
10
|
attr_accessor(:from)
|
11
11
|
attr_accessor(:class_name)
|
12
|
-
attr_accessor(:visible)
|
13
12
|
|
14
13
|
validates_presence_of(:owner)
|
15
14
|
validates_presence_of(:name)
|
16
15
|
validates_presence_of(:type)
|
17
16
|
validates_presence_of(:from)
|
18
17
|
validates_presence_of(:class_name)
|
19
|
-
validate(:visible_callable)
|
20
18
|
|
21
19
|
def initialize(**keyword_arguments)
|
22
20
|
super(**keyword_arguments)
|
@@ -46,14 +44,6 @@ module JSONAPI
|
|
46
44
|
end
|
47
45
|
end
|
48
46
|
|
49
|
-
def visible?(subject)
|
50
|
-
return visible if [true, false].include?(visible)
|
51
|
-
return subject.send(visible, type, self) if visible.is_a?(Symbol)
|
52
|
-
return visible.call(type, self) if visible.respond_to?(:call)
|
53
|
-
|
54
|
-
true
|
55
|
-
end
|
56
|
-
|
57
47
|
private def fetch_relation(subject)
|
58
48
|
@fetch_relationship ||= {}
|
59
49
|
@fetch_relationship[checksum(subject)] ||= subject.object.public_send(from)
|
@@ -75,14 +65,6 @@ module JSONAPI
|
|
75
65
|
class_name.constantize
|
76
66
|
end
|
77
67
|
|
78
|
-
private def visible_callable
|
79
|
-
return if [true, false].include?(visible)
|
80
|
-
return if visible.is_a?(Symbol)
|
81
|
-
return if visible.respond_to?(:call)
|
82
|
-
|
83
|
-
errors.add(:visible, "not callable or boolean")
|
84
|
-
end
|
85
|
-
|
86
68
|
private def unlessing(object, proc)
|
87
69
|
unless proc.call()
|
88
70
|
yield(object)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require("spec_helper")
|
2
2
|
|
3
3
|
RSpec.describe(JSONAPI::Materializer::Resource) do
|
4
|
-
let(:resource) {ArticleMaterializer.new(:object => Article.find(1)
|
4
|
+
let(:resource) {ArticleMaterializer.new(:object => Article.find(1))}
|
5
5
|
|
6
6
|
before do
|
7
7
|
Account.create!(:id => 9, :name => "Dan Gebhardt", :twitter => "dgeb")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-materializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kurtis Rainbolt-Greene
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -246,7 +246,6 @@ files:
|
|
246
246
|
- lib/jsonapi/materializer/collection.rb
|
247
247
|
- lib/jsonapi/materializer/collection_spec.rb
|
248
248
|
- lib/jsonapi/materializer/configuration.rb
|
249
|
-
- lib/jsonapi/materializer/context.rb
|
250
249
|
- lib/jsonapi/materializer/controller.rb
|
251
250
|
- lib/jsonapi/materializer/error.rb
|
252
251
|
- lib/jsonapi/materializer/error/invalid_accept_header.rb
|