Revert "Merge branch 'context'"

This reverts commit 1bd8180a94, reversing
changes made to 9bb32331f4.
This commit is contained in:
Santiago Pastorino
2014-01-07 15:02:09 -02:00
parent 1bd8180a94
commit ed9a5288f9
8 changed files with 51 additions and 72 deletions

View File

@@ -2,45 +2,48 @@ require 'test_helper'
module ActiveModel
class Serializer
class ContextTest < ActiveModel::TestCase
def test_context_using_a_hash
serializer = UserSerializer.new(nil, context: { a: 1, b: 2 })
assert_equal(1, serializer.context[:a])
assert_equal(2, serializer.context[:b])
class ScopeTest < ActiveModel::TestCase
def setup
@serializer = ProfileSerializer.new(nil, scope: current_user)
end
def test_context_using_an_object
serializer = UserSerializer.new(nil, context: Struct.new(:a, :b).new(1, 2))
assert_equal(1, serializer.context.a)
assert_equal(2, serializer.context.b)
def test_scope
assert_equal('user', @serializer.scope)
end
private
def current_user
'user'
end
end
class ContextAssociationTest < ActiveModel::TestCase
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, context: { admin: true })
@user_serializer = UserSerializer.new(@user, scope: 'user')
end
def teardown
UserSerializer._associations[:profile] = @old_association
end
def test_context_passed_through
def test_scope_passed_through
@association.serializer_class = Class.new(ActiveModel::Serializer) do
def name
context[:admin] ? 'Admin' : 'User'
scope
end
attributes :name
end
assert_equal({
name: 'Name 1', email: 'mail@server.com', profile: { name: 'Admin' }
name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
}, @user_serializer.serializable_hash)
end
end
end
end