1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-25 01:14:43 +00:00
Files
wrestlingdev.com/app/models/mat.rb

51 lines
1.1 KiB
Ruby

class Mat < ActiveRecord::Base
belongs_to :tournament
has_many :matches
validates :name, presence: true
before_destroy do
if tournament.matches.size > 0
tournament.reset_mats
matsToAssign = tournament.mats.select{|m| m.id != self.id}
tournament.assign_mats(matsToAssign)
end
end
after_create do
if tournament.matches.size > 0
tournament.reset_mats
matsToAssign = tournament.mats
tournament.assign_mats(matsToAssign)
end
end
def assign_next_match
t_matches = tournament.matches.select{|m| m.mat_id == nil && m.finished != 1 && m.bout_number != nil}.sort_by{|m| m.bout_number}
if t_matches.size > 0 and self.unfinished_matches.size < 4
match = t_matches.sort_by{|m| m.bout_number}.first
match.mat_id = self.id
if match.save
if match.w1
match.wrestler1.touch
match.wrestler1.school.touch
end
if match.w2
match.wrestler2.touch
match.wrestler2.school.touch
end
return true
else
return false
end
else
return true
end
end
def unfinished_matches
matches.select{|m| m.finished != 1}.sort_by{|m| m.bout_number}
end
end