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
2067f677
Commit
2067f677
authored
Aug 19, 2019
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix N+1s queries while loading users
parent
50ff074e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
2 deletions
+17
-2
app/controllers/projects/starrers_controller.rb
app/controllers/projects/starrers_controller.rb
+2
-2
app/models/users_star_project.rb
app/models/users_star_project.rb
+1
-0
spec/controllers/projects/starrers_controller_spec.rb
spec/controllers/projects/starrers_controller_spec.rb
+14
-0
No files found.
app/controllers/projects/starrers_controller.rb
View file @
2067f677
...
...
@@ -5,11 +5,11 @@ class Projects::StarrersController < Projects::ApplicationController
def
index
@starrers
=
UsersStarProjectsFinder
.
new
(
@project
,
params
,
current_user:
@current_user
).
execute
@sort
=
params
[
:sort
].
presence
||
sort_value_name
@starrers
=
@starrers
.
preload_users
.
sort_by_attribute
(
@sort
).
page
(
params
[
:page
])
@public_count
=
@project
.
starrers
.
with_public_profile
.
size
@total_count
=
@project
.
starrers
.
size
@private_count
=
@total_count
-
@public_count
@sort
=
params
[
:sort
].
presence
||
sort_value_name
@starrers
=
@starrers
.
sort_by_attribute
(
@sort
).
page
(
params
[
:page
])
end
private
...
...
app/models/users_star_project.rb
View file @
2067f677
...
...
@@ -17,6 +17,7 @@ class UsersStarProject < ApplicationRecord
scope
:by_project
,
->
(
project
)
{
where
(
project_id:
project
.
id
)
}
scope
:with_visible_profile
,
->
(
user
)
{
joins
(
:user
).
merge
(
User
.
with_visible_profile
(
user
))
}
scope
:with_public_profile
,
->
{
joins
(
:user
).
merge
(
User
.
with_public_profile
)
}
scope
:preload_users
,
->
{
preload
(
:user
)
}
class
<<
self
def
sort_by_attribute
(
method
)
...
...
spec/controllers/projects/starrers_controller_spec.rb
View file @
2067f677
...
...
@@ -32,6 +32,20 @@ describe Projects::StarrersController do
end
end
context
'N+1 queries'
do
render_views
it
'avoids N+1s loading users'
,
:request_store
do
get_starrers
control_count
=
ActiveRecord
::
QueryRecorder
.
new
{
get_starrers
}.
count
create_list
(
:user
,
5
).
each
{
|
user
|
user
.
toggle_star
(
project
)
}
expect
{
get_starrers
}.
not_to
exceed_query_limit
(
control_count
)
end
end
context
'when project is public'
do
before
do
project
.
update_attribute
(
:visibility_level
,
Project
::
PUBLIC
)
...
...
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