Fix infinite recursion

The method for testing whether to include an association was causing
an infinite loop when two models referenced each other.
This commit is contained in:
Gary Gordon
2014-11-07 09:28:10 -05:00
parent 95d122046d
commit d97b2f5005
9 changed files with 54 additions and 13 deletions

View File

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