mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Fix support for custom root in JSON-API adapter
This commit is contained in:
parent
47deb87e81
commit
5f198667be
@ -8,7 +8,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = {})
|
||||||
@root = (options[:root] || serializer.json_key).to_s.pluralize.to_sym
|
@root = (options[:root] || serializer.json_key.to_s.pluralize).to_sym
|
||||||
@hash = {}
|
@hash = {}
|
||||||
|
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
|
|||||||
@ -24,6 +24,16 @@ module ActionController
|
|||||||
ActiveModel::Serializer.config.adapter = old_adapter
|
ActiveModel::Serializer.config.adapter = old_adapter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_using_custom_root_in_adapter_with_a_default
|
||||||
|
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, root: "profile"
|
||||||
|
ensure
|
||||||
|
ActiveModel::Serializer.config.adapter = old_adapter
|
||||||
|
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' }),
|
||||||
@ -57,6 +67,13 @@ module ActionController
|
|||||||
assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body
|
||||||
end
|
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
|
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user