mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Move responsibility of setting a serializer class and building a serializer for an association to the association class
This commit is contained in:
parent
616938dc63
commit
85a9c185ca
@ -159,18 +159,7 @@ end
|
|||||||
|
|
||||||
def serialize(association)
|
def serialize(association)
|
||||||
associated_data = send(association.name)
|
associated_data = send(association.name)
|
||||||
if associated_data.respond_to?(:to_ary) &&
|
association.build_serializer(associated_data).serializable_object
|
||||||
!(association.serializer_class &&
|
|
||||||
association.serializer_class <= ArraySerializer)
|
|
||||||
associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
|
|
||||||
else
|
|
||||||
serializable_obj = association.build_serializer(associated_data).serializable_object
|
|
||||||
if !(association.serializer_class && association.serializer_class <= ArraySerializer)
|
|
||||||
serializable_obj = [serializable_obj]
|
|
||||||
serializable_obj.compact!
|
|
||||||
end
|
|
||||||
serializable_obj
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_ids(association)
|
def serialize_ids(association)
|
||||||
|
|||||||
@ -29,6 +29,13 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializer_class=(serializer)
|
def serializer_class=(serializer)
|
||||||
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||||
|
|
||||||
|
if @serializer_class && !(@serializer_class <= ArraySerializer)
|
||||||
|
@options.merge!(each_serializer: @serializer_class)
|
||||||
|
@serializer_class = ArraySerializer
|
||||||
|
else
|
||||||
|
@serializer_class ||= ArraySerializer
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def embed=(embed)
|
def embed=(embed)
|
||||||
@ -37,8 +44,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_serializer(object)
|
def build_serializer(object)
|
||||||
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
@serializer_class.new(Array(object), @options)
|
||||||
@serializer_class.new(object, @options)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasOne < Association
|
class HasOne < Association
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user