From a8b96c96e87d613302bb6dda7d08bbeaa4d778b7 Mon Sep 17 00:00:00 2001 From: jcwimer Date: Wed, 27 Jan 2016 14:13:49 +0000 Subject: [PATCH] Swap page is now working and also moved swaping logic from tournament model to its own class --- app/controllers/tournaments_controller.rb | 15 ++++++-- app/controllers/wrestlers_controller.rb | 11 ++---- app/models/match.rb | 17 +++------ app/models/swap_wrestlers.rb | 42 +++++++++++++++++++++++ app/models/tournament.rb | 34 ++---------------- app/models/weight.rb | 2 +- app/models/wrestler.rb | 6 ++-- app/views/wrestlers/_form.html.erb | 21 ++++++------ config/routes.rb | 3 +- db/schema.rb | 2 +- 10 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 app/models/swap_wrestlers.rb diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 48e8425..b06c577 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -1,6 +1,6 @@ class TournamentsController < ApplicationController - before_action :set_tournament, only: [:weigh_in_sheet,:error,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets] - before_filter :check_access_manage, only: [:weigh_in_sheet,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches] + before_action :set_tournament, only: [:swap,:weigh_in_sheet,:error,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets] + before_filter :check_access_manage, only: [:swap,:weigh_in_sheet,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches] before_filter :check_access_destroy, only: [:destroy,:delegate,:remove_delegate] before_filter :check_tournament_errors, only: [:generate_matches] before_filter :check_for_matches, only: [:up_matches,:bracket,:all_brackets] @@ -9,6 +9,15 @@ class TournamentsController < ApplicationController end + def swap + @wrestler = Wrestler.find(params[:wrestler][:originalId]) + respond_to do |format| + if SwapWrestlers.new.swapWrestlers(params[:wrestler][:originalId], params[:wrestler][:swapId]) + format.html { redirect_to @wrestler, notice: 'Wrestler was successfully swaped.' } + format.json { render action: 'show', status: :created, location: @wrestler } + end + end + end def remove_teampointadjust if params[:teampointadjust] @@ -246,7 +255,7 @@ class TournamentsController < ApplicationController # 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, :date) + params.require(:tournament).permit(:name, :address, :director, :director_email, :tournament_type, :weigh_in_ref, :user_id, :date, :originalId, :swapId) end #Check for tournament owner diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb index 5f3e1f4..2a6caa8 100644 --- a/app/controllers/wrestlers_controller.rb +++ b/app/controllers/wrestlers_controller.rb @@ -1,16 +1,9 @@ class WrestlersController < ApplicationController - before_action :set_wrestler, only: [:show, :edit, :update, :destroy, :swap] + before_action :set_wrestler, only: [:show, :edit, :update, :destroy] before_filter :check_access, only: [:new,:create,:update,:destroy,:edit] - def swap - respond_to do |format| - if @wrestler.tournament.swapWrestlers(@wrestler.id, params[:wrestler][:swapId]) - format.html { redirect_to @wrestler, notice: 'Wrestler was successfully swaped.' } - format.json { render action: 'show', status: :created, location: @wrestler } - end - end - end + # GET /wrestlers/1 # GET /wrestlers/1.json diff --git a/app/models/match.rb b/app/models/match.rb index e79738a..b7059bd 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -7,14 +7,14 @@ class Match < ActiveRecord::Base after_update do - if self.finished == 1 && self.winner_id != nil - if self.w1 && self.w2 + if self.finished == 1 && self.winner_id != nil + if self.w1 && self.w2 wrestler1.touch wrestler2.touch end advance_wrestlers calcSchoolPoints - end + end end WIN_TYPES = ["Decision", "Major", "Tech Fall", "Pin", "Forfeit", "Injury Default", "Default", "DQ"] @@ -136,14 +136,5 @@ class Match < ActiveRecord::Base end end - def swapWrestlers(wrestler1_id,wrestler2_id) - if self.w1 == wrestler1_id - self.w1 = wrestler2_id - self.save - elsif self.w2 == wrestler1_id - self.w2 = wrestler2_id - self.save - end - - end + end diff --git a/app/models/swap_wrestlers.rb b/app/models/swap_wrestlers.rb new file mode 100644 index 0000000..c3a0502 --- /dev/null +++ b/app/models/swap_wrestlers.rb @@ -0,0 +1,42 @@ +class SwapWrestlers + attr_accessor :wrestler1_id, :wrestler2_id + + + def swapWrestlers(wrestler1_id,wrestler2_id) + w1 = Wrestler.find(wrestler1_id) + w2 = Wrestler.find(wrestler2_id) + + #placeholder guy + w3 = Wrestler.new + w3.weight_id = w1.weight_id + w3.original_seed = w1.original_seed + w3.seed = w1.seed + swapWrestlerMatches(w1.allMatches,w1.id,w3.id) + + #Swap wrestler 1 and wrestler 2 + swapWrestlerMatches(w2.allMatches,w2.id,w1.id) + w1.seed = w2.seed + + + swapWrestlerMatches(w3.allMatches,w3.id,w2.id) + w2.seed = w3.seed + + + w1.save + w2.save + end + + def swapWrestlerMatches(matchesToSwap,w1_id,w2_id) + matchesToSwap.each do |m| + if m.bracket_position == "Pool" + if m.w1 == w1_id + m.w1 = w2_id + m.save + elsif m.w2 == w1_id + m.w2 = w2_id + m.save + end + end + end + end +end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index d48a204..e1224f3 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -9,7 +9,7 @@ class Tournament < ActiveRecord::Base has_many :wrestlers, through: :weights has_many :matches, dependent: :destroy has_many :delegates, class_name: "TournamentDelegate" - + validates :date, :name, :tournament_type, :address, :director, :director_email , presence: true def self.search(search) @@ -136,36 +136,6 @@ class Tournament < ActiveRecord::Base end end - def swapWrestlers(wrestler1_id,wrestler2_id) - w1 = Wrestler.find(wrestler1_id) - w2 = Wrestler.find(wrestler2_id) - - #placeholder guy - w3 = Wrestler.new - w3.weight_id = w1.weight_id - w3.original_seed = w1.original_seed - w3.seed = w1.seed - swapWrestlerMatches(w1.allMatches,wrestler1_id,w3.id) - - #Swap wrestler 1 and wrestler 2 - swapWrestlerMatches(w2.allMatches,wrestler2_id,wrestler1_id) - w1.seed = w2.seed - - - swapWrestlerMatches(w3.allMatches,w3.id,wrestler2_id) - w2.seed = w3.seed - - - w1.save - w2.save - end - - def swapWrestlerMatches(matchesToSwap,w1_id,w2_id) - matchesToSwap.each do |m| - if m.bracket_position == "Pool" - m.swapWrestlers(w1_id,w2_id) - end - end - end + end diff --git a/app/models/weight.rb b/app/models/weight.rb index ef3128f..34f7363 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -64,7 +64,7 @@ class Weight < ActiveRecord::Base end def swapWrestlers(wrestler1_id,wrestler2_id) - self.tournament.swapWrestlers(wrestler1_id,wrestler2_id) + SwapWrestlers.new.swapWrestlers(wrestler1_id,wrestler2_id) end def returnPoolNumber(wrestler) diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index dbdb53e..b8ebd9e 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -4,16 +4,16 @@ class Wrestler < ActiveRecord::Base has_one :tournament, through: :weight has_many :matches, through: :weight has_many :deductedPoints, class_name: "Teampointadjust" - attr_accessor :poolNumber, :poolAdvancePoints, :swapId + attr_accessor :poolNumber, :poolAdvancePoints, :originalId, :swapId validates :name, :weight_id, :school_id, presence: true before_destroy do - self.tournament.destroyAllMatches + # self.tournament.destroyAllMatches end before_create do - self.tournament.destroyAllMatches + # self.tournament.destroyAllMatches end diff --git a/app/views/wrestlers/_form.html.erb b/app/views/wrestlers/_form.html.erb index d166388..b827403 100644 --- a/app/views/wrestlers/_form.html.erb +++ b/app/views/wrestlers/_form.html.erb @@ -16,14 +16,15 @@ <%= f.label :name %>
<%= f.text_field :name %> - - <% if @tournament %> - <%= f.hidden_field :school_id, :value => @school.id %> - <% else %> -
- <%= f.label 'School' %>
- <%= f.collection_select :school_id, @school.tournament.schools, :id, :name %> -
+ <% if can? :manage, @wrestler.tournament %> + <% if @tournament %> + <%= f.hidden_field :school_id, :value => @school.id %> + <% else %> +
+ <%= f.label 'School' %>
+ <%= f.collection_select :school_id, @school.tournament.schools, :id, :name %> +
+ <% end %> <% end %>
<%= f.label 'Weight Class' %>
@@ -56,8 +57,8 @@ <% if can? :manage, @wrestler.tournament %>

