Commit cd85c22f authored by Bob Van Landuyt's avatar Bob Van Landuyt

Rename hierarchies to descendants where applicable

parent cd8e1b85
module GroupDescendant
def hierarchy(hierarchy_base = nil)
expand_hierarchy_for_child(self, self, hierarchy_base)
def hierarchy(hierarchy_top = nil)
expand_hierarchy_for_child(self, self, hierarchy_top)
end
def expand_hierarchy_for_child(child, hierarchy, hierarchy_base)
if child.parent.nil? && hierarchy_base.present?
def expand_hierarchy_for_child(child, hierarchy, hierarchy_top)
if child.parent.nil? && hierarchy_top.present?
raise ArgumentError.new('specified base is not part of the tree')
end
if child.parent && child.parent != hierarchy_base
if child.parent && child.parent != hierarchy_top
expand_hierarchy_for_child(child.parent,
{ child.parent => hierarchy },
hierarchy_base)
hierarchy_top)
else
hierarchy
end
end
def merge_hierarchy(other_element, hierarchy_base = nil)
GroupDescendant.merge_hierarchies([self, other_element], hierarchy_base)
def merge_hierarchy(other_element, hierarchy_top = nil)
GroupDescendant.build_hierarchy([self, other_element], hierarchy_top)
end
def self.merge_hierarchies(hierarchies, hierarchy_base = nil)
hierarchies = Array.wrap(hierarchies)
return if hierarchies.empty?
def self.build_hierarchy(descendants, hierarchy_top = nil)
descendants = Array.wrap(descendants)
return if descendants.empty?
unless hierarchies.all? { |hierarchy| hierarchy.is_a?(GroupDescendant) }
unless descendants.all? { |hierarchy| hierarchy.is_a?(GroupDescendant) }
raise ArgumentError.new('element is not a hierarchy')
end
first_hierarchy, *other_hierarchies = hierarchies
merged = first_hierarchy.hierarchy(hierarchy_base)
first_descendant, *other_descendants = descendants
merged = first_descendant.hierarchy(hierarchy_top)
other_hierarchies.each do |child|
next_hierarchy = child.hierarchy(hierarchy_base)
merged = merge_values(merged, next_hierarchy)
other_descendants.each do |descendant|
next_descendant = descendant.hierarchy(hierarchy_top)
merged = merge_hash_tree(merged, next_descendant)
end
merged
end
def self.merge_values(first_child, second_child)
def self.merge_hash_tree(first_child, second_child)
# When the first is an array, we need to go over every element to see if
# we can merge deeper. If no match is found, we add the element to the array
#
......@@ -55,7 +55,7 @@ module GroupDescendant
# Handled cases:
# [Hash, Hash]
elsif first_child.is_a?(Hash) && second_child.is_a?(Hash)
first_child.deep_merge(second_child) { |key, first, second| merge_values(first, second) }
first_child.deep_merge(second_child) { |key, first, second| merge_hash_tree(first, second) }
# If only one of them is a hash, and one of them is a GroupHierachy-object
# we can check if its already in the hash. If so, we don't need to do anything
#
......@@ -77,7 +77,7 @@ module GroupDescendant
def self.merge_hash_into_array(array, new_hash)
if mergeable_index = array.index { |element| element.is_a?(Hash) && (element.keys & new_hash.keys).any? }
array[mergeable_index] = merge_values(array[mergeable_index], new_hash)
array[mergeable_index] = merge_hash_tree(array[mergeable_index], new_hash)
else
array << new_hash
end
......
......@@ -27,7 +27,7 @@ class GroupChildSerializer < BaseSerializer
if children.is_a?(GroupDescendant)
represent_hierarchy(children.hierarchy(hierarchy_root), opts).first
else
hierarchies = GroupDescendant.merge_hierarchies(children, hierarchy_root)
hierarchies = GroupDescendant.build_hierarchy(children, hierarchy_root)
represent_hierarchy(hierarchies, opts)
end
end
......
......@@ -40,7 +40,7 @@ describe GroupDescendant, :nested_groups do
end
end
describe '.merge_hierarchies' do
describe '.build_hierarchy' do
it 'combines hierarchies until the top' do
other_subgroup = create(:group, parent: parent)
other_subsub_group = create(:group, parent: subgroup)
......@@ -49,7 +49,7 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = { parent => [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }] }
expect(described_class.merge_hierarchies(groups)).to eq(expected_hierarchy)
expect(described_class.build_hierarchy(groups)).to eq(expected_hierarchy)
end
it 'combines upto a given parent' do
......@@ -60,7 +60,7 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }]
expect(described_class.merge_hierarchies(groups, parent)).to eq(expected_hierarchy)
expect(described_class.build_hierarchy(groups, parent)).to eq(expected_hierarchy)
end
it 'handles building a tree out of order' do
......@@ -71,7 +71,7 @@ describe GroupDescendant, :nested_groups do
groups = [subsub_group, other_subgroup2, other_subsub_group]
expected_hierarchy = { parent => [{ subgroup => subsub_group }, other_subgroup2, { other_subgroup => other_subsub_group }] }
expect(described_class.merge_hierarchies(groups)).to eq(expected_hierarchy)
expect(described_class.build_hierarchy(groups)).to eq(expected_hierarchy)
end
end
end
......@@ -113,7 +113,7 @@ describe GroupDescendant, :nested_groups do
end
end
describe '.merge_hierarchies' do
describe '.build_hierarchy' do
it 'combines hierarchies until the top' do
other_project = create(:project, namespace: parent)
other_subgroup_project = create(:project, namespace: subgroup)
......@@ -122,7 +122,7 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = { parent => [other_project, { subgroup => [subsub_group, other_subgroup_project] }] }
expect(described_class.merge_hierarchies(elements)).to eq(expected_hierarchy)
expect(described_class.build_hierarchy(elements)).to eq(expected_hierarchy)
end
it 'combines upto a given parent' do
......@@ -133,13 +133,13 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = [other_project, { subgroup => [subsub_group, other_subgroup_project] }]
expect(described_class.merge_hierarchies(elements, parent)).to eq(expected_hierarchy)
expect(described_class.build_hierarchy(elements, parent)).to eq(expected_hierarchy)
end
it 'merges to elements in the same hierarchy' do
expected_hierarchy = { parent => subgroup }
expect(described_class.merge_hierarchies([parent, subgroup])).to eq(expected_hierarchy)
expect(described_class.build_hierarchy([parent, subgroup])).to eq(expected_hierarchy)
end
it 'merges complex hierarchies' do
......@@ -164,7 +164,7 @@ describe GroupDescendant, :nested_groups do
{ other_subgroup => other_subproject }
]
actual_hierarchy = described_class.merge_hierarchies(projects, parent)
actual_hierarchy = described_class.build_hierarchy(projects, parent)
expect(actual_hierarchy).to eq(expected_hierarchy)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment