From f86a577c8ba73094f35e62c2e15fc565dd273f0c Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Wed, 4 Nov 2015 16:10:56 -0500 Subject: [PATCH] Moving pages to tournament from static_pages --- app/controllers/static_pages_controller.rb | 47 -------------- app/controllers/tournaments_controller.rb | 61 +++++++++++++------ app/views/layouts/_header.html.erb | 6 +- app/views/static_pages/all_brackets.html.erb | 2 +- app/views/static_pages/home.html.erb | 2 +- .../generate_matches.html.erb | 0 app/views/tournaments/index.html.erb | 35 ++++++----- .../no_matches.html.erb} | 0 app/views/tournaments/show.html.erb | 4 +- .../team_scores.html.erb | 0 .../up_matches.html.erb | 0 .../weights.html.erb | 0 config/routes.rb | 13 ++-- .../static_pages_controller_test.rb | 15 ----- .../tournaments_controller_test.rb | 40 ++++++++++-- 15 files changed, 111 insertions(+), 114 deletions(-) rename app/views/{static_pages => tournaments}/generate_matches.html.erb (100%) rename app/views/{static_pages/noMatches.html.erb => tournaments/no_matches.html.erb} (100%) rename app/views/{static_pages => tournaments}/team_scores.html.erb (100%) rename app/views/{static_pages => tournaments}/up_matches.html.erb (100%) rename app/views/{static_pages => tournaments}/weights.html.erb (100%) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index a6a3e6d..b4edeca 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -1,31 +1,7 @@ class StaticPagesController < ApplicationController before_filter :check_access, only: [:createCustomWeights,:generate_matches,:weigh_in] - def tournaments - @tournaments = Tournament.all.includes(:user,:matches,:mats) - end - def up_matches - if params[:tournament] - @tournament = Tournament.where(:id => params[:tournament]).includes(:matches,:mats).first - end - if @tournament - @matches = @tournament.matches.where(mat_id: nil).order('bout_number ASC').limit(10).includes(:wrestlers) - @mats = @tournament.mats.includes(:matches) - if @tournament.matches.empty? - redirect_to "/static_pages/noMatches?tournament=#{@tournament.id}" - end - end - end - def team_scores - if params[:tournament] - @tournament = Tournament.find(params[:tournament]) - end - if @tournament - @schools = School.where(tournament_id: @tournament.id) - @schools.sort_by{|x|[x.score]} - end - end def results if params[:tournament] @@ -58,15 +34,6 @@ class StaticPagesController < ApplicationController end end - def weights - if params[:tournament] - @tournament = Tournament.find(params[:tournament]) - end - if @tournament - @weights = Weight.where(tournament_id: @tournament.id) - @weights = @weights.sort_by{|x|[x.max]} - end - end def createCustomWeights @tournament = Tournament.find(params[:tournament]) @@ -77,20 +44,6 @@ class StaticPagesController < ApplicationController end - def noMatches - if params[:tournament] - @tournament = Tournament.find(params[:tournament]) - end - end - - def generate_matches - if params[:tournament] - @tournament = Tournament.find(params[:tournament]) - end - if @tournament - @tournament.generateMatchups - end - end def weigh_in if params[:wrestler] diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 45ebfe2..5eb8b1c 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -1,32 +1,51 @@ class TournamentsController < ApplicationController - before_action :set_tournament, only: [:show, :edit, :update, :destroy] - before_filter :check_access, only: [:update,:edit,:destroy] + before_action :set_tournament, only: [:show, :edit, :update, :destroy,:up_matches,:no_matches,:team_scores,:weights,:generate_matches] + before_filter :check_access, only: [:update,:edit,:destroy,:generate_matches] + before_filter :check_for_matches, only: [:up_matches] + - # GET /tournaments - # GET /tournaments.json - def index - @tournaments = Tournament.all + def generate_matches + @tournament.generateMatchups + end + + def weights + @weights = @tournament.weights + @weights.sort_by{|w| w.max} + end + + def team_scores + @schools = @tournament.schools + @schools.sort_by{|s| s.score} + end + + + def no_matches + + end + + + def up_matches + @matches = @tournament.matches.where(mat_id: nil).order('bout_number ASC').limit(10).includes(:wrestlers) + @mats = @tournament.mats.includes(:matches) + end + + def index + @tournaments = Tournament.all.limit(50).includes(:schools,:weights,:mats,:matches,:user,:wrestlers) end - # GET /tournaments/1 - # GET /tournaments/1.json def show @schools = @tournament.schools @weights = @tournament.weights.sort_by{|x|[x.max]} @mats = @tournament.mats end - # GET /tournaments/new def new @tournament = Tournament.new end - # GET /tournaments/1/edit def edit end - # POST /tournaments - # POST /tournaments.json def create if user_signed_in? else @@ -44,8 +63,6 @@ class TournamentsController < ApplicationController end end - # PATCH/PUT /tournaments/1 - # PATCH/PUT /tournaments/1.json def update respond_to do |format| if @tournament.update(tournament_params) @@ -58,8 +75,6 @@ class TournamentsController < ApplicationController end end - # DELETE /tournaments/1 - # DELETE /tournaments/1.json def destroy @tournament.destroy respond_to do |format| @@ -71,16 +86,26 @@ class TournamentsController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_tournament - @tournament = Tournament.where(:id => params[:id]).includes(:schools,:weights,:mats).first + @tournament = Tournament.where(:id => params[:id]).includes(:schools,:weights,:mats,:matches,:user,:wrestlers).first end # Never trust parameters from the scary internet, only allow the white list through. def tournament_params params.require(:tournament).permit(:name, :address, :director, :director_email, :tournament_type, :weigh_in_ref, :user_id) end + + #Check for tournament owner def check_access if current_user != @tournament.user - redirect_to root_path + redirect_to '/static_pages/not_allowed' + end + end + + def check_for_matches + if @tournament + if @tournament.matches.empty? + redirect_to "/tournaments/#{@tournament.id}/no_matches" + end end end end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 9a250a6..58fa920 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -5,10 +5,10 @@