From c9e1e153df0449c5942f768df419b3b98404dab5 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 12 Jun 2019 21:53:36 +0200 Subject: [PATCH] Add interactor to advance all teams stuck in single team matches After playoffs are created, before saving the stage this interactor goes to the intermediate stage, looks for all the matches that are :single_team matches and populates the match below with their respective "winner". --- app/interactors/add_playoffs_to_tournament.rb | 3 ++- .../advance_teams_in_intermediate_stage.rb | 14 ++++++++++++++ .../add_playoffs_to_tournament_and_save.rb | 2 +- ...ms_in_intermediate_stage_interactor_spec.rb | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 app/interactors/advance_teams_in_intermediate_stage.rb create mode 100644 spec/interactors/advance_teams_in_intermediate_stage_interactor_spec.rb diff --git a/app/interactors/add_playoffs_to_tournament.rb b/app/interactors/add_playoffs_to_tournament.rb index 9efc2ec..59753b0 100644 --- a/app/interactors/add_playoffs_to_tournament.rb +++ b/app/interactors/add_playoffs_to_tournament.rb @@ -12,7 +12,8 @@ class AddPlayoffsToTournament else tournament.stages.concat playoff_stages end - context.object_to_save = tournament + context.intermediate_stage = tournament.stages.find(&:intermediate_stage?) + context.object_to_save = [tournament] else context.fail! end diff --git a/app/interactors/advance_teams_in_intermediate_stage.rb b/app/interactors/advance_teams_in_intermediate_stage.rb new file mode 100644 index 0000000..cf060b6 --- /dev/null +++ b/app/interactors/advance_teams_in_intermediate_stage.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class AdvanceTeamsInIntermediateStage + include Interactor + + def call + intermediate_stage = context.intermediate_stage + return if intermediate_stage.nil? + + intermediate_stage.matches.select { |m| m.state == 'single_team' } + .each { |match| PopulateMatchBelowAndSave.call(match: match) } + context.object_to_save << intermediate_stage + end +end diff --git a/app/organizers/add_playoffs_to_tournament_and_save.rb b/app/organizers/add_playoffs_to_tournament_and_save.rb index 766add6..e2e5c49 100644 --- a/app/organizers/add_playoffs_to_tournament_and_save.rb +++ b/app/organizers/add_playoffs_to_tournament_and_save.rb @@ -3,5 +3,5 @@ class AddPlayoffsToTournamentAndSave include Interactor::Organizer - organize AddPlayoffsToTournament, SaveApplicationRecordObject + organize AddPlayoffsToTournament, AdvanceTeamsInIntermediateStage, SaveApplicationRecordObject end diff --git a/spec/interactors/advance_teams_in_intermediate_stage_interactor_spec.rb b/spec/interactors/advance_teams_in_intermediate_stage_interactor_spec.rb new file mode 100644 index 0000000..cdb13ce --- /dev/null +++ b/spec/interactors/advance_teams_in_intermediate_stage_interactor_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +RSpec.describe AdvanceTeamsInIntermediateStage do + context 'intermediate_stage is nil' do + let(:context) do + AdvanceTeamsInIntermediateStage.call(intermediate_stage: nil) + end + + it 'succeeds' do + expect(context).to be_a_success + end + + it 'doesn\'t call PopulateMatchBelow' do + expect(PopulateMatchBelowAndSave).not_to receive(:call) + context + end + end +end