mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
In a controller, the current controller name should be the default root for collections
This commit is contained in:
parent
e23553be23
commit
87d2d77ac8
@ -37,6 +37,10 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def _render_option_json(json, options)
|
def _render_option_json(json, options)
|
||||||
|
if json.is_a?(Array)
|
||||||
|
options[:root] ||= controller_name
|
||||||
|
end
|
||||||
|
|
||||||
if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
|
if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
|
||||||
json = serializer.new(json, serialization_scope, options)
|
json = serializer.new(json, serialization_scope, options)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,10 +20,14 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def as_json(*)
|
def as_json(*)
|
||||||
hash = { :object => @object.as_json, :scope => @scope.as_json }
|
hash = { :object => serializable_hash, :scope => @scope.as_json }
|
||||||
hash.merge!(:options => true) if @options[:options]
|
hash.merge!(:options => true) if @options[:options]
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def serializable_hash
|
||||||
|
@object.as_json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class JsonSerializable
|
class JsonSerializable
|
||||||
@ -91,6 +95,11 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
render :json => JsonSerializable.new
|
render :json => JsonSerializable.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_json_with_serializer_and_implicit_root
|
||||||
|
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||||
|
render :json => [JsonSerializable.new]
|
||||||
|
end
|
||||||
|
|
||||||
def render_json_with_serializer_and_options
|
def render_json_with_serializer_and_options
|
||||||
@current_user = Struct.new(:as_json).new(:current_user => true)
|
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||||
render :json => JsonSerializable.new, :options => true
|
render :json => JsonSerializable.new, :options => true
|
||||||
@ -172,6 +181,11 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
assert_match '"object":{"serializable_object":true}', @response.body
|
assert_match '"object":{"serializable_object":true}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_json_with_serializer_and_implicit_root
|
||||||
|
get :render_json_with_serializer_and_implicit_root
|
||||||
|
assert_match '"test":[{"serializable_object":true}]', @response.body
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_json_with_serializer_and_options
|
def test_render_json_with_serializer_and_options
|
||||||
get :render_json_with_serializer_and_options
|
get :render_json_with_serializer_and_options
|
||||||
assert_match '"scope":{"current_user":true}', @response.body
|
assert_match '"scope":{"current_user":true}', @response.body
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user