Merge pull request #700 from arenoir/sparse_fieldsets

sparse fieldsets
This commit is contained in:
Alexandre de Oliveira
2015-01-06 09:38:51 -02:00
10 changed files with 131 additions and 5 deletions

View File

@@ -45,6 +45,18 @@ module ActiveModel
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
end
def test_limiting_linked_post_fields
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
expected = [{
title: 'New Post',
links: {
comments: ["1"],
author: "1"
}
}]
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
end
def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)

View File

@@ -26,6 +26,15 @@ module ActiveModel
{ title: "New Post", body: "Body", id: "2", links: { comments: [], author: "1" } }
], @adapter.serializable_hash[:posts])
end
def test_limiting_fields
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
assert_equal([
{ title: "Hello!!", links: { comments: [], author: "1" } },
{ title: "New Post", links: { comments: [], author: "1" } }
], @adapter.serializable_hash[:posts])
end
end
end
end

View File

@@ -32,7 +32,7 @@ module ActiveModel
def test_includes_comment_ids
assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments])
end
def test_includes_linked_comments
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
expected = [{
@@ -53,6 +53,24 @@ module ActiveModel
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
end
def test_limit_fields_of_linked_comments
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
expected = [{
id: "1",
links: {
post: "1",
author: nil
}
}, {
id: "2",
links: {
post: "1",
author: nil
}
}]
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
end
def test_no_include_linked_if_comments_is_empty
serializer = PostSerializer.new(@post_without_comments)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)