Bugfix: include nested has_many association

Currently, doing `include: author.bio` would work correctly, but not for
has_many associations such as `include: author.roles`. This fixes it.

The problem was basically that we were not handling arrays for has_many linked,
as happens for ArraySerializers.
This commit is contained in:
Alexandre de Oliveira
2014-11-11 14:35:00 -02:00
parent 33e8d09ad0
commit 971f501e55
6 changed files with 59 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ module ActiveModel
def setup
@author = Author.new(name: 'Steve K.')
@author.bio = nil
@author.roles = []
@post = Post.new({ title: 'New Post', body: 'Body' })
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
@post.comments = [@comment]
@@ -42,6 +43,7 @@ module ActiveModel
def test_has_many
assert_equal(
{ posts: { type: :has_many, options: { embed: :ids } },
roles: { type: :has_many, options: { embed: :ids } },
bio: { type: :belongs_to, options: {} } },
@author_serializer.class._associations
)
@@ -52,6 +54,9 @@ module ActiveModel
elsif name == :bio
assert_equal({}, options)
assert_nil serializer
elsif name == :roles
assert_equal({embed: :ids}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
else
flunk "Unknown association: #{name}"
end