mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Extract id_key to a method
This commit is contained in:
parent
a41de0286f
commit
9f5e872621
@ -69,7 +69,7 @@ module ActiveModel
|
|||||||
if key = options[:key]
|
if key = options[:key]
|
||||||
key
|
key
|
||||||
elsif embed_ids?
|
elsif embed_ids?
|
||||||
"#{@name.to_s.singularize}_ids".to_sym
|
id_key
|
||||||
else
|
else
|
||||||
@name
|
@name
|
||||||
end
|
end
|
||||||
@ -83,6 +83,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def id_key
|
||||||
|
"#{@name.to_s.singularize}_ids".to_sym
|
||||||
|
end
|
||||||
|
|
||||||
def serialize
|
def serialize
|
||||||
associated_object.map do |item|
|
associated_object.map do |item|
|
||||||
find_serializable(item).serializable_hash
|
find_serializable(item).serializable_hash
|
||||||
@ -96,9 +100,8 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serialize_ids
|
def serialize_ids
|
||||||
ids_key = "#{@name.to_s.singularize}_ids".to_sym
|
if !options[:embed_key] && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key)
|
||||||
if !options[:embed_key] && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(ids_key)
|
source_serializer.object.read_attribute_for_serialization(id_key)
|
||||||
source_serializer.object.read_attribute_for_serialization(ids_key)
|
|
||||||
else
|
else
|
||||||
associated_object.map do |item|
|
associated_object.map do |item|
|
||||||
item.read_attribute_for_serialization(embed_key)
|
item.read_attribute_for_serialization(embed_key)
|
||||||
@ -134,7 +137,7 @@ module ActiveModel
|
|||||||
if key = options[:key]
|
if key = options[:key]
|
||||||
key
|
key
|
||||||
elsif embed_ids? && !polymorphic?
|
elsif embed_ids? && !polymorphic?
|
||||||
"#{@name}_id".to_sym
|
id_key
|
||||||
else
|
else
|
||||||
@name
|
@name
|
||||||
end
|
end
|
||||||
@ -148,6 +151,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def id_key
|
||||||
|
"#{@name}_id".to_sym
|
||||||
|
end
|
||||||
|
|
||||||
def polymorphic_key
|
def polymorphic_key
|
||||||
associated_object.class.to_s.demodulize.underscore.to_sym
|
associated_object.class.to_s.demodulize.underscore.to_sym
|
||||||
end
|
end
|
||||||
@ -172,8 +179,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serialize_ids
|
def serialize_ids
|
||||||
id_key = "#{@name}_id".to_sym
|
|
||||||
|
|
||||||
if polymorphic?
|
if polymorphic?
|
||||||
if associated_object
|
if associated_object
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user