gitlab-org--gitlab-foss/app/serializers/group_child_serializer.rb
Bob Van Landuyt ee2744c60d Don't wrap arrays twice: children are already wrapped in an array
We do the wrapping in an array in represent_hierarchy for children.
2017-10-04 22:49:41 +02:00

49 lines
1.3 KiB
Ruby

class GroupChildSerializer < BaseSerializer
include WithPagination
attr_reader :hierarchy_root, :should_expand_hierarchy
entity GroupChildEntity
def expand_hierarchy(hierarchy_root = nil)
tap do
@hierarchy_root = hierarchy_root
@should_expand_hierarchy = true
end
end
def represent(resource, opts = {}, entity_class = nil)
if should_expand_hierarchy
paginator.paginate(resource) if paginated?
represent_hierarchies(resource, opts)
else
super(resource, opts)
end
end
protected
def represent_hierarchies(children, opts)
if children.is_a?(GroupDescendant)
represent_hierarchy(children.hierarchy(hierarchy_root), opts).first
else
hierarchies = GroupDescendant.merge_hierarchies(children, hierarchy_root)
represent_hierarchy(hierarchies, opts)
end
end
def represent_hierarchy(hierarchy, opts)
serializer = self.class.new(parameters)
if hierarchy.is_a?(Hash)
hierarchy.map do |parent, children|
serializer.represent(parent, opts)
.merge(children: Array.wrap(serializer.represent_hierarchy(children, opts)))
end
elsif hierarchy.is_a?(Array)
hierarchy.map { |child| serializer.represent_hierarchy(child, opts) }
else
serializer.represent(hierarchy, opts)
end
end
end