Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
cd85c22f
Commit
cd85c22f
authored
Sep 22, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename hierarchies to descendants where applicable
parent
cd8e1b85
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
30 deletions
+30
-30
app/models/concerns/group_descendant.rb
app/models/concerns/group_descendant.rb
+20
-20
app/serializers/group_child_serializer.rb
app/serializers/group_child_serializer.rb
+1
-1
spec/models/concerns/group_descendant_spec.rb
spec/models/concerns/group_descendant_spec.rb
+9
-9
No files found.
app/models/concerns/group_descendant.rb
View file @
cd85c22f
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
(
hierarchie
s
)
return
if
hierarchie
s
.
empty?
def
self
.
build_hierarchy
(
descendants
,
hierarchy_top
=
nil
)
descendants
=
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_
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
...
...
app/serializers/group_child_serializer.rb
View file @
cd85c22f
...
...
@@ -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
...
...
spec/models/concerns/group_descendant_spec.rb
View file @
cd85c22f
...
...
@@ -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
...
...
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