mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
with ember :ids, :include => true, has_one
associations should include plural versions at the root, not singular ones.
This commit is contained in:
parent
1470e3a3f4
commit
4965558d27
@ -105,6 +105,8 @@ module ActiveModel
|
|||||||
{ key => array }
|
{ key => array }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias serialize_many serialize
|
||||||
|
|
||||||
def serialize_ids(collection, scope)
|
def serialize_ids(collection, scope)
|
||||||
# Use pluck or select_columns if available
|
# Use pluck or select_columns if available
|
||||||
# return collection.ids if collection.respond_to?(:ids)
|
# return collection.ids if collection.respond_to?(:ids)
|
||||||
@ -130,6 +132,20 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def serialize_many(object, scope, context, options)
|
||||||
|
if polymorphic?
|
||||||
|
if object
|
||||||
|
find_serializable(object, scope, context, options).as_json(:root => polymorphic_key(object))
|
||||||
|
else
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
key = self.key.to_s.pluralize.to_sym
|
||||||
|
value = object && find_serializable(object, scope, context, options).as_json(:root => false)
|
||||||
|
value = value ? [value] : []
|
||||||
|
{ key => value }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def serialize_ids(object, scope)
|
def serialize_ids(object, scope)
|
||||||
if polymorphic? && object
|
if polymorphic? && object
|
||||||
@ -312,7 +328,7 @@ module ActiveModel
|
|||||||
# object without the root.
|
# object without the root.
|
||||||
def serializable_hash
|
def serializable_hash
|
||||||
if _embed == :ids
|
if _embed == :ids
|
||||||
merge_associations(@hash, associations) if _root_embed
|
merge_associations(@hash, plural_associations) if _root_embed
|
||||||
attributes.merge(association_ids)
|
attributes.merge(association_ids)
|
||||||
elsif _embed == :objects
|
elsif _embed == :objects
|
||||||
attributes.merge(associations)
|
attributes.merge(associations)
|
||||||
@ -346,6 +362,17 @@ module ActiveModel
|
|||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def plural_associations
|
||||||
|
hash = {}
|
||||||
|
|
||||||
|
_associations.each do |association|
|
||||||
|
associated_object = send(association.name)
|
||||||
|
hash.merge! association.serialize_many(associated_object, scope, self, :hash => @hash)
|
||||||
|
end
|
||||||
|
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
# Returns a hash representation of the serializable
|
# Returns a hash representation of the serializable
|
||||||
# object associations ids.
|
# object associations ids.
|
||||||
def association_ids
|
def association_ids
|
||||||
|
|||||||
@ -37,9 +37,10 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
def initialize(attributes)
|
def initialize(attributes)
|
||||||
super(attributes)
|
super(attributes)
|
||||||
self.comments ||= []
|
self.comments ||= []
|
||||||
|
self.author = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :comments
|
attr_accessor :comments, :author
|
||||||
def active_model_serializer; PostSerializer; end
|
def active_model_serializer; PostSerializer; end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -223,6 +224,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
Class.new(ActiveModel::Serializer) do
|
Class.new(ActiveModel::Serializer) do
|
||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
has_many :comments, :serializer => CommentSerializer
|
has_many :comments, :serializer => CommentSerializer
|
||||||
|
has_one :author, :serializer => DefaultUserSerializer
|
||||||
|
|
||||||
if type != :super
|
if type != :super
|
||||||
define_method :serializable_hash do
|
define_method :serializable_hash do
|
||||||
@ -244,6 +246,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
assert_equal({
|
assert_equal({
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
|
:author => nil,
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :title => "Comment1" },
|
{ :title => "Comment1" },
|
||||||
{ :title => "Comment2" }
|
{ :title => "Comment2" }
|
||||||
@ -256,7 +259,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
|
|
||||||
serializer.class_eval do
|
serializer.class_eval do
|
||||||
def as_json(*)
|
def as_json(*)
|
||||||
{ :post => serializable_hash }.merge(associations)
|
{ :post => serializable_hash }.merge(plural_associations)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -270,12 +273,14 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
:comments => [1, 2]
|
:comments => [1, 2],
|
||||||
|
:author => nil
|
||||||
},
|
},
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :title => "Comment1" },
|
{ :title => "Comment1" },
|
||||||
{ :title => "Comment2" }
|
{ :title => "Comment2" }
|
||||||
]
|
],
|
||||||
|
:authors => []
|
||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -344,15 +349,16 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
:comments => [1, 2]
|
:comments => [1, 2],
|
||||||
|
:author => nil
|
||||||
}
|
}
|
||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_embed_ids_include_true
|
def test_embed_ids_include_true
|
||||||
serializer = post_serializer(:super)
|
serializer_class = post_serializer(:super)
|
||||||
|
|
||||||
serializer.class_eval do
|
serializer_class.class_eval do
|
||||||
root :post
|
root :post
|
||||||
embed :ids, :include => true
|
embed :ids, :include => true
|
||||||
end
|
end
|
||||||
@ -361,18 +367,38 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
||||||
post.comments = comments
|
post.comments = comments
|
||||||
|
|
||||||
serializer = serializer.new(post, nil)
|
serializer = serializer_class.new(post, nil)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
:comments => [1, 2]
|
:comments => [1, 2],
|
||||||
|
:author => nil
|
||||||
},
|
},
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :title => "Comment1" },
|
{ :title => "Comment1" },
|
||||||
{ :title => "Comment2" }
|
{ :title => "Comment2" }
|
||||||
]
|
],
|
||||||
|
:authors => []
|
||||||
|
}, serializer.as_json)
|
||||||
|
|
||||||
|
post.author = User.new(:id => 1)
|
||||||
|
|
||||||
|
serializer = serializer_class.new(post, nil)
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body of new post",
|
||||||
|
:comments => [1, 2],
|
||||||
|
:author => 1
|
||||||
|
},
|
||||||
|
:comments => [
|
||||||
|
{ :title => "Comment1" },
|
||||||
|
{ :title => "Comment2" }
|
||||||
|
],
|
||||||
|
:authors => [{ :first_name => "Jose", :last_name => "Valim" }]
|
||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -394,6 +420,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
|
:author => nil,
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :title => "Comment1" },
|
{ :title => "Comment1" },
|
||||||
{ :title => "Comment2" }
|
{ :title => "Comment2" }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user