Implement change match score Interactor
This commit is contained in:
parent
2fc6d8e75b
commit
826d9717e3
|
|
@ -4,7 +4,11 @@ class ChangeMatchScore
|
|||
include Interactor
|
||||
|
||||
def call
|
||||
context.match.score_team1 = context.score_team1
|
||||
context.match.score_team2 = context.score_team2
|
||||
match = context.match
|
||||
match.score_team1 = context.score_team1
|
||||
match.score_team2 = context.score_team2
|
||||
match.status = match.evaluate_status
|
||||
match.save
|
||||
context.match = match
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -8,4 +8,18 @@ class Match < ApplicationRecord
|
|||
has_many :scores, dependent: :destroy
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class UpdateMatchScore
|
||||
class UpdateMatch
|
||||
include Interactor::Organizer
|
||||
|
||||
organize ChangeMatchScore, UpdateMatchScore
|
||||
organize ChangeMatchScore
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Reference in New Issue