Don't memoize association serializer

This commit is contained in:
Jorge Bejar
2014-01-09 15:30:19 -02:00
committed by Santiago Pastorino
parent 3329a43d02
commit df481b2b35
5 changed files with 69 additions and 16 deletions

View File

@@ -78,6 +78,36 @@ module ActiveModel
ensure
PostSerializer._associations[:comments] = @old_association
end
def test_embed_object_for_has_one_association_with_nil_value
@association = UserSerializer._associations[:profile]
@old_association = @association.dup
@association.embed = :objects
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
class << @user1
def profile
nil
end
end
class << @user2
def profile
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
end
end
@serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts)
assert_equal([
{ name: "User 1", email: "email1@server.com", profile: nil },
{ name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } }
], @serializer.as_json)
ensure
UserSerializer._associations[:profile] = @old_association
end
end
end
end

View File

@@ -128,7 +128,7 @@ module ActiveModel
def test_associations_using_a_given_serializer
@association.embed = :ids
@association.embed_in_root = true
@association.serializer_class = Class.new(ActiveModel::Serializer) do
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
def content
object.read_attribute_for_serialization(:content) + '!'
end
@@ -145,7 +145,7 @@ module ActiveModel
def test_associations_embedding_ids_using_a_given_array_serializer
@association.embed = :ids
@association.embed_in_root = true
@association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
@association.serializer_from_options = Class.new(ActiveModel::ArraySerializer) do
def serializable_object
{ my_content: ['fake'] }
end
@@ -158,7 +158,7 @@ module ActiveModel
end
def test_associations_embedding_objects_using_a_given_array_serializer
@association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
@association.serializer_from_options = Class.new(ActiveModel::ArraySerializer) do
def serializable_object
{ my_content: ['fake'] }
end

View File

@@ -119,7 +119,7 @@ module ActiveModel
def test_associations_embedding_ids_using_a_given_serializer
@association.embed = :ids
@association.embed_in_root = true
@association.serializer_class = Class.new(ActiveModel::Serializer) do
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
def name
'fake'
end
@@ -134,7 +134,7 @@ module ActiveModel
end
def test_associations_embedding_objects_using_a_given_serializer
@association.serializer_class = Class.new(ActiveModel::Serializer) do
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
def name
'fake'
end

View File

@@ -31,7 +31,7 @@ module ActiveModel
end
def test_scope_passed_through
@association.serializer_class = Class.new(ActiveModel::Serializer) do
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
def name
scope
end