Method: GroupDescendant.build_hierarchy

Defined in:
app/models/concerns/group_descendant.rb

.build_hierarchy(descendants, hierarchy_top = nil, opts = {}) ⇒ Object

Merges all hierarchies of the given groups or projects into an array of hashes. All ancestors need to be loaded into the given descendants to avoid queries down the line.

Options:

upto_preloaded_ancestors_only: boolean - When `true`, the hierarchy expansions stops at the
                                         highest level preloaded ancestor. The hierarchy isn't
                                         guaranteed to reach the `hierarchy_top`.

> GroupDescendant.merge_hierarchy([project, child_group, child_group2, parent])

> { parent => [{ child_group => project}, child_group2] }



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/concerns/group_descendant.rb', line 30

def self.build_hierarchy(descendants, hierarchy_top = nil, opts = {})
  descendants = Array.wrap(descendants).uniq
  return [] if descendants.empty?

  unless descendants.all?(GroupDescendant)
    raise ArgumentError, _('element is not a hierarchy')
  end

  all_hierarchies = descendants.map do |descendant|
    descendant.hierarchy(hierarchy_top, descendants, opts)
  end

  Gitlab::Utils::MergeHash.merge(all_hierarchies)
end