1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-24 17:04:43 +00:00

Added a next bout number to the mat show page on the match stats

This commit is contained in:
2025-01-20 11:41:07 -05:00
parent cc62e1c2f1
commit 60814fdd94
4 changed files with 76 additions and 4 deletions

View File

@@ -22,6 +22,7 @@ class MatchesController < ApplicationController
end
def stat
# @show_next_bout_button = false
if params[:match]
@match = Match.where(:id => params[:match]).includes(:wrestlers).first
end
@@ -60,7 +61,9 @@ class MatchesController < ApplicationController
respond_to do |format|
if @match.update(match_params)
if session[:return_path]
format.html { redirect_to session.delete(:return_path), notice: 'Match was successfully updated.' }
sanitized_return_path = sanitize_return_path(session[:return_path])
format.html { redirect_to sanitized_return_path, notice: 'Match was successfully updated.' }
session.delete(:return_path) # Remove the session variable
else
format.html { redirect_to "/tournaments/#{@match.tournament.id}", notice: 'Match was successfully updated.' }
end
@@ -92,4 +95,12 @@ class MatchesController < ApplicationController
def check_access
authorize! :manage, @match.tournament
end
def sanitize_return_path(path)
uri = URI.parse(path)
params = Rack::Utils.parse_nested_query(uri.query)
params.delete("bout_number") # Remove the bout_number param
uri.query = params.to_query.presence # Rebuild the query string or set it to nil if empty
uri.to_s # Return the full path as a string
end
end

View File

@@ -6,7 +6,18 @@ class MatsController < ApplicationController
# GET /mats/1
# GET /mats/1.json
def show
bout_number_param = params[:bout_number] # Read the bout_number from the URL params
if bout_number_param
@show_next_bout_button = false
@match = @mat.unfinished_matches.find { |m| m.bout_number == bout_number_param.to_i }
else
@show_next_bout_button = true
@match = @mat.unfinished_matches.first
end
@next_match = @mat.unfinished_matches.second # Second unfinished match on the mat
@wrestlers = []
if @match
if @match.w1
@@ -19,6 +30,7 @@ class MatsController < ApplicationController
@wrestler1_school_name = "N/A"
@wrestler1_last_match = nil
end
if @match.w2
@wrestler2_name = @match.wrestler2.name
@wrestler2_school_name = @match.wrestler2.school.name
@@ -29,8 +41,10 @@ class MatsController < ApplicationController
@wrestler2_school_name = "N/A"
@wrestler2_last_match = nil
end
@tournament = @match.tournament
end
session[:return_path] = request.original_fullpath
session[:error_return_path] = request.original_fullpath
end

View File

@@ -11,6 +11,9 @@
</div>
<% end %>
<h4>Bout <strong><%= @match.bout_number %></strong></h4>
<% if @show_next_bout_button && @next_match %>
<%= link_to "Skip to Next Match for Mat #{@mat.name}", mat_path(@mat, bout_number: @next_match.bout_number), class: "btn btn-primary" %>
<% end %>
<h4>Bracket Position: <strong><%= @match.bracket_position %></strong></h4>
<table class="table">
<thead>

View File

@@ -221,6 +221,50 @@ class MatsControllerTest < ActionController::TestCase
post_match_update_from_mat_show
assert_redirected_to "/mats/#{@mat.id}"
end
test "logged in tournament owner can show mat with bout_number param" do
sign_in_owner
# Set a specific bout number to test
bout_number = @match.bout_number
# Call the show action with the bout_number param
get :show, params: { id: @mat.id, bout_number: bout_number }
# Assert the response is successful
assert_response :success
# Check if the bout_number is rendered on the page
assert_match /#{bout_number}/, response.body, "The bout_number should be rendered on the page"
end
test "logged in tournament owner should redirect back to the first unfinished bout on a mat after submitting a match with a bout number param" do
sign_in_owner
first_bout_number = @mat.unfinished_matches.first.bout_number
# Set a specific bout number to test
bout_number = @match.bout_number
# Call the show action with the bout_number param
get :show, params: { id: @mat.id, bout_number: bout_number }
# Submit the match
old_controller = @controller
@controller = MatchesController.new
patch :update, params: { id: @match.id, match: { tournament_id: 1, mat_id: @mat.id } }
@controller = old_controller
# Assert the redirect
assert_redirected_to mat_path(@mat) # Verify redirection to /mats/1
# Explicitly follow the redirect with the named route
get :show, params: { id: @mat.id }
# Check if the first_bout_number is rendered on the page
assert_match /#{first_bout_number}/, response.body, "The first unfinished bout_number should be rendered on the page"
end
#TESTS THAT NEED MATCHES PUT ABOVE THIS
test "redirect show if no matches" do
sign_in_owner