diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 370662d..0d605fd 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -66,6 +66,7 @@ class MatchesController < ApplicationController # PATCH/PUT /matches/1 def update new_state = match_params['state'] + synced_tournament = nil Match.transaction do if @match.update(match_params) @@ -78,12 +79,13 @@ class MatchesController < ApplicationController end render json: @match - push_sync_if_needed!(@match.tournament) + synced_tournament = @match.tournament else render json: @match.errors, status: :unprocessable_entity raise ActiveRecord::Rollback end end + push_sync_if_needed!(synced_tournament) unless synced_tournament.nil? end private diff --git a/app/controllers/stages_controller.rb b/app/controllers/stages_controller.rb index 8760686..fcdac4d 100644 --- a/app/controllers/stages_controller.rb +++ b/app/controllers/stages_controller.rb @@ -19,17 +19,19 @@ class StagesController < ApplicationController return end + synced_tournament = nil Stage.transaction do if @stage.update(stage_params) handle_group_stage_end render json: @stage - push_sync_if_needed!(@stage.tournament) + synced_tournament = @stage.tournament else render json: @stage.errors, status: :unprocessable_entity raise ActiveRecord::Rollback end end + push_sync_if_needed!(synced_tournament) unless synced_tournament.nil? else render json: { error: 'The state attribute may only be changed to finished' diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 5576941..cf48ba7 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -85,6 +85,7 @@ class TournamentsController < ApplicationController # PATCH/PUT /tournaments/1 def update + synced_tournament = nil Tournament.transaction do if only_playoff_teams_amount_changed @tournament.instant_finalists_amount, @tournament.intermediate_round_participants_amount = @@ -94,13 +95,14 @@ class TournamentsController < ApplicationController ) end if @tournament.update(tournament_params) - push_sync_if_needed!(@tournament) + synced_tournament = @tournament render json: @tournament else render json: @tournament.errors, status: :unprocessable_entity raise ActiveRecord::Rollback end end + push_sync_if_needed!(synced_tournament) unless synced_tournament.nil? end # DELETE /tournaments/1 diff --git a/spec/controllers/stages_controller_spec.rb b/spec/controllers/stages_controller_spec.rb index 8a66ced..7e768a0 100644 --- a/spec/controllers/stages_controller_spec.rb +++ b/spec/controllers/stages_controller_spec.rb @@ -55,11 +55,11 @@ RSpec.describe StagesController, type: :controller do end it 'adds new stages to the tournament' do - expect(running_group_stage.tournament.stages.size).to be > 1 + expect(running_group_stage.tournament.reload.stages.size).to be > 1 end it 'adds the right teams' do - expect(running_group_stage.tournament.stages.max_by(&:level).teams) + expect(running_group_stage.tournament.reload.stages.max_by(&:level).teams) .to match_array(GroupStageService.get_advancing_teams(running_group_stage)) end end