Skip to content

Commit 4f576a1

Browse files
committed
Adjusts JsonApi adapter to serialize relationships in a nested relationships hash
1 parent ca41901 commit 4f576a1

File tree

9 files changed

+150
-150
lines changed

9 files changed

+150
-150
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def serializable_hash(options = {})
2929
end
3030
else
3131
@hash[:data] = attributes_for_serializer(serializer, @options)
32-
add_resource_links(@hash[:data], serializer)
32+
add_resource_relationships(@hash[:data], serializer)
3333
end
3434
@hash
3535
end
@@ -41,18 +41,18 @@ def fragment_cache(cached_hash, non_cached_hash)
4141

4242
private
4343

44-
def add_links(resource, name, serializers)
45-
resource[:links] ||= {}
46-
resource[:links][name] ||= { linkage: [] }
47-
resource[:links][name][:linkage] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
44+
def add_relationships(resource, name, serializers)
45+
resource[:relationships] ||= {}
46+
resource[:relationships][name] ||= { data: [] }
47+
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
4848
end
4949

50-
def add_link(resource, name, serializer, val=nil)
51-
resource[:links] ||= {}
52-
resource[:links][name] = { linkage: nil }
50+
def add_relationship(resource, name, serializer, val=nil)
51+
resource[:relationships] ||= {}
52+
resource[:relationships][name] = { data: nil }
5353

5454
if serializer && serializer.object
55-
resource[:links][name][:linkage] = { type: serializer.type, id: serializer.id.to_s }
55+
resource[:relationships][name][:data] = { type: serializer.type, id: serializer.id.to_s }
5656
end
5757
end
5858

@@ -68,7 +68,7 @@ def add_included(resource_name, serializers, parent = nil)
6868
serializers.each do |serializer|
6969
attrs = attributes_for_serializer(serializer, @options)
7070

71-
add_resource_links(attrs, serializer, add_included: false)
71+
add_resource_relationships(attrs, serializer, add_included: false)
7272

7373
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
7474
end
@@ -128,19 +128,19 @@ def check_assoc(assoc)
128128
end
129129
end
130130

131-
def add_resource_links(attrs, serializer, options = {})
131+
def add_resource_relationships(attrs, serializer, options = {})
132132
options[:add_included] = options.fetch(:add_included, true)
133133

134134
serializer.each_association do |name, association, opts|
135-
attrs[:links] ||= {}
135+
attrs[:relationships] ||= {}
136136

137137
if association.respond_to?(:each)
138-
add_links(attrs, name, association)
138+
add_relationships(attrs, name, association)
139139
else
140140
if opts[:virtual_value]
141-
add_link(attrs, name, nil, opts[:virtual_value])
141+
add_relationship(attrs, name, nil, opts[:virtual_value])
142142
else
143-
add_link(attrs, name, association)
143+
add_relationship(attrs, name, association)
144144
end
145145
end
146146

test/action_controller/json_api_linked_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ def test_render_resource_with_nested_has_many_include
104104
"attributes" => {
105105
"name" => "Steve K."
106106
},
107-
"links" => {
108-
"posts" => { "linkage" => [] },
109-
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
110-
"bio" => { "linkage" => nil }
107+
"relationships" => {
108+
"posts" => { "data" => [] },
109+
"roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
110+
"bio" => { "data" => nil }
111111
}
112112
}, {
113113
"id" => "1",
@@ -117,8 +117,8 @@ def test_render_resource_with_nested_has_many_include
117117
"description" => nil,
118118
"slug" => "admin-1"
119119
},
120-
"links" => {
121-
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
120+
"relationships" => {
121+
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
122122
}
123123
}, {
124124
"id" => "2",
@@ -128,8 +128,8 @@ def test_render_resource_with_nested_has_many_include
128128
"description" => nil,
129129
"slug" => "colab-2"
130130
},
131-
"links" => {
132-
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
131+
"relationships" => {
132+
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
133133
}
134134
}
135135
]

test/adapter/json_api/belongs_to_test.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def setup
3232
end
3333

3434
def test_includes_post_id
35-
expected = { linkage: { type: "posts", id: "42" } }
35+
expected = { data: { type: "posts", id: "42" } }
3636

37-
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
37+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
3838
end
3939

