From c6626988db9b1b143e1247da3c16c84cd3459b28 Mon Sep 17 00:00:00 2001 From: jcwimer Date: Tue, 12 Jan 2016 17:36:45 +0000 Subject: [PATCH] Added pages for teampointsadjust and spearated wrestler and school deducted points --- app/controllers/tournaments_controller.rb | 32 +++++++++- app/models/school.rb | 9 ++- app/models/teampointadjust.rb | 25 +++++++- app/models/tournament.rb | 15 +++++ app/views/layouts/_lsidebar.html.erb | 1 + .../tournaments/teampointadjust.html.erb | 62 +++++++++++++++++++ config/routes.rb | 3 + ...52946_add_school_id_to_teampointsadjust.rb | 5 ++ db/schema.rb | 3 +- 9 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 app/views/tournaments/teampointadjust.html.erb create mode 100644 db/migrate/20160112152946_add_school_id_to_teampointsadjust.rb diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 4bbec15..bcdc67c 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -1,10 +1,38 @@ class TournamentsController < ApplicationController - before_action :set_tournament, only: [: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: [:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches] + before_action :set_tournament, only: [: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: [: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_for_matches, only: [:up_matches,:bracket,:all_brackets] + def remove_teampointadjust + if params[:teampointadjust] + @points = Teampointadjust.find(params[:teampointadjust]) + @points.destroy + respond_to do |format| + format.html { redirect_to "/tournaments/#{@tournament.id}/teampointadjust", notice: 'Point adjustment removed successfully' } + end + end + end + + def teampointadjust + if params[:teampointadjust] + @points = Teampointadjust.new + @points.wrestler_id = params[:teampointadjust]["wrestler_id"] + @points.school_id = params[:teampointadjust]["school_id"] + @points.points = params[:teampointadjust]["points"] + respond_to do |format| + if @points.save + format.html { redirect_to "/tournaments/#{@tournament.id}/teampointadjust", notice: 'Point adjustment added successfully' } + else + format.html { redirect_to "/tournaments/#{@tournament.id}/teampointadjust", notice: 'There was an issue saving point adjustment please try again' } + end + end + else + @point_adjustments = @tournament.pointAdjustments + end + end + def remove_delegate if params[:delegate] @delegate = TournamentDelegate.find(params[:delegate]) diff --git a/app/models/school.rb b/app/models/school.rb index 35eba7d..a29eabd 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -1,7 +1,7 @@ class School < ActiveRecord::Base belongs_to :tournament, touch: true has_many :wrestlers, dependent: :destroy - has_many :deductedPoints, through: :wrestlers + has_many :deductedPoints, class_name: "Teampointadjust" has_many :delegates, class_name: "SchoolDelegate" validates :name, presence: true @@ -33,9 +33,14 @@ class School < ActiveRecord::Base def totalDeductedPoints points = 0 - self.deductedPoints.each do |d| + deductedPoints.each do |d| points = points + d.points end + self.wrestlers.each do |w| + w.deductedPoints.each do |d| + points = points + d.points + end + end points end end diff --git a/app/models/teampointadjust.rb b/app/models/teampointadjust.rb index ca2d708..c78f9d0 100644 --- a/app/models/teampointadjust.rb +++ b/app/models/teampointadjust.rb @@ -1,7 +1,28 @@ class Teampointadjust < ActiveRecord::Base belongs_to :wrestler + belongs_to :school - after_save do - self.wrestler.lastFinishedMatch.advance_wrestlers + after_save do + advance_wrestlers_and_calc_team_score + end + + after_destroy do + advance_wrestlers_and_calc_team_score + end + + def advance_wrestlers_and_calc_team_score + #Team score needs calculated + if self.wrestler_id != nil + #In case this affects pool order + if self.wrestler.lastFinishedMatch + self.wrestler.lastFinishedMatch.advance_wrestlers + end + self.wrestler.school.calcScore + elsif self.school_id != nil + self.school.calcScore + end + end + if Rails.env.production? + handle_asynchronously :advance_wrestlers_and_calc_team_score end end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index e2a983a..3469424 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -86,4 +86,19 @@ class Tournament < ActiveRecord::Base m.save end end + + def pointAdjustments + point_adjustments = [] + self.schools.each do |s| + s.deductedPoints.each do |d| + point_adjustments << d + end + end + self.wrestlers.each do |w| + w.deductedPoints.each do |d| + point_adjustments << d + end + end + point_adjustments + end end diff --git a/app/views/layouts/_lsidebar.html.erb b/app/views/layouts/_lsidebar.html.erb index 1b72ab6..ccfabe7 100644 --- a/app/views/layouts/_lsidebar.html.erb +++ b/app/views/layouts/_lsidebar.html.erb @@ -21,6 +21,7 @@
<%= link_to "Weigh In Page" , "/tournaments/#{@tournament.id}/weigh_in" %>
<%= link_to "All Matches" , "/tournaments/#{@tournament.id}/matches" %>
<%= link_to "Full Screen Bout Board" , "/tournaments/#{@tournament.id}/up_matches?print=true" %> +
<%= link_to "Deduct Team Points" , "/tournaments/#{@tournament.id}/teampointadjust" %> <% if can? :destroy, @tournament %>
<%= link_to "Tournament Delegation" , "/tournaments/#{@tournament.id}/delegate" %> <% end %> diff --git a/app/views/tournaments/teampointadjust.html.erb b/app/views/tournaments/teampointadjust.html.erb new file mode 100644 index 0000000..3844354 --- /dev/null +++ b/app/views/tournaments/teampointadjust.html.erb @@ -0,0 +1,62 @@ +<%= link_to "Back to #{@tournament_name}", "/tournaments/#{@tournament_id}", :class=>"btn btn-default" %> +
+
+ + + +

New Point Adjustment

+
+
+ + + + + + + + + + + + <%= form_for Teampointadjust.new, :url => url_for(:controller => 'tournaments', :action => 'teampointadjust', :method => "post") do |f| %> + + + + + +
Wrestler DeductedSchool DeductedPoints To Deduct
<%= f.collection_select :wrestler_id, @tournament.wrestlers, :id, :name, :include_blank => true %><%= f.collection_select :school_id, @tournament.schools, :id, :name, :include_blank => true %><%= f.number_field :points, :step => 'any' %>
+

Please leave either wrestler or school blank. Please do not choose both (the team will be double decuted the points)

+ <% if can? :manage, @tournament %> + <%= submit_tag "Deduct Points", :class=>"btn btn-success"%> + <% end %> + <% end %> +
+<% if @point_adjustments.size > 0 %> +

Point Adjustments

+
+
+ + + + + + + + + + <% @point_adjustments.each do |point_adjustment| %> + + + + + + + <% end %> + +
AgainstPoints Deducted
<% if point_adjustment.school_id != nil %> + <%= point_adjustment.school.name %> + <% elsif point_adjustment.wrestler_id != nil %> + <%= point_adjustment.wrestler.name %> + <% end %> + <%= point_adjustment.points %><%= link_to 'Remove Point Adjustment', "/tournaments/#{@tournament.id}/#{point_adjustment.id}/remove_teampointadjust", method: :delete, confirm: 'Are you sure?', :class=>"btn btn-danger btn-sm" %>
+<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 78c826b..204a84f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,6 +45,9 @@ Wrestling::Application.routes.draw do get 'tournaments/:id/school_delegate' => 'tournaments#school_delegate', :as => :school_delegate post 'tournaments/:id/school_delegate' => 'tournaments#school_delegate', :as => :set_school_delegate delete 'tournaments/:id/:delegate/remove_school_delegate' => 'tournaments#remove_school_delegate', :as => :delete_school_delegate_path + get 'tournaments/:id/teampointadjust' => 'tournaments#teampointadjust' + post 'tournaments/:id/teampointadjust' => 'tournaments#teampointadjust' + delete 'tournaments/:id/:teampointadjust/remove_teampointadjust' => 'tournaments#remove_teampointadjust' # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/migrate/20160112152946_add_school_id_to_teampointsadjust.rb b/db/migrate/20160112152946_add_school_id_to_teampointsadjust.rb new file mode 100644 index 0000000..5102f27 --- /dev/null +++ b/db/migrate/20160112152946_add_school_id_to_teampointsadjust.rb @@ -0,0 +1,5 @@ +class AddSchoolIdToTeampointsadjust < ActiveRecord::Migration + def change + add_column :teampointadjusts, :school_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 2426aa0..f37635e 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: 20160106031418) do +ActiveRecord::Schema.define(version: 20160112152946) do create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false @@ -86,6 +86,7 @@ ActiveRecord::Schema.define(version: 20160106031418) do t.integer "wrestler_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "school_id" end add_index "teampointadjusts", ["wrestler_id"], name: "index_teampointadjusts_on_wrestler_id"