active_model_serializers/test/unit/active_model/serializer/scope_test.rb
Santiago Pastorino ed9a5288f9 Revert "Merge branch 'context'"
This reverts commit 1bd8180a94, reversing
changes made to 9bb32331f4.
2014-01-07 15:02:09 -02:00

50 lines
1.1 KiB
Ruby

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