First try to implement ArraySerializer

This commit is contained in:
Tema Bolshakov
2014-08-28 19:16:24 +04:00
parent 1b718b6d48
commit c1fdfc1cdc
6 changed files with 46 additions and 45 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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