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
Jérome Perrin
gitlab-ce
Commits
199425ce
Commit
199425ce
authored
Jun 28, 2017
by
Tiago Botelho
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Inserts exact matches of username, email and name to the top of the user search list
parent
4f620eb9
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
26 deletions
+47
-26
app/models/user.rb
app/models/user.rb
+10
-1
changelogs/unreleased/26125-match-username-on-search.yml
changelogs/unreleased/26125-match-username-on-search.yml
+5
-0
spec/models/user_spec.rb
spec/models/user_spec.rb
+32
-25
No files found.
app/models/user.rb
View file @
199425ce
...
@@ -299,11 +299,20 @@ class User < ActiveRecord::Base
...
@@ -299,11 +299,20 @@ class User < ActiveRecord::Base
table
=
arel_table
table
=
arel_table
pattern
=
"%
#{
query
}
%"
pattern
=
"%
#{
query
}
%"
order
=
<<~
SQL
CASE
WHEN users.name = %{query} THEN 0
WHEN users.username = %{query} THEN 1
WHEN users.email = %{query} THEN 2
ELSE 3
END
SQL
where
(
where
(
table
[
:name
].
matches
(
pattern
)
table
[
:name
].
matches
(
pattern
)
.
or
(
table
[
:email
].
matches
(
pattern
))
.
or
(
table
[
:email
].
matches
(
pattern
))
.
or
(
table
[
:username
].
matches
(
pattern
))
.
or
(
table
[
:username
].
matches
(
pattern
))
)
)
.
reorder
(
order
%
{
query:
ActiveRecord
::
Base
.
connection
.
quote
(
query
)
},
id: :desc
)
end
end
# searches user by given pattern
# searches user by given pattern
...
...
changelogs/unreleased/26125-match-username-on-search.yml
0 → 100644
View file @
199425ce
---
title
:
Inserts exact matches of name, username and email to the top of the search
list
merge_request
:
12525
author
:
spec/models/user_spec.rb
View file @
199425ce
...
@@ -754,42 +754,49 @@ describe User, models: true do
...
@@ -754,42 +754,49 @@ describe User, models: true do
end
end
describe
'.search'
do
describe
'.search'
do
let
(
:user
)
{
create
(
:user
)
}
let!
(
:user
)
{
create
(
:user
,
name:
'user'
,
username:
'usern'
,
email:
'email@gmail.com'
)
}
let!
(
:user2
)
{
create
(
:user
,
name:
'user name'
,
username:
'username'
,
email:
'someemail@gmail.com'
)
}
it
'returns users with a matching name'
do
describe
'name matching'
do
expect
(
described_class
.
search
(
user
.
name
)).
to
eq
([
user
])
it
'returns users with a matching name with exact match first'
do
expect
(
described_class
.
search
(
user
.
name
)).
to
eq
([
user
,
user2
])
end
end
it
'returns users with a partially matching name'
do
it
'returns users with a partially matching name'
do
expect
(
described_class
.
search
(
user
.
name
[
0
..
2
])).
to
eq
([
user
])
expect
(
described_class
.
search
(
user
.
name
[
0
..
2
])).
to
eq
([
user2
,
user
])
end
end
it
'returns users with a matching name regardless of the casing'
do
it
'returns users with a matching name regardless of the casing'
do
expect
(
described_class
.
search
(
user
.
name
.
upcase
)).
to
eq
([
user
])
expect
(
described_class
.
search
(
user2
.
name
.
upcase
)).
to
eq
([
user2
])
end
end
end
describe
'email matching'
do
it
'returns users with a matching Email'
do
it
'returns users with a matching Email'
do
expect
(
described_class
.
search
(
user
.
email
)).
to
eq
([
user
])
expect
(
described_class
.
search
(
user
.
email
)).
to
eq
([
user
,
user2
])
end
end
it
'returns users with a partially matching Email'
do
it
'returns users with a partially matching Email'
do
expect
(
described_class
.
search
(
user
.
email
[
0
..
2
])).
to
eq
([
user
])
expect
(
described_class
.
search
(
user
.
email
[
0
..
2
])).
to
eq
([
user2
,
user
])
end
end
it
'returns users with a matching Email regardless of the casing'
do
it
'returns users with a matching Email regardless of the casing'
do
expect
(
described_class
.
search
(
user
.
email
.
upcase
)).
to
eq
([
user
])
expect
(
described_class
.
search
(
user2
.
email
.
upcase
)).
to
eq
([
user2
])
end
end
end
describe
'username matching'
do
it
'returns users with a matching username'
do
it
'returns users with a matching username'
do
expect
(
described_class
.
search
(
user
.
username
)).
to
eq
([
user
])
expect
(
described_class
.
search
(
user
.
username
)).
to
eq
([
user
,
user2
])
end
end
it
'returns users with a partially matching username'
do
it
'returns users with a partially matching username'
do
expect
(
described_class
.
search
(
user
.
username
[
0
..
2
])).
to
eq
([
user
])
expect
(
described_class
.
search
(
user
.
username
[
0
..
2
])).
to
eq
([
user2
,
user
])
end
end
it
'returns users with a matching username regardless of the casing'
do
it
'returns users with a matching username regardless of the casing'
do
expect
(
described_class
.
search
(
user
.
username
.
upcase
)).
to
eq
([
user
])
expect
(
described_class
.
search
(
user2
.
username
.
upcase
)).
to
eq
([
user2
])
end
end
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