Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
167fd713
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
167fd713
编写于
10月 02, 2017
作者:
B
Bob Van Landuyt
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Always preload all elements in a hierarchy to avoid extra queries.
上级
ef043063
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
31 addition
and
5 deletion
+31
-5
app/models/concerns/group_descendant.rb
app/models/concerns/group_descendant.rb
+12
-3
spec/models/concerns/group_descendant_spec.rb
spec/models/concerns/group_descendant_spec.rb
+19
-2
未找到文件。
app/models/concerns/group_descendant.rb
浏览文件 @
167fd713
module
GroupDescendant
def
hierarchy
(
hierarchy_top
=
nil
,
preloaded
=
[])
def
hierarchy
(
hierarchy_top
=
nil
,
preloaded
=
nil
)
preloaded
||=
ancestors_upto
(
hierarchy_top
)
expand_hierarchy_for_child
(
self
,
self
,
hierarchy_top
,
preloaded
)
end
...
...
@@ -24,12 +25,20 @@ module GroupDescendant
private
def
expand_hierarchy_for_child
(
child
,
hierarchy
,
hierarchy_top
,
preloaded
=
[])
def
ancestors_upto
(
hierarchy_top
=
nil
)
if
hierarchy_top
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
hierarchy_top
)).
base_and_descendants
else
Gitlab
::
GroupHierarchy
.
new
(
Group
.
where
(
id:
self
)).
all_groups
end
end
def
expand_hierarchy_for_child
(
child
,
hierarchy
,
hierarchy_top
,
preloaded
)
parent
=
preloaded
.
detect
{
|
possible_parent
|
possible_parent
.
is_a?
(
Group
)
&&
possible_parent
.
id
==
child
.
parent_id
}
parent
||=
child
.
parent
if
parent
.
nil?
&&
hierarchy_top
.
present?
raise
ArgumentError
.
new
(
'specified
base
is not part of the tree'
)
raise
ArgumentError
.
new
(
'specified
top
is not part of the tree'
)
end
if
parent
&&
parent
!=
hierarchy_top
...
...
spec/models/concerns/group_descendant_spec.rb
浏览文件 @
167fd713
...
...
@@ -7,6 +7,23 @@ describe GroupDescendant, :nested_groups do
context
'for a group'
do
describe
'#hierarchy'
do
it
'only queries once for the ancestors'
do
# make sure the subsub_group does not have anything cached
test_group
=
create
(
:group
,
parent:
subsub_group
).
reload
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
test_group
.
hierarchy
}.
count
expect
(
query_count
).
to
eq
(
1
)
end
it
'only queries once for the ancestors when a top is given'
do
test_group
=
create
(
:group
,
parent:
subsub_group
).
reload
query_count
=
ActiveRecord
::
QueryRecorder
.
new
{
test_group
.
hierarchy
(
subgroup
)
}.
count
expect
(
query_count
).
to
eq
(
1
)
end
it
'builds a hierarchy for a group'
do
expected_hierarchy
=
{
parent
=>
{
subgroup
=>
subsub_group
}
}
...
...
@@ -20,7 +37,7 @@ describe GroupDescendant, :nested_groups do
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'
)
expect
{
subsub_group
.
hierarchy
(
build_stubbed
(
:group
))
}.
to
raise_error
(
'specified top
is not part of the tree'
)
end
end
...
...
@@ -77,7 +94,7 @@ describe GroupDescendant, :nested_groups do
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'
)
expect
{
project
.
hierarchy
(
build_stubbed
(
:group
))
}.
to
raise_error
(
'specified top
is not part of the tree'
)
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录