Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
5dad635d17e1
Commit
cd85c22f
authored
Sep 22, 2017
by
Bob Van Landuyt
Browse files
Rename hierarchies to descendants where applicable
parent
748345e062f6
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/models/concerns/group_descendant.rb
View file @
5dad635d
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
_hierarch
ies
([
self
,
other_element
],
hierarchy_
base
)
def
merge_hierarchy
(
other_element
,
hierarchy_
top
=
nil
)
GroupDescendant
.
build
_hierarch
y
([
self
,
other_element
],
hierarchy_
top
)
end
def
self
.
merge
_hierarch
ies
(
hierarchie
s
,
hierarchy_
base
=
nil
)
hierarchie
s
=
Array
.
wrap
(
hierarchie
s
)
return
if
hierarchie
s
.
empty?
def
self
.
build
_hierarch
y
(
descendant
s
,
hierarchy_
top
=
nil
)
descendant
s
=
Array
.
wrap
(
descendant
s
)
return
if
descendant
s
.
empty?
unless
hierarchie
s
.
all?
{
|
hierarchy
|
hierarchy
.
is_a?
(
GroupDescendant
)
}
unless
descendant
s
.
all?
{
|
hierarchy
|
hierarchy
.
is_a?
(
GroupDescendant
)
}
raise
ArgumentError
.
new
(
'element is not a hierarchy'
)
end
first_
hierarchy
,
*
other_hierarchies
=
hierarchie
s
merged
=
first_
hierarchy
.
hierarchy
(
hierarchy_
base
)
first_
descendant
,
*
other_descendants
=
descendant
s
merged
=
first_
descendant
.
hierarchy
(
hierarchy_
top
)
other_
hierarchie
s
.
each
do
|
child
|
next_
hierarchy
=
child
.
hierarchy
(
hierarchy_
base
)
merged
=
merge_
values
(
merged
,
next_
hierarchy
)
other_
descendant
s
.
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 @@ def self.merge_values(first_child, second_child)
# 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 @@ def self.merge_values(first_child, second_child)
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
...
...
app/serializers/group_child_serializer.rb
View file @
5dad635d
...
...
@@ -27,7 +27,7 @@ def represent_hierarchies(children, opts)
if
children
.
is_a?
(
GroupDescendant
)
represent_hierarchy
(
children
.
hierarchy
(
hierarchy_root
),
opts
).
first
else
hierarchies
=
GroupDescendant
.
merge
_hierarch
ies
(
children
,
hierarchy_root
)
hierarchies
=
GroupDescendant
.
build
_hierarch
y
(
children
,
hierarchy_root
)
represent_hierarchy
(
hierarchies
,
opts
)
end
end
...
...
spec/models/concerns/group_descendant_spec.rb
View file @
5dad635d
...
...
@@ -40,7 +40,7 @@
end
end
describe
'.
merge
_hierarch
ies
'
do
describe
'.
build
_hierarch
y
'
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 @@
expected_hierarchy
=
{
parent
=>
[
other_subgroup
,
{
subgroup
=>
[
subsub_group
,
other_subsub_group
]
}]
}
expect
(
described_class
.
merge
_hierarch
ies
(
groups
)).
to
eq
(
expected_hierarchy
)
expect
(
described_class
.
build
_hierarch
y
(
groups
)).
to
eq
(
expected_hierarchy
)
end
it
'combines upto a given parent'
do
...
...
@@ -60,7 +60,7 @@
expected_hierarchy
=
[
other_subgroup
,
{
subgroup
=>
[
subsub_group
,
other_subsub_group
]
}]
expect
(
described_class
.
merge
_hierarch
ies
(
groups
,
parent
)).
to
eq
(
expected_hierarchy
)
expect
(
described_class
.
build
_hierarch
y
(
groups
,
parent
)).
to
eq
(
expected_hierarchy
)
end
it
'handles building a tree out of order'
do
...
...
@@ -71,7 +71,7 @@
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
_hierarch
ies
(
groups
)).
to
eq
(
expected_hierarchy
)
expect
(
described_class
.
build
_hierarch
y
(
groups
)).
to
eq
(
expected_hierarchy
)
end
end
end
...
...
@@ -113,7 +113,7 @@
end
end
describe
'.
merge
_hierarch
ies
'
do
describe
'.
build
_hierarch
y
'
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 @@
expected_hierarchy
=
{
parent
=>
[
other_project
,
{
subgroup
=>
[
subsub_group
,
other_subgroup_project
]
}]
}
expect
(
described_class
.
merge
_hierarch
ies
(
elements
)).
to
eq
(
expected_hierarchy
)
expect
(
described_class
.
build
_hierarch
y
(
elements
)).
to
eq
(
expected_hierarchy
)
end
it
'combines upto a given parent'
do
...
...
@@ -133,13 +133,13 @@
expected_hierarchy
=
[
other_project
,
{
subgroup
=>
[
subsub_group
,
other_subgroup_project
]
}]
expect
(
described_class
.
merge
_hierarch
ies
(
elements
,
parent
)).
to
eq
(
expected_hierarchy
)
expect
(
described_class
.
build
_hierarch
y
(
elements
,
parent
)).
to
eq
(
expected_hierarchy
)
end
it
'merges to elements in the same hierarchy'
do
expected_hierarchy
=
{
parent
=>
subgroup
}
expect
(
described_class
.
merge
_hierarch
ies
([
parent
,
subgroup
])).
to
eq
(
expected_hierarchy
)
expect
(
described_class
.
build
_hierarch
y
([
parent
,
subgroup
])).
to
eq
(
expected_hierarchy
)
end
it
'merges complex hierarchies'
do
...
...
@@ -164,7 +164,7 @@
{
other_subgroup
=>
other_subproject
}
]
actual_hierarchy
=
described_class
.
merge
_hierarch
ies
(
projects
,
parent
)
actual_hierarchy
=
described_class
.
build
_hierarch
y
(
projects
,
parent
)
expect
(
actual_hierarchy
).
to
eq
(
expected_hierarchy
)
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment