vrfs988: refactored search with one accessor for text and filter type searches

This commit is contained in:
Jonathan Kolyer 2014-01-11 09:27:56 -06:00
parent ecdfde2cdb
commit 87ae9d97df
4 changed files with 48 additions and 48 deletions

View File

@ -2,7 +2,7 @@ module JamRuby
# not a active_record model; just a search result container
class Search
attr_accessor :filter_results, :text_results, :search_type
attr_accessor :results, :search_type
attr_accessor :user_counters, :page_num, :page_count
LIMIT = 10
@ -45,16 +45,16 @@ module JamRuby
@search_type = :musicians
User.musicians
end
@text_results = rel.where("(name_tsv @@ to_tsquery('jamenglish', ?))", tsquery).limit(10)
@results = rel.where("(name_tsv @@ to_tsquery('jamenglish', ?))", tsquery).limit(10)
end
def initialize(search_results=nil)
@text_results, @filter_results = [], []
@results = []
self
end
def self.create_tsquery(query)
return nil if query.nil? || query.length == 0
return nil if query.blank?
search_terms = query.split
return nil if search_terms.length == 0
@ -147,14 +147,14 @@ module JamRuby
COUNT_SESSION = :count_session
COUNTERS = [COUNT_FRIEND, COUNT_FOLLOW, COUNT_RECORD, COUNT_SESSION]
def musician_results_for_user(results, user)
@filter_results = results
def musician_results_for_user(_results, user)
@results = _results
if user
@user_counters = results.inject({}) { |hh,val| hh[val.id] = []; hh }
mids = "'#{@filter_results.map(&:id).join("','")}'"
@user_counters = @results.inject({}) { |hh,val| hh[val.id] = []; hh }
mids = "'#{@results.map(&:id).join("','")}'"
# this gets counts for each search result on friends/follows/records/sessions
results.each do |uu|
@results.each do |uu|
counters = { }
counters[COUNT_FRIEND] = Friendship.where(:user_id => uu.id).count
counters[COUNT_FOLLOW] = UserFollowing.where(:user_id => uu.id).count
@ -295,14 +295,14 @@ module JamRuby
srch.band_results_for_user(objs, current_user)
end
def band_results_for_user(results, user)
@filter_results = results
def band_results_for_user(_results, user)
@results = _results
if user
@user_counters = results.inject({}) { |hh,val| hh[val.id] = []; hh }
mids = "'#{@filter_results.map(&:id).join("','")}'"
@user_counters = @results.inject({}) { |hh,val| hh[val.id] = []; hh }
mids = "'#{@results.map(&:id).join("','")}'"
# this gets counts for each search result
results.each do |bb|
@results.each do |bb|
counters = { }
counters[COUNT_FOLLOW] = BandFollowing.where(:band_id => bb.id).count
counters[COUNT_RECORD] = Recording.where(:band_id => bb.id).count

View File

@ -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").text_results
ws = Search.band_search("Example Band").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").text_results
ws = Search.band_search("Ex").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Exa").text_results
ws = Search.band_search("Exa").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Exam").text_results
ws = Search.band_search("Exam").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Examp").text_results
ws = Search.band_search("Examp").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Exampl").text_results
ws = Search.band_search("Exampl").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Example").text_results
ws = Search.band_search("Example").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Ba").text_results
ws = Search.band_search("Ba").results
ws.length.should == 1
ws[0].id.should == @band.id
ws = Search.band_search("Ban").text_results
ws = Search.band_search("Ban").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").text_results
ws = Search.band_search("xa").results
ws.length.should == 0
ws = Search.band_search("le").text_results
ws = Search.band_search("le").results
ws.length.should == 0
end
it "should delete band" do
ws = Search.band_search("Example Band").text_results
ws = Search.band_search("Example Band").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").text_results
ws = Search.band_search("Example Band").results
ws.length.should == 0
end
it "should update band" do
ws = Search.band_search("Example Band").text_results
ws = Search.band_search("Example Band").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").text_results
ws = Search.band_search("Example Band").results
ws.length.should == 0
ws = Search.band_search("Bonus").text_results
ws = Search.band_search("Bonus").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").text_results
ws = Search.band_search("pea").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").text_results
ws = Search.band_search("pe").results
ws.length.should == 1
user_result = ws[0]
user_result.id.should == @band2.id
ws = Search.band_search("p").text_results
ws = Search.band_search("p").results
ws.length.should == 0
end
end

View File

@ -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").text_results
ws = Search.musician_search("Example User").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).text_results
ws = Search.fan_search(uu.name).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").text_results
ws = Search.musician_search("Example User").results
ws.length.should == 1
user_result = ws[0]
user_result.id.should == @user.id
@user.destroy
ws = Search.musician_search("Example User").text_results
ws = Search.musician_search("Example User").results
ws.length.should == 0
end
it "should update user" do
ws = Search.musician_search("Example User").text_results
ws = Search.musician_search("Example User").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").text_results
ws = Search.musician_search("Example User").results
ws.length.should == 0
ws = Search.musician_search("Bonus").text_results
ws = Search.musician_search("Bonus").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").text_results
ws = Search.musician_search("pea").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").text_results
ws = Search.musician_search("unconfirmed").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").text_results
ws = Search.musician_search("unconfirmed").results
ws.length.should == 1
user_result = ws[0]
user_result.id.should == @user3.id

View File

@ -1,13 +1,13 @@
object @search
if @search.bands_text_search?
child(:text_results => :bands) {
child(:results => :bands) {
attributes :id, :name, :location, :photo_url, :logo_url
}
end
if @search.musicians_text_search?
child(:text_results => :musicians) {
child(:results => :musicians) {
attributes :id, :first_name, :last_name, :name, :location, :photo_url
node :is_friend do |musician|
@ -30,7 +30,7 @@ if @search.musicians_filter_search?
@search.page_count
end
child(:filter_results => :musicians) {
child(:results => :musicians) {
attributes :id, :first_name, :last_name, :name, :city, :state, :country, :email, :online, :musician, :photo_url, :biography
node :is_friend do |musician|
@ -64,7 +64,7 @@ if @search.bands_filter_search?
@search.page_count
end
child(:filter_results => :bands) {
child(:results => :bands) {
attributes :id, :name, :city, :state, :country, :email, :photo_url, :biography, :logo_url
node :is_following do |band|
@ -89,7 +89,7 @@ if @search.bands_filter_search?
end
if @search.fans_text_search?
child(:text_results => :fans) {
child(:results => :fans) {
attributes :id, :first_name, :last_name, :name, :location, :photo_url
node :is_friend do |fan|