diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index d49e9c29..54f321f0 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -40,6 +40,8 @@ module ActionController end def _render_option_json(json, options) + options = default_serializer_options.merge(options) if default_serializer_options + serializer = options.delete(:serializer) || (json.respond_to?(:active_model_serializer) && json.active_model_serializer) @@ -59,7 +61,7 @@ module ActionController if serializer options[:scope] = serialization_scope unless options.has_key?(:scope) options[:url_options] = url_options - json = serializer.new(json, options.merge(default_serializer_options || {})) + json = serializer.new(json, options) end super end diff --git a/test/serialization_test.rb b/test/serialization_test.rb index abe4a77c..538101f7 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -54,6 +54,15 @@ class RenderJsonTest < ActionController::TestCase end end + class AnotherCustomSerializer + def initialize(*) + end + + def as_json(*) + { :rails => 'rocks' } + end + end + class HypermediaSerializable def active_model_serializer HypermediaSerializer @@ -175,9 +184,13 @@ class RenderJsonTest < ActionController::TestCase private def default_serializer_options - if params[:check_defaults] - { :check_defaults => true } - end + defaults = {} + defaults.merge!(:check_defaults => true) if params[:check_defaults] + defaults.merge!(:root => :awesome) if params[:check_default_root] + defaults.merge!(:scope => :current_admin) if params[:check_default_scope] + defaults.merge!(:serializer => AnotherCustomSerializer) if params[:check_default_serializer] + defaults.merge!(:each_serializer => AnotherCustomSerializer) if params[:check_default_each_serializer] + defaults end end @@ -258,11 +271,26 @@ class RenderJsonTest < ActionController::TestCase assert_match '"check_defaults":true', @response.body end + def test_render_json_with_serializer_checking_default_serailizer + get :render_json_with_serializer, :check_default_serializer => true + assert_match '{"rails":"rocks"}', @response.body + end + + def test_render_json_with_serializer_checking_default_scope + get :render_json_with_serializer, :check_default_scope => true + assert_match '"scope":"current_admin"', @response.body + 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_implicit_root_checking_default_each_serailizer + get :render_json_with_serializer_and_implicit_root, :check_default_each_serializer => true + assert_match '"test":[{"rails":"rocks"}]', @response.body + end + def test_render_json_with_serializer_and_options get :render_json_with_serializer_and_options assert_match '"scope":{"current_user":true}', @response.body @@ -275,6 +303,11 @@ class RenderJsonTest < ActionController::TestCase assert_match '"scope":{"current_user":false}', @response.body end + def test_render_json_with_serializer_and_scope_option_checking_default_scope + get :render_json_with_serializer_and_scope_option, :check_default_scope => true + assert_match '"scope":{"current_user":false}', @response.body + end + def test_render_json_with_serializer_api_but_without_serializer get :render_json_with_serializer_api_but_without_serializer assert_match '{"serializable_object":true}', @response.body @@ -285,6 +318,11 @@ class RenderJsonTest < ActionController::TestCase assert_match '{"hello":true}', @response.body end + def test_render_json_with_custom_serializer_checking_default_serailizer + get :render_json_with_custom_serializer, :check_default_serializer => true + assert_match '{"hello":true}', @response.body + end + def test_render_json_array_with_custom_serializer get :render_json_array_with_custom_serializer assert_match '{"test":[{"hello":true}]}', @response.body @@ -296,6 +334,11 @@ class RenderJsonTest < ActionController::TestCase end end + def test_render_json_array_with_custom_serializer_checking_default_each_serailizer + get :render_json_array_with_custom_serializer, :check_default_each_serializer => true + assert_match '{"test":[{"hello":true}]}', @response.body + end + def test_render_json_with_links get :render_json_with_links assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body @@ -306,11 +349,21 @@ class RenderJsonTest < ActionController::TestCase assert_equal '[]', @response.body end + def test_render_json_array_with_no_root_checking_default_root + get :render_json_array_with_no_root, :check_default_root => true + assert_equal '[]', @response.body + end + def test_render_json_empty_array get :render_json_empty_array assert_equal '{"test":[]}', @response.body end + def test_render_json_empty_array_checking_default_root + get :render_json_empty_array, :check_default_root => true + assert_equal '{"awesome":[]}', @response.body + end + def test_render_json_empty_arry_with_array_serializer_root_false ActiveModel::ArraySerializer.root = false get :render_json_empty_array