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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
fa276e30
Commit
fa276e30
authored
Sep 08, 2017
by
Maxim Rydkin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move `find_users` method into finder and add `load_group` method
parent
20ff87a2
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
28 deletions
+58
-28
app/controllers/autocomplete_controller.rb
app/controllers/autocomplete_controller.rb
+7
-19
app/finders/autocomplete_users_finder.rb
app/finders/autocomplete_users_finder.rb
+23
-5
spec/finders/autocomplete_users_finder_spec.rb
spec/finders/autocomplete_users_finder_spec.rb
+28
-4
No files found.
app/controllers/autocomplete_controller.rb
View file @
fa276e30
...
@@ -3,10 +3,10 @@ class AutocompleteController < ApplicationController
...
@@ -3,10 +3,10 @@ class AutocompleteController < ApplicationController
skip_before_action
:authenticate_user!
,
only:
[
:users
,
:award_emojis
]
skip_before_action
:authenticate_user!
,
only:
[
:users
,
:award_emojis
]
before_action
:load_project
,
only:
[
:users
]
before_action
:load_project
,
only:
[
:users
]
before_action
:
find_users
,
only:
[
:users
]
before_action
:
load_group
,
only:
[
:users
]
def
users
def
users
@users
=
AutocompleteUsersFinder
.
new
(
params:
params
,
current_user:
current_user
,
users:
@users
).
execute
@users
=
AutocompleteUsersFinder
.
new
(
params:
params
,
current_user:
current_user
,
project:
@project
,
group:
@group
).
execute
render
json:
@users
,
only:
[
:name
,
:username
,
:id
],
methods:
[
:avatar_url
]
render
json:
@users
,
only:
[
:name
,
:username
,
:id
],
methods:
[
:avatar_url
]
end
end
...
@@ -39,25 +39,13 @@ class AutocompleteController < ApplicationController
...
@@ -39,25 +39,13 @@ class AutocompleteController < ApplicationController
private
private
def
find_users
def
load_group
@users
=
@group
||=
begin
if
@project
if
@project
.
blank?
&&
params
[
:group_id
].
present?
user_ids
=
@project
.
team
.
users
.
pluck
(
:id
)
if
params
[
:author_id
].
present?
user_ids
<<
params
[
:author_id
]
end
User
.
where
(
id:
user_ids
)
elsif
params
[
:group_id
].
present?
group
=
Group
.
find
(
params
[
:group_id
])
group
=
Group
.
find
(
params
[
:group_id
])
return
render_404
unless
can?
(
current_user
,
:read_group
,
group
)
return
render_404
unless
can?
(
current_user
,
:read_group
,
group
)
group
group
.
users
end
elsif
current_user
User
.
all
else
User
.
none
end
end
end
end
...
...
app/finders/autocomplete_users_finder.rb
View file @
fa276e30
class
AutocompleteUsersFinder
class
AutocompleteUsersFinder
attr_reader
:current_user
,
:
users
,
:search
,
:skip_users
,
:page
,
attr_reader
:current_user
,
:
project
,
:group
,
:search
,
:skip_users
,
:per_page
,
:author_id
,
:params
:p
age
,
:p
er_page
,
:author_id
,
:params
def
initialize
(
params
:,
current_user
:,
users:
nil
)
def
initialize
(
params
:,
current_user
:,
project
:,
group
:
)
@current_user
=
current_user
@current_user
=
current_user
@users
=
users
@project
=
project
@group
=
group
@search
=
params
[
:search
]
@search
=
params
[
:search
]
@skip_users
=
params
[
:skip_users
]
@skip_users
=
params
[
:skip_users
]
@page
=
params
[
:page
]
@page
=
params
[
:page
]
...
@@ -14,7 +15,7 @@ class AutocompleteUsersFinder
...
@@ -14,7 +15,7 @@ class AutocompleteUsersFinder
end
end
def
execute
def
execute
items
=
users
||
User
.
none
items
=
find_users
items
=
items
.
active
items
=
items
.
active
items
=
items
.
reorder
(
:name
)
items
=
items
.
reorder
(
:name
)
items
=
items
.
search
(
search
)
if
search
.
present?
items
=
items
.
search
(
search
)
if
search
.
present?
...
@@ -39,4 +40,21 @@ class AutocompleteUsersFinder
...
@@ -39,4 +40,21 @@ class AutocompleteUsersFinder
items
items
end
end
private
def
find_users
return
users_from_project
if
project
return
group
.
users
if
group
return
User
.
all
if
current_user
User
.
none
end
def
users_from_project
user_ids
=
project
.
team
.
users
.
pluck
(
:id
)
user_ids
<<
author_id
if
author_id
.
present?
User
.
where
(
id:
user_ids
)
end
end
end
spec/finders/autocomplete_users_finder_spec.rb
View file @
fa276e30
...
@@ -9,15 +9,39 @@ describe AutocompleteUsersFinder do
...
@@ -9,15 +9,39 @@ describe AutocompleteUsersFinder do
let
(
:current_user
)
{
create
(
:user
)
}
let
(
:current_user
)
{
create
(
:user
)
}
let
(
:params
)
{
{}
}
let
(
:params
)
{
{}
}
let
(
:
users
)
{
User
.
al
l
}
let
(
:
project
)
{
ni
l
}
subject
{
described_class
.
new
(
params:
params
,
current_user:
current_user
,
users:
users
).
execute
.
to_a
}
let
(
:group
)
{
nil
}
context
'when users param not passed or nil'
do
subject
{
described_class
.
new
(
params:
params
,
current_user:
current_user
,
project:
project
,
group:
group
).
execute
.
to_a
}
let
(
:users
)
{
nil
}
context
'when current_user not passed or nil'
do
let
(
:current_user
)
{
nil
}
it
{
is_expected
.
to
match_array
([])
}
it
{
is_expected
.
to
match_array
([])
}
end
end
context
'when project passed'
do
let
(
:project
)
{
create
(
:project
)
}
it
{
is_expected
.
to
match_array
([
project
.
owner
])
}
context
'when author_id passed'
do
let
(
:params
)
{
{
author_id:
user2
.
id
}
}
it
{
is_expected
.
to
match_array
([
project
.
owner
,
user2
])
}
end
end
context
'when group passed and project not passed'
do
let
(
:group
)
{
create
(
:group
,
:public
)
}
before
do
group
.
add_users
([
user1
],
GroupMember
::
DEVELOPER
)
end
it
{
is_expected
.
to
match_array
([
user1
])
}
end
it
{
is_expected
.
to
match_array
([
user1
,
external_user
,
omniauth_user
,
current_user
])
}
it
{
is_expected
.
to
match_array
([
user1
,
external_user
,
omniauth_user
,
current_user
])
}
context
'when filtered by search'
do
context
'when filtered by search'
do
...
...
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