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

Clean up public/private api of `GroupDescendant`

So only methods that are used elsewhere are public.
parent 6c2de364
......@@ -3,28 +3,6 @@ module GroupDescendant
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 @@ module GroupDescendant
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 @@ module GroupDescendant
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 @@ describe GroupDescendant, :nested_groups do
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 @@ describe GroupDescendant, :nested_groups do
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