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
800aa296
Commit
800aa296
authored
Mar 01, 2016
by
Yorick Peterse
Committed by
Robert Speicher
Mar 11, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use ILIKE/LIKE for searching users
parent
508b6b46
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
12 deletions
+52
-12
app/models/user.rb
app/models/user.rb
+15
-1
spec/models/user_spec.rb
spec/models/user_spec.rb
+37
-11
No files found.
app/models/user.rb
View file @
800aa296
...
...
@@ -286,8 +286,22 @@ class User < ActiveRecord::Base
end
end
# Searches users matching the given query.
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String
#
# Returns an ActiveRecord::Relation.
def
search
(
query
)
where
(
"lower(name) LIKE :query OR lower(email) LIKE :query OR lower(username) LIKE :query"
,
query:
"%
#{
query
.
downcase
}
%"
)
table
=
User
.
arel_table
pattern
=
"%
#{
query
}
%"
where
(
table
[
:name
].
matches
(
pattern
).
or
(
table
[
:email
].
matches
(
pattern
)).
or
(
table
[
:username
].
matches
(
pattern
))
)
end
def
by_login
(
login
)
...
...
spec/models/user_spec.rb
View file @
800aa296
...
...
@@ -463,17 +463,43 @@ describe User, models: true do
end
end
describe
'search'
do
let
(
:user1
)
{
create
(
:user
,
username:
'James'
,
email:
'james@testing.com'
)
}
let
(
:user2
)
{
create
(
:user
,
username:
'jameson'
,
email:
'jameson@example.com'
)
}
it
"should be case insensitive"
do
expect
(
User
.
search
(
user1
.
username
.
upcase
).
to_a
).
to
eq
([
user1
])
expect
(
User
.
search
(
user1
.
username
.
downcase
).
to_a
).
to
eq
([
user1
])
expect
(
User
.
search
(
user2
.
username
.
upcase
).
to_a
).
to
eq
([
user2
])
expect
(
User
.
search
(
user2
.
username
.
downcase
).
to_a
).
to
eq
([
user2
])
expect
(
User
.
search
(
user1
.
username
.
downcase
).
to_a
.
size
).
to
eq
(
2
)
expect
(
User
.
search
(
user2
.
username
.
downcase
).
to_a
.
size
).
to
eq
(
1
)
describe
'.search'
do
let
(
:user
)
{
create
(
:user
)
}
it
'returns users with a matching name'
do
expect
(
described_class
.
search
(
user
.
name
)).
to
eq
([
user
])
end
it
'returns users with a partially matching name'
do
expect
(
described_class
.
search
(
user
.
name
[
0
..
2
])).
to
eq
([
user
])
end
it
'returns users with a matching name regarding of the casing'
do
expect
(
described_class
.
search
(
user
.
name
.
upcase
)).
to
eq
([
user
])
end
it
'returns users with a matching Email'
do
expect
(
described_class
.
search
(
user
.
email
)).
to
eq
([
user
])
end
it
'returns users with a partially matching Email'
do
expect
(
described_class
.
search
(
user
.
email
[
0
..
2
])).
to
eq
([
user
])
end
it
'returns users with a matching Email regarding of the casing'
do
expect
(
described_class
.
search
(
user
.
email
.
upcase
)).
to
eq
([
user
])
end
it
'returns users with a matching username'
do
expect
(
described_class
.
search
(
user
.
username
)).
to
eq
([
user
])
end
it
'returns users with a partially matching username'
do
expect
(
described_class
.
search
(
user
.
username
[
0
..
2
])).
to
eq
([
user
])
end
it
'returns users with a matching username regarding of the casing'
do
expect
(
described_class
.
search
(
user
.
username
.
upcase
)).
to
eq
([
user
])
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