diff --git a/app/models/person.rb b/app/models/person.rb index caf229ae1122bdc56f8c6cb5857696f8e5e331d5..0f93cd4c7445eced13304b34f3dd00ba2498b1a4 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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 diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index b7b90e876972c291e6940432469d5e1551efebb5..3683816f07e738026f2f01f3a6bb29e2af45ae23 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -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