From a78c57424aacc26e5d45ca98aa402a617aa31589 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Mon, 4 Nov 2013 08:58:34 -0600 Subject: [PATCH] vrfs-774: moving search logic into api_search/Search --- ruby/lib/jam_ruby/models/search.rb | 63 +++++++++++++++++--- ruby/lib/jam_ruby/models/user.rb | 46 -------------- web/app/assets/javascripts/findMusician.js | 2 +- web/app/controllers/api_search_controller.rb | 4 +- web/app/controllers/api_users_controller.rb | 12 +--- web/app/views/api_search/index.rabl | 2 +- web/app/views/api_users/index.rabl | 6 -- 7 files changed, 62 insertions(+), 73 deletions(-) diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index 877d1a634..fa839b559 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -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 diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index ee9a1ceca..fb7f5fbc4 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -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 diff --git a/web/app/assets/javascripts/findMusician.js b/web/app/assets/javascripts/findMusician.js index cd0982d4d..807f964e5 100644 --- a/web/app/assets/javascripts/findMusician.js +++ b/web/app/assets/javascripts/findMusician.js @@ -26,7 +26,7 @@ $.ajax({ type: "GET", - url: "/api/users?" + queryString, + url: "/api/search.json?" + queryString, async: true, success: afterLoadMusicians, complete: removeSpinner, diff --git a/web/app/controllers/api_search_controller.rb b/web/app/controllers/api_search_controller.rb index bca395dfc..6097c484b 100644 --- a/web/app/controllers/api_search_controller.rb +++ b/web/app/controllers/api_search_controller.rb @@ -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) diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index eb4de1f85..40a96b869 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -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 diff --git a/web/app/views/api_search/index.rabl b/web/app/views/api_search/index.rabl index 1bcb83003..54418f403 100644 --- a/web/app/views/api_search/index.rabl +++ b/web/app/views/api_search/index.rabl @@ -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) diff --git a/web/app/views/api_users/index.rabl b/web/app/views/api_users/index.rabl index fff72aa1b..27eb79ce0 100644 --- a/web/app/views/api_users/index.rabl +++ b/web/app/views/api_users/index.rabl @@ -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