mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
add fields to adapter initialize function, pull in master, add tests using includes with fields
This commit is contained in:
parent
c9c58e31e5
commit
fc1562c04a
@ -6,7 +6,7 @@ module ActionController
|
||||
|
||||
include ActionController::Renderers
|
||||
|
||||
ADAPTER_OPTION_KEYS = [:include, :root]
|
||||
ADAPTER_OPTION_KEYS = [:include, :fields, :root]
|
||||
|
||||
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
||||
define_method renderer_method do |resource, options|
|
||||
|
||||
@ -18,11 +18,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def as_json(options = {})
|
||||
if fields = options.delete(:fields)
|
||||
options[:fieldset] = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||
end
|
||||
|
||||
serializable_hash(options).to_json
|
||||
serializable_hash(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -7,15 +7,20 @@ module ActiveModel
|
||||
serializer.root = true
|
||||
@hash = {}
|
||||
@top = @options.fetch(:top) { @hash }
|
||||
|
||||
if fields = options.delete(:fields)
|
||||
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||
else
|
||||
@fieldset = options[:fieldset]
|
||||
end
|
||||
end
|
||||
|
||||
def serializable_hash(options = {})
|
||||
@root = (@options[:root] || serializer.json_key.to_s.pluralize).to_sym
|
||||
@fieldset = options[:fieldset]
|
||||
|
||||
if serializer.respond_to?(:each)
|
||||
@hash[@root] = serializer.map do |s|
|
||||
self.class.new(s, @options.merge(top: @top)).serializable_hash[@root]
|
||||
self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[@root]
|
||||
end
|
||||
else
|
||||
@hash[@root] = attributes_for_serializer(serializer, @options)
|
||||
@ -94,7 +99,6 @@ module ActiveModel
|
||||
private
|
||||
|
||||
def attributes_for_serializer(serializer, options)
|
||||
|
||||
if fields = @fieldset && @fieldset.fields_for(serializer)
|
||||
options[:fields] = fields
|
||||
end
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user