BUGFIX: exact username match not correctly prioritized on search

This commit is contained in:
Sam 2014-05-15 12:22:35 +10:00
parent 8bf19e244a
commit 198cf851ff
2 changed files with 20 additions and 14 deletions

View File

@ -6,10 +6,11 @@ class UserSearch
@term_like = "#{term.downcase}%"
@topic_id = opts[:topic_id]
@searching_user = opts[:searching_user]
@limit = opts[:limit] || 20
end
def search
users = User.order(User.sql_fragment("CASE WHEN username_lower = ? THEN 0 ELSE 1 END ASC", :term))
users = User.order(User.sql_fragment("CASE WHEN username_lower = ? THEN 0 ELSE 1 END ASC", @term.downcase))
if @term.present?
if SiteSetting.enable_names?
@ -37,7 +38,7 @@ class UserSearch
end
users.order("CASE WHEN last_seen_at IS NULL THEN 0 ELSE 1 END DESC, last_seen_at DESC, username ASC")
.limit(20)
.limit(@limit)
end
end

View File

@ -5,12 +5,12 @@ describe UserSearch do
let(:topic) { Fabricate :topic }
let(:topic2) { Fabricate :topic }
let(:topic3) { Fabricate :topic }
let(:user1) { Fabricate :user, username: "mrblonde", name: "Michael Madsen" }
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker" }
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth" }
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi" }
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino" }
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel" }
let(:user1) { Fabricate :user, username: "mrb", name: "Michael Madsen", last_seen_at: 10.days.ago }
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker", last_seen_at: 9.days.ago }
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth", last_seen_at: 8.days.ago }
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi", last_seen_at: 7.days.ago }
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino", last_seen_at: 6.days.ago }
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel", last_seen_at: 5.days.ago }
let(:admin) { Fabricate :admin, username: "theadmin" }
let(:moderator) { Fabricate :moderator, username: "themod" }
@ -70,8 +70,8 @@ describe UserSearch do
results = search_for("MR", searching_user: admin)
results.size.should == 6
results = search_for("MRB", searching_user: admin)
results.size.should == 3
results = search_for("MRB", searching_user: admin, limit: 2)
results.size.should == 2
# topic priority
results = search_for("mrb", topic_id: topic.id)
@ -79,21 +79,26 @@ describe UserSearch do
results = search_for("mrb", topic_id: topic2.id)
results.first.should == user2
results[1].should == user2
results = search_for("mrb", topic_id: topic3.id)
results.first.should == user5
results[1].should == user5
# When searching by name is enabled, it returns the record
SiteSetting.stubs(:enable_names).returns(true)
SiteSetting.enable_names = true
results = search_for("Tarantino")
results.size.should == 1
# When searching by name is disabled, it will not return the record
SiteSetting.stubs(:enable_names).returns(false)
SiteSetting.enable_names = false
results = search_for("Tarantino")
results.size.should == 0
# find an exact match first
results = search_for("mrB")
results.first.should == user1
end
end