Adjusts JsonApi adapter to serialize relationships in a nested relationships hash

This commit is contained in:
Benedikt Deicke 2015-05-21 16:35:35 +02:00
parent ca41901fb8
commit 4f576a1463
9 changed files with 150 additions and 150 deletions

View File

@ -29,7 +29,7 @@ module ActiveModel
end end
else else
@hash[:data] = attributes_for_serializer(serializer, @options) @hash[:data] = attributes_for_serializer(serializer, @options)
add_resource_links(@hash[:data], serializer) add_resource_relationships(@hash[:data], serializer)
end end
@hash @hash
end end
@ -41,18 +41,18 @@ module ActiveModel
private private
def add_links(resource, name, serializers) def add_relationships(resource, name, serializers)
resource[:links] ||= {} resource[:relationships] ||= {}
resource[:links][name] ||= { linkage: [] } resource[:relationships][name] ||= { data: [] }
resource[:links][name][:linkage] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } } resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
end end
def add_link(resource, name, serializer, val=nil) def add_relationship(resource, name, serializer, val=nil)
resource[:links] ||= {} resource[:relationships] ||= {}
resource[:links][name] = { linkage: nil } resource[:relationships][name] = { data: nil }
if serializer && serializer.object if serializer && serializer.object
resource[:links][name][:linkage] = { type: serializer.type, id: serializer.id.to_s } resource[:relationships][name][:data] = { type: serializer.type, id: serializer.id.to_s }
end end
end end
@ -68,7 +68,7 @@ module ActiveModel
serializers.each do |serializer| serializers.each do |serializer|
attrs = attributes_for_serializer(serializer, @options) attrs = attributes_for_serializer(serializer, @options)
add_resource_links(attrs, serializer, add_included: false) add_resource_relationships(attrs, serializer, add_included: false)
@hash[:included].push(attrs) unless @hash[:included].include?(attrs) @hash[:included].push(attrs) unless @hash[:included].include?(attrs)
end end
@ -128,19 +128,19 @@ module ActiveModel
end end
end end
def add_resource_links(attrs, serializer, options = {}) def add_resource_relationships(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true) options[:add_included] = options.fetch(:add_included, true)
serializer.each_association do |name, association, opts| serializer.each_association do |name, association, opts|
attrs[:links] ||= {} attrs[:relationships] ||= {}
if association.respond_to?(:each) if association.respond_to?(:each)
add_links(attrs, name, association) add_relationships(attrs, name, association)
else else
if opts[:virtual_value] if opts[:virtual_value]
add_link(attrs, name, nil, opts[:virtual_value]) add_relationship(attrs, name, nil, opts[:virtual_value])
else else
add_link(attrs, name, association) add_relationship(attrs, name, association)
end end
end end

View File

@ -104,10 +104,10 @@ module ActionController
"attributes" => { "attributes" => {
"name" => "Steve K." "name" => "Steve K."
}, },
"links" => { "relationships" => {
"posts" => { "linkage" => [] }, "posts" => { "data" => [] },
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] }, "roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
"bio" => { "linkage" => nil } "bio" => { "data" => nil }
} }
}, { }, {
"id" => "1", "id" => "1",
@ -117,8 +117,8 @@ module ActionController
"description" => nil, "description" => nil,
"slug" => "admin-1" "slug" => "admin-1"
}, },
"links" => { "relationships" => {
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } } "author" => { "data" => { "type" =>"authors", "id" => "1" } }
} }
}, { }, {
"id" => "2", "id" => "2",
@ -128,8 +128,8 @@ module ActionController
"description" => nil, "description" => nil,
"slug" => "colab-2" "slug" => "colab-2"
}, },
"links" => { "relationships" => {
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } } "author" => { "data" => { "type" =>"authors", "id" => "1" } }
} }
} }
] ]

View File

