mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Unsuffixed association keys tests
This commit is contained in:
parent
6f4f30cda6
commit
4ac07de799
@ -242,6 +242,32 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
CONFIG.default_key_type = :name
|
||||||
|
class NameKeyPostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :title, :body
|
||||||
|
|
||||||
|
has_many :comments
|
||||||
|
end
|
||||||
|
CONFIG.default_key_type = nil
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
|
||||||
|
@association = NameKeyPostSerializer._associations[:comments]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
|
||||||
|
}, NameKeyPostSerializer.new(@post).serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_as_json
|
||||||
|
@association = NameKeyPostSerializer._associations[:comments]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
|
||||||
|
}, NameKeyPostSerializer.new(@post).as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -216,6 +216,32 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
CONFIG.default_key_type = :name
|
||||||
|
class NameKeyUserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name, :email
|
||||||
|
|
||||||
|
has_one :profile
|
||||||
|
end
|
||||||
|
CONFIG.default_key_type = nil
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
|
||||||
|
@association = NameKeyUserSerializer._associations[:profile]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id
|
||||||
|
}, NameKeyUserSerializer.new(@user).serializable_hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_name_key_embedding_ids_serialization_using_as_json
|
||||||
|
@association = NameKeyUserSerializer._associations[:profile]
|
||||||
|
@association.embed = :ids
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }
|
||||||
|
}, NameKeyUserSerializer.new(@user).as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user