1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-24 17:04:43 +00:00

Added baumspage roster importer

This commit is contained in:
2019-01-10 13:25:31 +00:00
parent 046b48370e
commit 31323593c9
9 changed files with 159 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update, :destroy, :stats]
before_action :set_school, only: [:import_baumspage_roster, :show, :edit, :update, :destroy, :stats]
before_action :check_access_director, only: [:new,:create,:destroy]
before_action :check_access_delegate, only: [:update,:edit]
before_action :check_access_delegate, only: [:import_baumspage_roster, :update,:edit]
def stats
@@ -70,6 +70,16 @@ class SchoolsController < ApplicationController
end
end
def import_baumspage_roster
import_text = params[:school][:baums_text]
respond_to do |format|
if BaumspageRosterImport.new(@school,import_text).import_roster
format.html { redirect_to "/schools/#{@school.id}", notice: 'Import successful' }
format.json { render action: 'show', status: :created, location: @school }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_school
@@ -78,7 +88,7 @@ class SchoolsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def school_params
params.require(:school).permit(:name, :score, :tournament_id)
params.require(:school).permit(:name, :score, :tournament_id, :baums_text)
end
def check_access_director

View File

@@ -6,6 +6,8 @@ class School < ActiveRecord::Base
validates :name, presence: true
attr_accessor :baums_text
before_destroy do
self.tournament.destroy_all_matches
end

View File

@@ -0,0 +1,65 @@
class BaumspageRosterImport
def initialize( school, import_text )
@school = school
@import_text = import_text
end
def import_roster
@school.wrestlers.each do |wrestler|
wrestler.destroy
end
parse_import_text
end
def parse_import_text
extra = false
@import_text.each_line do |line|
if line !~ /Extra Wrestlers/
if extra == false
parse_starter(line)
else
parse_extra(line)
end
else
extra = true
end
end
end
def parse_starter(line)
extra = false
wrestler_array = line.split(',')
wrestler_losses_array_spot = wrestler_array.size - 1
wrestler_wins_array_spot = wrestler_array.size - 2
last_criteria_array_spot = wrestler_wins_array_spot - 1
wrestler_criteria = ""
(4..last_criteria_array_spot).each do |criteria_line|
wrestler_criteria = wrestler_criteria + ", " + wrestler_array[criteria_line]
end
if wrestler_array[1]
create_wrestler("#{wrestler_array[2]} #{wrestler_array[1]}", "#{wrestler_array[0]}", "#{wrestler_criteria}", "#{wrestler_array[wrestler_wins_array_spot]}", "#{wrestler_array[wrestler_losses_array_spot]}",extra)
end
end
def parse_extra(line)
extra = true
wrestler_array = line.split(',')
wrestler_losses_array_spot = wrestler_array.size - 1
wrestler_wins_array_spot = wrestler_array.size - 2
if wrestler_array[1]
create_wrestler("#{wrestler_array[2]} #{wrestler_array[1]}", "#{wrestler_array[0]}", "", "#{wrestler_array[wrestler_wins_array_spot]}", "#{wrestler_array[wrestler_losses_array_spot]}",extra)
end
end
def create_wrestler(name,weight,criteria,season_win,season_loss,extra)
wrestler = Wrestler.new
wrestler.name = name
wrestler.school_id = @school.id
wrestler.weight_id = Weight.where("tournament_id = ? and max = ?", @school.tournament.id, weight).first.id
wrestler.criteria = criteria
wrestler.season_win = season_win
wrestler.season_loss = season_loss
wrestler.extra = extra
wrestler.save
end
end

View File

@@ -0,0 +1,11 @@
<% if can? :manage, @school %>
<br><br>
<h3>Import Baumspage Roster</h3>
<%= form_for(School.new, url: import_baumspage_roster_url(@school)) do |f| %>
<div class="field">
<%= f.label 'Baumspage roster text' %><br>
<%= f.text_area :baums_text, cols: "30", rows: "20" %>
</div>
<%= submit_tag "Import", :class=>"btn btn-success", data: { confirm: 'Are you sure? This will delete your current roster.' }%>
<% end %>
<% end %>

View File

@@ -74,3 +74,6 @@
</tbody>
</table>
<% if can? :manage, @school %>
<%= render 'baums_roster_import' %>
<% end %>

View File

@@ -39,5 +39,7 @@ module Wrestling
config.autoload_paths += %W(#{config.root}/app/services/wrestler_services)
config.autoload_paths += %W(#{config.root}/app/services/weight_services)
config.autoload_paths += %W(#{config.root}/app/services/bracket_advancement)
config.autoload_paths += %W(#{config.root}/app/services/school_services)
end
end

View File

@@ -56,6 +56,8 @@ Wrestling::Application.routes.draw do
post "/wrestlers/update_pool" => "wrestlers#update_pool"
get "schools/:id/stats" => "schools#stats"
post "/schools/:id/import_baumspage_roster" => "schools#import_baumspage_roster", :as => :import_baumspage_roster
#API
get "/api/tournaments" => "api#tournaments"
get "/api/tournaments/user" => "api#currentUserTournaments"

View File

@@ -53,6 +53,21 @@ class SchoolsControllerTest < ActionController::TestCase
assert_redirected_to '/static_pages/not_allowed'
end
def baums_import
baums_text = "***** 2019-01-09 13:36:50 *****
Some School
Some Guy
106,,,,,,,,,
113,Guy,Another,9,,,,,5,7
120,Guy2,Another,9,,,,,0,0
126,Guy3,Another,10,,,,5@120,2,2
******* Extra Wrestlers *******
120,Guy4,Another,10,0,3
126,Guy5,Another,9,,"
post :import_baumspage_roster, params: { id: @school.id, school: { baums_text: baums_text } }
end
test "logged in tournament owner should get edit school page" do
sign_in_owner
get_edit
@@ -172,4 +187,22 @@ class SchoolsControllerTest < ActionController::TestCase
success
end
test "logged in school delegate can import baumspage roster" do
sign_in_school_delegate
baums_import
assert_redirected_to "/schools/#{@school.id}"
end
test "logged in tournament delegate can import baumspage roster" do
sign_in_tournament_delegate
baums_import
assert_redirected_to "/schools/#{@school.id}"
end
test "logged in user cannot import baumspage roster" do
sign_in_non_owner
baums_import
redirect
end
end

View File

@@ -0,0 +1,28 @@
require 'test_helper'
class BaumspageImporterTest < ActionDispatch::IntegrationTest
def setup
@school = School.find(1)
@baums_text = "***** 2019-01-09 13:36:50 *****
Some School
Some Guy
106,,,,,,,,,
113,Guy,Another,9,,,,,5,7
120,Guy2,Another,9,,,,,0,0
126,Guy3,Another,10,,,,5@120,2,2
******* Extra Wrestlers *******
120,Guy4,Another,10,0,3
126,Guy5,Another,9,,"
end
test "5 wrestlers get created with Baumspage Importer" do
BaumspageRosterImport.new(@school,@baums_text).import_roster
assert @school.reload.wrestlers.size == 5
extras = @school.wrestlers.select{|w| w.extra == true}
assert extras.size == 2
guy = @school.wrestlers.select{|w| w.name == "Another Guy"}.first
assert guy.season_win == 5
assert guy.season_loss == 7
end
end