mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Add has_one and has_many :serialize => tests
This commit is contained in:
parent
cd3e5e9de2
commit
70ea6c6bc7
@ -9,16 +9,19 @@ module ActiveModel
|
||||
@embed_key = options[:embed_key] || :id
|
||||
@include = options[:include]
|
||||
|
||||
serializer = @options[:serializer]
|
||||
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||
self.serializer_class = @options[:serializer]
|
||||
end
|
||||
|
||||
attr_reader :name, :embed_ids, :embed_objects, :embed_key
|
||||
attr_reader :name, :embed_ids, :embed_objects, :embed_key, :serializer_class
|
||||
attr_accessor :include
|
||||
alias embed_ids? embed_ids
|
||||
alias embed_objects? embed_objects
|
||||
alias include? include
|
||||
|
||||
def serializer_class=(serializer)
|
||||
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||
end
|
||||
|
||||
def embed=(embed)
|
||||
@embed_ids = embed == :id || embed == :ids
|
||||
@embed_objects = embed == :object || embed == :objects
|
||||
|
||||
@ -7,16 +7,15 @@ module ActiveModel
|
||||
def setup
|
||||
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
@post_serializer.class._associations[0].include = false
|
||||
@post_serializer.class._associations[0].embed = :ids
|
||||
@association = PostSerializer._associations[0]
|
||||
@association.include = false
|
||||
@association.embed = :ids
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
associations = @post_serializer.class._associations
|
||||
|
||||
assert_equal 1, associations.length
|
||||
assert_kind_of Association::HasMany, associations[0]
|
||||
assert_equal 'comments', associations[0].name
|
||||
assert_equal 1, PostSerializer._associations.length
|
||||
assert_kind_of Association::HasMany, @association
|
||||
assert_equal 'comments', @association.name
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||
@ -32,32 +31,50 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||
@post_serializer.class._associations[0].embed = :objects
|
||||
@association.embed = :objects
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_as_json
|
||||
@post_serializer.class._associations[0].embed = :objects
|
||||
@association.embed = :objects
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||
@post_serializer.class._associations[0].include = true
|
||||
@association.include = true
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }, 'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||
@post_serializer.class._associations[0].include = true
|
||||
@association.include = true
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }, 'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_using_a_given_serializer
|
||||
@old_serializer = @association.serializer_class
|
||||
@association.include = true
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def content
|
||||
'fake'
|
||||
end
|
||||
|
||||
attributes :content
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }, 'comments' => [{ 'content' => 'fake' }, { 'content' => 'fake' }]
|
||||
}, @post_serializer.as_json)
|
||||
ensure
|
||||
@association.serializer_class = @old_serializer
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -57,6 +57,24 @@ module ActiveModel
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_using_a_given_serializer
|
||||
@old_serializer = @association.serializer_class
|
||||
@association.include = true
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def name
|
||||
'fake'
|
||||
end
|
||||
|
||||
attributes :name
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'fake' }
|
||||
}, @user_serializer.as_json)
|
||||
ensure
|
||||
@association.serializer_class = @old_serializer
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user