mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
merge upstream/master
This commit is contained in:
@@ -140,7 +140,7 @@ module ActiveModel
|
||||
self.class._associations.dup.each do |name, options|
|
||||
association = object.send(name)
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(association)
|
||||
serializer = serializer_class.new(association)
|
||||
serializer = serializer_class.new(association) if serializer_class
|
||||
|
||||
if block_given?
|
||||
block.call(name, serializer, options[:options])
|
||||
|
||||
@@ -10,13 +10,14 @@ module ActiveModel
|
||||
|
||||
def initialize(serializer, options = {})
|
||||
@serializer = serializer
|
||||
@options = options
|
||||
end
|
||||
|
||||
def serializable_hash(options = {})
|
||||
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
||||
end
|
||||
|
||||
def to_json(options = {})
|
||||
def as_json(options = {})
|
||||
if fields = options.delete(:fields)
|
||||
options[:fieldset] = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||
end
|
||||
|
||||
@@ -13,7 +13,11 @@ module ActiveModel
|
||||
array_serializer = association
|
||||
@result[name] = array_serializer.map { |item| item.attributes(opts) }
|
||||
else
|
||||
@result[name] = association.attributes(options)
|
||||
if association
|
||||
@result[name] = association.attributes(options)
|
||||
else
|
||||
@result[name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,25 +5,23 @@ module ActiveModel
|
||||
def initialize(serializer, options = {})
|
||||
super
|
||||
serializer.root = true
|
||||
@hash = {}
|
||||
@top = @options.fetch(:top) { @hash }
|
||||
end
|
||||
|
||||
def serializable_hash(options = {})
|
||||
@root = (options[:root] || serializer.json_key).to_s.pluralize.to_sym
|
||||
@hash = {}
|
||||
@root = (@options[:root] || serializer.json_key.to_s.pluralize).to_sym
|
||||
@fieldset = options[:fieldset]
|
||||
|
||||
if serializer.respond_to?(:each)
|
||||
opt = @fieldset ? {fieldset: @fieldset} : {}
|
||||
|
||||
@hash[@root] = serializer.map{|s| self.class.new(s).serializable_hash(opt)[@root] }
|
||||
@hash[@root] = serializer.map do |s|
|
||||
self.class.new(s, @options.merge(top: @top)).serializable_hash[@root]
|
||||
end
|
||||
else
|
||||
@hash[@root] = attributes_for_serializer(serializer, {})
|
||||
@hash[@root] = attributes_for_serializer(serializer, @options)
|
||||
|
||||
serializer.each_association do |name, association, opts|
|
||||
@hash[@root][:links] ||= {}
|
||||
unless options[:embed] == :ids
|
||||
@hash[:linked] ||= {}
|
||||
end
|
||||
|
||||
if association.respond_to?(:each)
|
||||
add_links(name, association, opts)
|
||||
@@ -37,23 +35,59 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def add_links(name, serializers, options)
|
||||
@hash[@root][:links][name] ||= []
|
||||
@hash[@root][:links][name] += serializers.map{|serializer| serializer.id.to_s }
|
||||
if serializers.first
|
||||
type = serializers.first.object.class.to_s.underscore.pluralize
|
||||
end
|
||||
if name.to_s == type || !type
|
||||
@hash[@root][:links][name] ||= []
|
||||
@hash[@root][:links][name] += serializers.map{|serializer| serializer.id.to_s }
|
||||
else
|
||||
@hash[@root][:links][name] ||= {}
|
||||
@hash[@root][:links][name][:type] = type
|
||||
@hash[@root][:links][name][:ids] ||= []
|
||||
@hash[@root][:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s }
|
||||
end
|
||||
|
||||
unless options[:embed] == :ids
|
||||
@hash[:linked][name] ||= []
|
||||
@hash[:linked][name] += serializers.map { |item| attributes_for_serializer(item, options) }
|
||||
unless serializers.none? || @options[:embed] == :ids
|
||||
serializers.each do |serializer|
|
||||
add_linked(name, serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_link(name, serializer, options)
|
||||
@hash[@root][:links][name] = serializer.id.to_s
|
||||
if serializer
|
||||
type = serializer.object.class.to_s.underscore
|
||||
if name.to_s == type || !type
|
||||
@hash[@root][:links][name] = serializer.id.to_s
|
||||
else
|
||||
@hash[@root][:links][name] ||= {}
|
||||
@hash[@root][:links][name][:type] = type
|
||||
@hash[@root][:links][name][:id] = serializer.id.to_s
|
||||
end
|
||||
|
||||
unless options[:embed] == :ids
|
||||
plural_name = name.to_s.pluralize.to_sym
|
||||
unless @options[:embed] == :ids
|
||||
add_linked(name, serializer)
|
||||
end
|
||||
else
|
||||
@hash[@root][:links][name] = nil
|
||||
end
|
||||
end
|
||||
|
||||
@hash[:linked][plural_name] ||= []
|
||||
@hash[:linked][plural_name].push attributes_for_serializer(serializer, options)
|
||||
def add_linked(resource, serializer, parent = nil)
|
||||
resource_path = [parent, resource].compact.join('.')
|
||||
if include_assoc? resource_path
|
||||
plural_name = resource.to_s.pluralize.to_sym
|
||||
attrs = attributes_for_serializer(serializer, @options)
|
||||
@top[:linked] ||= {}
|
||||
@top[:linked][plural_name] ||= []
|
||||
@top[:linked][plural_name].push attrs unless @top[:linked][plural_name].include? attrs
|
||||
end
|
||||
|
||||
unless serializer.respond_to?(:each)
|
||||
serializer.each_association do |name, association, opts|
|
||||
add_linked(name, association, resource) if association
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -69,6 +103,10 @@ module ActiveModel
|
||||
attributes[:id] = attributes[:id].to_s if attributes[:id]
|
||||
attributes
|
||||
end
|
||||
|
||||
def include_assoc? assoc
|
||||
@options[:include] && @options[:include].split(',').include?(assoc.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user