active_model_serializers/test/action_controller/serialization_test.rb
Alexandre de Oliveira bd27da1b76 Adds support for meta attribute
Currently, 0.10.0.pre doesn't support `meta` option in `render`. This
way, there's no way to support features such as pagination. `0.9` had
this feature in place.

This adds support for it, as well as fixes small things in README.md.

This won't support `meta` in array responses because arrays don't have
keys, obviously. Also, the response should have a `root` key, otherwise
no `meta` will be included.

In some cases, for example using JsonApi, ArraySerializer will result in
a response with a `root`. In that case, `meta` will be included.
2015-01-05 02:56:33 -02:00

112 lines
4.1 KiB
Ruby

require 'test_helper'
module ActionController
module Serialization
class ImplicitSerializerTest < ActionController::TestCase
class MyController < ActionController::Base
def render_using_implicit_serializer
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
end
def render_using_custom_root
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root"
end
def render_using_default_adapter_root
old_adapter = ActiveModel::Serializer.config.adapter
# JSON-API adapter sets root by default
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
ensure
ActiveModel::Serializer.config.adapter = old_adapter
end
def render_using_custom_root_in_adapter_with_a_default
# JSON-API adapter sets root by default
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "profile", adapter: :json_api
end
def render_array_using_implicit_serializer
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
end
def render_array_using_implicit_serializer_and_meta
old_adapter = ActiveModel::Serializer.config.adapter
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]
render json: array, meta: { total: 10 }
ensure
ActiveModel::Serializer.config.adapter = old_adapter
end
end
tests MyController
# We just have Null for now, this will change
def test_render_using_implicit_serializer
get :render_using_implicit_serializer
assert_equal 'application/json', @response.content_type
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
end
def test_render_using_custom_root
get :render_using_custom_root
assert_equal 'application/json', @response.content_type
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
end
def test_render_using_default_root
get :render_using_default_adapter_root
assert_equal 'application/json', @response.content_type
assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body
end
def test_render_using_custom_root_in_adapter_with_a_default
get :render_using_custom_root_in_adapter_with_a_default
assert_equal 'application/json', @response.content_type
assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body
end
def test_render_array_using_implicit_serializer
get :render_array_using_implicit_serializer
assert_equal 'application/json', @response.content_type
expected = [
{
name: 'Name 1',
description: 'Description 1',
},
{
name: 'Name 2',
description: 'Description 2',
}
]
assert_equal expected.to_json, @response.body
end
def test_render_array_using_implicit_serializer_and_meta
get :render_array_using_implicit_serializer_and_meta
assert_equal 'application/json', @response.content_type
assert_equal '{"profiles":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body
end
end
end
end