Support overriding association methods

You can override associations to define custom scope on them.
This commit is contained in:
Alexandre de Oliveira
2015-01-29 16:48:41 -02:00
parent 652493848a
commit e47231cdc8
15 changed files with 106 additions and 13 deletions

View File

@@ -9,11 +9,14 @@ module ActiveModel
@author = Author.new(id: 1, name: 'Steve K.')
@author.bio = nil
@author.roles = []
@blog = Blog.new(id: 23, name: 'AMS Blog')
@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]
@post.blog = @blog
@anonymous_post.comments = []
@anonymous_post.blog = nil
@comment.post = @post
@comment.author = nil
@post.author = @author
@@ -39,6 +42,7 @@ module ActiveModel
body: 'Body',
links: {
comments: ["1"],
blog: "999",
author: "1"
}
}]
@@ -51,6 +55,7 @@ module ActiveModel
title: 'New Post',
links: {
comments: ["1"],
blog: "999",
author: "1"
}
}]
@@ -61,7 +66,7 @@ module ActiveModel
serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
assert_equal({comments: [], author: nil}, adapter.serializable_hash[:posts][:links])
assert_equal({comments: [], blog: nil, author: nil}, adapter.serializable_hash[:posts][:links])
end
def test_include_type_for_association_when_different_than_name
@@ -101,6 +106,7 @@ module ActiveModel
id: "42",
links: {
comments: ["1"],
blog: "999",
author: "1"
}
}, {
@@ -109,6 +115,7 @@ module ActiveModel
id: "43",
links: {
comments: [],
blog: nil,
author: nil
}
}]

View File

@@ -8,10 +8,13 @@ module ActiveModel
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@author.bio = nil
@blog = Blog.new(id: 23, name: 'AMS Blog')
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@first_post.comments = []
@second_post.comments = []
@first_post.blog = @blog
@second_post.blog = nil
@first_post.author = @author
@second_post.author = @author
@author.posts = [@first_post, @second_post]
@@ -22,16 +25,16 @@ module ActiveModel
def test_include_multiple_posts
assert_equal([
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], author: "1" } },
{ title: "New Post", body: "Body", id: "2", links: { comments: [], author: "1" } }
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: nil, author: "1" } }
], @adapter.serializable_hash[:posts])
end
def test_limiting_fields
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
assert_equal([
{ title: "Hello!!", links: { comments: [], author: "1" } },
{ title: "New Post", links: { comments: [], author: "1" } }
{ title: "Hello!!", links: { comments: [], blog: "999", author: "1" } },
{ title: "New Post", links: { comments: [], blog: nil, author: "1" } }
], @adapter.serializable_hash[:posts])
end

View File

@@ -16,6 +16,9 @@ module ActiveModel
@second_post.author = @author
@first_post.comments = []
@second_post.comments = []
@blog = Blog.new(id: 23, name: 'AMS Blog')
@first_post.blog = @blog
@second_post.blog = nil
@serializer = AuthorSerializer.new(@author)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)

View File

@@ -18,6 +18,8 @@ module ActiveModel
@first_comment.author = nil
@second_comment.post = @post
@second_comment.author = nil
@blog = Blog.new(id: 23, name: 'AMS Blog')
@post.blog = @blog
@serializer = PostPreviewSerializer.new(@post)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(

View File

@@ -24,6 +24,8 @@ module ActiveModel
@blog = Blog.new(id: 1, name: "My Blog!!")
@blog.writer = @author
@blog.articles = [@post]
@post.blog = @blog
@post_without_comments.blog = nil
@serializer = PostSerializer.new(@post)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
@@ -32,7 +34,7 @@ module ActiveModel
def test_includes_comment_ids
assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments])
end
def test_includes_linked_comments
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
expected = [{

View File

@@ -13,6 +13,10 @@ module ActiveModel
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@third_post = Post.new(id: 3, title: 'Yet Another Post', body: 'Body')
@blog = Blog.new({ name: 'AMS Blog' })
@first_post.blog = @blog
@second_post.blog = @blog
@third_post.blog = nil
@first_post.comments = []
@second_post.comments = []
@first_post.author = @author1
@@ -124,6 +128,7 @@ module ActiveModel
id: "1",
links: {
comments: ["1", "2"],
blog: "999",
author: "1"
}
}, {
@@ -132,6 +137,7 @@ module ActiveModel
id: "3",
links: {
comments: [],
blog: nil,
author: "1"
}
}]