diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index d8fcfa6..0e252ee 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -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 @@ -75,7 +78,7 @@ class MatchesController < ApplicationController end end end - end + end private @@ -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 diff --git a/app/controllers/mats_controller.rb b/app/controllers/mats_controller.rb index 5a9ce42..afeac39 100644 --- a/app/controllers/mats_controller.rb +++ b/app/controllers/mats_controller.rb @@ -6,7 +6,18 @@ class MatsController < ApplicationController # GET /mats/1 # GET /mats/1.json def show - @match = @mat.unfinished_matches.first + 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,11 +41,13 @@ 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 + end # GET /mats/new def new diff --git a/app/views/matches/_matchstats.html.erb b/app/views/matches/_matchstats.html.erb index f538542..9774c16 100644 --- a/app/views/matches/_matchstats.html.erb +++ b/app/views/matches/_matchstats.html.erb @@ -11,6 +11,9 @@ <% end %>

Bout <%= @match.bout_number %>

+ <% 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 %>

Bracket Position: <%= @match.bracket_position %>

diff --git a/test/controllers/mats_controller_test.rb b/test/controllers/mats_controller_test.rb index 9b77f05..08f911a 100644 --- a/test/controllers/mats_controller_test.rb +++ b/test/controllers/mats_controller_test.rb @@ -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