4040
def test_includes_linked_post
@@ -46,10 +46,10 @@ def test_includes_linked_post
4646
title: 'New Post',
4747
body: 'Body',
4848
},
49-
links: {
50-
comments: { linkage: [ { type: "comments", id: "1" } ] },
51-
blog: { linkage: { type: "blogs", id: "999" } },
52-
author: { linkage: { type: "authors", id: "1" } }
49+
relationships: {
50+
comments: { data: [ { type: "comments", id: "1" } ] },
51+
blog: { data: { type: "blogs", id: "999" } },
52+
author: { data: { type: "authors", id: "1" } }
5353
}
5454
}]
5555
assert_equal expected, @adapter.serializable_hash[:included]
@@ -63,10 +63,10 @@ def test_limiting_linked_post_fields
6363
attributes: {
6464
title: 'New Post'
6565
},
66-
links: {
67-
comments: { linkage: [ { type: "comments", id: "1" } ] },
68-
blog: { linkage: { type: "blogs", id: "999" } },
69-
author: { linkage: { type: "authors", id: "1" } }
66+
relationships: {
67+
comments: { data: [ { type: "comments", id: "1" } ] },
68+
blog: { data: { type: "blogs", id: "999" } },
69+
author: { data: { type: "authors", id: "1" } }
7070
}
7171
}]
7272
assert_equal expected, @adapter.serializable_hash[:included]
@@ -76,22 +76,22 @@ def test_include_nil_author
7676
serializer = PostSerializer.new(@anonymous_post)
7777
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
7878

79-
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
79+
assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
8080
end
8181

8282
def test_include_type_for_association_when_different_than_name
8383
serializer = BlogSerializer.new(@blog)
8484
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
85-
links = adapter.serializable_hash[:data][:links]
85+
relationships = adapter.serializable_hash[:data][:relationships]
8686
expected = {
8787
writer: {
88-
linkage: {
88+
data: {
8989
type: "authors",
9090
id: "1"
9191
}
9292
},
9393
articles: {
94-
linkage: [
94+
data: [
9595
{
9696
type: "posts",
9797
id: "42"
@@ -103,7 +103,7 @@ def test_include_type_for_association_when_different_than_name
103103
]
104104
}
105105
}
106-
assert_equal expected, links
106+
assert_equal expected, relationships
107107
end
108108

109109
def test_include_linked_resources_with_type_name
@@ -117,10 +117,10 @@ def test_include_linked_resources_with_type_name
117117
attributes: {
118118
name: "Steve K."
119119
},
120-
links: {
121-
posts: { linkage: [] },
122-
roles: { linkage: [] },
123-
bio: { linkage: nil }
120+
relationships: {
121+
posts: { data: [] },
122+
roles: { data: [] },
123+
bio: { data: nil }
124124
}
125125
},{
126126
id: "42",
@@ -129,10 +129,10 @@ def test_include_linked_resources_with_type_name
129129
title: "New Post",
130130
body: "Body"
131131
},
132-
links: {
133-
comments: { linkage: [ { type: "comments", id: "1" } ] },
134-
blog: { linkage: { type: "blogs", id: "999" } },
135-
author: { linkage: { type: "authors", id: "1" } }
132+
relationships: {
133+
comments: { data: [ { type: "comments", id: "1" } ] },
134+
blog: { data: { type: "blogs", id: "999" } },
135+
author: { data: { type: "authors", id: "1" } }
136136
}
137137
}, {
138138
id: "43",
@@ -141,10 +141,10 @@ def test_include_linked_resources_with_type_name
141141
title: "Hello!!",
142142
body: "Hello, world!!"
143143
},
144-
links: {
145-
comments: { linkage: [] },
146-
blog: { linkage: { type: "blogs", id: "999" } },
147-
author: { linkage: nil }
144+
relationships: {
145+
comments: { data: [] },
146+
blog: { data: { type: "blogs", id: "999" } },
147+
author: { data: nil }
148148
}
149149
}
150150
]

test/adapter/json_api/collection_test.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def test_include_multiple_posts
3333
title: "Hello!!",
3434
body: "Hello, world!!"
3535
},
36-
links: {
37-
comments: { linkage: [] },
38-
blog: { linkage: { type: "blogs", id: "999" } },
39-
author: { linkage: { type: "authors", id: "1" } }
36+
relationships: {
37+
comments: { data: [] },
38+
blog: { data: { type: "blogs", id: "999" } },
39+
author: { data: { type: "authors", id: "1" } }
4040
}
4141
},
4242
{
@@ -46,10 +46,10 @@ def test_include_multiple_posts
4646
title: "New Post",
4747
body: "Body"
4848
},
49-
links: {
50-
comments: { linkage: [] },
51-
blog: { linkage: { type: "blogs", id: "999" } },
52-
author: { linkage: { type: "authors", id: "1" } }
49+
relationships: {
50+
comments: { data: [] },
51+
blog: { data: { type: "blogs", id: "999" } },
52+
author: { data: { type: "authors", id: "1" } }
5353
}
5454
}
5555
]
@@ -67,10 +67,10 @@ def test_limiting_fields
6767
attributes: {
6868
title: "Hello!!"
6969
},
70-
links: {
71-
comments: { linkage: [] },
72-
blog: { linkage: { type: "blogs", id: "999" } },
73-
author: { linkage: { type: "authors", id: "1" } }
70+
relationships: {
71+
comments: { data: [] },
72+
blog: { data: { type: "blogs", id: "999" } },
73+
author: { data: { type: "authors", id: "1" } }
7474
}
7575
},
7676
{
@@ -79,10 +79,10 @@ def test_limiting_fields
7979
attributes: {
8080
title: "New Post"
8181
},
82-
links: {
83-
comments: { linkage: [] },
84-
blog: { linkage: { type: "blogs", id: "999" } },
85-
author: { linkage: { type: "authors", id: "1" } }
82+
relationships: {
83+
comments: { data: [] },
84+
blog: { data: { type: "blogs", id: "999" } },
85+
author: { data: { type: "authors", id: "1" } }
8686
}
8787
}
8888
]

