mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Fixed tournament search to search each term for both date and name
This commit is contained in:
@@ -221,7 +221,8 @@ class TournamentsController < ApplicationController
|
||||
|
||||
def index
|
||||
if params[:search]
|
||||
@tournaments = Tournament.limit(200).search(params[:search]).order("created_at DESC")
|
||||
# @tournaments = Tournament.limit(200).search(params[:search]).order("date DESC")
|
||||
@tournaments = Tournament.limit(200).search_date_name(params[:search]).order("date DESC")
|
||||
else
|
||||
@tournaments = Tournament.all.sort_by{|t| t.days_until_start}.first(20)
|
||||
end
|
||||
|
||||
@@ -16,9 +16,26 @@ class Tournament < ActiveRecord::Base
|
||||
Delayed::Job.where(job_owner_id: self.id)
|
||||
end
|
||||
|
||||
def self.search(search)
|
||||
where("date LIKE ? or name LIKE ?", "%#{search}%", "%#{search}%")
|
||||
end
|
||||
def self.search_date_name(pattern)
|
||||
if pattern.blank? # blank? covers both nil and empty string
|
||||
all
|
||||
else
|
||||
search_functions = []
|
||||
search_variables = []
|
||||
search_terms = pattern.split(' ').map{|word| "%#{word.downcase}%"}
|
||||
search_terms.each do |word|
|
||||
search_functions << '(LOWER(name) LIKE ? or LOWER(date) LIKE ?)'
|
||||
# add twice for both ?'s in the function above
|
||||
search_variables << word
|
||||
search_variables << word
|
||||
end
|
||||
like_patterns = search_functions.join(' and ')
|
||||
# puts "where(#{like_patterns})"
|
||||
# puts *search_variables
|
||||
# example: (LOWER(name LIKE ? or LOWER(date) LIKE ?) and (LOWER(name) LIKE ? or LOWER(date) LIKE ?), %test%, %test%, %2016%, %2016%
|
||||
where("#{like_patterns}", *search_variables)
|
||||
end
|
||||
end
|
||||
|
||||
def days_until_start
|
||||
time = (Date.today - self.date).to_i
|
||||
|
||||
@@ -27,4 +27,58 @@ class TournamentTest < ActiveSupport::TestCase
|
||||
assert tournament.weights.select{|w| w.max == weight.to_i}.count == 1
|
||||
end
|
||||
end
|
||||
|
||||
test "Tournament search_date_name returns results for all terms separately and non case sensitive" do
|
||||
tournament = Tournament.new
|
||||
tournament.name = "League Test Tournament D1"
|
||||
tournament.address = "some place"
|
||||
tournament.director = "some guy"
|
||||
tournament.director_email= "test@test.com"
|
||||
tournament.tournament_type = "Pool to bracket"
|
||||
tournament.date = "2015-12-30"
|
||||
tournament.save
|
||||
|
||||
tournament = Tournament.new
|
||||
tournament.name = "League Test Tournament D2"
|
||||
tournament.address = "some place"
|
||||
tournament.director = "some guy"
|
||||
tournament.director_email= "test@test.com"
|
||||
tournament.tournament_type = "Pool to bracket"
|
||||
tournament.date = "2015-12-30"
|
||||
tournament.save
|
||||
|
||||
tournament = Tournament.new
|
||||
tournament.name = "League Test Tournament D1"
|
||||
tournament.address = "some place"
|
||||
tournament.director = "some guy"
|
||||
tournament.director_email= "test@test.com"
|
||||
tournament.tournament_type = "Pool to bracket"
|
||||
tournament.date = "2016-12-30"
|
||||
tournament.save
|
||||
|
||||
tournament = Tournament.new
|
||||
tournament.name = "League Test Tournament D2"
|
||||
tournament.address = "some place"
|
||||
tournament.director = "some guy"
|
||||
tournament.director_email= "test@test.com"
|
||||
tournament.tournament_type = "Pool to bracket"
|
||||
tournament.date = "2016-12-30"
|
||||
tournament.save
|
||||
|
||||
tournament = Tournament.new
|
||||
tournament.name = "Test Tournament"
|
||||
tournament.address = "some place"
|
||||
tournament.director = "some guy"
|
||||
tournament.director_email= "test@test.com"
|
||||
tournament.tournament_type = "Pool to bracket"
|
||||
tournament.date = "2016-12-30"
|
||||
tournament.save
|
||||
|
||||
tournaments = Tournament.limit(200).search_date_name("league 2016").order("date DESC")
|
||||
assert tournaments.count == 2
|
||||
tournaments.each do |tournament|
|
||||
assert tournament.date.to_s.include? "2016"
|
||||
assert tournament.name.include? "League"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user