diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index dcb9849..98e8e51 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -80,7 +80,11 @@ class TournamentsController < ApplicationController end def index - @tournaments = Tournament.all.sort_by{|t| t.daysUntil} + if params[:search] + @tournaments = Tournament.search(params[:search]).order("created_at DESC") + else + @tournaments = Tournament.all.sort_by{|t| t.daysUntil}.first(20) + end end def show diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 35477a5..60e48b4 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -11,6 +11,10 @@ class Tournament < ActiveRecord::Base validates :date, :name, :tournament_type, :address, :director, :director_email , presence: true + def self.search(search) + where("date LIKE ? or name LIKE ?", "%#{search}%", "%#{search}%") + end + def daysUntil time = (Date.today - self.date).to_i if time < 0 diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 6b6d8f9..71977c8 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -8,8 +8,8 @@
Search by name or date YYYY-MM-DD