Implement change match score Interactor

This commit is contained in:
Daniel Schädler 2018-11-20 13:49:04 +01:00
parent 2fc6d8e75b
commit 826d9717e3
5 changed files with 51 additions and 31 deletions

View File

@ -4,7 +4,11 @@ class ChangeMatchScore
include Interactor include Interactor
def call def call
context.match.score_team1 = context.score_team1 match = context.match
context.match.score_team2 = context.score_team2 match.score_team1 = context.score_team1
match.score_team2 = context.score_team2
match.status = match.evaluate_status
match.save
context.match = match
end end
end end

View File

@ -1,27 +0,0 @@
# frozen_string_literal: true
require 'match'
class UpdateMatchStatus
include Interactor
def call
context.match.status = evaluate_new_match_state(context.match)
end
def evaluate_new_match_state(match)
score_team1 = match.score_team1
score_team2 = match.score_team2
if score_team1 < score_team2
return Match.team2_won
elsif score_team2 < score_team1
return Match.team1_won
else
if match.is_group_match
return Match.undecided
else
return Match.in_progress
end
end
end
end

View File

@ -8,4 +8,18 @@ class Match < ApplicationRecord
has_many :scores, dependent: :destroy has_many :scores, dependent: :destroy
validates :scores, length: { maximum: 2 } validates :scores, length: { maximum: 2 }
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

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class UpdateMatchScore class UpdateMatch
include Interactor::Organizer include Interactor::Organizer
organize ChangeMatchScore, UpdateMatchScore organize ChangeMatchScore
end end

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
describe ChangeMatchScore do
subject(:context) do
@match = create(:match)
@score_team1 = 4
@score_team2 = 2
@expected_match_status = :team1_won
ChangeMatchScore.call(match: @match, score_team1: @score_team1, score_team2: @score_team2)
end
before do
allow(Match).to receive(:save).and_return(true)
allow(Match).to receive(:evaluate_status).and_return(@expected_match_status)
end
it 'succeeds' do
expect(context).to be_a_success
end
it 'changes match score accordingly' do
expect(context.match.score_team1).to eq(@score_team1)
expect(context.match.score_team2).to eq(@score_team2)
end
it 'changes match state accordingly' do
expect(context.match.status).to eq(@expected_match_status)
end
end