From 3c98fd0545df2a7b74192970821bc60f680ebbb5 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 10 May 2019 11:35:58 +0200 Subject: [PATCH] Update group_scores on match_score change if match is a group match --- app/controllers/match_scores_controller.rb | 1 + app/interactors/update_groups_group_scores.rb | 11 +++++ .../update_groups_group_scores_and_save.rb | 7 +++ ...ate_groups_group_scores_interactor_spec.rb | 44 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 app/interactors/update_groups_group_scores.rb create mode 100644 app/organizers/update_groups_group_scores_and_save.rb create mode 100644 spec/interactors/update_groups_group_scores_interactor_spec.rb diff --git a/app/controllers/match_scores_controller.rb b/app/controllers/match_scores_controller.rb index 80c18ae..92f7dc0 100644 --- a/app/controllers/match_scores_controller.rb +++ b/app/controllers/match_scores_controller.rb @@ -13,6 +13,7 @@ class MatchScoresController < ApplicationController # PATCH/PUT /scores/1 def update if @match_score.update(match_score_params) + UpdateGroupsGroupScoresAndSave.call(group: @match_score.match.group) if @match_score.part_of_group_match? render json: @match_score else render json: @match_score.errors, status: :unprocessable_entity diff --git a/app/interactors/update_groups_group_scores.rb b/app/interactors/update_groups_group_scores.rb new file mode 100644 index 0000000..3ea49e2 --- /dev/null +++ b/app/interactors/update_groups_group_scores.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class UpdateGroupsGroupScores + include Interactor + + def call + context.object_to_save = GroupStageService.update_group_scores(context.group) + rescue StandardError + context.fail! + end +end diff --git a/app/organizers/update_groups_group_scores_and_save.rb b/app/organizers/update_groups_group_scores_and_save.rb new file mode 100644 index 0000000..41d641f --- /dev/null +++ b/app/organizers/update_groups_group_scores_and_save.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class UpdateGroupsGroupScoresAndSave + include Interactor::Organizer + + organize UpdateGroupsGroupScores, SaveApplicationRecordObject +end diff --git a/spec/interactors/update_groups_group_scores_interactor_spec.rb b/spec/interactors/update_groups_group_scores_interactor_spec.rb new file mode 100644 index 0000000..83d1068 --- /dev/null +++ b/spec/interactors/update_groups_group_scores_interactor_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +RSpec.describe UpdateGroupsGroupScores do + before do + @group = create(:group) + @group_scores = create_list(:group_score, 2) + end + + context 'save succeeds' do + let(:context) do + UpdateGroupsGroupScores.call(group: @group) + end + + before do + allow(GroupStageService) + .to receive(:update_group_scores).with(@group) + .and_return(@group_scores) + end + + it 'succeeds' do + expect(context).to be_a_success + end + + it 'provides the objects to save' do + expect(context.object_to_save).to eq(@group_scores) + end + end + + context 'exception is thrown' do + let(:context) do + UpdateGroupsGroupScores.call(group: @group) + end + before do + allow(GroupStageService) + .to receive(:update_group_scores).with(@group) + .and_throw('This failed :(') + end + + it 'fails' do + test = context.failure? + expect(test).to eq(true) + end + end +end