mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Support has_one to be compatible with 0.8.x
Update README and CHANGELOG
This commit is contained in:
44
test/adapter/json_api/has_one_test.rb
Normal file
44
test/adapter/json_api/has_one_test.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class JsonApi
|
||||
class HasOneTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@bio = Bio.new(id: 43, content: 'AMS Contributor')
|
||||
@author.bio = @bio
|
||||
@bio.author = @author
|
||||
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
||||
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@post.comments = [@comment]
|
||||
@anonymous_post.comments = []
|
||||
@comment.post = @post
|
||||
@comment.author = nil
|
||||
@post.author = @author
|
||||
@anonymous_post.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@blog.writer = @author
|
||||
@blog.articles = [@post, @anonymous_post]
|
||||
@author.posts = []
|
||||
@author.roles = []
|
||||
|
||||
@serializer = AuthorSerializer.new(@author)
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts')
|
||||
end
|
||||
|
||||
def test_includes_bio_id
|
||||
assert_equal("43", @adapter.serializable_hash[:authors][:links][:bio])
|
||||
end
|
||||
|
||||
def test_includes_linked_bio
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')
|
||||
assert_equal([{id: "43", :content=>"AMS Contributor", :links=>{:author=>"1"}}], @adapter.serializable_hash[:linked][:bios])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
2
test/fixtures/poro.rb
vendored
2
test/fixtures/poro.rb
vendored
@@ -99,7 +99,7 @@ AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
||||
|
||||
has_many :posts, embed: :ids
|
||||
has_many :roles, embed: :ids
|
||||
belongs_to :bio
|
||||
has_one :bio
|
||||
end
|
||||
|
||||
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
||||
|
||||
@@ -23,7 +23,6 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def setup
|
||||
@author = Author.new(name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@@ -43,11 +42,11 @@ module ActiveModel
|
||||
@comment_serializer = CommentSerializer.new(@comment)
|
||||
end
|
||||
|
||||
def test_has_many
|
||||
def test_has_many_and_has_one
|
||||
assert_equal(
|
||||
{ posts: { type: :has_many, association_options: { embed: :ids } },
|
||||
roles: { type: :has_many, association_options: { embed: :ids } },
|
||||
bio: { type: :belongs_to, association_options: {} } },
|
||||
bio: { type: :has_one, association_options: {} } },
|
||||
@author_serializer.class._associations
|
||||
)
|
||||
@author_serializer.each_association do |name, serializer, options|
|
||||
@@ -67,7 +66,11 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_belongs_to
|
||||
assert_equal({post: {type: :belongs_to, association_options: {}}, :author=>{:type=>:belongs_to, :association_options=>{}}}, @comment_serializer.class._associations)
|
||||
assert_equal(
|
||||
{ post: { type: :belongs_to, association_options: {} },
|
||||
author: { type: :belongs_to, association_options: {} } },
|
||||
@comment_serializer.class._associations
|
||||
)
|
||||
@comment_serializer.each_association do |name, serializer, options|
|
||||
if name == :post
|
||||
assert_equal({}, options)
|
||||
|
||||
Reference in New Issue
Block a user