diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 68e242a..9ab5a69 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -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 diff --git a/app/controllers/mats_controller.rb b/app/controllers/mats_controller.rb index 69647ce..9485d71 100644 --- a/app/controllers/mats_controller.rb +++ b/app/controllers/mats_controller.rb @@ -12,6 +12,7 @@ class MatsController < ApplicationController @w2 = @match.wrestler2 @wrestlers = [@w1,@w2] end + session[:return_path] = request.original_fullpath end # GET /mats/new diff --git a/app/models/match.rb b/app/models/match.rb index fd735cf..0051fc3 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -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 diff --git a/app/views/matches/_form.html.erb b/app/views/matches/_form.html.erb index 313744e..c9b834d 100644 --- a/app/views/matches/_form.html.erb +++ b/app/views/matches/_form.html.erb @@ -14,14 +14,18 @@ - - + + +
School: <%= @w2.school.name %> +
Last Match: <%= if @w2.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) else "N/A" end%> @@ -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 %>
diff --git a/app/views/matches/edit.html.erb b/app/views/matches/edit.html.erb index 01900a0..ac81e15 100644 --- a/app/views/matches/edit.html.erb +++ b/app/views/matches/edit.html.erb @@ -1,3 +1,2 @@

<%= @w1.name %> VS. <%= @w2.name %>

-<% @redirect_path = "/tournaments/#{@tournament.id}/matches" %> <%= render 'form' %> diff --git a/app/views/mats/show.html.erb b/app/views/mats/show.html.erb index 54fbf31..cb1a252 100644 --- a/app/views/mats/show.html.erb +++ b/app/views/mats/show.html.erb @@ -1,11 +1,6 @@ - Mat <%= @mat.name %> -

+

Mat <%= @mat.name %>

+

Tournament: <%= @mat.tournament.name %>

-

- Tournament: - <%= @mat.tournament.name %> -

-<% @redirect_path = request.original_fullpath %> <% if @match %> <%= render 'matches/form' %> <% else %> diff --git a/test/controllers/matches_controller_test.rb b/test/controllers/matches_controller_test.rb index e17cf75..2c5c2d3 100644 --- a/test/controllers/matches_controller_test.rb +++ b/test/controllers/matches_controller_test.rb @@ -12,6 +12,11 @@ class MatchesControllerTest < ActionController::TestCase def post_update 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 } @@ -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 diff --git a/test/controllers/mats_controller_test.rb b/test/controllers/mats_controller_test.rb index e415a9e..934e787 100644 --- a/test/controllers/mats_controller_test.rb +++ b/test/controllers/mats_controller_test.rb @@ -4,9 +4,10 @@ class MatsControllerTest < ActionController::TestCase include Devise::Test::ControllerHelpers setup do - @tournament = Tournament.find(1) + @tournament = Tournament.find(1) # @tournament.generateMatchups - @mat = mats(:one) + @match = Match.where("tournament_id = ? and mat_id = ?",1,1).first + @mat = mats(:one) end def create @@ -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 diff --git a/test/controllers/tournaments_controller_test.rb b/test/controllers/tournaments_controller_test.rb index da0c695..371baab 100644 --- a/test/controllers/tournaments_controller_test.rb +++ b/test/controllers/tournaments_controller_test.rb @@ -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
<%= @w1.name %> Name: <%= @w1.name %>
Last Match: <%= if @w1.last_match != nil then time_ago_in_words(@w1.last_match.updated_at) end%>
<%= @w2.name %> +
School: <%= @w1.school.name %> +
Last Match: <%= if @w1.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) else "N/A" end%>
Name: <%= @w2.name %>
Last Match: <%= if @w2.last_match != nil then time_ago_in_words(@w2.last_match.updated_at) end%>