with ember :ids, :include => true, has_one

associations should include plural versions at
the root, not singular ones.
This commit is contained in:
Yehuda Katz
2012-01-10 14:59:06 -07:00
parent 1470e3a3f4
commit 4965558d27
2 changed files with 65 additions and 11 deletions

View File

@@ -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