Implement method to check if stage is over

This commit is contained in:
Daniel Schädler 2019-06-16 17:31:34 +02:00
parent 68efd3caae
commit 4e72d015ff
2 changed files with 64 additions and 0 deletions

View File

@ -18,4 +18,14 @@ class Stage < ApplicationRecord
[]
end
end
def over?
return matches.find { |m| m.state != 'finished' }.nil? unless matches.size.zero?
unless groups.size.zero? && groups.map(&:matches).flatten.size.zero?
return groups.map(&:matches).flatten.find { |m| m.state != 'finished' }.nil?
end
false
end
end

View File

@ -40,4 +40,58 @@ RSpec.describe Stage, type: :model do
end
end
end
describe '#over?' do
context 'group stage' do
context 'with unfinished matches' do
it 'returns false' do
expect(create(:group_stage).over?).to eq(false)
end
end
context 'with all matches finished' do
let(:finished_group_stage) do
group_stage = create(:group_stage)
group_stage.groups.map(&:matches).flatten.each do |m|
m.state = :finished
m.save!
end
group_stage
end
it 'returns true' do
expect(finished_group_stage.over?).to eq(true)
end
end
end
context 'playoff stage' do
context 'with unfinished matches' do
it 'returns false' do
expect(create(:playoff_stage).over?).to eq(false)
end
end
context 'with all matches finished' do
let(:finished_playoff_stage) do
playoff_stage = create(:playoff_stage)
playoff_stage.matches.each do |m|
m.state = :finished
m.save!
end
playoff_stage
end
it 'returns true' do
expect(finished_playoff_stage.over?).to eq(true)
end
end
end
context 'empty stage' do
it 'returns false' do
expect(create(:stage).over?).to eq(false)
end
end
end
end