1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-24 17:04:43 +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] before_action :set_mat_assignment_rule, only: [:edit, :update, :destroy]
def index 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 @weights_by_id = @tournament.weights.index_by(&:id) # For quick lookup
end end

View File

@@ -1,11 +1,11 @@
class StaticPagesController < ApplicationController class StaticPagesController < ApplicationController
def my_tournaments def my_tournaments
tournaments_created = current_user.tournaments tournaments_created = current_user.tournaments.to_a
tournaments_delegated = current_user.delegated_tournaments tournaments_delegated = current_user.delegated_tournaments.to_a
all_tournaments = tournaments_created + tournaments_delegated all_tournaments = tournaments_created + tournaments_delegated
@tournaments = all_tournaments.sort_by{|t| t.days_until_start} @tournaments = all_tournaments.sort_by{|t| t.days_until_start}
@schools = current_user.delegated_schools @schools = current_user.delegated_schools.includes(:tournament)
end end
def not_allowed 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] before_action :check_access_read, only: [:all_results,:up_matches,:bracket,:all_brackets]
def weigh_in_sheet def weigh_in_sheet
@schools = @tournament.schools.includes(wrestlers: :weight)
end end
def calculate_team_scores def calculate_team_scores
@@ -92,12 +92,9 @@ class TournamentsController < ApplicationController
end end
end end
end end
@users_delegates = [] @users_delegates = SchoolDelegate.includes(:user, :school)
@tournament.schools.each do |s| .joins(:school)
s.delegates.each do |d| .where(schools: { tournament_id: @tournament.id })
@users_delegates << d
end
end
end end
def delegate def delegate
@@ -115,11 +112,13 @@ class TournamentsController < ApplicationController
end end
end end
end end
@users_delegates = @tournament.delegates @users_delegates = @tournament.delegates.includes(:user)
end end
def matches 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 if @match
@w1 = @match.wrestler1 @w1 = @match.wrestler1
@w2 = @match.wrestler2 @w2 = @match.wrestler2
@@ -129,10 +128,18 @@ class TournamentsController < ApplicationController
def weigh_in_weight def weigh_in_weight
if params[:wrestler] 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 end
if params[:weight] 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_id = @tournament.id
@tournament_name = @tournament.name @tournament_name = @tournament.name
@weights = @tournament.weights @weights = @tournament.weights
@@ -159,8 +166,11 @@ class TournamentsController < ApplicationController
def all_brackets def all_brackets
@schools = @tournament.schools @schools = @tournament.schools
@schools = @schools.sort_by{|s| s.page_score_string}.reverse! @schools = @schools.sort_by{|s| s.page_score_string}.reverse!
@matches = @tournament.matches.includes(:wrestlers,:schools) @weights = @tournament.weights.includes(:matches, wrestlers: :school)
@weights = @tournament.weights.includes(:matches,:wrestlers) 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 end
def bracket def bracket
@@ -210,18 +220,31 @@ class TournamentsController < ApplicationController
.where("loser1_name != ? OR loser1_name IS NULL", "BYE") .where("loser1_name != ? OR loser1_name IS NULL", "BYE")
.where("loser2_name != ? OR loser2_name IS NULL", "BYE") .where("loser2_name != ? OR loser2_name IS NULL", "BYE")
.order('bout_number ASC') .order('bout_number ASC')
.limit(10).includes(:wrestlers) .limit(10)
.includes({ wrestler1: :school }, { wrestler2: :school }, { weight: :matches })
@mats = @tournament.mats.includes(:matches) @mats = @tournament.mats.includes(:matches)
end end
def bout_sheets 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] if params[:round]
round = params[:round] round = params[:round]
if round != "All" 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 else
@matches = @tournament.matches.sort_by{|match| match.bout_number} @matches = matches_scope
.includes(:weight)
.order(:bout_number)
end 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
end end

View File

@@ -96,7 +96,9 @@ class Mat < ApplicationRecord
@queue_matches = if ids.empty? @queue_matches = if ids.empty?
[nil, nil, nil, nil] [nil, nil, nil, nil]
else 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 } slot_ids.map { |match_id| match_id ? matches_by_id[match_id] : nil }
end end
@queue_match_slot_ids = slot_ids @queue_match_slot_ids = slot_ids

View File

