mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Use controller name as root when serializing an array and not root is defined
This commit is contained in:
parent
86b9d5a226
commit
aa23e811cc
@ -71,6 +71,7 @@ module ActionController
|
|||||||
return unless serializer
|
return unless serializer
|
||||||
|
|
||||||
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
||||||
|
options[:resource_name] = self.controller_name if resource.respond_to?(:to_ary)
|
||||||
|
|
||||||
serializer.new(resource, options)
|
serializer.new(resource, options)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -16,6 +16,7 @@ module ActiveModel
|
|||||||
@object = object
|
@object = object
|
||||||
@root = options[:root]
|
@root = options[:root]
|
||||||
@root = self.class._root if @root.nil?
|
@root = self.class._root if @root.nil?
|
||||||
|
@root = options[:resource_name] if @root.nil?
|
||||||
@meta_key = options[:meta_key] || :meta
|
@meta_key = options[:meta_key] || :meta
|
||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@each_serializer = options[:each_serializer]
|
@each_serializer = options[:each_serializer]
|
||||||
|
|||||||
@ -131,5 +131,21 @@ module ActionController
|
|||||||
assert_equal '{"hello":"world"}', @response.body
|
assert_equal '{"hello":"world"}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class ArraySerializerTest < ActionController::TestCase
|
||||||
|
class MyController < ActionController::Base
|
||||||
|
def render_array
|
||||||
|
render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests MyController
|
||||||
|
|
||||||
|
def test_render_array
|
||||||
|
get :render_array
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"my":[{"name":"Name 1","description":"Description 1"}]}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user