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

Rename hierarchies to descendants where applicable

parent cd8e1b85
module GroupDescendant module GroupDescendant
def hierarchy(hierarchy_base = nil) def hierarchy(hierarchy_top = nil)
expand_hierarchy_for_child(self, self, hierarchy_base) expand_hierarchy_for_child(self, self, hierarchy_top)
end end
def expand_hierarchy_for_child(child, hierarchy, hierarchy_base) def expand_hierarchy_for_child(child, hierarchy, hierarchy_top)
if child.parent.nil? && hierarchy_base.present? if child.parent.nil? && hierarchy_top.present?
raise ArgumentError.new('specified base is not part of the tree') raise ArgumentError.new('specified base is not part of the tree')
end end
if child.parent && child.parent != hierarchy_base if child.parent && child.parent != hierarchy_top
expand_hierarchy_for_child(child.parent, expand_hierarchy_for_child(child.parent,
{ child.parent => hierarchy }, { child.parent => hierarchy },
hierarchy_base) hierarchy_top)
else else
hierarchy hierarchy
end end
end end
def merge_hierarchy(other_element, hierarchy_base = nil) def merge_hierarchy(other_element, hierarchy_top = nil)
GroupDescendant.merge_hierarchies([self, other_element], hierarchy_base) GroupDescendant.build_hierarchy([self, other_element], hierarchy_top)
end end
def self.merge_hierarchies(hierarchies, hierarchy_base = nil) def self.build_hierarchy(descendants, hierarchy_top = nil)
hierarchies = Array.wrap(hierarchies) descendants = Array.wrap(descendants)
return if hierarchies.empty? 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') raise ArgumentError.new('element is not a hierarchy')
end end
first_hierarchy, *other_hierarchies = hierarchies first_descendant, *other_descendants = descendants
merged = first_hierarchy.hierarchy(hierarchy_base) merged = first_descendant.hierarchy(hierarchy_top)
other_hierarchies.each do |child| other_descendants.each do |descendant|
next_hierarchy = child.hierarchy(hierarchy_base) next_descendant = descendant.hierarchy(hierarchy_top)
merged = merge_values(merged, next_hierarchy) merged = merge_hash_tree(merged, next_descendant)
end end
merged merged
end 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 # 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 # we can merge deeper. If no match is found, we add the element to the array
# #
...@@ -55,7 +55,7 @@ module GroupDescendant ...@@ -55,7 +55,7 @@ module GroupDescendant
# Handled cases: # Handled cases:
# [Hash, Hash] # [Hash, Hash]
elsif first_child.is_a?(Hash) && second_child.is_a?(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 # 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 # we can check if its already in the hash. If so, we don't need to do anything
# #
...@@ -77,7 +77,7 @@ module GroupDescendant ...@@ -77,7 +77,7 @@ module GroupDescendant
def self.merge_hash_into_array(array, new_hash) 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? } 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 else
array << new_hash array << new_hash
end end
......
...@@ -27,7 +27,7 @@ class GroupChildSerializer < BaseSerializer ...@@ -27,7 +27,7 @@ class GroupChildSerializer < BaseSerializer
if children.is_a?(GroupDescendant) if children.is_a?(GroupDescendant)
represent_hierarchy(children.hierarchy(hierarchy_root), opts).first represent_hierarchy(children.hierarchy(hierarchy_root), opts).first
else else
hierarchies = GroupDescendant.merge_hierarchies(children, hierarchy_root) hierarchies = GroupDescendant.build_hierarchy(children, hierarchy_root)
represent_hierarchy(hierarchies, opts) represent_hierarchy(hierarchies, opts)
end end
end end
......
...@@ -40,7 +40,7 @@ describe GroupDescendant, :nested_groups do ...@@ -40,7 +40,7 @@ describe GroupDescendant, :nested_groups do
end end
end end
describe '.merge_hierarchies' do describe '.build_hierarchy' do
it 'combines hierarchies until the top' do it 'combines hierarchies until the top' do
other_subgroup = create(:group, parent: parent) other_subgroup = create(:group, parent: parent)
other_subsub_group = create(:group, parent: subgroup) other_subsub_group = create(:group, parent: subgroup)
...@@ -49,7 +49,7 @@ describe GroupDescendant, :nested_groups do ...@@ -49,7 +49,7 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = { parent => [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }] } 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 end
it 'combines upto a given parent' do it 'combines upto a given parent' do
...@@ -60,7 +60,7 @@ describe GroupDescendant, :nested_groups do ...@@ -60,7 +60,7 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }] 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 end
it 'handles building a tree out of order' do it 'handles building a tree out of order' do
...@@ -71,7 +71,7 @@ describe GroupDescendant, :nested_groups do ...@@ -71,7 +71,7 @@ describe GroupDescendant, :nested_groups do
groups = [subsub_group, other_subgroup2, other_subsub_group] groups = [subsub_group, other_subgroup2, other_subsub_group]
expected_hierarchy = { parent => [{ subgroup => subsub_group }, other_subgroup2, { other_subgroup => 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 end
end end
...@@ -113,7 +113,7 @@ describe GroupDescendant, :nested_groups do ...@@ -113,7 +113,7 @@ describe GroupDescendant, :nested_groups do
end end
end end
describe '.merge_hierarchies' do describe '.build_hierarchy' do
it 'combines hierarchies until the top' do it 'combines hierarchies until the top' do
other_project = create(:project, namespace: parent) other_project = create(:project, namespace: parent)
other_subgroup_project = create(:project, namespace: subgroup) other_subgroup_project = create(:project, namespace: subgroup)
...@@ -122,7 +122,7 @@ describe GroupDescendant, :nested_groups do ...@@ -122,7 +122,7 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = { parent => [other_project, { subgroup => [subsub_group, other_subgroup_project] }] } 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 end
it 'combines upto a given parent' do it 'combines upto a given parent' do
...@@ -133,13 +133,13 @@ describe GroupDescendant, :nested_groups do ...@@ -133,13 +133,13 @@ describe GroupDescendant, :nested_groups do
expected_hierarchy = [other_project, { subgroup => [subsub_group, other_subgroup_project] }] 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 end
it 'merges to elements in the same hierarchy' do it 'merges to elements in the same hierarchy' do
expected_hierarchy = { parent => subgroup } 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 end
it 'merges complex hierarchies' do it 'merges complex hierarchies' do
...@@ -164,7 +164,7 @@ describe GroupDescendant, :nested_groups do ...@@ -164,7 +164,7 @@ describe GroupDescendant, :nested_groups do
{ other_subgroup => other_subproject } { 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) expect(actual_hierarchy).to eq(expected_hierarchy)
end 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