mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Implement has_many
This commit is contained in:
19
test/fixtures/poro.rb
vendored
19
test/fixtures/poro.rb
vendored
@@ -25,6 +25,15 @@ end
|
||||
class Profile < Model
|
||||
end
|
||||
|
||||
class Post < Model
|
||||
def comments
|
||||
@comments ||= [Comment.new(content: 'C1'),
|
||||
Comment.new(content: 'C2')]
|
||||
end
|
||||
end
|
||||
|
||||
class Comment < Model
|
||||
end
|
||||
|
||||
###
|
||||
## Serializers
|
||||
@@ -43,3 +52,13 @@ class ProfileSerializer < ActiveModel::Serializer
|
||||
|
||||
attributes :name, :description
|
||||
end
|
||||
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
|
||||
has_many :comments
|
||||
end
|
||||
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
attributes :content
|
||||
end
|
||||
|
||||
63
test/unit/active_model/serializer/has_many_test.rb
Normal file
63
test/unit/active_model/serializer/has_many_test.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
require 'test_helper'
|
||||
require 'active_model/serializer'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class HasManyTest < ActiveModel::TestCase
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_as_json
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||
@post_serializer.class._associations[0].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
|
||||
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
|
||||
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
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user