From 826d9717e3003f7d1eaf96fe672c69ec5da54eca Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Tue, 20 Nov 2018 13:49:04 +0100 Subject: [PATCH] Implement change match score Interactor --- app/interactors/ChangeMatchScore.rb | 8 ++++-- app/interactors/UpdateMatchStatus.rb | 27 ------------------- app/models/match.rb | 14 ++++++++++ app/organizers/UpdateMatchScore.rb | 4 +-- spec/interactors/change_match_score_spec.rb | 29 +++++++++++++++++++++ 5 files changed, 51 insertions(+), 31 deletions(-) delete mode 100644 app/interactors/UpdateMatchStatus.rb create mode 100644 spec/interactors/change_match_score_spec.rb diff --git a/app/interactors/ChangeMatchScore.rb b/app/interactors/ChangeMatchScore.rb index 5cd5546..dde812c 100644 --- a/app/interactors/ChangeMatchScore.rb +++ b/app/interactors/ChangeMatchScore.rb @@ -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 diff --git a/app/interactors/UpdateMatchStatus.rb b/app/interactors/UpdateMatchStatus.rb deleted file mode 100644 index ae26f3d..0000000 --- a/app/interactors/UpdateMatchStatus.rb +++ /dev/null @@ -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 diff --git a/app/models/match.rb b/app/models/match.rb index a0e837b..0c5fc23 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -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 diff --git a/app/organizers/UpdateMatchScore.rb b/app/organizers/UpdateMatchScore.rb index 016b550..7bbd959 100644 --- a/app/organizers/UpdateMatchScore.rb +++ b/app/organizers/UpdateMatchScore.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -class UpdateMatchScore +class UpdateMatch include Interactor::Organizer - organize ChangeMatchScore, UpdateMatchScore + organize ChangeMatchScore end diff --git a/spec/interactors/change_match_score_spec.rb b/spec/interactors/change_match_score_spec.rb new file mode 100644 index 0000000..e1769bb --- /dev/null +++ b/spec/interactors/change_match_score_spec.rb @@ -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