Skip to content

Commit

Permalink
Further cleanup included_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
beauby committed Sep 1, 2015
1 parent 91c5cbe commit dfa49b5
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def serializable_hash(options = nil)
@hash[:data] = attributes_for(serializer, options)
relationships = relationships_for(serializer)
@hash[:data][:relationships] = relationships if relationships.any?
add_included_relationships(serializer)
included = included_for(serializer)
@hash[:included] = included if included.any?
end
@hash
end
Expand Down Expand Up @@ -116,37 +117,36 @@ def relationships_for(serializer)
relationships
end

def add_included_relationships(serializer)
serializer.associations.each do |association|
Array(association.serializer).each do |assoc_serializer|
add_included(association.key, assoc_serializer)
end
end
def included_for(serializer)
serializer.associations.flat_map { |assoc| _included_for(assoc.key, assoc.serializer) }.uniq
end

def add_included(resource_name, serializer, parent = nil)
def _included_for(resource_name, serializer, parent = nil)
if serializer.respond_to?(:each)
serializer.each { |s| add_included(resource_name, s, parent) }
return
serializer.flat_map { |s| _included_for(resource_name, s, parent) }.uniq
else
return unless serializer.object
end

resource_path = [parent, resource_name].compact.join('.')

if include_assoc?(resource_path)
@hash[:included] ||= []

attrs = attributes_for(serializer, @options)
relationships = relationships_for(serializer)
attrs[:relationships] = relationships if relationships.any?

@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
end
if !serializer || !serializer.object
[]
else
result = []
resource_path = [parent, resource_name].compact.join('.')

if include_assoc?(resource_path)
attrs = attributes_for(serializer, @options)
relationships = relationships_for(serializer)
attrs[:relationships] = relationships if relationships.any?
result.push(attrs)
end

if include_nested_assoc?(resource_path)
serializer.associations.each do |association|
add_included(association.key, association.serializer, resource_path) if association.serializer
if include_nested_assoc?(resource_path)
serializer.associations.each do |association|
if association.serializer
result.concat(_included_for(association.key, association.serializer, resource_path))
result.uniq!
end
end
end
result
end
end
end
Expand Down

0 comments on commit dfa49b5

Please sign in to comment.