mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Wrap association into Serializers
This commit is contained in:
parent
fa4ee9d645
commit
466c7d5dd8
@ -98,14 +98,23 @@ module ActiveModel
|
|||||||
@object = object
|
@object = object
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes(options = {})
|
||||||
self.class._attributes.dup.each_with_object({}) do |name, hash|
|
self.class._attributes.dup.each_with_object({}) do |name, hash|
|
||||||
hash[name] = send(name)
|
hash[name] = send(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def associations
|
def associations(options = {})
|
||||||
self.class._associations.dup
|
self.class._associations.dup.each_with_object({}) do |(name, value), hash|
|
||||||
|
association = object.send(name)
|
||||||
|
serializer_class = ActiveModel::Serializer.serializer_for(association)
|
||||||
|
if serializer_class
|
||||||
|
serializer = serializer_class.new(association)
|
||||||
|
hash[name] = serializer
|
||||||
|
else
|
||||||
|
hash[name] = association
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class ArraySerializer
|
class ArraySerializer < Serializer
|
||||||
|
def initialize(object)
|
||||||
|
@object = object
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -26,58 +26,38 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Post = Class.new(Model)
|
||||||
|
Comment = Class.new(Model)
|
||||||
|
PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :title, :body
|
||||||
|
|
||||||
|
has_many :comments
|
||||||
|
end
|
||||||
|
|
||||||
|
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id, :body
|
||||||
|
|
||||||
|
belongs_to :post
|
||||||
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@post = Model.new({ title: 'New Post', body: 'Body' })
|
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||||
@comment = Model.new({ id: 1, body: 'ZOMG A COMMENT' })
|
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||||
@post.comments = [@comment]
|
@post.comments = [@comment]
|
||||||
@comment.post = @post
|
@comment.post = @post
|
||||||
|
|
||||||
@post_serializer_class = def_serializer do
|
@post_serializer = PostSerializer.new(@post)
|
||||||
attributes :title, :body
|
@comment_serializer = CommentSerializer.new(@comment)
|
||||||
end
|
|
||||||
|
|
||||||
@comment_serializer_class = def_serializer do
|
|
||||||
attributes :id, :body
|
|
||||||
end
|
|
||||||
|
|
||||||
@post_serializer = @post_serializer_class.new(@post)
|
|
||||||
@comment_serializer = @comment_serializer_class.new(@comment)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_has_many
|
def test_has_many
|
||||||
@post_serializer_class.class_eval do
|
|
||||||
has_many :comments
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations)
|
assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations)
|
||||||
|
assert_kind_of(ActiveModel::Serializer::ArraySerializer, @post_serializer.associations[:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_has_one
|
def test_has_one
|
||||||
@comment_serializer_class.class_eval do
|
|
||||||
belongs_to :post
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal({post: {type: :belongs_to, options: {}}}, @comment_serializer.class._associations)
|
assert_equal({post: {type: :belongs_to, options: {}}}, @comment_serializer.class._associations)
|
||||||
end
|
assert_kind_of(PostSerializer, @comment_serializer.associations[:post])
|
||||||
|
|
||||||
def test_associations
|
|
||||||
@comment_serializer_class.class_eval do
|
|
||||||
belongs_to :post
|
|
||||||
has_many :comments
|
|
||||||
end
|
|
||||||
|
|
||||||
expected_associations = {
|
|
||||||
post: {
|
|
||||||
type: :belongs_to,
|
|
||||||
options: {}
|
|
||||||
},
|
|
||||||
comments: {
|
|
||||||
type: :has_many,
|
|
||||||
options: {}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
assert_equal(expected_associations, @comment_serializer.associations)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user