mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Add polymorphic key to utility method
This commit is contained in:
parent
da0c33f53c
commit
54d3f2edf0
@ -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
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user