diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index 0395a3475..15493e19c 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -1,7 +1,10 @@ module JamRuby + # not a active_record model; just a search result container class Search - attr_accessor :filter_results, :text_results + attr_accessor :filter_results, :text_results, :search_type + attr_accessor :user_counters, :page_num, :page_count + LIMIT = 10 SEARCH_TEXT_TYPES = [:musicians, :bands, :fans] @@ -20,18 +23,27 @@ module JamRuby end def self.text_search(params, user = nil) - return [] if params.blank? || params[:query].blank? || 2 > params[:query].length + srch = Search.new + unless (params.blank? || params[:query].blank? || 2 > params[:query].length) + srch.text_search(params, user) + end + srch + end + def text_search(params, user = nil) tsquery = Search.create_tsquery(params[:query]) return [] if tsquery.blank? rel = case params[SEARCH_TEXT_TYPE_ID].to_s when 'bands' - Band.scoped + @search_type = :bands + Band.scoped when 'fans' - User.fans + @search_type = :fans + User.fans else - User.musicians + @search_type = :musicians + User.musicians end @text_results = rel.where("(name_tsv @@ to_tsquery('jamenglish', ?))", tsquery).limit(10) end @@ -59,8 +71,6 @@ module JamRuby args end - attr_accessor :user_counters, :page_num, :page_count - PARAM_MUSICIAN = :srch_m PARAM_BAND = :srch_b @@ -82,6 +92,8 @@ module JamRuby end def self.musician_filter(params={}, current_user=nil) + @search_type = :musicians_filter + rel = User.musicians unless (instrument = params[:instrument]).blank? rel = rel.joins("RIGHT JOIN musicians_instruments AS minst ON minst.user_id = users.id") @@ -180,6 +192,22 @@ module JamRuby public + def musicians_text? + :musicians == @search_type + end + + def fans_text? + :fans == @search_type + end + + def bands_text? + :bands == @search_type + end + + def is_filter? + @search_type == :musicians_filter || @search_type == :band_filter + end + def follow_count(musician) _count(musician, COUNT_FOLLOW) end @@ -224,6 +252,7 @@ module JamRuby end def self.band_filter(params={}, current_user=nil) + @search_type = :band_filter rel = Band.scoped unless (genre = params[:genre]).blank? diff --git a/ruby/spec/jam_ruby/models/band_search_spec.rb b/ruby/spec/jam_ruby/models/band_search_spec.rb index 6d3c07d1c..0a5c0f84a 100644 --- a/ruby/spec/jam_ruby/models/band_search_spec.rb +++ b/ruby/spec/jam_ruby/models/band_search_spec.rb @@ -13,7 +13,7 @@ describe User do end it "should allow search of one band with an exact match" do - ws = Search.band_search("Example Band") + ws = Search.band_search("Example Band").text_results ws.length.should == 1 band_result = ws[0] band_result.name.should == @band.name @@ -22,61 +22,61 @@ describe User do end it "should allow search of one band with partial matches" do - ws = Search.band_search("Ex") + ws = Search.band_search("Ex").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Exa") + ws = Search.band_search("Exa").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Exam") + ws = Search.band_search("Exam").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Examp") + ws = Search.band_search("Examp").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Exampl") + ws = Search.band_search("Exampl").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Example") + ws = Search.band_search("Example").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Ba") + ws = Search.band_search("Ba").text_results ws.length.should == 1 ws[0].id.should == @band.id - ws = Search.band_search("Ban") + ws = Search.band_search("Ban").text_results ws.length.should == 1 ws[0].id.should == @band.id end it "should not match mid-word searchs" do - ws = Search.band_search("xa") + ws = Search.band_search("xa").text_results ws.length.should == 0 - ws = Search.band_search("le") + ws = Search.band_search("le").text_results ws.length.should == 0 end it "should delete band" do - ws = Search.band_search("Example Band") + ws = Search.band_search("Example Band").text_results ws.length.should == 1 band_result = ws[0] band_result.id.should == @band.id @band.destroy # delete doesn't work; you have to use destroy. - ws = Search.band_search("Example Band") + ws = Search.band_search("Example Band").text_results ws.length.should == 0 end it "should update band" do - ws = Search.band_search("Example Band") + ws = Search.band_search("Example Band").text_results ws.length.should == 1 band_result = ws[0] band_result.id.should == @band.id @@ -84,10 +84,10 @@ describe User do @band.name = "bonus-stuff" @band.save - ws = Search.band_search("Example Band") + ws = Search.band_search("Example Band").text_results ws.length.should == 0 - ws = Search.band_search("Bonus") + ws = Search.band_search("Bonus").text_results ws.length.should == 1 band_result = ws[0] band_result.id.should == @band.id @@ -96,7 +96,7 @@ describe User do it "should tokenize correctly" do @band2 = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "US", ["hip hop"], user.id, nil, nil) - ws = Search.band_search("pea") + ws = Search.band_search("pea").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @band2.id @@ -105,12 +105,12 @@ describe User do it "should not return anything with a 1 character search" do @band2 = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "US", ["hip hop"], user.id, nil, nil) - ws = Search.band_search("pe") + ws = Search.band_search("pe").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @band2.id - ws = Search.band_search("p") + ws = Search.band_search("p").text_results ws.length.should == 0 end end diff --git a/ruby/spec/jam_ruby/models/user_search_spec.rb b/ruby/spec/jam_ruby/models/user_search_spec.rb index 305493f23..76662cdba 100644 --- a/ruby/spec/jam_ruby/models/user_search_spec.rb +++ b/ruby/spec/jam_ruby/models/user_search_spec.rb @@ -10,7 +10,7 @@ describe User do it "should allow search of one user" do uu = FactoryGirl.create(:user, :musician => false) - ws = Search.musician_search("Example User") + ws = Search.musician_search("Example User").text_results ws.length.should == 1 user_result = ws[0] user_result.first_name.should == @user.first_name @@ -22,25 +22,25 @@ describe User do it "should allow search of one fan" do uu = FactoryGirl.create(:user, :musician => false) - ws = Search.fan_search(uu.name) + ws = Search.fan_search(uu.name).text_results expect(ws.length).to be(1) expect(ws[0].id).to eq(uu.id) end it "should delete user" do - ws = Search.musician_search("Example User") + ws = Search.musician_search("Example User").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @user.id @user.destroy - ws = Search.musician_search("Example User") + ws = Search.musician_search("Example User").text_results ws.length.should == 0 end it "should update user" do - ws = Search.musician_search("Example User") + ws = Search.musician_search("Example User").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @user.id @@ -49,10 +49,10 @@ describe User do @user.last_name = "more-junk" @user.save - ws = Search.musician_search("Example User") + ws = Search.musician_search("Example User").text_results ws.length.should == 0 - ws = Search.musician_search("Bonus") + ws = Search.musician_search("Bonus").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @user.id @@ -63,7 +63,7 @@ describe User do @user2 = FactoryGirl.create(:user, first_name: "peaches", last_name: "test", email: "peach@example.com", password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true, city: "Apex", state: "NC", country: "US") - ws = Search.musician_search("pea") + ws = Search.musician_search("pea").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @user2.id @@ -73,14 +73,14 @@ describe User do @user3 = FactoryGirl.create(:user, first_name: "unconfirmed", last_name: "unconfirmed", email: "unconfirmed@example.com", password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: false, city: "Apex", state: "NC", country: "US") - ws = Search.musician_search("unconfirmed") + ws = Search.musician_search("unconfirmed").text_results ws.length.should == 1 # Ok, confirm the user, and see them show up @user3.email_confirmed = true @user3.save - ws = Search.musician_search("unconfirmed") + ws = Search.musician_search("unconfirmed").text_results ws.length.should == 1 user_result = ws[0] user_result.id.should == @user3.id diff --git a/web/app/views/api_search/index.rabl b/web/app/views/api_search/index.rabl index e7102cee9..e3dc98bb9 100644 --- a/web/app/views/api_search/index.rabl +++ b/web/app/views/api_search/index.rabl @@ -1,13 +1,13 @@ object @search -if @search.bands.present? - child(:bands => :bands) { +if @search.bands_text.present? + child(:bands => @search.text_results) { attributes :id, :name, :location, :photo_url, :logo_url } end -if @search.musicians.present? - child(:musicians => :musicians) { +if @search.musicians_text? + child(:musicians => @search.text_results) { attributes :id, :first_name, :last_name, :name, :location, :photo_url node :is_friend do |musician| @@ -88,8 +88,8 @@ if @search.bands_filter.present? } end -unless @search.fans.nil? || @search.fans.size == 0 - child(:fans => :fans) { +if @search.fans_text? + child(:fans => @search.text_results) { attributes :id, :first_name, :last_name, :name, :location, :photo_url node :is_friend do |fan| @@ -98,14 +98,3 @@ unless @search.fans.nil? || @search.fans.size == 0 } end -unless @search.recordings.nil? || @search.recordings.size == 0 - child(:recordings => :recordings) { - attributes :id, :name - } -end - -unless @search.friends.nil? || @search.friends.size == 0 - child(:friends => :friends) { - attributes :id, :first_name, :last_name, :name, :location, :email, :online, :photo_url, :musician - } -end