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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
51c4b20c
Commit
51c4b20c
authored
Jan 05, 2017
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Namespace code related to nested groups
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
parent
4b43126d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
47 additions
and
13 deletions
+47
-13
app/helpers/groups_helper.rb
app/helpers/groups_helper.rb
+1
-1
app/models/group.rb
app/models/group.rb
+1
-1
app/models/namespace.rb
app/models/namespace.rb
+20
-2
app/models/route.rb
app/models/route.rb
+2
-2
spec/models/namespace_spec.rb
spec/models/namespace_spec.rb
+22
-6
spec/models/route_spec.rb
spec/models/route_spec.rb
+1
-1
No files found.
app/helpers/groups_helper.rb
View file @
51c4b20c
...
@@ -14,7 +14,7 @@ module GroupsHelper
...
@@ -14,7 +14,7 @@ module GroupsHelper
def
group_title
(
group
,
name
=
nil
,
url
=
nil
)
def
group_title
(
group
,
name
=
nil
,
url
=
nil
)
full_title
=
''
full_title
=
''
group
.
parent
s
.
each
do
|
parent
|
group
.
ancestor
s
.
each
do
|
parent
|
full_title
+=
link_to
(
simple_sanitize
(
parent
.
name
),
group_path
(
parent
))
full_title
+=
link_to
(
simple_sanitize
(
parent
.
name
),
group_path
(
parent
))
full_title
+=
' / '
.
html_safe
full_title
+=
' / '
.
html_safe
end
end
...
...
app/models/group.rb
View file @
51c4b20c
...
@@ -201,7 +201,7 @@ class Group < Namespace
...
@@ -201,7 +201,7 @@ class Group < Namespace
end
end
def
members_with_parents
def
members_with_parents
GroupMember
.
where
(
requested_at:
nil
,
source_id:
parent
s
.
map
(
&
:id
).
push
(
id
))
GroupMember
.
where
(
requested_at:
nil
,
source_id:
ancestor
s
.
map
(
&
:id
).
push
(
id
))
end
end
def
users_with_parents
def
users_with_parents
...
...
app/models/namespace.rb
View file @
51c4b20c
...
@@ -183,8 +183,26 @@ class Namespace < ActiveRecord::Base
...
@@ -183,8 +183,26 @@ class Namespace < ActiveRecord::Base
end
end
end
end
def
parents
# Scopes the model on ancestors of the record
@parents
||=
parent
?
parent
.
parents
+
[
parent
]
:
[]
def
ancestors
if
parent_id
path
=
route
.
path
paths
=
[]
until
path
.
blank?
path
=
path
.
rpartition
(
'/'
).
first
paths
<<
path
end
self
.
class
.
joins
(
:route
).
where
(
'routes.path IN (?)'
,
paths
).
order_id_asc
else
self
.
class
.
none
end
end
# Scopes the model on direct and indirect children of the record
def
descendants
self
.
class
.
joins
(
:route
).
where
(
'routes.path LIKE ?'
,
"
#{
route
.
path
}
/%"
).
order_id_asc
end
end
private
private
...
...
app/models/route.rb
View file @
51c4b20c
...
@@ -8,9 +8,9 @@ class Route < ActiveRecord::Base
...
@@ -8,9 +8,9 @@ class Route < ActiveRecord::Base
presence:
true
,
presence:
true
,
uniqueness:
{
case_sensitive:
false
}
uniqueness:
{
case_sensitive:
false
}
after_update
:rename_
children
,
if: :path_changed?
after_update
:rename_
descendants
,
if: :path_changed?
def
rename_
children
def
rename_
descendants
# We update each row separately because MySQL does not have regexp_replace.
# We update each row separately because MySQL does not have regexp_replace.
# rubocop:disable Rails/FindEach
# rubocop:disable Rails/FindEach
Route
.
where
(
'path LIKE ?'
,
"
#{
path_was
}
/%"
).
each
do
|
route
|
Route
.
where
(
'path LIKE ?'
,
"
#{
path_was
}
/%"
).
each
do
|
route
|
...
...
spec/models/namespace_spec.rb
View file @
51c4b20c
...
@@ -5,6 +5,8 @@ describe Namespace, models: true do
...
@@ -5,6 +5,8 @@ describe Namespace, models: true do
it
{
is_expected
.
to
have_many
:projects
}
it
{
is_expected
.
to
have_many
:projects
}
it
{
is_expected
.
to
have_many
:project_statistics
}
it
{
is_expected
.
to
have_many
:project_statistics
}
it
{
is_expected
.
to
belong_to
:parent
}
it
{
is_expected
.
to
have_many
:children
}
it
{
is_expected
.
to
validate_presence_of
(
:name
)
}
it
{
is_expected
.
to
validate_presence_of
(
:name
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:name
).
scoped_to
(
:parent_id
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:name
).
scoped_to
(
:parent_id
)
}
...
@@ -182,17 +184,31 @@ describe Namespace, models: true do
...
@@ -182,17 +184,31 @@ describe Namespace, models: true do
it
{
expect
(
nested_group
.
full_name
).
to
eq
(
"
#{
group
.
name
}
/
#{
nested_group
.
name
}
"
)
}
it
{
expect
(
nested_group
.
full_name
).
to
eq
(
"
#{
group
.
name
}
/
#{
nested_group
.
name
}
"
)
}
end
end
describe
'#
parent
s'
do
describe
'#
ancestor
s'
do
let
(
:group
)
{
create
(
:group
)
}
let
(
:group
)
{
create
(
:group
)
}
let
(
:nested_group
)
{
create
(
:group
,
parent:
group
)
}
let
(
:nested_group
)
{
create
(
:group
,
parent:
group
)
}
let
(
:deep_nested_group
)
{
create
(
:group
,
parent:
nested_group
)
}
let
(
:deep_nested_group
)
{
create
(
:group
,
parent:
nested_group
)
}
let
(
:very_deep_nested_group
)
{
create
(
:group
,
parent:
deep_nested_group
)
}
let
(
:very_deep_nested_group
)
{
create
(
:group
,
parent:
deep_nested_group
)
}
it
'returns the correct parents'
do
it
'returns the correct ancestors'
do
expect
(
very_deep_nested_group
.
parents
).
to
eq
([
group
,
nested_group
,
deep_nested_group
])
expect
(
very_deep_nested_group
.
ancestors
).
to
eq
([
group
,
nested_group
,
deep_nested_group
])
expect
(
deep_nested_group
.
parents
).
to
eq
([
group
,
nested_group
])
expect
(
deep_nested_group
.
ancestors
).
to
eq
([
group
,
nested_group
])
expect
(
nested_group
.
parents
).
to
eq
([
group
])
expect
(
nested_group
.
ancestors
).
to
eq
([
group
])
expect
(
group
.
parents
).
to
eq
([])
expect
(
group
.
ancestors
).
to
eq
([])
end
end
describe
'#descendants'
do
let!
(
:group
)
{
create
(
:group
)
}
let!
(
:nested_group
)
{
create
(
:group
,
parent:
group
)
}
let!
(
:deep_nested_group
)
{
create
(
:group
,
parent:
nested_group
)
}
let!
(
:very_deep_nested_group
)
{
create
(
:group
,
parent:
deep_nested_group
)
}
it
'returns the correct descendants'
do
expect
(
very_deep_nested_group
.
descendants
.
to_a
).
to
eq
([])
expect
(
deep_nested_group
.
descendants
.
to_a
).
to
eq
([
very_deep_nested_group
])
expect
(
nested_group
.
descendants
.
to_a
).
to
eq
([
deep_nested_group
,
very_deep_nested_group
])
expect
(
group
.
descendants
.
to_a
).
to
eq
([
nested_group
,
deep_nested_group
,
very_deep_nested_group
])
end
end
end
end
end
end
spec/models/route_spec.rb
View file @
51c4b20c
...
@@ -14,7 +14,7 @@ describe Route, models: true do
...
@@ -14,7 +14,7 @@ describe Route, models: true do
it
{
is_expected
.
to
validate_uniqueness_of
(
:path
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:path
)
}
end
end
describe
'#rename_
children
'
do
describe
'#rename_
descendants
'
do
let!
(
:nested_group
)
{
create
(
:group
,
path:
"test"
,
parent:
group
)
}
let!
(
:nested_group
)
{
create
(
:group
,
path:
"test"
,
parent:
group
)
}
let!
(
:deep_nested_group
)
{
create
(
:group
,
path:
"foo"
,
parent:
nested_group
)
}
let!
(
:deep_nested_group
)
{
create
(
:group
,
path:
"foo"
,
parent:
nested_group
)
}
let!
(
:similar_group
)
{
create
(
:group
,
path:
'gitlab-org'
)
}
let!
(
:similar_group
)
{
create
(
:group
,
path:
'gitlab-org'
)
}
...
...
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