Skip to content

Commit 6f34471

Browse files
committed
Merge pull request #2 from mrcasals/add_relationships
Move links to relationships
2 parents 3d62126 + 3aeb3c7 commit 6f34471

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/jsonapi-serializers/serializer.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ def relationship_related_link(attribute_name)
7979
def links
8080
data = {}
8181
data.merge!({'self' => self_link}) if self_link
82+
end
8283

84+
def relationships
85+
data = {}
8386
# Merge in data for has_one relationships.
8487
has_one_relationships.each do |attribute_name, object|
8588
formatted_attribute_name = format_name(attribute_name)
@@ -260,7 +263,6 @@ def self.serialize(objects, options = {})
260263
# Given all the primary objects (either the single root object or collection of objects),
261264
# recursively search and find related associations that were specified as includes.
262265
objects = options[:is_collection] ? objects.to_a : [objects]
263-
serializers = []
264266
objects.compact.each do |obj|
265267
# Use the mutability of relationship_data as the return datastructure to take advantage
266268
# of the internal special merging logic.
@@ -296,6 +298,7 @@ def self.serialize_primary(object, options = {})
296298
# http://jsonapi.org/format/#document-structure-resource-objects
297299
data.merge!({'attributes' => serializer.attributes}) if !serializer.attributes.nil?
298300
data.merge!({'links' => serializer.links}) if !serializer.links.nil?
301+
data.merge!({'relationships' => serializer.relationships}) if !serializer.relationships.nil?
299302
data.merge!({'meta' => serializer.meta}) if !serializer.meta.nil?
300303
data
301304
end

spec/serializer_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def serialize_primary(object, options = {})
2525
'links' => {
2626
'self' => '/posts/1',
2727
},
28+
'relationships' => {},
2829
})
2930
end
3031
it 'can serialize primary data for a simple object with a long name' do
@@ -38,6 +39,8 @@ def serialize_primary(object, options = {})
3839
},
3940
'links' => {
4041
'self' => '/long-comments/1',
42+
},
43+
'relationships' => {
4144
'user' => {
4245
'self' => '/long-comments/1/links/user',
4346
'related' => '/long-comments/1/user',
@@ -62,6 +65,7 @@ def serialize_primary(object, options = {})
6265
'links' => {
6366
'self' => '/posts/1',
6467
},
68+
'relationships' => {},
6569
'meta' => {
6670
'copyright' => 'Copyright 2015 Example Corp.',
6771
'authors' => [
@@ -83,6 +87,8 @@ def serialize_primary(object, options = {})
8387
},
8488
'links' => {
8589
'self' => '/posts/1',
90+
},
91+
'relationships' => {
8692
# Both to-one and to-many links are present, but neither include linkage:
8793
'author' => {
8894
'self' => '/posts/1/links/author',
@@ -113,6 +119,8 @@ def serialize_primary(object, options = {})
113119
},
114120
'links' => {
115121
'self' => '/posts/1',
122+
},
123+
'relationships' => {
116124
'author' => {
117125
'self' => '/posts/1/links/author',
118126
'related' => '/posts/1/author',
@@ -145,6 +153,8 @@ def serialize_primary(object, options = {})
145153
},
146154
'links' => {
147155
'self' => '/posts/1',
156+
},
157+
'relationships' => {
148158
'author' => {
149159
'self' => '/posts/1/links/author',
150160
'related' => '/posts/1/author',
@@ -180,6 +190,8 @@ def serialize_primary(object, options = {})
180190
},
181191
'links' => {
182192
'self' => '/posts/1',
193+
},
194+
'relationships' => {
183195
'author' => {
184196
'self' => '/posts/1/links/author',
185197
'related' => '/posts/1/author',
@@ -213,6 +225,8 @@ def serialize_primary(object, options = {})
213225
},
214226
'links' => {
215227
'self' => '/posts/1',
228+
},
229+
'relationships' => {
216230
'author' => {
217231
'self' => '/posts/1/links/author',
218232
'related' => '/posts/1/author',
@@ -414,7 +428,7 @@ def serialize_primary(object, options = {})
414428
],
415429
}
416430
includes = ['long-comments.post.author']
417-
actual_data = JSONAPI::Serializer.serialize(post, include: ['long-comments.post.author'])
431+
actual_data = JSONAPI::Serializer.serialize(post, include: includes)
418432
# Multiple expectations for better diff output for debugging.
419433
expect(actual_data['data']).to eq(expected_data['data'])
420434
expect(actual_data['included']).to eq(expected_data['included'])

0 commit comments

Comments
 (0)