Allow passing in the serializer class from the render method

This commit is contained in:
Carl Lerche 2012-01-20 12:17:37 -08:00
parent dd32f91a25
commit 4e8a2af1ec
2 changed files with 22 additions and 1 deletions

View File

@ -44,7 +44,10 @@ module ActionController
options[:root] ||= controller_name
end
if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
serializer = options.delete(:serializer) ||
(json.respond_to?(:active_model_serializer) && json.active_model_serializer)
if serializer
options[:scope] = serialization_scope
if default_options = default_serializer_options

View File

@ -45,6 +45,15 @@ class RenderJsonTest < ActionController::TestCase
end
end
class CustomSerializer
def initialize(*)
end
def as_json(*)
{ :hello => true }
end
end
class TestController < ActionController::Base
protect_from_forgery
@ -111,6 +120,10 @@ class RenderJsonTest < ActionController::TestCase
render :json => JsonSerializable.new(true)
end
def render_json_with_custom_serializer
render :json => [], :serializer => CustomSerializer
end
private
def default_serializer_options
if params[:check_defaults]
@ -211,4 +224,9 @@ class RenderJsonTest < ActionController::TestCase
get :render_json_with_serializer_api_but_without_serializer
assert_match '{"serializable_object":true}', @response.body
end
def test_render_json_with_custom_serializer
get :render_json_with_custom_serializer
assert_match '{"hello":true}', @response.body
end
end