From 5d4ab429064e83d92f006bc22a46dda495f1308b Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Mon, 28 Oct 2013 12:03:58 -0500 Subject: [PATCH] vrfs-774: first musician searches working properly --- ruby/lib/jam_ruby/models/search.rb | 4 +- ruby/lib/jam_ruby/models/user.rb | 15 ++++--- .../jam_ruby/models/musician_search_spec.rb | 39 ++++++++++++++++--- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index da3209923..7a2cead22 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -5,10 +5,10 @@ module JamRuby LIMIT = 10 - ORDER_LIKED = ['Most Liked', :liked] ORDER_FOLLOWS = ['Most Followed', :followed] + ORDER_LIKED = ['Most Liked', :liked] ORDER_PLAYING = ['Playing Now', :playing] - ORDERINGS = [ORDER_LIKED, ORDER_FOLLOWS, ORDER_PLAYING] + ORDERINGS = [ORDER_FOLLOWS, ORDER_LIKED, ORDER_PLAYING] ORDERING_KEYS = ORDERINGS.collect { |oo| oo[1] } def self.order_param(params) diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 28b08db71..5fd2224c5 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -8,8 +8,8 @@ module JamRuby devise :database_authenticatable, :recoverable, :rememberable - # include Geokit::ActsAsMappable::Glue unless defined?(acts_as_mappable) - # acts_as_mappable + include Geokit::ActsAsMappable::Glue unless defined?(acts_as_mappable) + acts_as_mappable after_update :check_lat_lng @@ -925,12 +925,17 @@ module JamRuby case ordering = Search.order_param(params) when :liked - rel = rel.order("SELECT COUNT(*) FROM users_likers WHERE users_likers.user_id = ") when :followed + rel = rel.select("COUNT(follows) AS fcount, users.id") + 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 - - rel.page(params[:page].to_i) + perpage = params[:per_page] || 20 + page = [params[:page].to_i, 1].max + rel = rel.paginate(:page => page, :per_page => perpage) + rel end def self.search(query, options = { :limit => 10 }) diff --git a/ruby/spec/jam_ruby/models/musician_search_spec.rb b/ruby/spec/jam_ruby/models/musician_search_spec.rb index 7d3a9a47e..61e9d0285 100644 --- a/ruby/spec/jam_ruby/models/musician_search_spec.rb +++ b/ruby/spec/jam_ruby/models/musician_search_spec.rb @@ -15,14 +15,43 @@ describe User do state: "NC", country: "USA" } - @user1 = FactoryGirl.create(:user, params) + @users = [] + @users << @user1 = FactoryGirl.create(:user, params) params[:email] = "user2@example.com" - @user2 = FactoryGirl.create(:user, params) + @users << @user2 = FactoryGirl.create(:user, params) + params[:email] = "user3@example.com" + @users << @user3 = FactoryGirl.create(:user, params) + params[:email] = "user4@example.com" + @users << @user4 = FactoryGirl.create(:user, params) end - it "should find all musicians sorted by likes with pagination" do - pending - User.musician_search + it "should find all musicians sorted by followers with pagination" do + # establish sorting order + @user4.followers.concat([@user2, @user3, @user4]) + @user3.followers.concat([@user3, @user4]) + @user2.followers.concat([@user1]) + @user4.followers.count.should == 3 + + UserFollower.count.should == 6 + + # get all the users in correct order + params = { :per_page => @users.size } + results = User.musician_search(params) + results.all.count.should == @users.size + + results.each_with_index do |uu, idx| + uu.id.should == @users.reverse[idx].id + end + + # refresh the order to ensure it works right + @user2.followers.concat([@user3, @user4, @user2]) + results = User.musician_search(params) + results[0].id.should == @user2.id + + # make sure pagination works right + params = { :per_page => 2, :page => 1 } + results = User.musician_search(params) + results.all.count.should == 2 end it "should find all musicians sorted by follows with pagination" do