mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-31 19:45:45 +00:00
Here is another way to handle functionality that you may not want bloating your ActiveRecord class, yet belongs _on_ the model. In this case, I feel there is an abstraction we are still sorely missing.
60 lines
1.0 KiB
Ruby
60 lines
1.0 KiB
Ruby
class Tournament < ActiveRecord::Base
|
|
|
|
include GeneratesLoserNames
|
|
|
|
has_many :schools, dependent: :destroy
|
|
has_many :weights, dependent: :destroy
|
|
has_many :mats, dependent: :destroy
|
|
has_many :wrestlers, through: :weights
|
|
|
|
|
|
def tournament_types
|
|
["Pool to bracket"]
|
|
end
|
|
|
|
def matches
|
|
@matches = Match.where(tournament_id: self.id)
|
|
end
|
|
|
|
def createCustomWeights(value)
|
|
self.weights.destroy_all
|
|
if value == 'hs'
|
|
Weight::HS_WEIGHT_CLASSES.each do |w|
|
|
self.weights.create(max: w)
|
|
end
|
|
else
|
|
raise "Unspecified behavior"
|
|
end
|
|
end
|
|
|
|
def upcomingMatches
|
|
if matches.nil?
|
|
return nil
|
|
else
|
|
matches
|
|
end
|
|
end
|
|
|
|
def generateMatchups
|
|
@matches = Tournamentmatchgen.new(self).genMatches()
|
|
end
|
|
|
|
def destroyAllMatches
|
|
matches.destroy_all
|
|
end
|
|
|
|
def matchesByRound(round)
|
|
matches.joins(:weight).where(round: round).order("weights.max")
|
|
end
|
|
|
|
def assignBouts
|
|
bout_counts = Hash.new(0)
|
|
matches.each do |m|
|
|
m.bout_number = m.round * 1000 + bout_counts[m.round]
|
|
bout_counts[m.round] += 1
|
|
m.save!
|
|
end
|
|
end
|
|
|
|
end
|