Wrap association into Serializers

This commit is contained in:
Tema Bolshakov
2014-08-28 18:46:19 +04:00
parent fa4ee9d645
commit 466c7d5dd8
3 changed files with 35 additions and 44 deletions

View File

@@ -98,14 +98,23 @@ module ActiveModel
@object = object
end
def attributes
def attributes(options = {})
self.class._attributes.dup.each_with_object({}) do |name, hash|
hash[name] = send(name)
end
end
def associations
self.class._associations.dup
def associations(options = {})
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
end
end
end
end