mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-12 16:25:41 +00:00
Re assign bouts to mats if mat is destroyed or created
This commit is contained in:
@@ -2,10 +2,26 @@ class Mat < ActiveRecord::Base
|
||||
belongs_to :tournament
|
||||
has_many :matches
|
||||
|
||||
before_destroy do
|
||||
if tournament.matches.size > 0
|
||||
tournament.resetMats
|
||||
matsToAssign = tournament.mats.select{|m| m.id != self.id}
|
||||
tournament.assignMats(matsToAssign)
|
||||
end
|
||||
end
|
||||
|
||||
after_create do
|
||||
if tournament.matches.size > 0
|
||||
tournament.resetMats
|
||||
matsToAssign = tournament.mats
|
||||
tournament.assignMats(matsToAssign)
|
||||
end
|
||||
end
|
||||
|
||||
def assignNextMatch
|
||||
t_matches = tournament.matches.where(mat_id: nil)
|
||||
t_matches = tournament.matches.select{|m| m.mat_id == nil}
|
||||
if t_matches.size > 0
|
||||
match = t_matches.order(:bout_number).first
|
||||
match = t_matches.sort_by{|m| m.bout_number}.first
|
||||
match.mat_id = self.id
|
||||
match.save
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user