diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb
index 40a6ca1..7740d5e 100644
--- a/app/controllers/schools_controller.rb
+++ b/app/controllers/schools_controller.rb
@@ -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
diff --git a/app/models/school.rb b/app/models/school.rb
index 2181e74..738da56 100644
--- a/app/models/school.rb
+++ b/app/models/school.rb
@@ -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
diff --git a/app/services/school_services/baumspage_roster_import.rb b/app/services/school_services/baumspage_roster_import.rb
new file mode 100644
index 0000000..e0acccf
--- /dev/null
+++ b/app/services/school_services/baumspage_roster_import.rb
@@ -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
\ No newline at end of file
diff --git a/app/views/schools/_baums_roster_import.html.erb b/app/views/schools/_baums_roster_import.html.erb
new file mode 100644
index 0000000..4af48b8
--- /dev/null
+++ b/app/views/schools/_baums_roster_import.html.erb
@@ -0,0 +1,11 @@
+<% if can? :manage, @school %>
+
+