1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-07 14:57:48 +00:00

Fixed a number of N+1 issues on low traffic pages. I also added relevant html tests for these pages.

This commit is contained in:
2026-02-17 22:27:11 -05:00
parent d359be3ea1
commit 8670ce38c3
16 changed files with 330 additions and 103 deletions

View File

@@ -4,7 +4,7 @@ class MatAssignmentRulesController < ApplicationController
before_action :set_mat_assignment_rule, only: [:edit, :update, :destroy]
def index
@mat_assignment_rules = @tournament.mat_assignment_rules
@mat_assignment_rules = @tournament.mat_assignment_rules.includes(:mat)
@weights_by_id = @tournament.weights.index_by(&:id) # For quick lookup
end

View File

@@ -1,11 +1,11 @@
class StaticPagesController < ApplicationController
def my_tournaments
tournaments_created = current_user.tournaments
tournaments_delegated = current_user.delegated_tournaments
tournaments_created = current_user.tournaments.to_a
tournaments_delegated = current_user.delegated_tournaments.to_a
all_tournaments = tournaments_created + tournaments_delegated
@tournaments = all_tournaments.sort_by{|t| t.days_until_start}
@schools = current_user.delegated_schools
@schools = current_user.delegated_schools.includes(:tournament)
end
def not_allowed

View File

@@ -7,7 +7,7 @@ class TournamentsController < ApplicationController
before_action :check_access_read, only: [:all_results,:up_matches,:bracket,:all_brackets]
def weigh_in_sheet
@schools = @tournament.schools.includes(wrestlers: :weight)
end
def calculate_team_scores
@@ -92,12 +92,9 @@ class TournamentsController < ApplicationController
end
end
end
@users_delegates = []
@tournament.schools.each do |s|
s.delegates.each do |d|
@users_delegates << d
end
end
@users_delegates = SchoolDelegate.includes(:user, :school)
.joins(:school)
.where(schools: { tournament_id: @tournament.id })
end
def delegate
@@ -115,11 +112,13 @@ class TournamentsController < ApplicationController
end
end
end
@users_delegates = @tournament.delegates
@users_delegates = @tournament.delegates.includes(:user)
end
def matches
@matches = @tournament.matches.includes(:wrestlers,:schools).sort_by{|m| m.bout_number}
@matches = @tournament.matches
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
.order(:bout_number)
if @match
@w1 = @match.wrestler1
@w2 = @match.wrestler2
@@ -129,10 +128,18 @@ class TournamentsController < ApplicationController
def weigh_in_weight
if params[:wrestler]
Wrestler.update(params[:wrestler].keys, params[:wrestler].values)
sanitized_wrestlers = params.require(:wrestler).to_unsafe_h.each_with_object({}) do |(wrestler_id, attributes), result|
permitted = ActionController::Parameters.new(attributes).permit(:offical_weight)
result[wrestler_id] = permitted
end
Wrestler.update(sanitized_wrestlers.keys, sanitized_wrestlers.values) if sanitized_wrestlers.present?
redirect_to "/tournaments/#{@tournament.id}/weigh_in/#{params[:weight]}", notice: "Weights were successfully recorded."
return
end
if params[:weight]
@weight = Weight.where(:id => params[:weight]).includes(:wrestlers).first
@weight = Weight.where(id: params[:weight])
.includes(wrestlers: [:school, :weight])
.first
@tournament_id = @tournament.id
@tournament_name = @tournament.name
@weights = @tournament.weights
@@ -159,8 +166,11 @@ class TournamentsController < ApplicationController
def all_brackets
@schools = @tournament.schools
@schools = @schools.sort_by{|s| s.page_score_string}.reverse!
@matches = @tournament.matches.includes(:wrestlers,:schools)
@weights = @tournament.weights.includes(:matches,:wrestlers)
@weights = @tournament.weights.includes(:matches, wrestlers: :school)
all_matches = @tournament.matches.includes(:weight, { wrestler1: :school }, { wrestler2: :school })
all_wrestlers = @tournament.wrestlers.includes(:school, :weight)
@matches_by_weight_id = all_matches.group_by(&:weight_id)
@wrestlers_by_weight_id = all_wrestlers.group_by(&:weight_id)
end
def bracket
@@ -210,18 +220,31 @@ class TournamentsController < ApplicationController
.where("loser1_name != ? OR loser1_name IS NULL", "BYE")
.where("loser2_name != ? OR loser2_name IS NULL", "BYE")
.order('bout_number ASC')
.limit(10).includes(:wrestlers)
.limit(10)
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
@mats = @tournament.mats.includes(:matches)
end
def bout_sheets
matches_scope = @tournament.matches
.where("loser1_name != ? OR loser1_name IS NULL", "BYE")
.where("loser2_name != ? OR loser2_name IS NULL", "BYE")
if params[:round]
round = params[:round]
if round != "All"
@matches = @tournament.matches.where("round = ?",round).sort_by{|match| match.bout_number}
@matches = matches_scope
.where(round: round)
.includes(:weight)
.order(:bout_number)
else
@matches = @tournament.matches.sort_by{|match| match.bout_number}
@matches = matches_scope
.includes(:weight)
.order(:bout_number)
end
wrestler_ids = @matches.flat_map { |match| [match.w1, match.w2] }.compact.uniq
@wrestlers_by_id = Wrestler.includes(:school).where(id: wrestler_ids).index_by(&:id)
end
end