test/adapter/json_api/has_many_embed_ids_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def setup
2626

2727
def test_includes_comment_ids
2828
expected = {
29-
linkage: [
29+
data: [
3030
{ type: "posts", id: "1"},
3131
{ type: "posts", id: "2"}
3232
]
3333
}
3434

35-
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
35+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
3636
end
3737

3838
def test_no_includes_linked_comments

test/adapter/json_api/has_many_explicit_serializer_test.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def setup
3030

3131
def test_includes_comment_ids
3232
expected = {
33-
linkage: [
33+
data: [
3434
{ type: 'comments', id: '1' },
3535
{ type: 'comments', id: '2' }
3636
]
3737
}
3838

39-
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
39+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
4040
end
4141

4242
def test_includes_linked_data
@@ -45,22 +45,22 @@ def test_includes_linked_data
4545
{
4646
id: '1',
4747
type: 'comments',
48-
links: {
49-
post: { linkage: { type: 'posts', id: @post.id.to_s } }
48+
relationships: {
49+
post: { data: { type: 'posts', id: @post.id.to_s } }
5050
}
5151
},
5252
{
5353
id: '2',
5454
type: 'comments',
55-
links: {
56-
post: { linkage: { type: 'posts', id: @post.id.to_s } }
55+
relationships: {
56+
post: { data: { type: 'posts', id: @post.id.to_s } }
5757
}
5858
},
5959
{
6060
id: @author.id.to_s,
6161
type: "authors",
62-
links: {
63-
posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] }
62+
relationships: {
63+
posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
6464
}
6565
}
6666
]
@@ -70,26 +70,26 @@ def test_includes_linked_data
7070

7171
def test_includes_author_id
7272
expected = {
73-
linkage: { type: "authors", id: @author.id.to_s }
73+
data: { type: "authors", id: @author.id.to_s }
7474
}
7575

76-
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
76+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
7777
end
7878

7979
def test_explicit_serializer_with_null_resource
8080
@post.author = nil
8181

82-
expected = { linkage: nil }
82+
expected = { data: nil }
8383

84-
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
84+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
8585
end
8686

8787
def test_explicit_serializer_with_null_collection
8888
@post.comments = []
8989

90-
expected = { linkage: [] }
90+
expected = { data: [] }
9191

92-
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
92+
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
9393
end
9494
end
9595
end

0 commit comments

Comments
 (0)