@@ -5,6 +5,8 @@ class Match < ApplicationRecord
belongs_to :weight, touch: true belongs_to :weight, touch: true
belongs_to :mat, touch: true, optional: true belongs_to :mat, touch: true, optional: true
belongs_to :winner, class_name: 'Wrestler', foreign_key: 'winner_id', 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 :wrestlers, :through => :weight
has_many :schools, :through => :wrestlers has_many :schools, :through => :wrestlers
validate :score_validation, :win_type_validation, :bracket_position_validation, :overtime_type_validation validate :score_validation, :win_type_validation, :bracket_position_validation, :overtime_type_validation
@@ -178,14 +180,6 @@ class Match < ApplicationRecord
end end
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 def w1_name
if self.w1 != nil if self.w1 != nil
wrestler1.name wrestler1.name
@@ -203,7 +197,7 @@ class Match < ApplicationRecord
end end
def w1_bracket_name 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 = ""
return_string_ending = "" return_string_ending = ""
if self.w1 and self.winner_id == self.w1 if self.w1 and self.winner_id == self.w1
@@ -223,7 +217,7 @@ class Match < ApplicationRecord
end end
def w2_bracket_name 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 = ""
return_string_ending = "" return_string_ending = ""
if self.w2 and self.winner_id == self.w2 if self.w2 and self.winner_id == self.w2
@@ -289,6 +283,17 @@ class Match < ApplicationRecord
self.weight.max self.weight.max
end 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) def replace_loser_name_with_wrestler(w,loser_name)
if self.loser1_name == loser_name if self.loser1_name == loser_name
self.w1 = w.id self.w1 = w.id

View File

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

View File

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

View File

@@ -95,8 +95,6 @@ table.smallText tr td { font-size: 10px; }
<table class='smallText'> <table class='smallText'>
<tr> <tr>
<td valign="top" style="padding: 10px;"> <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) %> <% @pools = @weight.pool_rounds(@matches) %>
<%= render 'pool' %> <%= render 'pool' %>
</td> </td>

View File

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

View File

@@ -54,28 +54,28 @@
} }
</style> </style>
<% @matches.each do |match| %> <% @matches.each do |match| %>
<% if match.w1 && match.w2 %> <% w1 = @wrestlers_by_id[match.w1] %>
<% w1 = Wrestler.find(match.w1) %> <% w2 = @wrestlers_by_id[match.w2] %>
<% w2 = Wrestler.find(match.w2) %> <% w1_name = w1&.name || match.loser1_name %>
<% end %> <% w2_name = w2&.name || match.loser2_name %>
<div class="pagebreak"> <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>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> <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> <table>
<thead> <thead>
<tr class="small-row"> <tr class="small-row">
<th class="fixed-width">Circle Winner</th> <th class="fixed-width">Circle Winner</th>
<th> <th>
<p><%= match.w1_name %>-<%= w1&.school&.name %></p> <p><%= w1_name %>-<%= w1&.school&.name %></p>
</th> </th>
<th> <th>
<p><%= match.w2_name %>-<%= w2&.school&.name %></p> <p><%= w2_name %>-<%= w2&.school&.name %></p>
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr class="small-row"> <tr class="small-row">
<td class="fixed-width"></td> <td class="fixed-width"></td>

View File

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

View File

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

View File

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

View File

@@ -215,5 +215,21 @@ class MatAssignmentRulesControllerTest < ActionController::TestCase
assert_equal [1, 2, 3], rule.weight_classes assert_equal [1, 2, 3], rule.weight_classes
assert_equal ['A1', 'B2'], rule.bracket_positions assert_equal ['A1', 'B2'], rule.bracket_positions
assert_equal [1, 2], rule.rounds assert_equal [1, 2], rule.rounds
end end
test "index lists created mat assignment rule once in html" do
sign_in_owner
unique_mat = Mat.create!(name: "Unique Mat #{SecureRandom.hex(4)}", tournament_id: @tournament.id)
MatAssignmentRule.create!(
mat_id: unique_mat.id,
tournament_id: @tournament.id,
weight_classes: [1],
bracket_positions: ['1/2'],
rounds: [2]
)
index
assert_response :success
assert_equal 1, response.body.scan(unique_mat.name).size
end
end end

View File

