From 0aea26967a1a1ad7c13cc0fdf1162bff4e71d77d Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Thu, 18 Feb 2021 20:27:44 +0000 Subject: [PATCH] Added quick create high school girls weight classes --- app/controllers/tournaments_controller.rb | 2 +- app/models/tournament.rb | 12 ++++-------- app/models/weight.rb | 6 +++++- app/views/layouts/_tournament-navbar.html.erb | 3 ++- test/models/tournament_test.rb | 16 ++++++++++++++++ 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index d940233..ec766f0 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -165,7 +165,7 @@ class TournamentsController < ApplicationController end def create_custom_weights - @custom = params[:customValue].to_s + @custom = params[:customValue].split(",") @tournament.create_pre_defined_weights(@custom) redirect_to "/tournaments/#{@tournament.id}" end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 519b0c0..b164e42 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -31,15 +31,11 @@ class Tournament < ActiveRecord::Base def tournament_types ["Pool to bracket","Modified 16 Man Double Elimination","Double Elimination 1-6"] end - - def create_pre_defined_weights(value) + + def create_pre_defined_weights(weight_classes) weights.destroy_all - if value == 'hs' - Weight::HS_WEIGHT_CLASSES.each do |w| - weights.create(max: w) - end - else - raise "Unspecified behavior" + weight_classes.each do |w| + weights.create(max: w) end end diff --git a/app/models/weight.rb b/app/models/weight.rb index b76fdf4..ff5343e 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -7,7 +7,11 @@ class Weight < ActiveRecord::Base validates :max, presence: true - HS_WEIGHT_CLASSES = [106,113,120,126,132,138,145,152,160,170,182,195,220,285] + # passed via layouts/_tournament-navbar.html.erb + # tournaments controller does a .split(',') on this string and creates an array via commas + # tournament model runs the code via method create_pre_defined_weights + HS_WEIGHT_CLASSES = "106,113,120,126,132,138,145,152,160,170,182,195,220,285" + HS_GIRLS_WEIGHT_CLASSES = "101,106,111,116,121,126,131,137,143,150,160,170,189,235" before_destroy do self.tournament.destroy_all_matches diff --git a/app/views/layouts/_tournament-navbar.html.erb b/app/views/layouts/_tournament-navbar.html.erb index 5f1ffad..25ba253 100644 --- a/app/views/layouts/_tournament-navbar.html.erb +++ b/app/views/layouts/_tournament-navbar.html.erb @@ -42,7 +42,8 @@ <% end %> <% end %>
  • Time Savers
  • -
  • <%= link_to "Create High School Weights (106-285)" , "/tournaments/#{@tournament.id}/create_custom_weights?customValue=hs",data: { confirm: 'Are you sure? This will delete all current weights.' } %>
  • +
  • <%= link_to "Create Boys High School Weights (106-285)" , "/tournaments/#{@tournament.id}/create_custom_weights?customValue=#{Weight::HS_WEIGHT_CLASSES}",data: { confirm: 'Are you sure? This will delete all current weights.' } %>
  • +
  • <%= link_to "Create Girls High School Weights (101-235)" , "/tournaments/#{@tournament.id}/create_custom_weights?customValue=#{Weight::HS_GIRLS_WEIGHT_CLASSES}",data: { confirm: 'Are you sure? This will delete all current weights.' } %>
  • Tournament Actions
  • <%= link_to "Calculate Team Scores" , "/tournaments/#{@tournament.id}/calculate_team_scores", method: :post %>
  • <%= link_to "Generate Brackets" , "/tournaments/#{@tournament.id}/generate_matches", data: { confirm: 'Are you sure? This will delete all current matches.' } %>
  • diff --git a/test/models/tournament_test.rb b/test/models/tournament_test.rb index 555909c..ddca102 100644 --- a/test/models/tournament_test.rb +++ b/test/models/tournament_test.rb @@ -11,4 +11,20 @@ class TournamentTest < ActiveSupport::TestCase assert_not tourney.valid? assert_equal [:date, :name, :tournament_type, :address, :director, :director_email], tourney.errors.attribute_names end + + test "Tournament create_pre_defined_weights High School Boys Weights" do + tournament = Tournament.find(1) + tournament.create_pre_defined_weights(Weight::HS_WEIGHT_CLASSES.split(",")) + Weight::HS_WEIGHT_CLASSES.split(",").each do |weight| + assert tournament.weights.select{|w| w.max == weight.to_i}.count == 1 + end + end + + test "Tournament create_pre_defined_weights High School Girls Weights" do + tournament = Tournament.find(1) + tournament.create_pre_defined_weights(Weight::HS_GIRLS_WEIGHT_CLASSES.split(",")) + Weight::HS_GIRLS_WEIGHT_CLASSES.split(",").each do |weight| + assert tournament.weights.select{|w| w.max == weight.to_i}.count == 1 + end + end end