From 1304f86652a2dbd2fac171b3bc1795194a84c4c8 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 12:01:34 +0200 Subject: [PATCH] Add winner method to match --- app/models/match.rb | 5 +++++ spec/models/match_spec.rb | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/match.rb b/app/models/match.rb index 19edf02..2f0410b 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -19,6 +19,11 @@ class Match < ApplicationRecord stage ? stage.owner : group.owner end + def winner + evaluate_winner + # This should maybe be cached or put into db + end + private def evaluate_winner diff --git a/spec/models/match_spec.rb b/spec/models/match_spec.rb index 01d34c5..f24ce70 100644 --- a/spec/models/match_spec.rb +++ b/spec/models/match_spec.rb @@ -43,6 +43,23 @@ RSpec.describe Match, type: :model do end end + context '#winner' do + before do + @undecided_match = create(:undecided_group_match) + @decided_match = create(:decided_playoff_match) + end + + it 'returns a winner Team for a decided match' do + winner = @decided_match.winner + expect(winner).to be_a Team + end + + it 'returns nil for an undecided match' do + winner = @undecided_match.winner + expect(winner).to be(nil) + end + end + context '#teams' do before do @playoff_match = create(:running_playoff_match)