mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +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:
@@ -105,6 +105,8 @@ module ActiveModel
|
||||
{ key => array }
|
||||
end
|
||||
|
||||
alias serialize_many serialize
|
||||
|
||||
def serialize_ids(collection, scope)
|
||||
# Use pluck or select_columns if available
|
||||
# return collection.ids if collection.respond_to?(:ids)
|
||||
@@ -130,6 +132,20 @@ module ActiveModel
|
||||
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)
|
||||
if polymorphic? && object
|
||||
@@ -312,7 +328,7 @@ module ActiveModel
|
||||
# object without the root.
|
||||
def serializable_hash
|
||||
if _embed == :ids
|
||||
merge_associations(@hash, associations) if _root_embed
|
||||
merge_associations(@hash, plural_associations) if _root_embed
|
||||
attributes.merge(association_ids)
|
||||
elsif _embed == :objects
|
||||
attributes.merge(associations)
|
||||
@@ -346,6 +362,17 @@ module ActiveModel
|
||||
hash
|
||||
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
|
||||
# object associations ids.
|
||||
def association_ids
|
||||
|
||||
Reference in New Issue
Block a user