@@ -36,4 +36,41 @@ class StaticPagesControllerTest < ActionController::TestCase
get :my_tournaments get :my_tournaments
success success
end end
test "my_tournaments page lists delegated tournament and delegated school once in html" do
user = users(:two)
sign_in_non_owner
delegated_tournament = Tournament.create!(
name: "Delegated Tournament #{SecureRandom.hex(4)}",
address: "123 Delegate St",
director: "Director",
director_email: "delegate_tournament_#{SecureRandom.hex(4)}@example.com",
tournament_type: "Pool to bracket",
date: Date.today,
is_public: true
)
TournamentDelegate.create!(tournament_id: delegated_tournament.id, user_id: user.id)
school_tournament = Tournament.create!(
name: "School Tournament #{SecureRandom.hex(4)}",
address: "456 School St",
director: "Director",
director_email: "delegate_school_#{SecureRandom.hex(4)}@example.com",
tournament_type: "Pool to bracket",
date: Date.today + 1,
is_public: true
)
delegated_school = School.create!(
name: "Delegated School #{SecureRandom.hex(4)}",
tournament_id: school_tournament.id
)
SchoolDelegate.create!(school_id: delegated_school.id, user_id: user.id)
get :my_tournaments
assert_response :success
assert_equal 1, response.body.scan(delegated_tournament.name).size
assert_equal 1, response.body.scan(delegated_school.name).size
assert_equal 1, response.body.scan(school_tournament.name).size
end
end end

View File