View File

@@ -96,7 +96,9 @@ class Mat < ApplicationRecord
@queue_matches = if ids.empty?
[nil, nil, nil, nil]
else
matches_by_id = Match.where(id: ids).index_by(&:id)
matches_by_id = Match.where(id: ids)
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
.index_by(&:id)
slot_ids.map { |match_id| match_id ? matches_by_id[match_id] : nil }
end
@queue_match_slot_ids = slot_ids

View File

@@ -5,6 +5,8 @@ class Match < ApplicationRecord
belongs_to :weight, touch: true
belongs_to :mat, touch: true, optional: true
belongs_to :winner, class_name: 'Wrestler', foreign_key: 'winner_id', optional: true
belongs_to :wrestler1, class_name: 'Wrestler', foreign_key: 'w1', optional: true
belongs_to :wrestler2, class_name: 'Wrestler', foreign_key: 'w2', optional: true
has_many :wrestlers, :through => :weight
has_many :schools, :through => :wrestlers
validate :score_validation, :win_type_validation, :bracket_position_validation, :overtime_type_validation
@@ -178,14 +180,6 @@ class Match < ApplicationRecord
end
end
def wrestler1
wrestlers.select{|w| w.id == self.w1}.first
end
def wrestler2
wrestlers.select{|w| w.id == self.w2}.first
end
def w1_name
if self.w1 != nil
wrestler1.name
@@ -203,7 +197,7 @@ class Match < ApplicationRecord
end
def w1_bracket_name
first_round = self.weight.matches.sort_by{|m| m.round}.first.round
first_round = first_round_for_weight
return_string = ""
return_string_ending = ""
if self.w1 and self.winner_id == self.w1
@@ -223,7 +217,7 @@ class Match < ApplicationRecord
end
def w2_bracket_name
first_round = self.weight.matches.sort_by{|m| m.round}.first.round
first_round = first_round_for_weight
return_string = ""
return_string_ending = ""
if self.w2 and self.winner_id == self.w2
@@ -289,6 +283,17 @@ class Match < ApplicationRecord
self.weight.max
end
def first_round_for_weight
return @first_round_for_weight if defined?(@first_round_for_weight)
@first_round_for_weight =
if association(:weight).loaded? && self.weight&.association(:matches)&.loaded?
self.weight.matches.map(&:round).compact.min
else
Match.where(weight_id: self.weight_id).minimum(:round)
end
end
def replace_loser_name_with_wrestler(w,loser_name)
if self.loser1_name == loser_name
self.w1 = w.id

View File

@@ -97,18 +97,11 @@ class Tournament < ApplicationRecord
end
def pointAdjustments
point_adjustments = []
self.schools.each do |s|
s.deductedPoints.each do |d|
point_adjustments << d
end
end
self.wrestlers.each do |w|
w.deductedPoints.each do |d|
point_adjustments << d
end
end
point_adjustments
school_scope = Teampointadjust.where(school_id: schools.select(:id))
wrestler_scope = Teampointadjust.where(wrestler_id: wrestlers.select(:id))
Teampointadjust.includes(:school, :wrestler)
.merge(school_scope.or(wrestler_scope))
end
def remove_school_delegations