@ -32,9 +32,9 @@ module ActiveModel
end end
def test_includes_post_id def test_includes_post_id
expected = { linkage: { type: "posts", id: "42" } } expected = { data: { type: "posts", id: "42" } }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
end end
def test_includes_linked_post def test_includes_linked_post
@ -46,10 +46,10 @@ module ActiveModel
title: 'New Post', title: 'New Post',
body: 'Body', body: 'Body',
}, },
links: { relationships: {
comments: { linkage: [ { type: "comments", id: "1" } ] }, comments: { data: [ { type: "comments", id: "1" } ] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}] }]
assert_equal expected, @adapter.serializable_hash[:included] assert_equal expected, @adapter.serializable_hash[:included]
@ -63,10 +63,10 @@ module ActiveModel
attributes: { attributes: {
title: 'New Post' title: 'New Post'
}, },
links: { relationships: {
comments: { linkage: [ { type: "comments", id: "1" } ] }, comments: { data: [ { type: "comments", id: "1" } ] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}] }]
assert_equal expected, @adapter.serializable_hash[:included] assert_equal expected, @adapter.serializable_hash[:included]
@ -76,22 +76,22 @@ module ActiveModel
serializer = PostSerializer.new(@anonymous_post) serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links]) assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
end end
def test_include_type_for_association_when_different_than_name def test_include_type_for_association_when_different_than_name
serializer = BlogSerializer.new(@blog) serializer = BlogSerializer.new(@blog)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
links = adapter.serializable_hash[:data][:links] relationships = adapter.serializable_hash[:data][:relationships]
expected = { expected = {
writer: { writer: {
linkage: { data: {
type: "authors", type: "authors",
id: "1" id: "1"
} }
}, },
articles: { articles: {
linkage: [ data: [
{ {
type: "posts", type: "posts",
id: "42" id: "42"
@ -103,7 +103,7 @@ module ActiveModel
] ]
} }
} }
assert_equal expected, links assert_equal expected, relationships
end end
def test_include_linked_resources_with_type_name def test_include_linked_resources_with_type_name
@ -117,10 +117,10 @@ module ActiveModel
attributes: { attributes: {
name: "Steve K." name: "Steve K."
}, },
links: { relationships: {
posts: { linkage: [] }, posts: { data: [] },
roles: { linkage: [] }, roles: { data: [] },
bio: { linkage: nil } bio: { data: nil }
} }
},{ },{
id: "42", id: "42",
@ -129,10 +129,10 @@ module ActiveModel
title: "New Post", title: "New Post",
body: "Body" body: "Body"
}, },
links: { relationships: {
comments: { linkage: [ { type: "comments", id: "1" } ] }, comments: { data: [ { type: "comments", id: "1" } ] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}, { }, {
id: "43", id: "43",
@ -141,10 +141,10 @@ module ActiveModel
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!" body: "Hello, world!!"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: nil } author: { data: nil }
} }
} }
] ]

View File

@ -33,10 +33,10 @@ module ActiveModel
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!" body: "Hello, world!!"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}, },
{ {
@ -46,10 +46,10 @@ module ActiveModel
title: "New Post", title: "New Post",
body: "Body" body: "Body"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
} }
] ]
@ -67,10 +67,10 @@ module ActiveModel
attributes: { attributes: {
title: "Hello!!" title: "Hello!!"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}, },
{ {
@ -79,10 +79,10 @@ module ActiveModel
attributes: { attributes: {
title: "New Post" title: "New Post"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
} }
] ]

View File

@ -26,13 +26,13 @@ module ActiveModel
def test_includes_comment_ids def test_includes_comment_ids
expected = { expected = {
linkage: [ data: [
{ type: "posts", id: "1"}, { type: "posts", id: "1"},
{ type: "posts", id: "2"} { type: "posts", id: "2"}
] ]
} }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
end end
def test_no_includes_linked_comments def test_no_includes_linked_comments

View File

@ -30,13 +30,13 @@ module ActiveModel
def test_includes_comment_ids def test_includes_comment_ids
expected = { expected = {
linkage: [ data: [
{ type: 'comments', id: '1' }, { type: 'comments', id: '1' },
{ type: 'comments', id: '2' } { type: 'comments', id: '2' }
] ]
} }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
end end
def test_includes_linked_data def test_includes_linked_data
@ -45,22 +45,22 @@ module ActiveModel
{ {
id: '1', id: '1',
type: 'comments', type: 'comments',
links: { relationships: {
post: { linkage: { type: 'posts', id: @post.id.to_s } } post: { data: { type: 'posts', id: @post.id.to_s } }
} }
}, },
{ {
id: '2', id: '2',
type: 'comments', type: 'comments',
links: { relationships: {
post: { linkage: { type: 'posts', id: @post.id.to_s } } post: { data: { type: 'posts', id: @post.id.to_s } }
} }
}, },
{ {
id: @author.id.to_s, id: @author.id.to_s,
type: "authors", type: "authors",
links: { relationships: {
posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] } posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
} }
} }
] ]
@ -70,26 +70,26 @@ module ActiveModel
def test_includes_author_id def test_includes_author_id
expected = { expected = {
linkage: { type: "authors", id: @author.id.to_s } data: { type: "authors", id: @author.id.to_s }
} }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
end end
def test_explicit_serializer_with_null_resource def test_explicit_serializer_with_null_resource
@post.author = nil @post.author = nil
expected = { linkage: nil } expected = { data: nil }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
end end
def test_explicit_serializer_with_null_collection def test_explicit_serializer_with_null_collection
@post.comments = [] @post.comments = []
expected = { linkage: [] } expected = { data: [] }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
end end
end end
end end

