Skip to content

Commit

Permalink
Merge pull request #306 from dgalarza/controller_action_scope_name_fix
Browse files Browse the repository at this point in the history
Allow a controller to properly override scope_name
  • Loading branch information
spastorino committed May 22, 2013
2 parents 50c9f02 + bbc3ae4 commit f533fa2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def build_json(controller, resource, options)
end

options[:scope] = controller.serialization_scope unless options.has_key?(:scope)
options[:scope_name] = controller._serialization_scope
options[:scope_name] = controller._serialization_scope unless options.has_key?(:scope_name)
options[:url_options] = controller.url_options

serializer.new(resource, options)
Expand Down
32 changes: 32 additions & 0 deletions test/serialization_scope_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,35 @@ def test_override_scope_name_with_controller
assert_equal '{"admin_user":{"admin":true}}', @response.body
end
end

class SerializationActionScopeOverrideTest < ActionController::TestCase
TestUser = Struct.new(:name, :admin)

class AdminUserSerializer < ActiveModel::Serializer
attributes :admin?
def admin?
current_admin.admin
end
end

class AdminUserTestController < ActionController::Base
protect_from_forgery
before_filter { request.format = :json }

def current_admin
TestUser.new('Bob', true)
end

def render_new_user
render :json => TestUser.new('pete', false), :serializer => AdminUserSerializer, :scope => current_admin, :scope_name => :current_admin
end
end

tests AdminUserTestController

def test_override_scope_name_with_controller
get :render_new_user
assert_equal '{"admin_user":{"admin":true}}', @response.body
end

end

0 comments on commit f533fa2

Please sign in to comment.