mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Support overriding association methods
You can override associations to define custom scope on them.
This commit is contained in:
@@ -15,6 +15,9 @@ module ActiveModel
|
||||
@comment.post = @post
|
||||
@comment.author = nil
|
||||
@anonymous_post.author = nil
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@post.blog = @blog
|
||||
@anonymous_post.blog = nil
|
||||
|
||||
@serializer = CommentSerializer.new(@comment)
|
||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||
@@ -28,7 +31,7 @@ module ActiveModel
|
||||
serializer = PostSerializer.new(@anonymous_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||
|
||||
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash)
|
||||
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: nil, author: nil}, adapter.serializable_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,16 +13,40 @@ module ActiveModel
|
||||
@second_post.comments = []
|
||||
@first_post.author = @author
|
||||
@second_post.author = @author
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@first_post.blog = @blog
|
||||
@second_post.blog = nil
|
||||
|
||||
@serializer = ArraySerializer.new([@first_post, @second_post])
|
||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||
end
|
||||
|
||||
def test_include_multiple_posts
|
||||
assert_equal([
|
||||
{title: "Hello!!", body: "Hello, world!!", id: 1, comments: [], author: {id: 1, name: "Steve K."}},
|
||||
{title: "New Post", body: "Body", id: 2, comments: [], author: {id: 1, name: "Steve K."}}
|
||||
], @adapter.serializable_hash)
|
||||
expected = [{
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
id: 1,
|
||||
comments: [],
|
||||
author: {
|
||||
id: 1,
|
||||
name: "Steve K."
|
||||
},
|
||||
blog: {
|
||||
id: 999,
|
||||
name: "Custom blog"
|
||||
}
|
||||
}, {
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
id: 2,
|
||||
comments: [],
|
||||
author: {
|
||||
id: 1,
|
||||
name: "Steve K."
|
||||
},
|
||||
blog: nil
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,6 +14,8 @@ module ActiveModel
|
||||
@post.author = @author
|
||||
@first_comment.post = @post
|
||||
@second_comment.post = @post
|
||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||
@post.blog = @blog
|
||||
|
||||
@serializer = PostSerializer.new(@post)
|
||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||
|
||||
Reference in New Issue
Block a user