Swap Bracket Position

- <%= form_for @wrestler, :url => url_for(:controller => 'wrestlers', :action => 'swap', :method => "post") do |f| %> - + <%= form_for(Wrestler.new, url: swap_wrestlers_path(@wrestler.tournament)) do |f| %> + <%= f.hidden_field :originalId, :value => @wrestler.id %>
<%= f.label 'Swap With' %>
<%= f.collection_select :swapId, @wrestler.weight.wrestlers, :id, :name %> diff --git a/config/routes.rb b/config/routes.rb index bc73232..c3c436c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,7 @@ Wrestling::Application.routes.draw do post "/weights/:id" => "weights#show" - patch "/wrestlers/:id/swap" => "wrestlers#swap" + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". @@ -51,6 +51,7 @@ Wrestling::Application.routes.draw do post 'tournaments/:id/teampointadjust' => 'tournaments#teampointadjust' delete 'tournaments/:id/:teampointadjust/remove_teampointadjust' => 'tournaments#remove_teampointadjust' get 'tournaments/:id/error' => 'tournaments#error' + post "/tournaments/:id/swap" => "tournaments#swap", :as => :swap_wrestlers # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/schema.rb b/db/schema.rb index f37635e..e320d6e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160112152946) do +ActiveRecord::Schema.define(version: 20160126173424) do create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false