Module: GroupDescendant

Included in:
Group, Project
Defined in:
app/models/concerns/group_descendant.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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

Instance Method Details

#hierarchy(hierarchy_top = nil, preloaded = nil, opts = {}) ⇒ Object

Returns the hierarchy of a project or group in the from of a hash upto a given top.

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.

project.hierarchy => { parent_group => { child_group => project } }



14
15
16
17
# File 'app/models/concerns/group_descendant.rb', line 14

def hierarchy(hierarchy_top = nil, preloaded = nil, opts = {})
  preloaded ||= ancestors_upto(hierarchy_top)
  expand_hierarchy_for_child(self, self, hierarchy_top, preloaded, opts)
end