Add State to Model

This commit is contained in:
Daniel Schädler 2018-11-29 10:47:17 +01:00
parent fd02a1ecaf
commit 1c318cde1a
1 changed files with 16 additions and 0 deletions

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class Match < ApplicationRecord class Match < ApplicationRecord
enum state: %i[single_team not_ready not_started in_progress team1_won team2_won undecided]
belongs_to :stage, optional: true belongs_to :stage, optional: true
belongs_to :group, optional: true belongs_to :group, optional: true
has_many :scores, dependent: :destroy has_many :scores, dependent: :destroy
@ -14,4 +16,18 @@ class Match < ApplicationRecord
def stage_xor_group def stage_xor_group
errors.add(:stage_xor_group, 'Stage and Group missing or both present') unless stage.present? ^ group.present? errors.add(:stage_xor_group, 'Stage and Group missing or both present') unless stage.present? ^ group.present?
end end
def evaluate_status
if score_team1 < score_team2
:team2_won
elsif score_team2 < score_team1
:team1_won
else
group_match? ? :undecided : :in_progress
end
end
def group_match?
group.present?
end
end end