mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #28 from carllerche/master
Allow specifying the serializer class in the render options (+ other tiny fix)
This commit is contained in:
@@ -44,7 +44,10 @@ module ActionController
|
|||||||
options[:root] ||= controller_name
|
options[:root] ||= controller_name
|
||||||
end
|
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
|
options[:scope] = serialization_scope
|
||||||
|
|
||||||
if default_options = default_serializer_options
|
if default_options = default_serializer_options
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ module ActiveModel
|
|||||||
# It serializes an array checking if each element that implements
|
# It serializes an array checking if each element that implements
|
||||||
# the +active_model_serializer+ method.
|
# the +active_model_serializer+ method.
|
||||||
class ArraySerializer
|
class ArraySerializer
|
||||||
attr_reader :object
|
attr_reader :object, :options
|
||||||
|
|
||||||
def initialize(object, options={})
|
def initialize(object, options={})
|
||||||
@object, @options = object, options
|
@object, @options = object, options
|
||||||
|
|||||||
@@ -45,6 +45,15 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class CustomSerializer
|
||||||
|
def initialize(*)
|
||||||
|
end
|
||||||
|
|
||||||
|
def as_json(*)
|
||||||
|
{ :hello => true }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class TestController < ActionController::Base
|
class TestController < ActionController::Base
|
||||||
protect_from_forgery
|
protect_from_forgery
|
||||||
|
|
||||||
@@ -111,6 +120,10 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
render :json => JsonSerializable.new(true)
|
render :json => JsonSerializable.new(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_json_with_custom_serializer
|
||||||
|
render :json => [], :serializer => CustomSerializer
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def default_serializer_options
|
def default_serializer_options
|
||||||
if params[:check_defaults]
|
if params[:check_defaults]
|
||||||
@@ -211,4 +224,9 @@ class RenderJsonTest < ActionController::TestCase
|
|||||||
get :render_json_with_serializer_api_but_without_serializer
|
get :render_json_with_serializer_api_but_without_serializer
|
||||||
assert_match '{"serializable_object":true}', @response.body
|
assert_match '{"serializable_object":true}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_json_with_custom_serializer
|
||||||
|
get :render_json_with_custom_serializer
|
||||||
|
assert_match '{"hello":true}', @response.body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user