mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
add fields to adapter initialize function, pull in master, add tests using includes with fields
This commit is contained in:
@@ -34,6 +34,11 @@ module ActiveModel
|
||||
assert_equal([{id: "42", title: 'New Post', body: 'Body'}], @adapter.serializable_hash[:linked][:posts])
|
||||
end
|
||||
|
||||
def test_limiting_linked_post_fields
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
||||
assert_equal([{title: 'New Post'}], @adapter.serializable_hash[:linked][:posts])
|
||||
end
|
||||
|
||||
def test_include_nil_author
|
||||
serializer = PostSerializer.new(@anonymous_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
@@ -25,6 +25,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
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class JsonApi
|
||||
class FieldsetTest < Minitest::Test
|
||||
def setup
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
comment_1 = Comment.new(id: 1, body: 'comment one')
|
||||
comment_2 = Comment.new(id: 2, body: 'comment two')
|
||||
@post.comments = [comment_1, comment_2]
|
||||
|
||||
@serializer = PostSerializer.new(@post)
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
||||
|
||||
end
|
||||
|
||||
def test_fieldset_with_fields_array
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
||||
|
||||
assert_equal(
|
||||
{:title=>"New Post", :links=>{:comments=>["1", "2"]}},
|
||||
@adapter.serializable_hash({fieldset: fieldset})[:posts]
|
||||
)
|
||||
end
|
||||
|
||||
def test_fieldset_with_hash
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new({post: [:body]})
|
||||
|
||||
assert_equal(
|
||||
{:body=>"Body", :links=>{:comments=>["1", "2"]}},
|
||||
@adapter.serializable_hash({fieldset: fieldset})[:posts]
|
||||
)
|
||||
end
|
||||
|
||||
def test_fieldset_with_multiple_hashes
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new({post: [:title], comment: [:body]})
|
||||
|
||||
assert_equal(
|
||||
[{:body=>"comment one" }, {:body=>"comment two"}],
|
||||
@adapter.serializable_hash({fieldset: fieldset})[:linked][:comments]
|
||||
)
|
||||
|
||||
#don't understand how this is getting set.
|
||||
@serializer.class._associations[:comments][:options] = {}
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -40,6 +40,14 @@ module ActiveModel
|
||||
], @adapter.serializable_hash[:linked][:comments])
|
||||
end
|
||||
|
||||
def test_limit_fields_of_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:body]})
|
||||
assert_equal([
|
||||
{body: 'ZOMG A COMMENT'},
|
||||
{body: 'ZOMG ANOTHER COMMENT'}
|
||||
], @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)
|
||||
|
||||
Reference in New Issue
Block a user