jsonapi-serializers 0.3.0 → 0.3.1
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 +4 -4
- data/README.md +3 -1
- data/lib/jsonapi-serializers/serializer.rb +27 -7
- data/lib/jsonapi-serializers/version.rb +1 -1
- data/spec/serializer_spec.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adb7f4634374e6733cd5a2e1dee11e7863b64922
|
4
|
+
data.tar.gz: 3d3d1a0225b0a96d681918dcc4fefde4e3df9668
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe35e4ad8cfa091a813fb5e30542482c5b4c9214b068cd5f490d4b71c4e155794c12d05be0ab17b95fc4c8ec7bd2f088c240c25727fc938c75df583596709791
|
7
|
+
data.tar.gz: 176d36a30d6165a1a7463d0cf87e36349fabb64bfdbeb09b7e67d5f86a1d3052610b8b870883426622a00e9018aabd47765bfc8546e6270a67e2be2e67f9567d
|
data/README.md
CHANGED
@@ -17,6 +17,7 @@ This library is up-to-date with the finalized v1 JSON API spec.
|
|
17
17
|
* [Custom attributes](#custom-attributes)
|
18
18
|
* [More customizations](#more-customizations)
|
19
19
|
* [Base URL](#base-url)
|
20
|
+
* [Root metadata](#root-metadata)
|
20
21
|
* [Relationships](#relationships)
|
21
22
|
* [Compound documents and includes](#compound-documents-and-includes)
|
22
23
|
* [Relationship path handling](#relationship-path-handling)
|
@@ -296,7 +297,7 @@ JSONAPI::Serializer.serialize(post, base_url: 'http://example.com')
|
|
296
297
|
|
297
298
|
Note: if you override `self_link` in your serializer and leave out `base_url`, it will not be included.
|
298
299
|
|
299
|
-
###
|
300
|
+
### Root metadata
|
300
301
|
|
301
302
|
You can pass a `meta` argument to specify top-level metadata:
|
302
303
|
|
@@ -518,6 +519,7 @@ end
|
|
518
519
|
|
519
520
|
## Release notes
|
520
521
|
|
522
|
+
* v0.3.1: Improve performance of loading included relationships.
|
521
523
|
* v0.3.0: Add top-level `meta` support.
|
522
524
|
* v0.2.6: Add `base_url` support.
|
523
525
|
* v0.2.5: Allow disabling ambiguous collection checks for Sequel support.
|
@@ -91,7 +91,7 @@ module JSONAPI
|
|
91
91
|
def relationships
|
92
92
|
data = {}
|
93
93
|
# Merge in data for has_one relationships.
|
94
|
-
has_one_relationships.each do |attribute_name,
|
94
|
+
has_one_relationships.each do |attribute_name, attr_data|
|
95
95
|
formatted_attribute_name = format_name(attribute_name)
|
96
96
|
|
97
97
|
data[formatted_attribute_name] = {}
|
@@ -102,6 +102,7 @@ module JSONAPI
|
|
102
102
|
data[formatted_attribute_name]['links']['related'] = links_related if links_related
|
103
103
|
|
104
104
|
if @_include_linkages.include?(formatted_attribute_name)
|
105
|
+
object = has_one_relationship(attribute_name, attr_data)
|
105
106
|
if object.nil?
|
106
107
|
# Spec: Resource linkage MUST be represented as one of the following:
|
107
108
|
# - null for empty to-one relationships.
|
@@ -120,7 +121,7 @@ module JSONAPI
|
|
120
121
|
end
|
121
122
|
|
122
123
|
# Merge in data for has_many relationships.
|
123
|
-
has_many_relationships.each do |attribute_name,
|
124
|
+
has_many_relationships.each do |attribute_name, attr_data|
|
124
125
|
formatted_attribute_name = format_name(attribute_name)
|
125
126
|
|
126
127
|
data[formatted_attribute_name] = {}
|
@@ -136,7 +137,7 @@ module JSONAPI
|
|
136
137
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
137
138
|
if @_include_linkages.include?(formatted_attribute_name)
|
138
139
|
data[formatted_attribute_name].merge!({'data' => []})
|
139
|
-
objects =
|
140
|
+
objects = has_many_relationship(attribute_name, attr_data) || []
|
140
141
|
objects.each do |obj|
|
141
142
|
related_object_serializer = JSONAPI::Serializer.find_serializer(obj)
|
142
143
|
data[formatted_attribute_name]['data'] << {
|
@@ -165,21 +166,29 @@ module JSONAPI
|
|
165
166
|
data = {}
|
166
167
|
self.class.to_one_associations.each do |attribute_name, attr_data|
|
167
168
|
next if !should_include_attr?(attr_data[:options][:if], attr_data[:options][:unless])
|
168
|
-
data[attribute_name] =
|
169
|
+
data[attribute_name] = attr_data
|
169
170
|
end
|
170
171
|
data
|
171
172
|
end
|
172
173
|
|
174
|
+
def has_one_relationship(attribute_name, attr_data)
|
175
|
+
evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block])
|
176
|
+
end
|
177
|
+
|
173
178
|
def has_many_relationships
|
174
179
|
return {} if self.class.to_many_associations.nil?
|
175
180
|
data = {}
|
176
181
|
self.class.to_many_associations.each do |attribute_name, attr_data|
|
177
182
|
next if !should_include_attr?(attr_data[:options][:if], attr_data[:options][:unless])
|
178
|
-
data[attribute_name] =
|
183
|
+
data[attribute_name] = attr_data
|
179
184
|
end
|
180
185
|
data
|
181
186
|
end
|
182
187
|
|
188
|
+
def has_many_relationship(attribute_name, attr_data)
|
189
|
+
evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block])
|
190
|
+
end
|
191
|
+
|
183
192
|
def should_include_attr?(if_method_name, unless_method_name)
|
184
193
|
# Allow "if: :show_title?" and "unless: :hide_title?" attribute options.
|
185
194
|
show_attr = true
|
@@ -358,17 +367,28 @@ module JSONAPI
|
|
358
367
|
is_valid_attr = false
|
359
368
|
if serializer.has_one_relationships.has_key?(unformatted_attr_name)
|
360
369
|
is_valid_attr = true
|
361
|
-
|
370
|
+
attr_data = serializer.has_one_relationships[unformatted_attr_name]
|
371
|
+
object = serializer.has_one_relationship(unformatted_attr_name, attr_data)
|
362
372
|
elsif serializer.has_many_relationships.has_key?(unformatted_attr_name)
|
363
373
|
is_valid_attr = true
|
364
374
|
is_collection = true
|
365
|
-
|
375
|
+
attr_data = serializer.has_many_relationships[unformatted_attr_name]
|
376
|
+
object = serializer.has_many_relationship(unformatted_attr_name, attr_data)
|
366
377
|
end
|
378
|
+
|
367
379
|
if !is_valid_attr
|
368
380
|
raise JSONAPI::Serializer::InvalidIncludeError.new(
|
369
381
|
"'#{attribute_name}' is not a valid include.")
|
370
382
|
end
|
371
383
|
|
384
|
+
if attribute_name.include?('_')
|
385
|
+
expected_name = serializer.format_name(attribute_name)
|
386
|
+
|
387
|
+
raise JSONAPI::Serializer::InvalidIncludeError.new(
|
388
|
+
"'#{attribute_name}' is not a valid include. Did you mean '#{expected_name}' ?"
|
389
|
+
)
|
390
|
+
end
|
391
|
+
|
372
392
|
# We're finding relationships for compound documents, so skip anything that doesn't exist.
|
373
393
|
next if object.nil?
|
374
394
|
|
data/spec/serializer_spec.rb
CHANGED
@@ -364,6 +364,13 @@ describe JSONAPI::Serializer do
|
|
364
364
|
error = JSONAPI::Serializer::AmbiguousCollectionError
|
365
365
|
expect { JSONAPI::Serializer.serialize(posts) }.to raise_error(error)
|
366
366
|
end
|
367
|
+
|
368
|
+
it 'raises error if include is not named correctly' do
|
369
|
+
post = create(:post)
|
370
|
+
error = JSONAPI::Serializer::InvalidIncludeError
|
371
|
+
expect { JSONAPI::Serializer.serialize(post, include: ['long_comments']) }.to raise_error(error)
|
372
|
+
end
|
373
|
+
|
367
374
|
it 'can serialize a nil object when given serializer' do
|
368
375
|
options = {serializer: MyApp::PostSerializer}
|
369
376
|
expect(JSONAPI::Serializer.serialize(nil, options)).to eq({'data' => nil})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Fotinakis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|