Merge pull request #689 from ride/fix-json-api-custom-root

Fix support for custom root in JSON-API adapter
This commit is contained in:
Alexandre de Oliveira 2014-10-29 15:06:13 -02:00
commit 617756623c
2 changed files with 18 additions and 1 deletions

View File

@ -8,7 +8,7 @@ module ActiveModel
end
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 = {}
if serializer.respond_to?(:each)

View File

@ -24,6 +24,16 @@ module ActionController
ActiveModel::Serializer.config.adapter = old_adapter
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
array = [
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
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