Add initial support for embed API
This commit is contained in:
parent
a230f040ff
commit
2abb2e617a
@ -21,7 +21,7 @@ def serialize(collection, scope)
|
|||||||
|
|
||||||
def serialize_ids(collection, scope)
|
def serialize_ids(collection, scope)
|
||||||
# use named scopes if they are present
|
# use named scopes if they are present
|
||||||
return collection.ids if collection.respond_to?(:ids)
|
#return collection.ids if collection.respond_to?(:ids)
|
||||||
|
|
||||||
collection.map do |item|
|
collection.map do |item|
|
||||||
item.read_attribute_for_serialization(:id)
|
item.read_attribute_for_serialization(:id)
|
||||||
@ -47,6 +47,9 @@ def serialize_ids(object, scope)
|
|||||||
self._associations = []
|
self._associations = []
|
||||||
|
|
||||||
class_attribute :_root
|
class_attribute :_root
|
||||||
|
class_attribute :_embed
|
||||||
|
self._embed = :objects
|
||||||
|
class_attribute :_root_embed
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def attributes(*attrs)
|
def attributes(*attrs)
|
||||||
@ -73,6 +76,11 @@ def has_one(*attrs)
|
|||||||
associate(Associations::HasOne, attrs)
|
associate(Associations::HasOne, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def embed(type, options={})
|
||||||
|
self._embed = type
|
||||||
|
self._root_embed = true if options[:include]
|
||||||
|
end
|
||||||
|
|
||||||
def root(name)
|
def root(name)
|
||||||
self._root = name
|
self._root = name
|
||||||
end
|
end
|
||||||
@ -97,14 +105,22 @@ def initialize(object, scope)
|
|||||||
|
|
||||||
def as_json(*)
|
def as_json(*)
|
||||||
if _root
|
if _root
|
||||||
{ _root => serializable_hash }
|
hash = { _root => serializable_hash }
|
||||||
|
hash.merge!(associations) if _root_embed
|
||||||
|
hash
|
||||||
else
|
else
|
||||||
serializable_hash
|
serializable_hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash
|
def serializable_hash
|
||||||
|
if _embed == :ids
|
||||||
|
attributes.merge(association_ids)
|
||||||
|
elsif _embed == :objects
|
||||||
attributes.merge(associations)
|
attributes.merge(associations)
|
||||||
|
else
|
||||||
|
attributes
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def associations
|
def associations
|
||||||
|
@ -226,6 +226,7 @@ def post_serializer(type)
|
|||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
has_many :comments, :serializer => CommentSerializer
|
has_many :comments, :serializer => CommentSerializer
|
||||||
|
|
||||||
|
if type != :super
|
||||||
define_method :serializable_hash do
|
define_method :serializable_hash do
|
||||||
post_hash = attributes
|
post_hash = attributes
|
||||||
post_hash.merge!(send(type))
|
post_hash.merge!(send(type))
|
||||||
@ -233,6 +234,7 @@ def post_serializer(type)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_associations
|
def test_associations
|
||||||
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
||||||
@ -325,4 +327,80 @@ def test_false_root
|
|||||||
serializer = Class.new(serializer)
|
serializer = Class.new(serializer)
|
||||||
assert_equal({ :author => nil }, serializer.new(blog, user).as_json)
|
assert_equal({ :author => nil }, serializer.new(blog, user).as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_embed_ids
|
||||||
|
serializer = post_serializer(:super)
|
||||||
|
|
||||||
|
serializer.class_eval do
|
||||||
|
root :post
|
||||||
|
embed :ids
|
||||||
|
end
|
||||||
|
|
||||||
|
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
||||||
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
||||||
|
post.comments = comments
|
||||||
|
|
||||||
|
serializer = serializer.new(post, nil)
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post",
|
||||||
|
:comments => [1, 2]
|
||||||
|
}
|
||||||
|
}, serializer.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_embed_ids_include_true
|
||||||
|
serializer = post_serializer(:super)
|
||||||
|
|
||||||
|
serializer.class_eval do
|
||||||
|
root :post
|
||||||
|
embed :ids, :include => true
|
||||||
|
end
|
||||||
|
|
||||||
|
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
||||||
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
||||||
|
post.comments = comments
|
||||||
|
|
||||||
|
serializer = serializer.new(post, nil)
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post",
|
||||||
|
:comments => [1, 2]
|
||||||
|
},
|
||||||
|
:comments => [
|
||||||
|
{ :title => "Comment1" },
|
||||||
|
{ :title => "Comment2" }
|
||||||
|
]
|
||||||
|
}, serializer.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_embed_objects
|
||||||
|
serializer = post_serializer(:super)
|
||||||
|
|
||||||
|
serializer.class_eval do
|
||||||
|
root :post
|
||||||
|
embed :objects
|
||||||
|
end
|
||||||
|
|
||||||
|
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
||||||
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
||||||
|
post.comments = comments
|
||||||
|
|
||||||
|
serializer = serializer.new(post, nil)
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post",
|
||||||
|
:comments => [
|
||||||
|
{ :title => "Comment1" },
|
||||||
|
{ :title => "Comment2" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}, serializer.as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user