View File

@ -33,9 +33,9 @@ module ActiveModel
end end
def test_includes_comment_ids def test_includes_comment_ids
expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] } expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
end end
def test_includes_linked_comments def test_includes_linked_comments
@ -46,9 +46,9 @@ module ActiveModel
attributes: { attributes: {
body: 'ZOMG A COMMENT' body: 'ZOMG A COMMENT'
}, },
links: { relationships: {
post: { linkage: { type: "posts", id: "1" } }, post: { data: { type: "posts", id: "1" } },
author: { linkage: nil } author: { data: nil }
} }
}, { }, {
id: "2", id: "2",
@ -56,9 +56,9 @@ module ActiveModel
attributes: { attributes: {
body: 'ZOMG ANOTHER COMMENT' body: 'ZOMG ANOTHER COMMENT'
}, },
links: { relationships: {
post: { linkage: { type: "posts", id: "1" } }, post: { data: { type: "posts", id: "1" } },
author: { linkage: nil } author: { data: nil }
} }
}] }]
assert_equal expected, @adapter.serializable_hash[:included] assert_equal expected, @adapter.serializable_hash[:included]
@ -69,16 +69,16 @@ module ActiveModel
expected = [{ expected = [{
id: "1", id: "1",
type: "comments", type: "comments",
links: { relationships: {
post: { linkage: { type: "posts", id: "1" } }, post: { data: { type: "posts", id: "1" } },
author: { linkage: nil } author: { data: nil }
} }
}, { }, {
id: "2", id: "2",
type: "comments", type: "comments",
links: { relationships: {
post: { linkage: { type: "posts", id: "1" } }, post: { data: { type: "posts", id: "1" } },
author: { linkage: nil } author: { data: nil }
} }
}] }]
assert_equal expected, @adapter.serializable_hash[:included] assert_equal expected, @adapter.serializable_hash[:included]
@ -94,9 +94,9 @@ module ActiveModel
def test_include_type_for_association_when_different_than_name def test_include_type_for_association_when_different_than_name
serializer = BlogSerializer.new(@blog) serializer = BlogSerializer.new(@blog)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
actual = adapter.serializable_hash[:data][:links][:articles] actual = adapter.serializable_hash[:data][:relationships][:articles]
expected = { expected = {
linkage: [{ data: [{
type: "posts", type: "posts",
id: "1" id: "1"
}] }]

View File

@ -30,9 +30,9 @@ module ActiveModel
end end
def test_includes_bio_id def test_includes_bio_id
expected = { linkage: { type: "bios", id: "43" } } expected = { data: { type: "bios", id: "43" } }
assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio]) assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
end end
def test_includes_linked_bio def test_includes_linked_bio
@ -46,8 +46,8 @@ module ActiveModel
content:"AMS Contributor", content:"AMS Contributor",
rating: nil rating: nil
}, },
links: { relationships: {
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
} }
] ]

View File

