mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Always use plural for linked types
Although spec is agnostic about inflection rules, examples given are plural
This commit is contained in:
parent
0f55f21266
commit
d82c599c68
@ -118,12 +118,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def serialized_object_type(serializer)
|
def serialized_object_type(serializer)
|
||||||
return false unless Array(serializer).first
|
return false unless Array(serializer).first
|
||||||
type_name = Array(serializer).first.object.class.to_s.demodulize.underscore
|
Array(serializer).first.object.class.to_s.demodulize.underscore.pluralize
|
||||||
if serializer.respond_to?(:first)
|
|
||||||
type_name.pluralize
|
|
||||||
else
|
|
||||||
type_name
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_resource_links(attrs, serializer, options = {})
|
def add_resource_links(attrs, serializer, options = {})
|
||||||
|
|||||||
@ -111,13 +111,13 @@ module ActionController
|
|||||||
"id" => "1",
|
"id" => "1",
|
||||||
"name" => "admin",
|
"name" => "admin",
|
||||||
"links" => {
|
"links" => {
|
||||||
"author" => { "linkage" => { "type" =>"author", "id" => "1" } }
|
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
"id" => "2",
|
"id" => "2",
|
||||||
"name" => "colab",
|
"name" => "colab",
|
||||||
"links" => {
|
"links" => {
|
||||||
"author" => { "linkage" => { "type" =>"author", "id" => "1" } }
|
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_post_id
|
def test_includes_post_id
|
||||||
expected = { linkage: { type: "post", id: "42" } }
|
expected = { linkage: { type: "posts", id: "42" } }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
|
assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
|
||||||
end
|
end
|
||||||
@ -45,8 +45,8 @@ module ActiveModel
|
|||||||
body: 'Body',
|
body: 'Body',
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||||
@ -58,8 +58,8 @@ module ActiveModel
|
|||||||
title: 'New Post',
|
title: 'New Post',
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||||
@ -69,7 +69,7 @@ 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: "blog", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
|
assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_type_for_association_when_different_than_name
|
def test_include_type_for_association_when_different_than_name
|
||||||
@ -79,7 +79,7 @@ module ActiveModel
|
|||||||
expected = {
|
expected = {
|
||||||
writer: {
|
writer: {
|
||||||
linkage: {
|
linkage: {
|
||||||
type: "author",
|
type: "authors",
|
||||||
id: "1"
|
id: "1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -119,8 +119,8 @@ module ActiveModel
|
|||||||
id: "42",
|
id: "42",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
comments: { linkage: [ { type: "comments", id: "1" } ] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
@ -128,7 +128,7 @@ module ActiveModel
|
|||||||
id: "43",
|
id: "43",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|||||||
@ -32,8 +32,8 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -42,8 +42,8 @@ module ActiveModel
|
|||||||
id: "2",
|
id: "2",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -59,16 +59,16 @@ module ActiveModel
|
|||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "New Post",
|
title: "New Post",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -25,7 +25,12 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_comment_ids
|
def test_includes_comment_ids
|
||||||
expected = {:linkage=>[{:type=>"posts", :id=>"1"}, {:type=>"posts", :id=>"2"}]}
|
expected = {
|
||||||
|
linkage: [
|
||||||
|
{ type: "posts", id: "1"},
|
||||||
|
{ type: "posts", id: "2"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
|
assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
|
||||||
end
|
end
|
||||||
|
|||||||
@ -45,13 +45,13 @@ module ActiveModel
|
|||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: 'post', id: @post.id.to_s } }
|
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '2',
|
id: '2',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: 'post', id: @post.id.to_s } }
|
post: { linkage: { type: 'posts', id: @post.id.to_s } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -62,7 +62,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_includes_author_id
|
def test_includes_author_id
|
||||||
expected = {
|
expected = {
|
||||||
linkage: { type: "author", id: @author.id.to_s }
|
linkage: { type: "authors", id: @author.id.to_s }
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
|
||||||
|
|||||||
@ -44,14 +44,14 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
body: 'ZOMG A COMMENT',
|
body: 'ZOMG A COMMENT',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "post", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
body: 'ZOMG ANOTHER COMMENT',
|
body: 'ZOMG ANOTHER COMMENT',
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "post", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
@ -63,13 +63,13 @@ module ActiveModel
|
|||||||
expected = [{
|
expected = [{
|
||||||
id: "1",
|
id: "1",
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "post", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "post", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|||||||
@ -30,7 +30,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_bio_id
|
def test_includes_bio_id
|
||||||
expected = { linkage: { type: "bio", id: "43" } }
|
expected = { linkage: { type: "bios", id: "43" } }
|
||||||
|
|
||||||
assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio])
|
assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio])
|
||||||
end
|
end
|
||||||
@ -43,7 +43,7 @@ module ActiveModel
|
|||||||
id: "43",
|
id: "43",
|
||||||
content:"AMS Contributor",
|
content:"AMS Contributor",
|
||||||
links: {
|
links: {
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -57,14 +57,14 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
body: "ZOMG A COMMENT",
|
body: "ZOMG A COMMENT",
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "post", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
body: "ZOMG ANOTHER COMMENT",
|
body: "ZOMG ANOTHER COMMENT",
|
||||||
links: {
|
links: {
|
||||||
post: { linkage: { type: "post", id: "1" } },
|
post: { linkage: { type: "posts", id: "1" } },
|
||||||
author: { linkage: nil }
|
author: { linkage: nil }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ module ActiveModel
|
|||||||
links: {
|
links: {
|
||||||
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
|
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
|
||||||
roles: { linkage: [] },
|
roles: { linkage: [] },
|
||||||
bio: { linkage: { type: "bio", id: "1" } }
|
bio: { linkage: { type: "bios", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
@ -84,7 +84,7 @@ module ActiveModel
|
|||||||
links: {
|
links: {
|
||||||
posts: { linkage: [ { type: "posts", id:"2" } ] },
|
posts: { linkage: [ { type: "posts", id:"2" } ] },
|
||||||
roles: { linkage: [] },
|
roles: { linkage: [] },
|
||||||
bio: { linkage: { type: "bio", id: "2" } }
|
bio: { linkage: { type: "bios", id: "2" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -93,13 +93,13 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
content: "AMS Contributor",
|
content: "AMS Contributor",
|
||||||
links: {
|
links: {
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
content: "Rails Contributor",
|
content: "Rails Contributor",
|
||||||
links: {
|
links: {
|
||||||
author: { linkage: { type: "author", id: "2" } }
|
author: { linkage: { type: "authors", id: "2" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -111,8 +111,8 @@ module ActiveModel
|
|||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -121,8 +121,8 @@ module ActiveModel
|
|||||||
body: "Body",
|
body: "Body",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "2" } }
|
author: { linkage: { type: "authors", id: "2" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -150,7 +150,7 @@ module ActiveModel
|
|||||||
links: {
|
links: {
|
||||||
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
posts: { linkage: [ { type: "posts", id: "10"}, { type: "posts", id: "30" }] },
|
||||||
roles: { linkage: [] },
|
roles: { linkage: [] },
|
||||||
bio: { linkage: { type: "bio", id: "1" }}
|
bio: { linkage: { type: "bios", id: "1" }}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -161,8 +161,8 @@ module ActiveModel
|
|||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
comments: { linkage: [ { type: "comments", id: "1"}, { type: "comments", id: "2" }] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "30",
|
id: "30",
|
||||||
@ -170,8 +170,8 @@ module ActiveModel
|
|||||||
body: "Body",
|
body: "Body",
|
||||||
links: {
|
links: {
|
||||||
comments: { linkage: [] },
|
comments: { linkage: [] },
|
||||||
blog: { linkage: { type: "blog", id: "999" } },
|
blog: { linkage: { type: "blogs", id: "999" } },
|
||||||
author: { linkage: { type: "author", id: "1" } }
|
author: { linkage: { type: "authors", id: "1" } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user