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
9d1348d6
Commit
9d1348d6
authored
Oct 10, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move the `ancestors_upto` to `Project` and `Namespace`
parent
7611e6a0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
54 additions
and
10 deletions
+54
-10
app/models/concerns/group_descendant.rb
app/models/concerns/group_descendant.rb
+11
-10
app/models/namespace.rb
app/models/namespace.rb
+7
-0
app/models/project.rb
app/models/project.rb
+7
-0
spec/models/namespace_spec.rb
spec/models/namespace_spec.rb
+14
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+15
-0
No files found.
app/models/concerns/group_descendant.rb
View file @
9d1348d6
module
GroupDescendant
# Returns the hierarchy of a project or group in the from of a hash upto a
# given top.
#
# > project.hierarchy
# => { parent_group => { child_group => project } }
def
hierarchy
(
hierarchy_top
=
nil
,
preloaded
=
nil
)
preloaded
||=
ancestors_upto
(
hierarchy_top
)
expand_hierarchy_for_child
(
self
,
self
,
hierarchy_top
,
preloaded
)
end
# Merges all hierarchies of the given groups or projects into an array of
# hashes. All ancestors need to be loaded into the given `descendants` to avoid
# queries down the line.
#
# > GroupDescendant.merge_hierarchy([project, child_group, child_group2, parent])
# => { parent => [{ child_group => project}, child_group2] }
def
self
.
build_hierarchy
(
descendants
,
hierarchy_top
=
nil
)
descendants
=
Array
.
wrap
(
descendants
).
uniq
return
[]
if
descendants
.
empty?
...
...
@@ -21,16 +32,6 @@ module GroupDescendant
private
def
ancestors_upto
(
hierarchy_top
=
nil
)
if
self
.
is_a?
(
Group
)
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
id
))
.
ancestors
(
upto:
hierarchy_top
)
else
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
parent_id
))
.
base_and_ancestors
(
upto:
hierarchy_top
)
end
end
def
expand_hierarchy_for_child
(
child
,
hierarchy
,
hierarchy_top
,
preloaded
)
parent
=
hierarchy_top
if
hierarchy_top
&&
child
.
parent_id
==
hierarchy_top
.
id
parent
||=
preloaded
.
detect
{
|
possible_parent
|
possible_parent
.
is_a?
(
Group
)
&&
possible_parent
.
id
==
child
.
parent_id
}
...
...
app/models/namespace.rb
View file @
9d1348d6
...
...
@@ -160,6 +160,13 @@ class Namespace < ActiveRecord::Base
.
base_and_ancestors
end
# returns all ancestors upto but excluding the the given namespace
# when no namespace is given, all ancestors upto the top are returned
def
ancestors_upto
(
top
=
nil
)
Gitlab
::
GroupHierarchy
.
new
(
self
.
class
.
where
(
id:
id
))
.
ancestors
(
upto:
top
)
end
def
self_and_ancestors
return
self
.
class
.
where
(
id:
id
)
unless
parent_id
...
...
app/models/project.rb
View file @
9d1348d6
...
...
@@ -471,6 +471,13 @@ class Project < ActiveRecord::Base
end
end
# returns all ancestor-groups upto but excluding the given namespace
# when no namespace is given, all ancestors upto the top are returned
def
ancestors_upto
(
top
=
nil
)
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
namespace_id
))
.
base_and_ancestors
(
upto:
top
)
end
def
lfs_enabled?
return
namespace
.
lfs_enabled?
if
self
[
:lfs_enabled
].
nil?
...
...
spec/models/namespace_spec.rb
View file @
9d1348d6
...
...
@@ -151,6 +151,20 @@ describe Namespace do
end
end
describe
'#ancestors_upto'
,
:nested_groups
do
let
(
:parent
)
{
create
(
:group
)
}
let
(
:child
)
{
create
(
:group
,
parent:
parent
)
}
let
(
:child2
)
{
create
(
:group
,
parent:
child
)
}
it
'returns all ancestors when no namespace is given'
do
expect
(
child2
.
ancestors_upto
).
to
contain_exactly
(
child
,
parent
)
end
it
'includes ancestors upto but excluding the given ancestor'
do
expect
(
child2
.
ancestors_upto
(
parent
)).
to
contain_exactly
(
child
)
end
end
describe
'#move_dir'
do
before
do
@namespace
=
create
:namespace
...
...
spec/models/project_spec.rb
View file @
9d1348d6
...
...
@@ -1731,6 +1731,21 @@ describe Project do
it
{
expect
(
project
.
gitea_import?
).
to
be
true
}
end
describe
'#ancestors_upto'
,
:nested_groups
do
let
(
:parent
)
{
create
(
:group
)
}
let
(
:child
)
{
create
(
:group
,
parent:
parent
)
}
let
(
:child2
)
{
create
(
:group
,
parent:
child
)
}
let
(
:project
)
{
create
(
:project
,
namespace:
child2
)
}
it
'returns all ancestors when no namespace is given'
do
expect
(
project
.
ancestors_upto
).
to
contain_exactly
(
child2
,
child
,
parent
)
end
it
'includes ancestors upto but excluding the given ancestor'
do
expect
(
project
.
ancestors_upto
(
parent
)).
to
contain_exactly
(
child2
,
child
)
end
end
describe
'#lfs_enabled?'
do
let
(
:project
)
{
create
(
:project
)
}
...
...
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