mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Allow a controller to properly override scope_name
This commit is contained in:
parent
fd8cb67b85
commit
bbc3ae44cc
@ -284,7 +284,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
options[:scope] = controller.serialization_scope unless options.has_key?(:scope)
|
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
|
options[:url_options] = controller.url_options
|
||||||
|
|
||||||
serializer.new(resource, options)
|
serializer.new(resource, options)
|
||||||
|
|||||||
@ -65,3 +65,35 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|||||||
assert_equal '{"admin_user":{"admin":true}}', @response.body
|
assert_equal '{"admin_user":{"admin":true}}', @response.body
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user