mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-05 22:21:26 +00:00
Added baumspage roster importer
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
65
app/services/school_services/baumspage_roster_import.rb
Normal file
65
app/services/school_services/baumspage_roster_import.rb
Normal 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
|
||||
11
app/views/schools/_baums_roster_import.html.erb
Normal file
11
app/views/schools/_baums_roster_import.html.erb
Normal 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 %>
|
||||
@@ -74,3 +74,6 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<% if can? :manage, @school %>
|
||||
<%= render 'baums_roster_import' %>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user