Commit 0f393724 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'fix-exact-matches-of-username-and-email-on-top-of-the-user-search' into 'master'

Fixes the user order being overriden in the autocomplete controller

See merge request !12868
parents e318afbe 574b3efd
......@@ -5,10 +5,10 @@ class AutocompleteController < ApplicationController
def users
@users ||= User.none
@users = @users.search(params[:search]) if params[:search].present?
@users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
@users = @users.active
@users = @users.reorder(:name)
@users = @users.search(params[:search]) if params[:search].present?
@users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
@users = @users.page(params[:page]).per(params[:per_page])
if params[:todo_filter].present? && current_user
......
......@@ -314,7 +314,7 @@ class User < ActiveRecord::Base
table[:name].matches(pattern)
.or(table[:email].matches(pattern))
.or(table[:username].matches(pattern))
).reorder(order % { query: ActiveRecord::Base.connection.quote(query) }, id: :desc)
).reorder(order % { query: ActiveRecord::Base.connection.quote(query) }, :name)
end
# searches user by given pattern
......
---
title: Exact matches of username and email are now on top of the user search
merge_request: 12868
author:
......@@ -97,6 +97,21 @@ describe AutocompleteController do
it { expect(body.size).to eq User.count }
end
context 'user order' do
it 'shows exact matches first' do
reported_user = create(:user, username: 'reported_user', name: 'Doug')
user = create(:user, username: 'user', name: 'User')
user1 = create(:user, username: 'user1', name: 'Ian')
sign_in(user)
get(:users, search: 'user')
response_usernames = JSON.parse(response.body).map { |user| user['username'] }
expect(response_usernames.take(3)).to match_array([user.username, reported_user.username, user1.username])
end
end
context 'limited users per page' do
let(:per_page) { 2 }
......
......@@ -763,7 +763,7 @@ describe User, models: true do
end
it 'returns users with a partially matching name' do
expect(described_class.search(user.name[0..2])).to eq([user2, user])
expect(described_class.search(user.name[0..2])).to eq([user, user2])
end
it 'returns users with a matching name regardless of the casing' do
......@@ -777,7 +777,7 @@ describe User, models: true do
end
it 'returns users with a partially matching Email' do
expect(described_class.search(user.email[0..2])).to eq([user2, user])
expect(described_class.search(user.email[0..2])).to eq([user, user2])
end
it 'returns users with a matching Email regardless of the casing' do
......@@ -791,7 +791,7 @@ describe User, models: true do
end
it 'returns users with a partially matching username' do
expect(described_class.search(user.username[0..2])).to eq([user2, user])
expect(described_class.search(user.username[0..2])).to eq([user, user2])
end
it 'returns users with a matching username regardless of the casing' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment