Make contacts appear at the beginning of search results in postgreSQL

This commit is contained in:
Raphael Sofaer 2011-06-15 20:04:30 -07:00
parent ab398555d6
commit f0cb2159c7
2 changed files with 15 additions and 25 deletions

View file

@ -71,11 +71,24 @@ class Person < ActiveRecord::Base
sql, tokens = self.search_query_string(query)
Person.searchable.where(sql, *tokens).joins(
"LEFT OUTER JOIN `contacts` ON `contacts`.user_id = #{user.id} AND `contacts`.person_id = `people`.id"
"LEFT OUTER JOIN contacts ON contacts.user_id = #{user.id} AND contacts.person_id = people.id"
).includes(:profile
).order("contacts.user_id DESC", "profiles.last_name ASC", "profiles.first_name ASC")
).order(search_order)
end
# @return [Array<String>] postgreSQL and mysql deal with null values in orders differently, it seems.
def self.search_order
@search_order ||= Proc.new {
order = if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
"ASC"
else
"DESC"
end
["contacts.user_id #{order}", "profiles.last_name ASC", "profiles.first_name ASC"]
}.call
end
def self.public_search(query, opts={})
return [] if query.to_s.blank? || query.to_s.length < 3

View file

@ -282,29 +282,6 @@ describe Person do
people = Person.search("AAA", @user)
people.map{|p| p.name}.should == [@casey_grippi, @yevgeniy_dodis, @robert_grimm, @eugene_weinstein].map{|p|p.name}
end
it "puts the searching user's incoming requests first" do
requestor = Factory(:user_with_aspect)
profile = requestor.person.profile
profile.first_name = "AAA"
profile.last_name = "Something"
profile.save
@robert_grimm.profile.first_name = "AAA"
@robert_grimm.profile.save
@eugene_weinstein.profile.first_name = "AAA"
@eugene_weinstein.profile.save
@yevgeniy_dodis.profile.first_name = "AAA"
@yevgeniy_dodis.profile.save
@casey_grippi.profile.first_name = "AAA"
@casey_grippi.profile.save
requestor.share_with(@user.person, requestor.aspects.first)
people = Person.search("AAA", @user)
people.map{|p| p.name}.should == [requestor.person, @yevgeniy_dodis, @robert_grimm, @casey_grippi, @eugene_weinstein].map{|p|p.name}
end
end
context 'people finders for webfinger' do