mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-12 16:25:41 +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
|
def index
|
||||||
if params[:search]
|
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
|
else
|
||||||
@tournaments = Tournament.all.sort_by{|t| t.days_until_start}.first(20)
|
@tournaments = Tournament.all.sort_by{|t| t.days_until_start}.first(20)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,9 +16,26 @@ class Tournament < ActiveRecord::Base
|
|||||||
Delayed::Job.where(job_owner_id: self.id)
|
Delayed::Job.where(job_owner_id: self.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search(search)
|
def self.search_date_name(pattern)
|
||||||
where("date LIKE ? or name LIKE ?", "%#{search}%", "%#{search}%")
|
if pattern.blank? # blank? covers both nil and empty string
|
||||||
end
|
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
|
def days_until_start
|
||||||
time = (Date.today - self.date).to_i
|
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
|
assert tournament.weights.select{|w| w.max == weight.to_i}.count == 1
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user