diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb index e671465..b8526ec 100644 --- a/app/controllers/wrestlers_controller.rb +++ b/app/controllers/wrestlers_controller.rb @@ -1,6 +1,6 @@ class WrestlersController < ApplicationController - before_action :set_wrestler, only: [:show, :edit, :update, :destroy] - before_action :check_access, only: [:new,:create,:update,:destroy,:edit] + before_action :set_wrestler, only: [:show, :edit, :update, :destroy, :update_pool] + before_action :check_access, only: [:new,:create,:update,:destroy,:edit,:update_pool] @@ -69,6 +69,25 @@ class WrestlersController < ApplicationController end end + def update_pool + @tournament = @wrestler.tournament + @weight = @wrestler.weight + @weights = @tournament.weights.sort_by{|w| w.max} + @school = @wrestler.school + if params[:wrestler]['pool'] + @wrestler.pool = params[:wrestler]['pool'] + respond_to do |format| + if @wrestler.update(wrestler_params) + format.html { redirect_to "/weights/#{@wrestler.weight.id}/", notice: 'Wrestler was successfully updated. Please re-generate this weight classes matches.' } + format.json { head :no_content } + else + format.html { render action: 'edit' } + format.json { render json: @wrestler.errors, status: :unprocessable_entity } + end + end + end + end + # DELETE /wrestlers/1 # DELETE /wrestlers/1.json def destroy @@ -88,14 +107,19 @@ class WrestlersController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def wrestler_params - params.require(:wrestler).permit(:name, :school_id, :weight_id, :seed, :original_seed, :season_win, :season_loss,:criteria,:extra,:offical_weight) + params.require(:wrestler).permit(:name, :school_id, :weight_id, :seed, :original_seed, :season_win, :season_loss,:criteria,:extra,:offical_weight,:pool) end def check_access if params[:school] @school = School.find(params[:school]) #@tournament = Tournament.find(@school.tournament.id) elsif params[:wrestler] - @school = School.find(params[:wrestler]["school_id"]) + if params[:wrestler]["school_id"] + @school = School.find(params[:wrestler]["school_id"]) + else + @wrestler = Wrestler.find(params[:wrestler]["id"]) + @school = @wrestler.school + end #@tournament = Tournament.find(@school.tournament.id) elsif @wrestler @school = @wrestler.school diff --git a/app/models/weight.rb b/app/models/weight.rb index bc86549..10144a1 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -17,6 +17,18 @@ class Weight < ActiveRecord::Base # self.tournament.destroyAllMatches end + def pools_with_bye + pool = 1 + pools_with_a_bye = [] + until pool > self.pools do + if wrestlersForPool(pool).first.hasAPoolBye + pools_with_a_bye << pool + end + pool = pool + 1 + end + pools_with_a_bye + end + def wrestlersForPool(poolNumber) #For some reason this does not work # wrestlers.select{|w| w.pool == poolNumber} @@ -97,5 +109,9 @@ class Weight < ActiveRecord::Base def poolOrder(pool) PoolOrder.new(wrestlersForPool(pool)).getPoolOrder end + + def wrestlersWithoutPool + wrestlers.select{|w| w.pool == nil} + end end diff --git a/app/services/weight_services/generate_pool_numbers.rb b/app/services/weight_services/generate_pool_numbers.rb index c8fb179..4844ffe 100644 --- a/app/services/weight_services/generate_pool_numbers.rb +++ b/app/services/weight_services/generate_pool_numbers.rb @@ -5,11 +5,11 @@ class GeneratePoolNumbers def savePoolNumbers if @weight.pools == 4 - saveFourPoolNumbers(@weight.wrestlers) + saveFourPoolNumbers(@weight.wrestlersWithoutPool) elsif @weight.pools == 2 - saveTwoPoolNumbers(@weight.wrestlers) + saveTwoPoolNumbers(@weight.wrestlersWithoutPool) elsif @weight.pools == 1 - saveOnePoolNumbers(@weight.wrestlers) + saveOnePoolNumbers(@weight.wrestlersWithoutPool) end end diff --git a/app/views/tournaments/bracket.html.erb b/app/views/tournaments/bracket.html.erb index 1ee6a27..442af5c 100644 --- a/app/views/tournaments/bracket.html.erb +++ b/app/views/tournaments/bracket.html.erb @@ -103,4 +103,20 @@ li:first-child,li:last-child {
<%= submit_tag "Swap", :class=>"btn btn-success"%> <% end %> +<% end %> +<% if can? :manage, @tournament %> +

+

Move wrestler to pool with a bye

+ <%= form_tag '/wrestlers/update_pool' do %> +
+ <%= label_tag 'Wrestler to move' %>
+ <%= collection_select(:wrestler, :id, @weight.wrestlers, :id, :name) %> +
+
+ <%= label_tag 'Pool to move to' %>
+ <%= select :wrestler, :pool, @weight.pools_with_bye %> +
+
+ <%= submit_tag "Move", :class=>"btn btn-success"%> + <% end %> <% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8a0b0c2..c13e345 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,6 +52,7 @@ Wrestling::Application.routes.draw do post "/tournaments/:id/swap" => "tournaments#swap", :as => :swap_wrestlers post 'weights/:id/re_gen' => 'weights#re_gen', :as => :regen_weight + post "/wrestlers/update_pool" => "wrestlers#update_pool" #API get "/api/tournaments" => "api#tournaments"