1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00

Fixed redirect for match editing and added controller tests. Added wrestler school name to match edit form. Added authorization tests to all matches page.

This commit is contained in:
2019-01-23 19:22:55 +00:00
parent bb8ec92bf0
commit b001445090
9 changed files with 52 additions and 21 deletions

View File

@@ -19,6 +19,7 @@ class MatchesController < ApplicationController
@wrestlers = [@w1,@w2]
@tournament = @match.tournament
end
session[:return_path] = "/tournaments/#{@tournament.id}/matches"
end
@@ -27,8 +28,8 @@ class MatchesController < ApplicationController
def update
respond_to do |format|
if @match.update(match_params)
if params[:match][:redirect_path]
format.html { redirect_to params[:match][:redirect_path], notice: 'Match was successfully updated.' }
if session[:return_path]
format.html { redirect_to session.delete(:return_path), notice: 'Match was successfully updated.' }
else
format.html { redirect_to "/tournaments/#{@match.tournament.id}", notice: 'Match was successfully updated.' }
end
@@ -49,7 +50,7 @@ class MatchesController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def match_params
params.require(:match).permit(:w1, :w2, :w1_stat, :w2_stat, :winner_id, :win_type, :score, :finished, :redirect_path)
params.require(:match).permit(:w1, :w2, :w1_stat, :w2_stat, :winner_id, :win_type, :score, :finished)
end
def check_access

View File

@@ -12,6 +12,7 @@ class MatsController < ApplicationController
@w2 = @match.wrestler2
@wrestlers = [@w1,@w2]
end
session[:return_path] = request.original_fullpath
end
# GET /mats/new

View File

@@ -5,7 +5,6 @@ class Match < ActiveRecord::Base
has_many :wrestlers, :through => :weight
after_update :after_finished_actions, :if => :saved_change_to_finished?
after_update :after_finished_actions, :if => :saved_change_to_winner_id?
attr_accessor :redirect_path
def after_finished_actions
if self.finished == 1 && self.winner_id != nil

View File

@@ -14,14 +14,18 @@
<table class="table">
<thead>
<tr>
<th><%= @w1.name %> <select id="w1-color" onchange="changeW1Color(this)">
<th>Name: <%= @w1.name %> <select id="w1-color" onchange="changeW1Color(this)">
<option value="green">Green</option>
<option value="red">Red</option>
</select><br>Last Match: <%= if @w1.last_match != nil then time_ago_in_words(@w1.last_match.updated_at) end%></th>
<th><%= @w2.name %> <select id="w2-color" onchange="changeW2Color(this)">
</select>
<br>School: <%= @w1.school.name %>
<br>Last Match: <%= if @w1.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) else "N/A" end%></th>
<th>Name: <%= @w2.name %> <select id="w2-color" onchange="changeW2Color(this)">
<option value="red">Red</option>
<option value="green">Green</option>
</select><br>Last Match: <%= if @w2.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) end%></th>
</select>
<br>School: <%= @w2.school.name %>
<br>Last Match: <%= if @w2.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) else "N/A" end%></th>
</tr>
</thead>
<tbody>
@@ -89,7 +93,6 @@
<%= f.hidden_field :finished, :value => 1 %>
<%= f.hidden_field :round, :value => @match.round %>
<%= f.hidden_field :redirect_path, :value => @redirect_path %>
<br>

View File

@@ -1,3 +1,2 @@
<h1><%= @w1.name %> VS. <%= @w2.name %></h1>
<% @redirect_path = "/tournaments/#{@tournament.id}/matches" %>
<%= render 'form' %>

View File

@@ -1,11 +1,6 @@
<strong>Mat <%= @mat.name %></strong>
</p>
<h3>Mat <%= @mat.name %></h3>
<h3>Tournament: <%= @mat.tournament.name %></h3>
<p>
<strong>Tournament:</strong>
<%= @mat.tournament.name %>
</p>
<% @redirect_path = request.original_fullpath %>
<% if @match %>
<%= render 'matches/form' %>
<% else %>

View File

@@ -13,6 +13,11 @@ class MatchesControllerTest < ActionController::TestCase
patch :update, params: { id: @match.id, match: {tournament_id: 1, mat_id: 1} }
end
def post_update_from_match_edit
get :edit, params: { id: @match.id }
patch :update, params: { id: @match.id, match: {tournament_id: 1, mat_id: 1} }
end
def get_edit
get :edit, params: { id: @match.id }
end
@@ -94,7 +99,12 @@ class MatchesControllerTest < ActionController::TestCase
test "logged in tournament delegate should post update match" do
sign_in_tournament_delegate
post_update
assert_redirected_to mat_path(1)
assert_redirected_to tournament_path(@tournament.id)
end
test "should redirect to all matches when posting a match update from match edit" do
sign_in_owner
post_update_from_match_edit
assert_redirected_to "/tournaments/#{@tournament.id}/matches"
end
end

View File

@@ -6,6 +6,7 @@ class MatsControllerTest < ActionController::TestCase
setup do
@tournament = Tournament.find(1)
# @tournament.generateMatchups
@match = Match.where("tournament_id = ? and mat_id = ?",1,1).first
@mat = mats(:one)
end
@@ -65,6 +66,14 @@ class MatsControllerTest < ActionController::TestCase
@tournament.destroy_all_matches
end
def post_match_update_from_mat_show
get :show, params: { id: @mat.id }
old_controller = @controller
@controller = MatchesController.new
patch :update, params: { id: @match.id, match: {tournament_id: 1, mat_id: @mat.id} }
@controller = old_controller
end
test "logged in tournament owner should get edit mat page" do
sign_in_owner
get_edit
@@ -203,7 +212,11 @@ class MatsControllerTest < ActionController::TestCase
success
end
test "redirect to mat show when posting a match from mat show" do
sign_in_owner
post_match_update_from_mat_show
assert_redirected_to "/mats/#{@mat.id}"
end
#TESTS THAT NEED MATCHES PUT ABOVE THIS
test "redirect show if no matches" do
sign_in_owner

View File

@@ -362,5 +362,15 @@ class TournamentsControllerTest < ActionController::TestCase
redirect
end
test 'logged in non owner should not get all matches page' do
sign_in_non_owner
get :matches, params: { id: 1 }
redirect
end
test 'logged in owner should get all matches page' do
sign_in_owner
get :matches, params: { id: 1 }
success
end
end