-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicitly set serializer for associations
Document specifying serializer for assocaition
- Loading branch information
Showing
6 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/adapter/json_api/has_many_explicit_serializer_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class JsonApi | ||
# Test 'has_many :assocs, serializer: AssocXSerializer' | ||
class HasManyExplicitSerializerTest < Minitest::Test | ||
def setup | ||
@post = Post.new(title: 'New Post', body: 'Body') | ||
@author = Author.new(name: 'Jane Blogger') | ||
@author.posts = [@post] | ||
@post.author = @author | ||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') | ||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') | ||
@post.comments = [@first_comment, @second_comment] | ||
@first_comment.post = @post | ||
@second_comment.post = @post | ||
|
||
@serializer = PostPreviewSerializer.new(@post) | ||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new( | ||
@serializer, | ||
include: 'comments,author,author' | ||
) | ||
end | ||
|
||
def test_includes_comment_ids | ||
assert_equal(['1', '2'], | ||
@adapter.serializable_hash[:posts][:links][:comments]) | ||
end | ||
|
||
def test_includes_linked_comments | ||
assert_equal([{ id: '1' }, { id: '2' }], | ||
@adapter.serializable_hash[:linked][:comments]) | ||
end | ||
|
||
def test_includes_author_id | ||
assert_equal(@author.id.to_s, | ||
@adapter.serializable_hash[:posts][:links][:author]) | ||
end | ||
|
||
def test_includes_linked_authors | ||
assert_equal([{ id: @author.id.to_s }], | ||
@adapter.serializable_hash[:linked][:authors]) | ||
end | ||
|
||
def test_explicit_serializer_with_null_resource | ||
@post.author = nil | ||
assert_equal(nil, | ||
@adapter.serializable_hash[:posts][:links][:author]) | ||
end | ||
|
||
def test_explicit_serializer_with_null_collection | ||
@post.comments = [] | ||
assert_equal([], | ||
@adapter.serializable_hash[:posts][:links][:comments]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters