make a public search method
This commit is contained in:
parent
310f529914
commit
c5b6cdff4e
2 changed files with 16 additions and 10 deletions
|
|
@ -38,7 +38,7 @@ class ApisController < ActionController::Metal
|
|||
##people
|
||||
def people_index
|
||||
set_defaults
|
||||
people = Person.search(params[:q], nil).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
||||
people = Person.public_search(params[:q]).paginate(:page => params[:page], :per_page => params[:per_page], :order => "profiles.last_name ASC, profiles.first_name ASC")
|
||||
render :json => people.as_json
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -43,9 +43,7 @@ class Person < ActiveRecord::Base
|
|||
|
||||
scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
|
||||
|
||||
def self.search(query, user)
|
||||
return [] if query.to_s.blank? || query.to_s.length < 3
|
||||
|
||||
def self.search_query_string(query)
|
||||
where_clause = <<-SQL
|
||||
profiles.first_name LIKE ? OR
|
||||
profiles.last_name LIKE ? OR
|
||||
|
|
@ -65,18 +63,26 @@ class Person < ActiveRecord::Base
|
|||
tokens.concat([token, token, token])
|
||||
tokens.concat([up_token, up_token])
|
||||
end
|
||||
#SELECT `people`.* FROM people
|
||||
# INNER JOIN `profiles` ON `profiles`.person_id = `people`.id
|
||||
# LEFT OUTER JOIN `contacts` ON (`contacts`.user_id = #{user.id} AND `contacts`.person_id = `people`.id)
|
||||
# WHERE `profiles`.searchable = true AND
|
||||
# `profiles`.first_name LIKE '%Max%'
|
||||
# ORDER BY `contacts`.user_id DESC
|
||||
[sql, tokens]
|
||||
end
|
||||
|
||||
def self.search(query, user)
|
||||
return [] if query.to_s.blank? || query.to_s.length < 3
|
||||
|
||||
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"
|
||||
).joins("LEFT OUTER JOIN `requests` ON `requests`.recipient_id = #{user.person.id} AND `requests`.sender_id = `people`.id"
|
||||
).order("contacts.user_id DESC", "requests.recipient_id DESC", "profiles.last_name ASC", "profiles.first_name ASC")
|
||||
end
|
||||
|
||||
|
||||
def self.public_search(query, opts={})
|
||||
return [] if query.to_s.blank? || query.to_s.length < 3
|
||||
sql, tokens = self.search_query_string(query)
|
||||
Person.searchable.where(sql, *tokens)
|
||||
end
|
||||
|
||||
def name(opts = {})
|
||||
@name ||= if profile.nil? || profile.first_name.nil? || profile.first_name.blank?
|
||||
self.diaspora_handle
|
||||
|
|
|
|||
Loading…
Reference in a new issue