mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 06:46:50 +00:00
Merge pull request #660 from kbullaughey/standardize_has_one_and_has_many_on_string_roots
Standardize has one and has many on string roots
This commit is contained in:
commit
42010cc8e8
@ -4,12 +4,11 @@ module ActiveModel
|
|||||||
class HasMany < Association
|
class HasMany < Association
|
||||||
def initialize(name, *args)
|
def initialize(name, *args)
|
||||||
super
|
super
|
||||||
@root_key = @embedded_key
|
@root_key = @embedded_key.to_s
|
||||||
@key ||= case CONFIG.default_key_type
|
@key ||= case CONFIG.default_key_type
|
||||||
when :name then name.to_s.pluralize
|
when :name then name.to_s.pluralize
|
||||||
else "#{name.to_s.singularize}_ids"
|
else "#{name.to_s.singularize}_ids"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializer_class(object, _)
|
def serializer_class(object, _)
|
||||||
@ -37,4 +36,4 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
12
test/fixtures/poro.rb
vendored
12
test/fixtures/poro.rb
vendored
@ -47,6 +47,12 @@ class Post < Model
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SpecialPost < Post
|
||||||
|
def special_comment
|
||||||
|
@speical_comment ||= Comment.new(content: 'special')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Comment < Model
|
class Comment < Model
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -110,6 +116,12 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
has_many :comments
|
has_many :comments
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class SpecialPostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :title, :body
|
||||||
|
has_many :comments, root: :comments, embed_in_root: true, embed: :ids
|
||||||
|
has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids
|
||||||
|
end
|
||||||
|
|
||||||
class CommentSerializer < ActiveModel::Serializer
|
class CommentSerializer < ActiveModel::Serializer
|
||||||
attributes :content
|
attributes :content
|
||||||
end
|
end
|
||||||
|
|||||||
@ -49,9 +49,9 @@ module ActiveModel
|
|||||||
'ar_tag_ids' => [1, 2],
|
'ar_tag_ids' => [1, 2],
|
||||||
'ar_section_id' => 1
|
'ar_section_id' => 1
|
||||||
},
|
},
|
||||||
ar_comments: [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
|
'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
|
||||||
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
|
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
|
||||||
ar_tags: [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
|
'ar_tags' => [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
|
||||||
'ar_sections' => [{ 'name' => 'ruby' }]
|
'ar_sections' => [{ 'name' => 'ruby' }]
|
||||||
}, post_serializer.as_json)
|
}, post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -85,7 +85,7 @@ module ActiveModel
|
|||||||
{title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
|
{title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
|
||||||
{title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
|
{title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
|
||||||
],
|
],
|
||||||
comments: [
|
'comments' => [
|
||||||
{content: "C1"},
|
{content: "C1"},
|
||||||
{content: "C2"},
|
{content: "C2"},
|
||||||
{content: "C3"},
|
{content: "C3"},
|
||||||
|
|||||||
@ -140,7 +140,7 @@ module ActiveModel
|
|||||||
{ id: c.object_id, type: model_name(c) }
|
{ id: c.object_id, type: model_name(c) }
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
attachments: [
|
'attachments' => [
|
||||||
{ type: :image, image: { url: 'U1' }},
|
{ type: :image, image: { url: 'U1' }},
|
||||||
{ type: :video, video: { html: 'H1' }}
|
{ type: :video, video: { html: 'H1' }}
|
||||||
]
|
]
|
||||||
@ -153,7 +153,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'mail' => { body: 'Body 1' },
|
'mail' => { body: 'Body 1' },
|
||||||
attachments: [
|
'attachments' => [
|
||||||
{ type: :image, image: { url: 'U1' }},
|
{ type: :image, image: { url: 'U1' }},
|
||||||
{ type: :video, video: { html: 'H1' }}
|
{ type: :video, video: { html: 'H1' }}
|
||||||
]
|
]
|
||||||
@ -178,7 +178,7 @@ module ActiveModel
|
|||||||
{ id: c.object_id, type: model_name(c) }
|
{ id: c.object_id, type: model_name(c) }
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
attachments: [
|
'attachments' => [
|
||||||
{ type: :image, image: { fake: 'fake' }},
|
{ type: :image, image: { fake: 'fake' }},
|
||||||
{ type: :video, video: { fake: 'fake' }}
|
{ type: :video, video: { fake: 'fake' }}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -111,7 +111,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||||
comments: [{ content: 'C1' }, { content: 'C2' }]
|
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ module ActiveModel
|
|||||||
name: 'Name 1',
|
name: 'Name 1',
|
||||||
posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }]
|
posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }]
|
||||||
},
|
},
|
||||||
comments: [{ content: 'C1' }, { content: 'C2' }]
|
"comments" => [{ content: 'C1' }, { content: 'C2' }]
|
||||||
}, category_serializer.as_json)
|
}, category_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1' },
|
'post' => { title: 'Title 1', body: 'Body 1' },
|
||||||
comments: [{ content: 'C1' }, { content: 'C2' }]
|
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||||
comments: [{ content: 'C1!' }, { content: 'C2!' }]
|
'comments' => [{ content: 'C1!' }, { content: 'C2!' }]
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -170,7 +170,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||||
comments: { my_content: ['fake'] }
|
'comments' => { my_content: ['fake'] }
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -191,16 +191,16 @@ module ActiveModel
|
|||||||
@association.embed_in_root_key = :linked
|
@association.embed_in_root_key = :linked
|
||||||
@association.embed = :ids
|
@association.embed = :ids
|
||||||
assert_equal({
|
assert_equal({
|
||||||
|
'post' => {
|
||||||
|
title: 'Title 1', body: 'Body 1',
|
||||||
|
'comment_ids' => @post.comments.map(&:object_id)
|
||||||
|
},
|
||||||
linked: {
|
linked: {
|
||||||
comments: [
|
'comments' => [
|
||||||
{ content: 'C1' },
|
{ content: 'C1' },
|
||||||
{ content: 'C2' }
|
{ content: 'C2' }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
'post' => {
|
|
||||||
title: 'Title 1', body: 'Body 1',
|
|
||||||
'comment_ids' => @post.comments.map(&:object_id)
|
|
||||||
}
|
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -211,18 +211,18 @@ module ActiveModel
|
|||||||
@association.embed_namespace = :links
|
@association.embed_namespace = :links
|
||||||
@association.key = :comments
|
@association.key = :comments
|
||||||
assert_equal({
|
assert_equal({
|
||||||
linked: {
|
|
||||||
comments: [
|
|
||||||
{ content: 'C1' },
|
|
||||||
{ content: 'C2' }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
'post' => {
|
'post' => {
|
||||||
title: 'Title 1', body: 'Body 1',
|
title: 'Title 1', body: 'Body 1',
|
||||||
links: {
|
links: {
|
||||||
comments: @post.comments.map(&:object_id)
|
comments: @post.comments.map(&:object_id)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
linked: {
|
||||||
|
'comments' => [
|
||||||
|
{ content: 'C1' },
|
||||||
|
{ content: 'C2' }
|
||||||
|
]
|
||||||
|
},
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,27 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class HasOneAndHasManyTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@post = SpecialPost.new({ title: 'T1', body: 'B1'})
|
||||||
|
@post_serializer = SpecialPostSerializer.new(@post)
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_side_load_has_one_and_has_many_in_same_array
|
||||||
|
assert_equal({
|
||||||
|
"post" => {
|
||||||
|
title: 'T1',
|
||||||
|
body: 'B1',
|
||||||
|
'comment_ids' => @post.comments.map { |c| c.object_id },
|
||||||
|
'special_comment_id' => @post_serializer.special_comment.object_id,
|
||||||
|
},
|
||||||
|
"comments" => [{ content: 'C1' }, { content: 'C2' }, { content: 'special' }]
|
||||||
|
}, @post_serializer.as_json(root: 'post'))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user