From 6b572460808886f5a62ecbf8c676e0e28c3e1bbb Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Mon, 9 Jan 2023 19:16:36 -0500 Subject: [PATCH] Fixed the mat#show route by defining the correct vars in the controller and added a route for assign_next_match on mat --- app/controllers/mats_controller.rb | 43 ++++- app/models/mat.rb | 10 +- app/views/mats/_match_edit_form.html.erb | 228 ----------------------- app/views/tournaments/show.html.erb | 3 +- config/routes.rb | 1 + 5 files changed, 49 insertions(+), 236 deletions(-) delete mode 100644 app/views/mats/_match_edit_form.html.erb diff --git a/app/controllers/mats_controller.rb b/app/controllers/mats_controller.rb index 9485d71..c245ad1 100644 --- a/app/controllers/mats_controller.rb +++ b/app/controllers/mats_controller.rb @@ -1,16 +1,35 @@ class MatsController < ApplicationController - before_action :set_mat, only: [:show, :edit, :update, :destroy] - before_action :check_access, only: [:new,:create,:update,:destroy,:edit,:show] + before_action :set_mat, only: [:show, :edit, :update, :destroy, :assign_next_match] + before_action :check_access, only: [:new,:create,:update,:destroy,:edit,:show, :assign_next_match] before_action :check_for_matches, only: [:show] # GET /mats/1 # GET /mats/1.json def show @match = @mat.unfinished_matches.first + @wrestlers = [] if @match - @w1 = @match.wrestler1 - @w2 = @match.wrestler2 - @wrestlers = [@w1,@w2] + if @match.w1 + @wrestler1_name = @match.wrestler1.name + @wrestler1_school_name = @match.wrestler1.school.name + @wrestler1_last_match = @match.wrestler1.last_match + @wrestlers.push(@match.wrestler1) + else + @wrestler1_name = "Not assigned" + @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 + @wrestler2_last_match = @match.wrestler2.last_match + @wrestlers.push(@match.wrestler2) + else + @wrestler2_name = "Not assigned" + @wrestler2_school_name = "N/A" + @wrestler2_last_match = nil + end + @tournament = @match.tournament end session[:return_path] = request.original_fullpath end @@ -44,6 +63,20 @@ class MatsController < ApplicationController end end + # POST /mats/1/assign_next_match + def assign_next_match + @tournament = @mat.tournament_id + respond_to do |format| + if @mat.assign_next_match + format.html { redirect_to "/tournaments/#{@mat.tournament.id}", notice: "Next Match on Mat #{@mat.name} successfully completed." } + format.json { head :no_content } + else + format.html { redirect_to "/tournaments/#{@mat.tournament.id}", alert: "There was an error." } + format.json { head :no_content } + end + end + end + # PATCH/PUT /mats/1 # PATCH/PUT /mats/1.json def update diff --git a/app/models/mat.rb b/app/models/mat.rb index c23ec82..b557f43 100644 --- a/app/models/mat.rb +++ b/app/models/mat.rb @@ -22,10 +22,16 @@ class Mat < ActiveRecord::Base def assign_next_match t_matches = tournament.matches.select{|m| m.mat_id == nil && m.finished != 1 && m.bout_number != nil}.sort_by{|m| m.bout_number} - if t_matches.size > 0 + if t_matches.size > 0 and self.unfinished_matches.size < 4 match = t_matches.sort_by{|m| m.bout_number}.first match.mat_id = self.id - match.save + if match.save + return true + else + return false + end + else + return true end end diff --git a/app/views/mats/_match_edit_form.html.erb b/app/views/mats/_match_edit_form.html.erb deleted file mode 100644 index 66ac5df..0000000 --- a/app/views/mats/_match_edit_form.html.erb +++ /dev/null @@ -1,228 +0,0 @@ -<%= form_for(@match) do |f| %> - <% if @match.errors.any? %> -
-

<%= pluralize(@match.errors.count, "error") %> prohibited this match from being saved:

- - -
- <% end %> -

Bout: <%= @match.bout_number %>

Round: <%= @match.round %>

Weight: <%= @match.weight_max %> lbs

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<%= @w1.name %> - <%= @w1.school.name %>
Last Match: <%= if @w1.last_match != nil then time_ago_in_words(@w1.last_match.updated_at) end%>
<%= @w2.name %> - <%= @w2.school.name %>
Last Match: <%= if @w2.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) end%>
<%= @w1.name %> Stats:
<%= f.text_area :w1_stat, cols: "30", rows: "10" %>
<%= @w2.name %> Stats:
<%= f.text_area :w2_stat, cols: "30", rows: "10" %>
<%= @w1.name %> Scoring
- - - - - -
<%= @w2.name %> Scoring
- - - - - -
<%= @w1.name %> Choice
- - -
<%= @w2.name %> Choice
- - -
<%= @w1.name %> Warnings
-
<%= @w2.name %> Warnings
-
Match Options
-
-
-
-

Match Results

-
-
- <%= f.label "Win Type" %>
- <%= f.select(:win_type, Match::WIN_TYPES) %> -
-
-
- <%= f.label "Winner" %> Please choose the winner
- <%= f.collection_select :winner_id, @wrestlers, :id, :name %> -
-
-
- <%= f.label "Final Score" %> Also put pin time here if applicable. If default or forfeit, leave blank. Example: 7-2, 17-2, or 2:34
- <%= f.text_field :score %> -
-
- - <%= f.hidden_field :finished, :value => 1 %> - <%= f.hidden_field :round, :value => @match.round %> - -
- -
- <%= f.submit onclick: "return confirm('Is the name of the winner ' + document.getElementById('match_winner_id').options[document.getElementById('match_winner_id').selectedIndex].text + '?')", :class=>"btn btn-success" %> - -
-<% end %> - - diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index 0257125..53f1369 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -110,7 +110,8 @@ <% if can? :manage, @tournament %> <%= link_to '', mat, method: :delete, data: { confirm: "Are you sure you want to delete Mat #{mat.name}?" }, :class=>"fas fa-trash-alt" %> - + <%= link_to '', "/mats/#{mat.id}/assign_next_match", method: :post, :class=>"fas fa-solid fa-arrow-right" %> + <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 0b2fcc4..0fb79c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Wrestling::Application.routes.draw do resources :mats + post "mats/:id/assign_next_match" => "mats#assign_next_match" resources :matches