mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Add AC::Serialization#serialization_scope as a class level method
This commit is contained in:
parent
0e0341effc
commit
29148cbe1c
@ -34,6 +34,12 @@ module ActionController
|
||||
self._serialization_scope = :current_user
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def serialization_scope(scope)
|
||||
self._serialization_scope = scope
|
||||
end
|
||||
end
|
||||
|
||||
def _render_option_json(resource, options)
|
||||
serializer = build_json_serializer(resource, options)
|
||||
|
||||
|
||||
@ -91,5 +91,29 @@ module ActionController
|
||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_admin"}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
class CallingSerializationScopeTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_calling_serialization_scope
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_user
|
||||
'current_user'
|
||||
end
|
||||
|
||||
serialization_scope :current_user
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_render_calling_serialization_scope
|
||||
get :render_calling_serialization_scope
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_user"}', @response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user