View File

@@ -53,19 +53,16 @@ class User < ApplicationRecord
end
def delegated_tournaments
tournaments_delegated = []
delegated_tournament_permissions.each do |t|
tournaments_delegated << t.tournament
end
tournaments_delegated
Tournament.joins(:delegates)
.where(tournament_delegates: { user_id: id })
.distinct
end
def delegated_schools
schools_delegated = []
delegated_school_permissions.each do |t|
schools_delegated << t.school
end
schools_delegated
School.joins(:delegates)
.where(school_delegates: { user_id: id })
.includes(:tournament)
.distinct
end
def self.search(search)

View File

@@ -95,8 +95,6 @@ table.smallText tr td { font-size: 10px; }
<table class='smallText'>
<tr>
<td valign="top" style="padding: 10px;">
<% @matches = @tournament.matches.select{|m| m.weight_id == @weight.id} %>
<% @wrestlers = Wrestler.where(weight_id: @weight.id) %>
<% @pools = @weight.pool_rounds(@matches) %>
<%= render 'pool' %>
</td>

View File

@@ -119,16 +119,17 @@
<% @weights.sort_by{|w| w.max}.each do |weight| %>
<% if @tournament.tournament_type == "Pool to bracket" %>
<!-- Need to define what the tournaments#bracket controller defines -->
<% @matches = @tournament.matches.select{|m| m.weight_id == weight.id} %>
<% @wrestlers = Wrestler.where(weight_id: weight.id) %>
<% @matches = @matches_by_weight_id[weight.id] || [] %>
<% @wrestlers = @wrestlers_by_weight_id[weight.id] || [] %>
<% @pools = weight.pool_rounds(@matches) %>
<% @weight = weight %>
<%= render 'bracket_partial' %>
<% elsif @tournament.tournament_type.include? "Modified 16 Man Double Elimination" or @tournament.tournament_type.include? "Regular Double Elimination" %>
<!-- Need to define what the tournaments#bracket controller defines -->
<% @matches = weight.matches %>
<% @matches = @matches_by_weight_id[weight.id] || [] %>
<% @wrestlers = @wrestlers_by_weight_id[weight.id] || [] %>
<% @weight = weight %>
<%= render 'bracket_partial' %>
<% end %>
<% end %>
<% end %>
<% end %>

View File

@@ -54,28 +54,28 @@
}
</style>
<% @matches.each do |match| %>
<% if match.w1 && match.w2 %>
<% w1 = Wrestler.find(match.w1) %>
<% w2 = Wrestler.find(match.w2) %>
<% end %>
<div class="pagebreak">
<p><strong>Bout Number:</strong> <%= match.bout_number %> <strong>Weight Class:</strong> <%= match.weight.max %> <strong>Round:</strong> <%= match.round %> <strong>Bracket Position:</strong> <%= match.bracket_position %></p>
<p><strong>Key: </strong>Takedown: T3, Escape: E1, Reversal: R2, Nearfall: N2 or N3 or N4, Stalling: S, Caution: C, Penalty Point: P1</p>
<% @matches.each do |match| %>
<% w1 = @wrestlers_by_id[match.w1] %>
<% w2 = @wrestlers_by_id[match.w2] %>
<% w1_name = w1&.name || match.loser1_name %>
<% w2_name = w2&.name || match.loser2_name %>
<div class="pagebreak">
<p><strong>Bout Number:</strong> <%= match.bout_number %> <strong>Weight Class:</strong> <%= match.weight.max %> <strong>Round:</strong> <%= match.round %> <strong>Bracket Position:</strong> <%= match.bracket_position %></p>
<p><strong>Key: </strong>Takedown: T3, Escape: E1, Reversal: R2, Nearfall: N2 or N3 or N4, Stalling: S, Caution: C, Penalty Point: P1</p>
<table>
<thead>
<tr class="small-row">
<th class="fixed-width">Circle Winner</th>
<th>
<p><%= match.w1_name %>-<%= w1&.school&.name %></p>
</th>
<th>
<p><%= match.w2_name %>-<%= w2&.school&.name %></p>
</th>
</tr>
</thead>
<th class="fixed-width">Circle Winner</th>
<th>
<p><%= w1_name %>-<%= w1&.school&.name %></p>
</th>
<th>
<p><%= w2_name %>-<%= w2&.school&.name %></p>
</th>
</tr>
</thead>
<tbody>
<tr class="small-row">
<td class="fixed-width"></td>

