This instance will be upgraded to Heptapod 0.31.0 (final) on 2022-05-24 at 14:00 UTC+2 (a few minutes of down time)

Commit ef043063 authored by Bob Van Landuyt's avatar Bob Van Landuyt
Browse files

Clean up public/private api of `GroupDescendant`

So only methods that are used elsewhere are public.
parent 94f29489fb7d
......@@ -3,28 +3,6 @@ def hierarchy(hierarchy_top = nil, preloaded = [])
expand_hierarchy_for_child(self, self, hierarchy_top, preloaded)
end
def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded = [])
parent = preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id }
parent ||= child.parent
if parent.nil? && hierarchy_top.present?
raise ArgumentError.new('specified base is not part of the tree')
end
if parent && parent != hierarchy_top
expand_hierarchy_for_child(parent,
{ parent => hierarchy },
hierarchy_top,
preloaded)
else
hierarchy
end
end
def merge_hierarchy(other_element, hierarchy_top = nil)
GroupDescendant.build_hierarchy([self, other_element], hierarchy_top)
end
def self.build_hierarchy(descendants, hierarchy_top = nil)
descendants = Array.wrap(descendants)
return if descendants.empty?
......@@ -44,7 +22,27 @@ def self.build_hierarchy(descendants, hierarchy_top = nil)
merged
end
def self.merge_hash_tree(first_child, second_child)
private
def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded = [])
parent = preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id }
parent ||= child.parent
if parent.nil? && hierarchy_top.present?
raise ArgumentError.new('specified base is not part of the tree')
end
if parent && parent != hierarchy_top
expand_hierarchy_for_child(parent,
{ parent => hierarchy },
hierarchy_top,
preloaded)
else
hierarchy
end
end
private_class_method 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
#
......@@ -79,7 +77,7 @@ def self.merge_hash_tree(first_child, second_child)
end
end
def self.merge_hash_into_array(array, new_hash)
private_class_method 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_hash_tree(array[mergeable_index], new_hash)
else
......
......@@ -24,22 +24,6 @@
end
end
describe '#parent' do
it 'returns the correct parent' do
expect(subsub_group.parent).to eq(subgroup)
end
end
describe '#merge_hierarchy' do
it 'combines hierarchies' do
other_subgroup = create(:group, parent: parent)
expected_hierarchy = { parent => [{ subgroup => subsub_group }, other_subgroup] }
expect(subsub_group.merge_hierarchy(other_subgroup)).to eq(expected_hierarchy)
end
end
describe '.build_hierarchy' do
it 'combines hierarchies until the top' do
other_subgroup = create(:group, parent: parent)
......@@ -97,22 +81,6 @@
end
end
describe '#parent' do
it 'returns the correct parent' do
expect(project.parent).to eq(subsub_group)
end
end
describe '#merge_hierarchy' do
it 'combines hierarchies' do
project = create(:project, namespace: parent)
expected_hierarchy = { parent => [{ subgroup => subsub_group }, project] }
expect(subsub_group.merge_hierarchy(project)).to eq(expected_hierarchy)
end
end
describe '.build_hierarchy' do
it 'combines hierarchies until the top' do
other_project = create(:project, namespace: parent)
......
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