Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorTiago Botelho <tiagonbotelho@hotmail.com>2017-06-28 18:05:02 +0300
committerTiago Botelho <tiagonbotelho@hotmail.com>2017-06-29 12:02:41 +0300
commit199425cee601733ef4f33ec5b76f5afe948cba61 (patch)
tree447b57a0f98a85a1cc4c17209f199ba35f0ea615 /app
parent4f620eb9e73df40007a2b34caf0e64da01f95668 (diff)
Inserts exact matches of username, email and name to the top of the user search list
Diffstat (limited to 'app')
-rw-r--r--app/models/user.rb11
1 files changed, 10 insertions, 1 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 6dd1b1415d6..35e0d021c47 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -299,11 +299,20 @@ class User < ActiveRecord::Base
table = arel_table
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(
table[:name].matches(pattern)
.or(table[:email].matches(pattern))
.or(table[:username].matches(pattern))
- )
+ ).reorder(order % { query: ActiveRecord::Base.connection.quote(query) }, id: :desc)
end
# searches user by given pattern