mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Nest scope under context option
This commit is contained in:
committed by
Santiago Pastorino
parent
8bd2542ed2
commit
0d8ef2b165
2
test/fixtures/poro.rb
vendored
2
test/fixtures/poro.rb
vendored
@@ -47,7 +47,7 @@ end
|
||||
class ProfileSerializer < ActiveModel::Serializer
|
||||
def description
|
||||
description = object.read_attribute_for_serialization(:description)
|
||||
scope ? "#{description} - #{scope}" : description
|
||||
context[:scope] ? "#{description} - #{context[:scope]}" : description
|
||||
end
|
||||
|
||||
attributes :name, :description
|
||||
|
||||
@@ -43,7 +43,7 @@ module ActionController
|
||||
class DefaultOptionsForSerializerScopeTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def default_serializer_options
|
||||
{ scope: current_admin }
|
||||
{ context: { scope: current_admin } }
|
||||
end
|
||||
|
||||
def render_using_scope_set_in_default_serializer_options
|
||||
@@ -73,7 +73,7 @@ module ActionController
|
||||
class ExplicitSerializerScopeTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_implicit_serializer_and_explicit_scope
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), scope: current_admin
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), context: { scope: current_admin }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -15,5 +15,24 @@ module ActiveModel
|
||||
assert_equal(2, serializer.context.b)
|
||||
end
|
||||
end
|
||||
|
||||
class ScopeTest < ActiveModel::TestCase
|
||||
def test_array_serializer_pass_context_to_item_serializers
|
||||
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||
serializer = ArraySerializer.new(array, context: { scope: current_user })
|
||||
|
||||
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
||||
{ name: 'Name 2', description: 'Description 2 - user' }]
|
||||
|
||||
assert_equal expected, serializer.serializable_array
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_user
|
||||
'user'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class ArraySerializer
|
||||
class ScopeTest < ActiveModel::TestCase
|
||||
def test_array_serializer_pass_options_to_items_serializers
|
||||
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||
serializer = ArraySerializer.new(array, scope: current_user)
|
||||
|
||||
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
||||
{ name: 'Name 2', description: 'Description 2 - user' }]
|
||||
|
||||
assert_equal expected, serializer.serializable_array
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_user
|
||||
'user'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -15,5 +15,32 @@ module ActiveModel
|
||||
assert_equal(2, serializer.context.b)
|
||||
end
|
||||
end
|
||||
|
||||
class ContextAssociationTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@association = UserSerializer._associations[:profile]
|
||||
@old_association = @association.dup
|
||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||
@user_serializer = UserSerializer.new(@user, context: { admin: true })
|
||||
end
|
||||
|
||||
def teardown
|
||||
UserSerializer._associations[:profile] = @old_association
|
||||
end
|
||||
|
||||
def test_context_passed_through
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def name
|
||||
context[:admin] ? 'Admin' : 'User'
|
||||
end
|
||||
|
||||
attributes :name
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
name: 'Name 1', email: 'mail@server.com', profile: { name: 'Admin' }
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class ScopeTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@serializer = ProfileSerializer.new(nil, scope: current_user)
|
||||
end
|
||||
|
||||
def test_scope
|
||||
assert_equal('user', @serializer.scope)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_user
|
||||
'user'
|
||||
end
|
||||
end
|
||||
|
||||
class NestedScopeTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@association = UserSerializer._associations[:profile]
|
||||
@old_association = @association.dup
|
||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||
@user_serializer = UserSerializer.new(@user, scope: 'user')
|
||||
end
|
||||
|
||||
def teardown
|
||||
UserSerializer._associations[:profile] = @old_association
|
||||
end
|
||||
|
||||
def test_scope_passed_through
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def name
|
||||
scope
|
||||
end
|
||||
|
||||
attributes :name
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user