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
438a0773
Commit
438a0773
authored
Sep 06, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a concern to build hierarchies of groups
parent
9f3995a0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
0 deletions
+90
-0
app/models/concerns/group_hierarchy.rb
app/models/concerns/group_hierarchy.rb
+27
-0
app/models/group.rb
app/models/group.rb
+1
-0
app/models/project.rb
app/models/project.rb
+1
-0
spec/models/concerns/group_hierarchy_spec.rb
spec/models/concerns/group_hierarchy_spec.rb
+61
-0
No files found.
app/models/concerns/group_hierarchy.rb
0 → 100644
View file @
438a0773
module
GroupHierarchy
def
hierarchy
(
hierarchy_base
=
nil
)
@hierarchy
||=
tree_for_child
(
self
,
self
,
hierarchy_base
)
end
def
parent
if
self
.
is_a?
(
Project
)
namespace
else
super
end
end
def
tree_for_child
(
child
,
tree
,
hierarchy_base
)
if
child
.
parent
.
nil?
&&
hierarchy_base
.
present?
raise
ArgumentError
.
new
(
'specified base is not part of the tree'
)
end
if
child
.
parent
!=
hierarchy_base
tree_for_child
(
child
.
parent
,
{
child
.
parent
=>
tree
},
hierarchy_base
)
else
tree
end
end
end
app/models/group.rb
View file @
438a0773
...
@@ -6,6 +6,7 @@ class Group < Namespace
...
@@ -6,6 +6,7 @@ class Group < Namespace
include
Avatarable
include
Avatarable
include
Referable
include
Referable
include
SelectForProjectAuthorization
include
SelectForProjectAuthorization
include
GroupHierarchy
has_many
:group_members
,
->
{
where
(
requested_at:
nil
)
},
dependent: :destroy
,
as: :source
# rubocop:disable Cop/ActiveRecordDependent
has_many
:group_members
,
->
{
where
(
requested_at:
nil
)
},
dependent: :destroy
,
as: :source
# rubocop:disable Cop/ActiveRecordDependent
alias_method
:members
,
:group_members
alias_method
:members
,
:group_members
...
...
app/models/project.rb
View file @
438a0773
...
@@ -17,6 +17,7 @@ class Project < ActiveRecord::Base
...
@@ -17,6 +17,7 @@ class Project < ActiveRecord::Base
include
ProjectFeaturesCompatibility
include
ProjectFeaturesCompatibility
include
SelectForProjectAuthorization
include
SelectForProjectAuthorization
include
Routable
include
Routable
include
GroupHierarchy
extend
Gitlab
::
ConfigHelper
extend
Gitlab
::
ConfigHelper
extend
Gitlab
::
CurrentSettings
extend
Gitlab
::
CurrentSettings
...
...
spec/models/concerns/group_hierarchy_spec.rb
0 → 100644
View file @
438a0773
require
'spec_helper'
describe
GroupHierarchy
,
:nested_groups
do
let
(
:parent
)
{
create
(
:group
)
}
let
(
:subgroup
)
{
create
(
:group
,
parent:
parent
)
}
let
(
:subsub_group
)
{
create
(
:group
,
parent:
subgroup
)
}
context
'for a group'
do
describe
'#hierarchy'
do
it
'builds a hierarchy for a group'
do
expected_hierarchy
=
{
parent
=>
{
subgroup
=>
subsub_group
}
}
expect
(
subsub_group
.
hierarchy
).
to
eq
(
expected_hierarchy
)
end
it
'builds a hierarchy upto a specified parent'
do
expected_hierarchy
=
{
subgroup
=>
subsub_group
}
expect
(
subsub_group
.
hierarchy
(
parent
)).
to
eq
(
expected_hierarchy
)
end
it
'raises an error if specifying a base that is not part of the tree'
do
expect
{
subsub_group
.
hierarchy
(
double
)
}.
to
raise_error
(
'specified base is not part of the tree'
)
end
end
describe
'#parent'
do
it
'returns the correct parent'
do
expect
(
subsub_group
.
parent
).
to
eq
(
subgroup
)
end
end
end
context
'for a project'
do
let
(
:project
)
{
create
(
:project
,
namespace:
subsub_group
)
}
describe
'#hierarchy'
do
it
'builds a hierarchy for a group'
do
expected_hierarchy
=
{
parent
=>
{
subgroup
=>
{
subsub_group
=>
project
}
}
}
expect
(
project
.
hierarchy
).
to
eq
(
expected_hierarchy
)
end
it
'builds a hierarchy upto a specified parent'
do
expected_hierarchy
=
{
subsub_group
=>
project
}
expect
(
project
.
hierarchy
(
subgroup
)).
to
eq
(
expected_hierarchy
)
end
it
'raises an error if specifying a base that is not part of the tree'
do
expect
{
project
.
hierarchy
(
double
)
}.
to
raise_error
(
'specified base is not part of the tree'
)
end
end
describe
'#parent'
do
it
'returns the correct parent'
do
expect
(
project
.
parent
).
to
eq
(
subsub_group
)
end
end
end
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