gitlab-org--gitlab-foss/app/serializers/group_child_serializer.rb
Bob Van Landuyt 8f6dac4991 Allow filtering children for a group
When fetching children for a group with a filter, we will search all
nested groups for results and render them in an expanded tree
2017-10-04 22:49:41 +02:00

46 lines
1.2 KiB
Ruby

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