mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
6
test/fixtures/active_record.rb
vendored
6
test/fixtures/active_record.rb
vendored
@@ -80,13 +80,13 @@ ARPost.create(title: 'New post',
|
||||
|
||||
short_tag = post.ar_tags.create(name: 'short')
|
||||
whiny_tag = post.ar_tags.create(name: 'whiny')
|
||||
happy_tag = post.ar_tags.create(name: 'happy')
|
||||
happy_tag = ARTag.create(name: 'happy')
|
||||
|
||||
post.ar_comments.create(body: 'what a dumb post').tap do |comment|
|
||||
comment.ar_tags.concat short_tag, whiny_tag
|
||||
comment.ar_tags.concat happy_tag, whiny_tag
|
||||
end
|
||||
|
||||
post.ar_comments.create(body: 'i liked it').tap do |comment|
|
||||
comment.ar_tags.concat short_tag, happy_tag
|
||||
comment.ar_tags.concat happy_tag, short_tag
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,9 +14,9 @@ module ActiveModel
|
||||
assert_equal({
|
||||
'ar_post' => {
|
||||
title: 'New post', body: 'A body!!!',
|
||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
||||
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'happy' }, { name: 'whiny' }] },
|
||||
{ body: 'i liked it', ar_tags: [{:name=>"happy"}, {:name=>"short"}] }],
|
||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }],
|
||||
ar_section: { 'name' => 'ruby' }
|
||||
}
|
||||
}, post_serializer.as_json)
|
||||
@@ -30,7 +30,7 @@ module ActiveModel
|
||||
'ar_post' => {
|
||||
title: 'New post', body: 'A body!!!',
|
||||
'ar_comment_ids' => [1, 2],
|
||||
'ar_tag_ids' => [1, 2, 3],
|
||||
'ar_tag_ids' => [1, 2],
|
||||
'ar_section_id' => 1
|
||||
}
|
||||
}, post_serializer.as_json)
|
||||
@@ -41,18 +41,20 @@ module ActiveModel
|
||||
post_serializer = ARPostSerializer.new(@post)
|
||||
|
||||
embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
|
||||
assert_equal({
|
||||
'ar_post' => {
|
||||
title: 'New post', body: 'A body!!!',
|
||||
'ar_comment_ids' => [1, 2],
|
||||
'ar_tag_ids' => [1, 2, 3],
|
||||
'ar_section_id' => 1
|
||||
},
|
||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
||||
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
||||
'ar_sections' => [{ 'name' => 'ruby' }]
|
||||
}, post_serializer.as_json)
|
||||
embed(ARCommentSerializer, embed: :ids, embed_in_root: true) do
|
||||
assert_equal({
|
||||
'ar_post' => {
|
||||
title: 'New post', body: 'A body!!!',
|
||||
'ar_comment_ids' => [1, 2],
|
||||
'ar_tag_ids' => [1, 2],
|
||||
'ar_section_id' => 1
|
||||
},
|
||||
ar_comments: [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
|
||||
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
|
||||
ar_tags: [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
|
||||
'ar_sections' => [{ 'name' => 'ruby' }]
|
||||
}, post_serializer.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ module ActiveModel
|
||||
@association.embed_in_root = true
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def content
|
||||
'fake'
|
||||
object.read_attribute_for_serialization(:content) + '!'
|
||||
end
|
||||
|
||||
attributes :content
|
||||
@@ -138,7 +138,7 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||
comments: [{ content: 'fake' }, { content: 'fake' }]
|
||||
comments: [{ content: 'C1!' }, { content: 'C2!' }]
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user