@@ -117,6 +117,29 @@ class TournamentsControllerTest < ActionController::TestCase
sign_in_owner sign_in_owner
get :weigh_in, params: { id: 1 } get :weigh_in, params: { id: 1 }
success success
assert_not_includes response.body, "Weights were successfully recorded."
end
test "printable weigh in sheet includes wrestler name school weight class and actual weight" do
sign_in_owner
@tournament.update!(weigh_in_ref: "Ref Smith")
wrestler = @tournament.weights.first.wrestlers.first
wrestler.update!(
name: "Printable Test Wrestler",
offical_weight: 106.4
)
school = wrestler.school
get :weigh_in_sheet, params: { id: @tournament.id, print: true }
assert_response :success
assert_includes response.body, school.name
assert_includes response.body, "Printable Test Wrestler"
assert_includes response.body, wrestler.weight.max.to_s
assert_includes response.body, "106.4"
assert_includes response.body, "Actual Weight"
assert_includes response.body, "Weigh In Ref:"
assert_includes response.body, "Ref Smith"
end end
test "logged in non tournament owner cannot access weigh_ins" do test "logged in non tournament owner cannot access weigh_ins" do
@@ -155,6 +178,27 @@ class TournamentsControllerTest < ActionController::TestCase
success success
end end
test "logged in tournament owner can save wrestler actual weight on weigh in weight page" do
sign_in_owner
wrestler = @tournament.weights.first.wrestlers.first
post :weigh_in_weight, params: {
id: @tournament.id,
weight: wrestler.weight_id,
wrestler: {
wrestler.id.to_s => { offical_weight: "108.2" }
}
}
assert_redirected_to "/tournaments/#{@tournament.id}/weigh_in/#{wrestler.weight_id}"
assert_equal "Weights were successfully recorded.", flash[:notice]
assert_equal 108.2, wrestler.reload.offical_weight.to_f
get :weigh_in_weight, params: { id: @tournament.id, weight: wrestler.weight_id }
assert_response :success
assert_equal 1, response.body.scan("Weights were successfully recorded.").size
end
test "logged in non tournament owner cannot access post weigh_in_weight" do test "logged in non tournament owner cannot access post weigh_in_weight" do
sign_in_non_owner sign_in_non_owner
post :weigh_in_weight, params: { id: 1, weight: 1, wrestler: @wrestlers } post :weigh_in_weight, params: { id: 1, weight: 1, wrestler: @wrestlers }
@@ -685,6 +729,33 @@ class TournamentsControllerTest < ActionController::TestCase
get :school_delegate, params: { id: 1 } get :school_delegate, params: { id: 1 }
success success
end end
test "delegate page renders created tournament delegate in html" do
user = User.create!(
email: "tournament_delegate_render_#{SecureRandom.hex(4)}@example.com",
password: "password"
)
TournamentDelegate.create!(tournament_id: @tournament.id, user_id: user.id)
sign_in_owner
get :delegate, params: { id: @tournament.id }
assert_response :success
assert_includes response.body, user.email
end
test "school_delegate page renders created school delegate in html" do
user = User.create!(
email: "school_delegate_render_#{SecureRandom.hex(4)}@example.com",
password: "password"
)
SchoolDelegate.create!(school_id: @school.id, user_id: user.id)
sign_in_owner
get :school_delegate, params: { id: @tournament.id }
assert_response :success
assert_includes response.body, user.email
assert_includes response.body, @school.name
end
test 'logged in tournament owner can delete a school delegate' do test 'logged in tournament owner can delete a school delegate' do
sign_in_owner sign_in_owner
@@ -721,6 +792,16 @@ class TournamentsControllerTest < ActionController::TestCase
get :teampointadjust, params: { id: 1 } get :teampointadjust, params: { id: 1 }
success success
end end
test "teampointadjust page lists created point deduction once in html" do
sign_in_owner
school = School.create!(name: "Point Deduction School #{SecureRandom.hex(3)}", tournament_id: @tournament.id)
adjustment = Teampointadjust.create!(school_id: school.id, points: 9876.5)
get :teampointadjust, params: { id: @tournament.id }
assert_response :success
assert_equal 1, response.body.scan(adjustment.points.to_s).size
end
test 'logged in tournament delegate cannot adjust team points' do test 'logged in tournament delegate cannot adjust team points' do
sign_in_school_delegate sign_in_school_delegate
@@ -949,6 +1030,25 @@ class TournamentsControllerTest < ActionController::TestCase
assert_redirected_to school_delegate_path(@tournament) assert_redirected_to school_delegate_path(@tournament)
assert_equal "School permission keys generated successfully.", flash[:notice] assert_equal "School permission keys generated successfully.", flash[:notice]
end end
test "generated school permission keys are displayed on school delegate page" do
sign_in_owner
post :generate_school_keys, params: { id: @tournament.id }
assert_redirected_to school_delegate_path(@tournament)
@tournament.schools.reload.each do |school|
assert_not_nil school.permission_key, "Expected permission key for school #{school.id}"
assert_not_empty school.permission_key, "Expected non-empty permission key for school #{school.id}"
end
get :school_delegate, params: { id: @tournament.id }
assert_response :success
@tournament.schools.each do |school|
expected_link_fragment = "/schools/#{school.id}?school_permission_key=#{school.permission_key}"
assert_includes response.body, expected_link_fragment
end
end
test "tournament delegate can delete school keys" do test "tournament delegate can delete school keys" do
sign_in_delegate sign_in_delegate
@@ -1180,4 +1280,52 @@ class TournamentsControllerTest < ActionController::TestCase
expected_page2_display = [expected_page2_size, 20].min expected_page2_display = [expected_page2_size, 20].min
assert_equal expected_page2_display, assigns(:tournaments).size, "second page should contain the remaining tournaments (or up to per_page)" assert_equal expected_page2_display, assigns(:tournaments).size, "second page should contain the remaining tournaments (or up to per_page)"
end end
test "bout_sheets renders wrestler names, school names, and round for selected round" do
tournament = create_double_elim_tournament_single_weight(8, "Regular Double Elimination 1-6")
tournament.update!(user_id: users(:one).id, is_public: true)
sign_in_owner
match = tournament.matches.where.not(w1: nil, w2: nil)
.where("loser1_name != ? OR loser1_name IS NULL", "BYE")
.where("loser2_name != ? OR loser2_name IS NULL", "BYE")
.order(:bout_number)
.first
assert_not_nil match, "Expected at least one fully populated non-BYE match"
round = match.round.to_s
w1 = Wrestler.find(match.w1)
w2 = Wrestler.find(match.w2)
get :bout_sheets, params: { id: tournament.id, round: round }
assert_response :success
assert_includes response.body, "Bout Number:</strong> #{match.bout_number}"
assert_includes response.body, "Round:</strong> #{match.round}"
assert_includes response.body, "#{w1.name}-#{w1.school.name}"
assert_includes response.body, "#{w2.name}-#{w2.school.name}"
end
test "bout_sheets filters out matches with BYE loser names" do
tournament = create_double_elim_tournament_single_weight(8, "Regular Double Elimination 1-6")
tournament.update!(user_id: users(:one).id, is_public: true)
sign_in_owner
bye_match = tournament.matches.order(:bout_number).first
assert_not_nil bye_match, "Expected at least one match to mark as BYE"
bye_match.update!(loser1_name: "BYE")
non_bye_match = tournament.matches.where.not(id: bye_match.id).where.not(w1: nil, w2: nil)
.where("loser1_name != ? OR loser1_name IS NULL", "BYE")
.where("loser2_name != ? OR loser2_name IS NULL", "BYE")
.order(:bout_number)
.first
assert_not_nil non_bye_match, "Expected at least one non-BYE match to remain"
get :bout_sheets, params: { id: tournament.id, round: "All" }
assert_response :success
assert_not_includes response.body, "Bout Number:</strong> #{bye_match.bout_number}"
assert_includes response.body, "Bout Number:</strong> #{non_bye_match.bout_number}"
end
end end