Skip to content

Commit

Permalink
Merge branch 'prioritize-matches-search-by-terms-#890' of https://git…
Browse files Browse the repository at this point in the history
…hub.com/RafaFP/mconf-web into RafaFP-prioritize-matches-search-by-terms-#890
  • Loading branch information
daronco committed Dec 1, 2016
2 parents bc809dc + e0938ef commit 6b69287
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
34 changes: 19 additions & 15 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,26 @@ def self.find_first_by_auth_conditions(warden_conditions)
default_scope { where(disabled: false) }

scope :search_by_terms, -> (words, include_private=false) {
query = joins(:profile).includes(:profile).order("profiles.full_name")

words ||= []
words = [words] unless words.is_a?(Array)
query_strs = []
query_params = []

words.each do |word|
str = "profiles.full_name LIKE ? OR users.username LIKE ?"
str += " OR users.email LIKE ?" if include_private
query_strs << str
query_params += ["%#{word}%", "%#{word}%"]
query_params += ["%#{word}%"] if include_private
query = joins(:profile).includes(:profile)

if words.present?
words ||= []
words = [words] unless words.is_a?(Array)
query_strs = []
query_params = []
query_orders = []

words.each do |word|
str = "profiles.full_name LIKE ? OR users.username LIKE ?"
str += " OR users.email LIKE ?" if include_private
query_strs << str
query_params += ["%#{word}%", "%#{word}%"]
query_params += ["%#{word}%"] if include_private
query_orders += ["CASE WHEN profiles.full_name LIKE '%#{word}%' THEN 1 ELSE 0 END + CASE WHEN users.username LIKE '%#{word}%' THEN 1 ELSE 0 END + CASE WHEN users.email LIKE '%#{word}%' THEN 1 ELSE 0 END"]
end
query = query.where(query_strs.join(' OR '), *query_params.flatten).order(query_orders.join(' + ') + " DESC")
end

query.where(query_strs.join(' OR '), *query_params.flatten)
query.order("profiles.full_name")
}

# Returns only the users that have the authentication methods selected.
Expand Down
15 changes: 15 additions & 0 deletions spec/controllers/manage_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@
it { assigns(:users)[3].should eql(@u1) }
end

context "on a search_by_terms orders @users by more hits" do
before {
@u1 = FactoryGirl.create(:user, :_full_name => 'First user created')
@u2 = user
@u2.profile.update_attributes(:full_name => 'Second user created')
@u3 = FactoryGirl.create(:user, :_full_name => 'A user starting with letter A')
@u4 = FactoryGirl.create(:user, :_full_name => 'Being someone starting with B')
}
before(:each) { get :users, :q => 'second user' }
it { assigns(:users).count.should be(3) }
it ("On top the user with 2 matches") { assigns(:users)[0].should eql(@u2) }
it ("Then another user with 1 match and has higher alphabetical order") { assigns(:users)[1].should eql(@u3) }
it ("Then another user with 1 match and has lower alphabetical order") { assigns(:users)[2].should eql(@u1) }
end

context "paginates the list of users" do
before {
45.times { FactoryGirl.create(:user) }
Expand Down

0 comments on commit 6b69287

Please sign in to comment.