mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Cleanup add_included.
This commit is contained in:
parent
f7612f2542
commit
91c5cbe0b9
@ -9,6 +9,11 @@ module ActiveModel
|
|||||||
super
|
super
|
||||||
@hash = { data: [] }
|
@hash = { data: [] }
|
||||||
|
|
||||||
|
@options[:include] ||= []
|
||||||
|
if @options[:include].is_a?(String)
|
||||||
|
@options[:include] = @options[:include].split(',')
|
||||||
|
end
|
||||||
|
|
||||||
if fields = options.delete(:fields)
|
if fields = options.delete(:fields)
|
||||||
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||||
else
|
else
|
||||||
@ -119,47 +124,43 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_included(resource_name, serializers, parent = nil)
|
def add_included(resource_name, serializer, parent = nil)
|
||||||
unless serializers.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
return unless serializers.object
|
serializer.each { |s| add_included(resource_name, s, parent) }
|
||||||
serializers = Array(serializers)
|
return
|
||||||
|
else
|
||||||
|
return unless serializer.object
|
||||||
end
|
end
|
||||||
|
|
||||||
resource_path = [parent, resource_name].compact.join('.')
|
resource_path = [parent, resource_name].compact.join('.')
|
||||||
|
|
||||||
if include_assoc?(resource_path)
|
if include_assoc?(resource_path)
|
||||||
@hash[:included] ||= []
|
@hash[:included] ||= []
|
||||||
|
|
||||||
serializers.each do |serializer|
|
|
||||||
attrs = attributes_for(serializer, @options)
|
attrs = attributes_for(serializer, @options)
|
||||||
relationships = relationships_for(serializer)
|
relationships = relationships_for(serializer)
|
||||||
attrs[:relationships] = relationships if relationships.any?
|
attrs[:relationships] = relationships if relationships.any?
|
||||||
|
|
||||||
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
|
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
serializers.each do |serializer|
|
if include_nested_assoc?(resource_path)
|
||||||
serializer.associations.each do |association|
|
serializer.associations.each do |association|
|
||||||
add_included(association.key, association.serializer, resource_path) if association.serializer
|
add_included(association.key, association.serializer, resource_path) if association.serializer
|
||||||
end if include_nested_assoc?(resource_path)
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_assoc?(assoc)
|
def include_assoc?(assoc)
|
||||||
return false unless @options[:include]
|
|
||||||
check_assoc("#{assoc}$")
|
check_assoc("#{assoc}$")
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_nested_assoc?(assoc)
|
def include_nested_assoc?(assoc)
|
||||||
return false unless @options[:include]
|
|
||||||
check_assoc("#{assoc}.")
|
check_assoc("#{assoc}.")
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_assoc(assoc)
|
def check_assoc(assoc)
|
||||||
include_opt = @options[:include]
|
@options[:include].any? { |s| s.match(/^#{assoc.gsub('.', '\.')}/) }
|
||||||
include_opt = include_opt.split(',') if include_opt.is_a?(String)
|
|
||||||
include_opt.any? do |s|
|
|
||||||
s.match(/^#{assoc.gsub('.', '\.')}/)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_links(options)
|
def add_links(options)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user