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
d4078b53
Commit
d4078b53
authored
Jul 27, 2019
by
Camil Staps
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix public/private starrers counts in special cases
parent
e726ed5e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
8 deletions
+59
-8
app/controllers/projects/starrers_controller.rb
app/controllers/projects/starrers_controller.rb
+20
-3
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
+38
-5
No files found.
app/controllers/projects/starrers_controller.rb
View file @
d4078b53
...
...
@@ -4,14 +4,31 @@ class Projects::StarrersController < Projects::ApplicationController
include
SortingHelper
def
index
@sort
=
params
[
:sort
].
presence
||
sort_value_name
@starrers
=
UsersStarProjectsFinder
.
new
(
@project
,
params
,
current_user:
@current_user
).
execute
# Normally the number of public starrers is equal to the number of visible
# starrers. We need to fix the counts in two cases: when the current user
# is an admin (and can see everything) and when the current user has a
# private profile and has starred the project (and can see itself).
@public_count
=
if
@current_user
&
.
admin?
@starrers
.
with_public_profile
.
count
elsif
@current_user
&
.
private_profile
&&
has_starred_project?
(
@starrers
)
@starrers
.
size
-
1
else
@starrers
.
size
end
@total_count
=
@project
.
starrers
.
size
@public_count
=
@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
def
has_starred_project?
(
starrers
)
starrers
.
first
{
|
starrer
|
starrer
.
user_id
==
current_user
.
id
}
end
end
app/models/users_star_project.rb
View file @
d4078b53
...
...
@@ -16,6 +16,7 @@ class UsersStarProject < ApplicationRecord
scope
:order_user_name_desc
,
->
{
joins
(
:user
).
merge
(
User
.
order_name_desc
)
}
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
)
}
class
<<
self
def
sort_by_attribute
(
method
)
...
...
spec/controllers/projects/starrers_controller_spec.rb
View file @
d4078b53
...
...
@@ -5,6 +5,7 @@ require 'spec_helper'
describe
Projects
::
StarrersController
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:private_user
)
{
create
(
:user
,
private_profile:
true
)
}
let
(
:admin
)
{
create
(
:user
,
admin:
true
)
}
let
(
:project
)
{
create
(
:project
,
:public
,
:repository
)
}
before
do
...
...
@@ -26,25 +27,57 @@ describe Projects::StarrersController do
project
.
update_attribute
(
:visibility_level
,
Project
::
PUBLIC
)
end
it
'only public starrers are visible for non logged in users'
do
context
'when no user is logged in'
do
before
do
get_starrers
end
it
'only public starrers are visible'
do
user_ids
=
assigns
[
:starrers
].
map
{
|
s
|
s
[
'user_id'
]
}
expect
(
user_ids
).
to
include
(
user
.
id
)
expect
(
user_ids
).
not_to
include
(
private_user
.
id
)
end
it
'public/private starrers counts are correct'
do
expect
(
assigns
[
:public_count
]).
to
eq
(
1
)
expect
(
assigns
[
:private_count
]).
to
eq
(
1
)
end
end
context
'when private user is logged in'
do
before
do
sign_in
(
private_user
)
get_starrers
end
it
'their star is also visible'
do
user_ids
=
assigns
[
:starrers
].
map
{
|
s
|
s
[
'user_id'
]
}
expect
(
user_ids
).
to
include
(
user
.
id
,
private_user
.
id
)
end
it
'public/private starrers counts are correct'
do
expect
(
assigns
[
:public_count
]).
to
eq
(
1
)
expect
(
assigns
[
:private_count
]).
to
eq
(
1
)
end
end
context
'when admin is logged in'
do
before
do
sign_in
(
admin
)
get_starrers
end
it
'all stars are visible'
do
user_ids
=
assigns
[
:starrers
].
map
{
|
s
|
s
[
'user_id'
]
}
expect
(
user_ids
).
to
include
(
user
.
id
,
private_user
.
id
)
end
it
'public/private starrers counts are correct'
do
expect
(
assigns
[
:public_count
]).
to
eq
(
1
)
expect
(
assigns
[
:private_count
]).
to
eq
(
1
)
end
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