View File

@@ -1,6 +1,12 @@
{
"tournament": {
"attributes": <%= @tournament.attributes.to_json %>,
<%
wrestlers_by_id = @tournament.wrestlers.index_by(&:id)
weights_by_id = @tournament.weights.index_by(&:id)
mats_by_id = @tournament.mats.index_by(&:id)
sorted_matches = @tournament.matches.sort_by(&:bout_number)
%>
{
"tournament": {
"attributes": <%= @tournament.attributes.to_json %>,
"schools": <%= @tournament.schools.map(&:attributes).to_json %>,
"weights": <%= @tournament.weights.map(&:attributes).to_json %>,
"mats": <%= @tournament.mats.map { |mat| mat.attributes.merge(
@@ -14,14 +20,14 @@
"weight": wrestler.weight&.attributes
}
) }.to_json %>,
"matches": <%= @tournament.matches.sort_by(&:bout_number).map { |match| match.attributes.merge(
{
"w1_name": Wrestler.find_by(id: match.w1)&.name,
"w2_name": Wrestler.find_by(id: match.w2)&.name,
"winner_name": Wrestler.find_by(id: match.winner_id)&.name,
"weight": Weight.find_by(id: match.weight_id)&.attributes,
"mat": Mat.find_by(id: match.mat_id)&.attributes
}
) }.to_json %>
}
"matches": <%= sorted_matches.map { |match| match.attributes.merge(
{
"w1_name": wrestlers_by_id[match.w1]&.name,
"w2_name": wrestlers_by_id[match.w2]&.name,
"winner_name": wrestlers_by_id[match.winner_id]&.name,
"weight": weights_by_id[match.weight_id]&.attributes,
"mat": mats_by_id[match.mat_id]&.attributes
}
) }.to_json %>
}
}

View File

@@ -12,9 +12,10 @@
height: 1in;
}
</style>
<% @tournament.schools.each do |school| %>
<% @schools.each do |school| %>
<table class="table table-striped table-bordered table-condensed pagebreak">
<h5><%= school.name %></h4>
<p><strong>Weigh In Ref:</strong> <%= @tournament.weigh_in_ref %></p>
<thead>
<tr>
<th>Name</th>
@@ -27,9 +28,9 @@
<tr>
<td><%= wrestler.name %></td>
<td><%= wrestler.weight.max %></td>
<td></td>
<td><%= wrestler.offical_weight %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %>

View File

@@ -1,4 +1,4 @@
<table class="table table-striped table-bordered">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
@@ -9,19 +9,19 @@
</tr>
</thead>
<tbody>
<%= form_tag @wrestlers_update_path do %>
<% @wrestlers.order("original_seed asc").each do |wrestler| %>
<% if wrestler.weight_id == @weight.id %>
<tr>
<%= form_tag "/tournaments/#{@tournament.id}/weigh_in/#{@weight.id}", method: :post do %>
<% @wrestlers.order("original_seed asc").each do |wrestler| %>
<% if wrestler.weight_id == @weight.id %>
<tr>
<td><%= wrestler.name %></td>
<td><%= wrestler.school.name %></td>
<td><%= wrestler.original_seed %></td>
<td><%= wrestler.weight.max %></td>
<td>
<% if user_signed_in? %>
<%= fields_for "wrestler[]", wrestler do |w| %>
<%= w.number_field :offical_weight, :step => 'any' %>
<% end %>
<% if user_signed_in? %>
<%= fields_for "wrestler[#{wrestler.id}]", wrestler do |w| %>
<%= w.number_field :offical_weight, :step => 'any' %>
<% end %>
<% else %>
<%= wrestler.offical_weight %>
<% end %>