mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
First try to implement ArraySerializer
This commit is contained in:
@@ -108,12 +108,7 @@ module ActiveModel
|
||||
self.class._associations.dup.each_with_object({}) do |(name, value), hash|
|
||||
association = object.send(name)
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(association)
|
||||
if serializer_class
|
||||
serializer = serializer_class.new(association)
|
||||
hash[name] = serializer
|
||||
else
|
||||
hash[name] = association
|
||||
end
|
||||
hash[name] = serializer_class.new(association)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,20 +3,21 @@ module ActiveModel
|
||||
class Adapter
|
||||
class JsonApiAdapter < Adapter
|
||||
def serializable_hash(options = {})
|
||||
hash = serializer.attributes.each_with_object({}) do |(attr, value), h|
|
||||
h[attr] = value
|
||||
end
|
||||
hash = serializer.attributes
|
||||
|
||||
serializer.associations(only: [:id]).each_with_object({}) do |(attr, value), h|
|
||||
case value
|
||||
when ActiveModel::Serializer::ArraySerializer
|
||||
# process has_many association
|
||||
when ActiveModel::Serializer
|
||||
# process belongs_to association
|
||||
else
|
||||
# what?
|
||||
end
|
||||
associations = serializer.associations(only: [:id]).each_with_object({}) do |(attr, value), h|
|
||||
h[attr] = case value
|
||||
when ActiveModel::Serializer::ArraySerializer
|
||||
value.attributes(options).map do |item|
|
||||
item.id
|
||||
end.to_a
|
||||
when ActiveModel::Serializer
|
||||
# process belongs_to association
|
||||
else
|
||||
# what?
|
||||
end
|
||||
end
|
||||
hash.merge(associations)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,13 @@ module ActiveModel
|
||||
def initialize(object)
|
||||
@object = object
|
||||
end
|
||||
|
||||
def attributes(options = {})
|
||||
object.map do |item|
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(item)
|
||||
serializer_class.new(item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user