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
|
##people
|
||||||
def people_index
|
def people_index
|
||||||
set_defaults
|
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
|
render :json => people.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,7 @@ class Person < ActiveRecord::Base
|
||||||
|
|
||||||
scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
|
scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
|
||||||
|
|
||||||
def self.search(query, user)
|
def self.search_query_string(query)
|
||||||
return [] if query.to_s.blank? || query.to_s.length < 3
|
|
||||||
|
|
||||||
where_clause = <<-SQL
|
where_clause = <<-SQL
|
||||||
profiles.first_name LIKE ? OR
|
profiles.first_name LIKE ? OR
|
||||||
profiles.last_name LIKE ? OR
|
profiles.last_name LIKE ? OR
|
||||||
|
|
@ -65,18 +63,26 @@ class Person < ActiveRecord::Base
|
||||||
tokens.concat([token, token, token])
|
tokens.concat([token, token, token])
|
||||||
tokens.concat([up_token, up_token])
|
tokens.concat([up_token, up_token])
|
||||||
end
|
end
|
||||||
#SELECT `people`.* FROM people
|
[sql, tokens]
|
||||||
# INNER JOIN `profiles` ON `profiles`.person_id = `people`.id
|
end
|
||||||
# LEFT OUTER JOIN `contacts` ON (`contacts`.user_id = #{user.id} AND `contacts`.person_id = `people`.id)
|
|
||||||
# WHERE `profiles`.searchable = true AND
|
def self.search(query, user)
|
||||||
# `profiles`.first_name LIKE '%Max%'
|
return [] if query.to_s.blank? || query.to_s.length < 3
|
||||||
# ORDER BY `contacts`.user_id DESC
|
|
||||||
|
sql, tokens = self.search_query_string(query)
|
||||||
Person.searchable.where(sql, *tokens).joins(
|
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"
|
||||||
).joins("LEFT OUTER JOIN `requests` ON `requests`.recipient_id = #{user.person.id} AND `requests`.sender_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")
|
).order("contacts.user_id DESC", "requests.recipient_id DESC", "profiles.last_name ASC", "profiles.first_name ASC")
|
||||||
end
|
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 = {})
|
def name(opts = {})
|
||||||
@name ||= if profile.nil? || profile.first_name.nil? || profile.first_name.blank?
|
@name ||= if profile.nil? || profile.first_name.nil? || profile.first_name.blank?
|
||||||
self.diaspora_handle
|
self.diaspora_handle
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue