Add polymorphic key to utility method

This commit is contained in:
adman65 2012-01-03 13:26:47 +02:00
parent da0c33f53c
commit 54d3f2edf0

View File

@ -120,7 +120,7 @@ module ActiveModel
def serialize(object, scope, context, options) def serialize(object, scope, context, options)
if polymorphic? if polymorphic?
if object if object
find_serializable(object, scope, context, options).as_json(:root => object.class.to_s.demodulize.underscore.to_sym) find_serializable(object, scope, context, options).as_json(:root => polymorphic_key(object))
else else
{} {}
end end
@ -131,10 +131,9 @@ module ActiveModel
def serialize_ids(object, scope) def serialize_ids(object, scope)
if polymorphic? if polymorphic?
{ {
object.class.to_s.demodulize.underscore.to_sym => object.read_attribute_for_serialization(:id), polymorphic_key(object) => object.read_attribute_for_serialization(:id),
} }
elsif object elsif object
{ key => object.read_attribute_for_serialization(:id) } { key => object.read_attribute_for_serialization(:id) }
@ -142,6 +141,10 @@ module ActiveModel
{ key => nil } { key => nil }
end end
end end
def polymorphic_key(object)
object.class.to_s.demodulize.underscore.to_sym
end
end end
end end