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
d8e504a8
Commit
d8e504a8
authored
Oct 11, 2017
by
Bob Van Landuyt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use Arel to build preloaded counts
parent
18907efb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
48 deletions
+45
-48
app/models/concerns/loaded_in_group_list.rb
app/models/concerns/loaded_in_group_list.rb
+45
-48
No files found.
app/models/concerns/loaded_in_group_list.rb
View file @
d8e504a8
module
LoadedInGroupList
extend
ActiveSupport
::
Concern
PROJECT_COUNT_SQL
=
<<~
PROJECTCOUNT
.
freeze
SELECT COUNT(*) AS preloaded_project_count
FROM projects
WHERE projects.namespace_id = namespaces.id
PROJECTCOUNT
SUBGROUP_COUNT_SQL
=
<<~
SUBGROUPCOUNT
.
freeze
(SELECT COUNT(*) AS preloaded_subgroup_count
FROM namespaces children
WHERE children.parent_id = namespaces.id)
SUBGROUPCOUNT
MEMBER_COUNT_SQL
=
<<~
MEMBERCOUNT
.
freeze
(SELECT COUNT(*) AS preloaded_member_count
FROM members
WHERE members.source_type = 'Namespace'
AND members.source_id = namespaces.id
AND members.requested_at IS NULL)
MEMBERCOUNT
COUNT_SELECTS
=
[
'namespaces.*'
,
SUBGROUP_COUNT_SQL
,
MEMBER_COUNT_SQL
].
freeze
module
ClassMethods
def
with_counts
(
archived
:)
selects
=
COUNT_SELECTS
.
dup
<<
project_count
(
archived
)
select
(
selects
)
selects_including_counts
=
[
'namespaces.*'
,
"(
#{
project_count_sql
(
archived
).
to_sql
}
)"
,
"(
#{
subgroup_count_sql
.
to_sql
}
)"
,
"(
#{
member_count_sql
.
to_sql
}
)"
]
select
(
selects_including_counts
)
end
def
with_selects_for_list
(
archived:
nil
)
with_route
.
with_counts
(
archived:
archived
)
end
def
project_count
(
archived
)
project_count
=
if
archived
==
'only'
PROJECT_COUNT_SQL
+
'AND projects.archived IS true'
elsif
Gitlab
::
Utils
.
to_boolean
(
archived
)
PROJECT_COUNT_SQL
else
PROJECT_COUNT_SQL
+
'AND projects.archived IS NOT true'
end
"(
#{
project_count
}
)"
private
def
project_count_sql
(
archived
=
nil
)
projects
=
Project
.
arel_table
namespaces
=
Namespace
.
arel_table
base_count
=
projects
.
project
(
Arel
.
star
.
count
.
as
(
'preloaded_project_count'
))
.
where
(
projects
[
:namespace_id
].
eq
(
namespaces
[
:id
]))
if
archived
==
'only'
base_count
.
where
(
projects
[
:archived
].
eq
(
true
))
elsif
Gitlab
::
Utils
.
to_boolean
(
archived
)
base_count
else
base_count
.
where
(
projects
[
:archived
].
not_eq
(
true
))
end
end
def
subgroup_count_sql
namespaces
=
Namespace
.
arel_table
children
=
namespaces
.
alias
(
'children'
)
namespaces
.
project
(
Arel
.
star
.
count
.
as
(
'preloaded_subgroup_count'
))
.
from
(
children
)
.
where
(
children
[
:parent_id
].
eq
(
namespaces
[
:id
]))
end
def
member_count_sql
members
=
Member
.
arel_table
namespaces
=
Namespace
.
arel_table
members
.
project
(
Arel
.
star
.
count
.
as
(
'preloaded_member_count'
))
.
where
(
members
[
:source_type
].
eq
(
Namespace
.
name
))
.
where
(
members
[
:source_id
].
eq
(
namespaces
[
:id
]))
.
where
(
members
[
:requested_at
].
eq
(
nil
))
end
end
...
...
@@ -50,26 +59,14 @@ module LoadedInGroupList
end
def
project_count
@project_count
||=
if
respond_to?
(
:preloaded_project_count
)
preloaded_project_count
else
projects
.
non_archived
.
count
end
@project_count
||=
try
(
:preloaded_project_count
)
||
projects
.
non_archived
.
count
end
def
subgroup_count
@subgroup_count
||=
if
respond_to?
(
:preloaded_subgroup_count
)
preloaded_subgroup_count
else
children
.
count
end
@subgroup_count
||=
try
(
:preloaded_subgroup_count
)
||
children
.
count
end
def
member_count
@member_count
||=
if
respond_to?
(
:preloaded_member_count
)
preloaded_member_count
else
users
.
count
end
@member_count
||=
try
(
:preloaded_member_count
)
||
users
.
count
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