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) ⇒ 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.

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

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/concerns/group_descendant.rb', line 20

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

  unless descendants.all? { |hierarchy| hierarchy.is_a?(GroupDescendant) }
    raise ArgumentError.new(_('element is not a hierarchy'))
  end

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

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

Instance Method Details

#hierarchy(hierarchy_top = nil, preloaded = nil) ⇒ Object

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

> project.hierarchy

> { parent_group => { child_group => project } }


9
10
11
12
# File 'app/models/concerns/group_descendant.rb', line 9

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