mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #936 from insphire/fix-meta-with-custom-root
Include meta when using json adapter with custom root
This commit is contained in:
commit
7b0a85fdda
@ -75,7 +75,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
serializer.json_key
|
@options.fetch(:root) { serializer.json_key }
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_meta(json)
|
def include_meta(json)
|
||||||
|
|||||||
@ -37,10 +37,11 @@ module ActiveModel
|
|||||||
@result = @core.merge @hash
|
@result = @core.merge @hash
|
||||||
end
|
end
|
||||||
|
|
||||||
if root = options.fetch(:root, serializer.json_key)
|
if root
|
||||||
@result = { root => @result }
|
@result = { root => @result }
|
||||||
|
else
|
||||||
|
@result
|
||||||
end
|
end
|
||||||
@result
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -49,4 +50,4 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,6 +14,11 @@ module ActionController
|
|||||||
render json: @profile, root: "custom_root"
|
render json: @profile, root: "custom_root"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_using_custom_root_and_meta
|
||||||
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
render json: @profile, root: "custom_root", meta: { total: 10 }
|
||||||
|
end
|
||||||
|
|
||||||
def render_using_default_adapter_root
|
def render_using_default_adapter_root
|
||||||
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
|
||||||
# JSON-API adapter sets root by default
|
# JSON-API adapter sets root by default
|
||||||
@ -28,6 +33,14 @@ module ActionController
|
|||||||
render json: @profile, root: "profile", adapter: :json_api
|
render json: @profile, root: "profile", adapter: :json_api
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_array_using_custom_root_and_meta
|
||||||
|
array = [
|
||||||
|
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||||
|
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
||||||
|
]
|
||||||
|
render json: array, root: "custom_root", meta: { total: 10 }
|
||||||
|
end
|
||||||
|
|
||||||
def render_array_using_implicit_serializer
|
def render_array_using_implicit_serializer
|
||||||
array = [
|
array = [
|
||||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||||
@ -161,6 +174,13 @@ module ActionController
|
|||||||
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_using_custom_root_and_meta
|
||||||
|
get :render_using_custom_root_and_meta
|
||||||
|
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"},"meta":{"total":10}}', @response.body
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_using_default_root
|
def test_render_using_default_root
|
||||||
get :render_using_default_adapter_root
|
get :render_using_default_adapter_root
|
||||||
|
|
||||||
@ -197,6 +217,25 @@ module ActionController
|
|||||||
assert_equal expected.to_json, @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_array_using_custom_root_and_meta
|
||||||
|
get :render_array_using_custom_root_and_meta
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
|
||||||
|
expected = { custom_root: [
|
||||||
|
{
|
||||||
|
name: 'Name 1',
|
||||||
|
description: 'Description 1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Name 2',
|
||||||
|
description: 'Description 2',
|
||||||
|
}],
|
||||||
|
meta: { total: 10 }
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal expected.to_json, @response.body
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_array_using_implicit_serializer
|
def test_render_array_using_implicit_serializer
|
||||||
get :render_array_using_implicit_serializer
|
get :render_array_using_implicit_serializer
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
|
|||||||
@ -48,8 +48,8 @@ module ActiveModel
|
|||||||
assert_equal expected, adapter.as_json
|
assert_equal expected, adapter.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_meta_is_not_used_on_arrays
|
def test_meta_is_not_present_on_arrays_without_root
|
||||||
serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta")
|
serializer = ArraySerializer.new([@blog], meta: {total: 10})
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
expected = [{
|
expected = [{
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -67,11 +67,38 @@ module ActiveModel
|
|||||||
assert_equal expected, adapter.as_json
|
assert_equal expected, adapter.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_meta_is_present_on_arrays_with_root
|
||||||
|
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
|
||||||
|
expected = {
|
||||||
|
'blog' => [{
|
||||||
|
id: 1,
|
||||||
|
name: "AMS Hints",
|
||||||
|
writer: {
|
||||||
|
id: 2,
|
||||||
|
name: "Steve"
|
||||||
|
},
|
||||||
|
articles: [{
|
||||||
|
id: 3,
|
||||||
|
title: "AMS",
|
||||||
|
body: nil
|
||||||
|
}]
|
||||||
|
}],
|
||||||
|
'haha_meta' => {
|
||||||
|
total: 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_equal expected, adapter.as_json
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def load_adapter(options)
|
def load_adapter(options)
|
||||||
serializer = AlternateBlogSerializer.new(@blog, options)
|
adapter_opts, serializer_opts =
|
||||||
ActiveModel::Serializer::Adapter::Json.new(serializer)
|
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
||||||
|
|
||||||
|
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
|
||||||
|
ActiveModel::Serializer::Adapter::Json.new(serializer, adapter_opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user