Add support for embed: :ids option for in associations

This commit is contained in:
Guillermo Iguaran
2014-10-10 16:48:55 -05:00
parent 97023db904
commit 188336522f
3 changed files with 54 additions and 8 deletions

View File

@@ -0,0 +1,31 @@
require 'test_helper'
module ActiveModel
class Serializer
class Adapter
class JsonApi
class HasManyEmbedIdsTest < Minitest::Test
def setup
@author = Author.new(name: 'Steve K.')
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@author.posts = [@first_post, @second_post]
@first_post.author = @author
@second_post.author = @author
@serializer = AuthorSerializer.new(@author)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
end
def test_includes_comment_ids
assert_equal([1, 2], @adapter.serializable_hash[:links][:posts])
end
def test_no_includes_linked_comments
assert_nil @adapter.serializable_hash[:linked]
end
end
end
end
end
end

View File

@@ -35,6 +35,7 @@ end
Post = Class.new(Model)
Comment = Class.new(Model)
Author = Class.new(Model)
PostSerializer = Class.new(ActiveModel::Serializer) do
attributes :title, :body, :id
@@ -47,3 +48,9 @@ CommentSerializer = Class.new(ActiveModel::Serializer) do
belongs_to :post
end
AuthorSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :name
has_many :posts, embed: :ids
end