diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 72b3fe7..505b818 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -5,7 +5,6 @@ import "@hotwired/turbo-rails"; import { createConsumer } from "@rails/actioncable"; // Import createConsumer directly import "jquery"; import "bootstrap"; -import "datatables.net"; // Stimulus setup import { Application } from "@hotwired/stimulus"; @@ -39,9 +38,9 @@ application.register("match-spectate", MatchSpectateController); } }).call(this); -console.log("Propshaft/Importmap application.js initialized with jQuery, Bootstrap, Stimulus, and DataTables."); +console.log("Propshaft/Importmap application.js initialized with jQuery, Bootstrap, and Stimulus."); // If you have custom JavaScript files in app/javascript/ that were previously // handled by Sprockets `require_tree`, you'll need to import them here explicitly. // For example: -// import "./my_custom_logic"; \ No newline at end of file +// import "./my_custom_logic"; diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 43f74cb..76562e9 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -116,9 +116,59 @@ class TournamentsController < ApplicationController end def matches - @matches = @tournament.matches - .includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches }) - .order(:bout_number) + per_page = 50 + @page = params[:page].to_i > 0 ? params[:page].to_i : 1 + offset = (@page - 1) * per_page + matches_table = Match.arel_table + + matches_scope = @tournament.matches.order(:bout_number) + + if params[:search].present? + wrestlers_table = Wrestler.arel_table + schools_table = School.arel_table + search_terms = params[:search].downcase.split + + search_terms.each do |term| + escaped_term = ActiveRecord::Base.sanitize_sql_like(term) + pattern = "%#{escaped_term}%" + + matching_wrestler_ids = Wrestler + .joins(:weight) + .left_outer_joins(:school) + .where(weights: { tournament_id: @tournament.id }) + .where( + wrestlers_table[:name].matches(pattern) + .or(schools_table[:name].matches(pattern)) + ) + .distinct + .select(:id) + + term_scope = @tournament.matches.where(w1: matching_wrestler_ids) + .or(@tournament.matches.where(w2: matching_wrestler_ids)) + + if term.match?(/\A\d+\z/) + term_scope = term_scope.or(@tournament.matches.where(bout_number: term.to_i)) + end + + matches_scope = matches_scope.where(id: term_scope.select(:id)) + end + end + + @total_count = matches_scope.count + @total_pages = (@total_count / per_page.to_f).ceil + @per_page = per_page + + loser1_not_bye = matches_table[:loser1_name].not_eq("BYE").or(matches_table[:loser1_name].eq(nil)) + loser2_not_bye = matches_table[:loser2_name].not_eq("BYE").or(matches_table[:loser2_name].eq(nil)) + + non_bye_scope = matches_scope.where(loser1_not_bye).where(loser2_not_bye) + @matches_without_byes_count = non_bye_scope.count + @unfinished_matches_without_byes_count = non_bye_scope.where(finished: [nil, 0]).count + + @matches = matches_scope + .includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches }) + .offset(offset) + .limit(per_page) if @match @w1 = @match.wrestler1 @w2 = @match.wrestler2 diff --git a/app/views/static_pages/my_tournaments.html.erb b/app/views/static_pages/my_tournaments.html.erb index 020549d..121a3a0 100644 --- a/app/views/static_pages/my_tournaments.html.erb +++ b/app/views/static_pages/my_tournaments.html.erb @@ -1,11 +1,5 @@ <% if @tournaments.size > 0 %>

My Tournaments

- diff --git a/app/views/tournaments/matches.html.erb b/app/views/tournaments/matches.html.erb index 6067457..671c273 100644 --- a/app/views/tournaments/matches.html.erb +++ b/app/views/tournaments/matches.html.erb @@ -1,13 +1,15 @@ - -

All <%= @tournament.name %> matches

- -
+ +<% matches_path = "/tournaments/#{@tournament.id}/matches" %> + +<%= form_tag(matches_path, method: :get, id: "search-form") do %> + <%= text_field_tag :search, params[:search], placeholder: "Search wrestler, school, or bout #" %> + <%= submit_tag "Search" %> +<% end %> + +

Search by wrestler name, school name, or bout number.

+ +
@@ -35,6 +37,49 @@ <% end %>
+ +<% if @total_pages.present? && @total_pages > 1 %> + + +

+ <% start_index = ((@page - 1) * @per_page) + 1 + end_index = [@page * @per_page, @total_count].min + %> + Showing <%= start_index %> - <%= end_index %> of <%= @total_count %> matches +

+<% end %> +
-

Total matches without byes: <%= @matches.select{|m| m.loser1_name != 'BYE' and m.loser2_name != 'BYE'}.size %>

-

Unfinished matches: <%= @matches.select{|m| m.finished != 1 and m.loser1_name != 'BYE' and m.loser2_name != 'BYE'}.size %>

+

Total matches without byes: <%= @matches_without_byes_count %>

+

Unfinished matches: <%= @unfinished_matches_without_byes_count %>

diff --git a/app/views/tournaments/up_matches.html.erb b/app/views/tournaments/up_matches.html.erb index d2c6960..540388d 100644 --- a/app/views/tournaments/up_matches.html.erb +++ b/app/views/tournaments/up_matches.html.erb @@ -1,8 +1,3 @@ -