From e7bfad8df00c735486545322c74f0673e1940090 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Mon, 27 Apr 2015 14:58:22 -0400 Subject: [PATCH] Auto creating of high school weights --- app/controllers/static_pages_controller.rb | 13 +++++++++++++ app/controllers/tournaments_controller.rb | 8 ++++++++ app/controllers/weights_controller.rb | 17 +++++++++-------- app/models/tournament.rb | 13 ++++++++++++- app/views/tournaments/show.html.erb | 6 ++++-- config/routes.rb | 3 +-- 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 5b611aa..38996d4 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -67,6 +67,19 @@ class StaticPagesController < ApplicationController end end + def createCustomWeights + if user_signed_in? + else + redirect_to root_path + end + @tournament = Tournament.find(params[:tournament]) + @custom = params[:customValue].to_s + @tournament.createCustomWeights(@custom) + + redirect_to "/tournaments/#{@tournament.id}" + end + + def noMatches if params[:tournament] @tournament = Tournament.find(params[:tournament]) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 5830ad1..9ad0842 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -61,6 +61,14 @@ class TournamentsController < ApplicationController end end + def createHsWeights + if params[:tournament] + if user_signed_in? + @tournament.createHsWeights + end + redirect_to "/tournaments/#{@tournament.id}" + end + end # DELETE /tournaments/1 # DELETE /tournaments/1.json def destroy diff --git a/app/controllers/weights_controller.rb b/app/controllers/weights_controller.rb index 065744f..9543845 100644 --- a/app/controllers/weights_controller.rb +++ b/app/controllers/weights_controller.rb @@ -42,15 +42,16 @@ class WeightsController < ApplicationController end @weight = Weight.new(weight_params) @tournament = Tournament.find(weight_params[:tournament_id]) - respond_to do |format| - if @weight.save - format.html { redirect_to @tournament, notice: 'Weight was successfully created.' } - format.json { render action: 'show', status: :created, location: @weight } - else - format.html { render action: 'new' } - format.json { render json: @weight.errors, status: :unprocessable_entity } + + respond_to do |format| + if @weight.save + format.html { redirect_to @tournament, notice: 'Weight was successfully created.' } + format.json { render action: 'show', status: :created, location: @weight } + else + format.html { render action: 'new' } + format.json { render json: @weight.errors, status: :unprocessable_entity } + end end - end end # PATCH/PUT /weights/1 diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 9fe683b..886518e 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -13,7 +13,18 @@ class Tournament < ActiveRecord::Base @matches = Match.where(tournament_id: self.id) end - + def createCustomWeights(value) + self.weights.destroy_all + if value == 'hs' + @weights = [106,113,120,132,138,145,152,160,170,182,195,220,285] + end + @weights.each do |w| + newWeight = Weight.new + newWeight.max = w + newWeight.tournament_id = self.id + newWeight.save + end + end def upcomingMatches if matches.nil? diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index f360deb..426f0c5 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -55,12 +55,14 @@ <% end %> - - + +

<% if user_signed_in? %> <%= link_to "New #{@tournament.name} Weight" , "/weights/new?tournament=#{@tournament.id}" %> +
+ <%= link_to "Create HS Weights" , "/static_pages/createCustomWeights?tournament=#{@tournament.id}&customValue=hs" %> <% end %>

diff --git a/config/routes.rb b/config/routes.rb index e3962af..dfd5dac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,8 +30,7 @@ Wrestling::Application.routes.draw do get 'static_pages/results' get 'static_pages/team_scores' get 'static_pages/noMatches' - - + get 'static_pages/createCustomWeights' # Example of regular route: # get 'products/:id' => 'catalog#view'