From 1e7c69c729842dce0cd1e23baedb50907a08032e Mon Sep 17 00:00:00 2001 From: Ray Cohen Date: Sat, 28 Jul 2012 19:05:45 -0400 Subject: [PATCH 1/2] Test for having scope option to render override the controller's serialization scope --- test/serialization_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/serialization_test.rb b/test/serialization_test.rb index 9706f70a..6b1a5712 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -131,6 +131,12 @@ class RenderJsonTest < ActionController::TestCase render :json => JsonSerializable.new, :options => true end + def render_json_with_serializer_and_scope_option + @current_user = Struct.new(:as_json).new(:current_user => true) + @scope = Struct.new(:as_json).new(:current_user => false) + render :json => JsonSerializable.new, :scope => @scope + end + def render_json_with_serializer_api_but_without_serializer @current_user = Struct.new(:as_json).new(:current_user => true) render :json => JsonSerializable.new(true) @@ -259,6 +265,11 @@ class RenderJsonTest < ActionController::TestCase assert_match '"options":true', @response.body end + def test_render_json_with_serializer_and_scope_option + get :render_json_with_serializer_and_scope_option + 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 From 424dacb457098dac427d74622b6f7fc9a6bba0fd Mon Sep 17 00:00:00 2001 From: Ray Cohen Date: Sat, 28 Jul 2012 19:05:45 -0400 Subject: [PATCH 2/2] scope option to render takes precedence over serialization_scope --- lib/action_controller/serialization.rb | 2 +- test/serialization_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index d52c6800..b4cbc73d 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -52,7 +52,7 @@ module ActionController end if serializer - options[:scope] = serialization_scope + options[:scope] = serialization_scope unless options.has_key?(:scope) options[:url_options] = url_options json = serializer.new(json, options.merge(default_serializer_options || {})) end diff --git a/test/serialization_test.rb b/test/serialization_test.rb index 6b1a5712..2bcc1c9a 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -133,8 +133,8 @@ class RenderJsonTest < ActionController::TestCase def render_json_with_serializer_and_scope_option @current_user = Struct.new(:as_json).new(:current_user => true) - @scope = Struct.new(:as_json).new(:current_user => false) - render :json => JsonSerializable.new, :scope => @scope + scope = Struct.new(:as_json).new(:current_user => false) + render :json => JsonSerializable.new, :scope => scope end def render_json_with_serializer_api_but_without_serializer