mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Pass options to associations
This commit is contained in:
committed by
Tema Bolshakov
parent
258b5953e2
commit
71a43a432a
@@ -3,13 +3,14 @@ module ActiveModel
|
||||
class Adapter
|
||||
class Json < Adapter
|
||||
def serializable_hash(options = {})
|
||||
@hash = serializer.attributes
|
||||
@hash = serializer.attributes(options)
|
||||
|
||||
serializer.associations.each do |name, association|
|
||||
serializer.each_association do |name, association, options|
|
||||
if association.respond_to?(:each)
|
||||
@hash[name] = association.map(&:attributes)
|
||||
array_serializer = association
|
||||
@hash[name] = array_serializer.map { |item| item.attributes(options) }
|
||||
else
|
||||
@hash[name] = association.attributes
|
||||
@hash[name] = association.attributes(options)
|
||||
end
|
||||
end
|
||||
@hash
|
||||
|
||||
@@ -5,31 +5,33 @@ module ActiveModel
|
||||
def serializable_hash(options = {})
|
||||
@hash = serializer.attributes
|
||||
|
||||
serializer.associations.each do |name, association|
|
||||
serializer.each_association do |name, association, options|
|
||||
@hash[:links] ||= {}
|
||||
@hash[:linked] ||= {}
|
||||
|
||||
if association.respond_to?(:each)
|
||||
add_links(name, association)
|
||||
add_links(name, association, options)
|
||||
else
|
||||
add_link(name, association)
|
||||
add_link(name, association, options)
|
||||
end
|
||||
end
|
||||
|
||||
@hash
|
||||
end
|
||||
|
||||
def add_links(name, serializers)
|
||||
def add_links(name, serializers, options)
|
||||
@hash[:links][name] ||= []
|
||||
@hash[:linked][name] ||= []
|
||||
@hash[:links][name] += serializers.map(&:id)
|
||||
@hash[:linked][name] += serializers.map(&:attributes)
|
||||
@hash[:linked][name] += serializers.map { |item| item.attributes(options) }
|
||||
end
|
||||
|
||||
def add_link(name, serializer)
|
||||
def add_link(name, serializer, options)
|
||||
plural_name = name.to_s.pluralize.to_sym
|
||||
@hash[:linked][plural_name] ||= []
|
||||
|
||||
@hash[:links][name] = serializer.id
|
||||
@hash[:linked][plural_name].push serializer.attributes
|
||||
@hash[:linked][plural_name].push serializer.attributes(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user