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