@ -59,10 +59,10 @@ module ActiveModel
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!" body: "Hello, world!!"
}, },
links: { relationships: {
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}, },
{ {
@ -72,10 +72,10 @@ module ActiveModel
title: "New Post", title: "New Post",
body: "Body" body: "Body"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "2" } } author: { data: { type: "authors", id: "2" } }
} }
} }
], ],
@ -86,9 +86,9 @@ module ActiveModel
attributes: { attributes: {
body: "ZOMG A COMMENT" body: "ZOMG A COMMENT"
}, },
links: { relationships: {
post: { linkage: { type: "posts", id: "10" } }, post: { data: { type: "posts", id: "10" } },
author: { linkage: nil } author: { data: nil }
} }
}, { }, {
id: "2", id: "2",
@ -96,9 +96,9 @@ module ActiveModel
attributes: { attributes: {
body: "ZOMG ANOTHER COMMENT", body: "ZOMG ANOTHER COMMENT",
}, },
links: { relationships: {
post: { linkage: { type: "posts", id: "10" } }, post: { data: { type: "posts", id: "10" } },
author: { linkage: nil } author: { data: nil }
} }
}, { }, {
id: "1", id: "1",
@ -106,10 +106,10 @@ module ActiveModel
attributes: { attributes: {
name: "Steve K." name: "Steve K."
}, },
links: { relationships: {
posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] }, posts: { data: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
roles: { linkage: [] }, roles: { data: [] },
bio: { linkage: { type: "bios", id: "1" } } bio: { data: { type: "bios", id: "1" } }
} }
}, { }, {
id: "1", id: "1",
@ -118,8 +118,8 @@ module ActiveModel
content: "AMS Contributor", content: "AMS Contributor",
rating: nil rating: nil
}, },
links: { relationships: {
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}, { }, {
id: "2", id: "2",
@ -127,10 +127,10 @@ module ActiveModel
attributes: { attributes: {
name: "Tenderlove" name: "Tenderlove"
}, },
links: { relationships: {
posts: { linkage: [ { type: "posts", id:"20" } ] }, posts: { data: [ { type: "posts", id:"20" } ] },
roles: { linkage: [] }, roles: { data: [] },
bio: { linkage: { type: "bios", id: "2" } } bio: { data: { type: "bios", id: "2" } }
} }
}, { }, {
id: "2", id: "2",
@ -139,8 +139,8 @@ module ActiveModel
rating: nil, rating: nil,
content: "Rails Contributor", content: "Rails Contributor",
}, },
links: { relationships: {
author: { linkage: { type: "authors", id: "2" } } author: { data: { type: "authors", id: "2" } }
} }
} }
] ]
@ -167,10 +167,10 @@ module ActiveModel
attributes: { attributes: {
name: "Steve K." name: "Steve K."
}, },
links: { relationships: {
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] }, posts: { data: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
roles: { linkage: [] }, roles: { data: [] },
bio: { linkage: { type: "bios", id: "1" }} bio: { data: { type: "bios", id: "1" }}
} }
}, { }, {
id: "10", id: "10",
@ -179,10 +179,10 @@ module ActiveModel
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!" body: "Hello, world!!"
}, },
links: { relationships: {
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] }, comments: { data: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
}, { }, {
id: "30", id: "30",
@ -191,10 +191,10 @@ module ActiveModel
title: "Yet Another Post", title: "Yet Another Post",
body: "Body" body: "Body"
}, },
links: { relationships: {
comments: { linkage: [] }, comments: { data: [] },
blog: { linkage: { type: "blogs", id: "999" } }, blog: { data: { type: "blogs", id: "999" } },
author: { linkage: { type: "authors", id: "1" } } author: { data: { type: "authors", id: "1" } }
} }
} }
] ]
@ -208,16 +208,16 @@ module ActiveModel
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)] spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
serializer = SpammyPostSerializer.new(spammy_post) serializer = SpammyPostSerializer.new(spammy_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
links = adapter.serializable_hash[:data][:links] relationships = adapter.serializable_hash[:data][:relationships]
expected = { expected = {
related: { related: {
linkage: [{ data: [{
type: 'unrelated_links', type: 'unrelated_links',
id: '456' id: '456'
}] }]
} }
} }
assert_equal expected, links assert_equal expected, relationships
end end
def test_multiple_references_to_same_resource def test_multiple_references_to_same_resource
@ -235,15 +235,15 @@ module ActiveModel
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!" body: "Hello, world!!"
}, },
links: { relationships: {
comments: { comments: {
linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}] data: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
}, },
blog: { blog: {
linkage: {type: "blogs", id: "999"} data: {type: "blogs", id: "999"}
}, },
author: { author: {
linkage: {type: "authors", id: "1"} data: {type: "authors", id: "1"}
} }
} }
} }
@ -268,9 +268,9 @@ module ActiveModel
title: "Hello!!", title: "Hello!!",
body: "Hello, world!!" body: "Hello, world!!"
}, },
links: { relationships: {
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] }, comments: { data: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
author: { linkage: nil } author: { data: nil }
} }
} }
} }