From 34d684ee9ac065126e88add79d063c13054b886d Mon Sep 17 00:00:00 2001 From: Kevin Bullaughey Date: Sun, 21 Sep 2014 07:40:55 -0400 Subject: [PATCH] added failing spec to show how both symbol and string keys are causing problems --- test/fixtures/poro.rb | 12 +++++++++ .../serializer/has_one_and_has_many_test.rb | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/unit/active_model/serializer/has_one_and_has_many_test.rb diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 410481fd..73fc0463 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -47,6 +47,12 @@ class Post < Model end end +class SpecialPost < Post + def special_comment + @speical_comment ||= Comment.new(content: 'special') + end +end + class Comment < Model end @@ -110,6 +116,12 @@ class PostSerializer < ActiveModel::Serializer has_many :comments 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 attributes :content end diff --git a/test/unit/active_model/serializer/has_one_and_has_many_test.rb b/test/unit/active_model/serializer/has_one_and_has_many_test.rb new file mode 100644 index 00000000..ba3f2e23 --- /dev/null +++ b/test/unit/active_model/serializer/has_one_and_has_many_test.rb @@ -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