vrfs-774: moving search logic into api_search/Search

This commit is contained in:
Jonathan Kolyer 2013-11-04 08:58:34 -06:00
parent 76c1b4ce0a
commit a78c57424a
7 changed files with 62 additions and 73 deletions

View File

@ -3,17 +3,66 @@ module JamRuby
class Search
attr_accessor :bands, :musicians, :fans, :recordings, :friends
PARAM_SEARCH_MUSICIAN = :search_m
LIMIT = 10
ORDER_FOLLOWS = ['Most Followed', :followed]
ORDER_PLAYS = ['Most Plays', :plays]
ORDER_PLAYING = ['Playing Now', :playing]
ORDERINGS = [ORDER_FOLLOWS, ORDER_PLAYS, ORDER_PLAYING]
ORDERING_KEYS = ORDERINGS.collect { |oo| oo[1] }
M_ORDER_FOLLOWS = ['Most Followed', :followed]
M_ORDER_PLAYS = ['Most Plays', :plays]
M_ORDER_PLAYING = ['Playing Now', :playing]
M_ORDERINGS = [M_ORDER_FOLLOWS, M_ORDER_PLAYS, M_ORDER_PLAYING]
M_ORDERING_KEYS = M_ORDERINGS.collect { |oo| oo[1] }
def self.order_param(params)
def self.musician_order_param(params)
ordering = params[:orderby]
ordering.blank? ? ORDERING_KEYS[0] : ORDERING_KEYS.detect { |oo| oo.to_s == ordering }
ordering.blank? ? M_ORDERING_KEYS[0] : M_ORDERING_KEYS.detect { |oo| oo.to_s == ordering }
end
def self.musician_search(params={}, current_user=nil)
rel = User.where(:musician => true)
unless (instrument = params[:instrument]).blank?
rel = rel.joins("RIGHT JOIN musicians_instruments AS minst ON minst.user_id = users.id")
.where(['minst.instrument_id = ? AND users.id IS NOT NULL', instrument])
end
location_distance, location_city = params[:distance], params[:city]
if location_distance && location_city
if geo = MaxMindGeo.where(:city => params[:city]).limit(1).first
citylatlng = [geo.lat, geo.lng]
rel = rel.within(location_distance, :origin => citylatlng)
end
elsif current_user
latlng = []
if current_user.lat.nil?
if params[:remote_ip]
if geo = MaxMindGeo.ip_lookup(params[:remote_ip])
latlng = [geo.lat, geo.lng]
end
end
else
latlng = [current_user.lat, current_user.lng]
end
distance = location_distance || 50
rel = rel.within(distance, :origin => latlng) unless latlng.blank?
end
case ordering = self.musician_order_param(params)
when :plays
when :followed
rel = rel.select("COUNT(follows) AS fcount, users.*")
rel = rel.joins("LEFT JOIN users_followers AS follows ON follows.user_id = users.id")
rel = rel.group("users.id")
rel = rel.order("COUNT(follows) DESC")
when :playing
end
perpage = params[:per_page] || 20
page = [params[:page].to_i, 1].max
rel = rel.paginate(:page => page, :per_page => perpage)
rel.includes([:instruments])
srch = Search.new
srch.musicians = rel.all
srch
end
# performs a site-white search

View File

@ -13,8 +13,6 @@ module JamRuby
after_save :check_lat_lng
PARAM_SEARCH_MUSICIAN = :search_m
attr_accessible :first_name, :last_name, :email, :city, :password, :password_confirmation, :state, :country, :birth_date, :subscribe_email, :terms_of_service, :original_fpfile, :cropped_fpfile, :cropped_s3_path, :photo_url, :crop_selection, :lat, :lng
# updating_password corresponds to a lost_password
@ -906,50 +904,6 @@ module JamRuby
end
end
def self.musician_search(params={}, current_user=nil)
rel = User.where(:musician => true)
unless (instrument = params[:instrument]).blank?
rel = rel.joins("RIGHT JOIN musicians_instruments AS minst ON minst.user_id = users.id")
.where(['minst.instrument_id = ? AND users.id IS NOT NULL', instrument])
end
location_distance, location_city = params[:distance], params[:city]
if location_distance && location_city
if geo = MaxMindGeo.where(:city => params[:city]).limit(1).first
citylatlng = [geo.lat, geo.lng]
rel = rel.within(location_distance, :origin => citylatlng)
end
elsif current_user
latlng = []
if current_user.lat.nil?
if params[:remote_ip]
if geo = MaxMindGeo.ip_lookup(params[:remote_ip])
latlng = [geo.lat, geo.lng]
end
end
else
latlng = [current_user.lat, current_user.lng]
end
distance = location_distance || 50
rel = rel.within(distance, :origin => latlng) unless latlng.blank?
end
case ordering = Search.order_param(params)
when :plays
when :followed
rel = rel.select("COUNT(follows) AS fcount, users.*")
rel = rel.joins("LEFT JOIN users_followers AS follows ON follows.user_id = users.id")
rel = rel.group("users.id")
rel = rel.order("COUNT(follows) DESC")
when :playing
end
perpage = params[:per_page] || 20
page = [params[:page].to_i, 1].max
rel = rel.paginate(:page => page, :per_page => perpage)
rel.includes([:instruments])
rel
end
def self.search(query, options = { :limit => 10 })
# only issue search if at least 2 characters are specified

View File

@ -26,7 +26,7 @@
$.ajax({
type: "GET",
url: "/api/users?" + queryString,
url: "/api/search.json?" + queryString,
async: true,
success: afterLoadMusicians,
complete: removeSpinner,

View File

@ -6,11 +6,11 @@ class ApiSearchController < ApiController
respond_to :json
def index
if 1 == params[User::PARAM_SEARCH_MUSICIAN].to_i
if 1 == params[Search::PARAM_SEARCH_MUSICIAN].to_i
logger.debug("*** params = #{params.inspect}")
query = params.clone
query[:remote_ip] = request.remote_ip
@search = User.musician_search(query, current_user)
@users = Search.musician_search(query, current_user)
respond_with @users, responder: ApiResponder, :status => 200
else
@search = Search.search(params[:query], current_user.id)

View File

@ -14,16 +14,8 @@ class ApiUsersController < ApiController
respond_to :json
def index
if 1 == params[User::PARAM_SEARCH_MUSICIAN].to_i
logger.debug("*** params = #{params.inspect}")
query = params.clone
query[:remote_ip] = request.remote_ip
@users = User.musician_search(query, current_user)
respond_with @users, responder: ApiResponder, :status => 200
else
@users = User.paginate(page: params[:page])
respond_with @users, responder: ApiResponder, :status => 200
end
@users = User.paginate(page: params[:page])
respond_with @users, responder: ApiResponder, :status => 200
end
def show

View File

@ -8,7 +8,7 @@ end
unless @search.musicians.nil? || @search.musicians.size == 0
child(:musicians => :musicians) {
attributes :id, :first_name, :last_name, :name, :location, :photo_url
attributes :id, :first_name, :last_name, :name, :city, :state, :country, :email, :online, :musician, :photo_url, :biography
node :is_friend do |musician|
musician.friends?(current_user)

View File

@ -2,9 +2,3 @@ collection @users
# do not retrieve all child collections when showing a list of users
attributes :id, :first_name, :last_name, :name, :city, :state, :country, :email, :online, :musician, :photo_url, :biography
if 1 == params[User::PARAM_SEARCH_MUSICIAN].to_i
child :instruments do
attributes :id
end
end