From 5004dec39abfbbd698390a8dfdac85ff4dafe68c Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Tue, 11 Jun 2019 21:45:25 +0200 Subject: [PATCH] Change playoff transition relevant attributes of tournament on update They are only updated when playoff_teams_amount changed alone --- app/controllers/tournaments_controller.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index cd7ebce..9fa7f18 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -64,10 +64,20 @@ class TournamentsController < ApplicationController # PATCH/PUT /tournaments/1 def update - if @tournament.update(tournament_params) - render json: @tournament - else - render json: @tournament.errors, status: :unprocessable_entity + Tournament.transaction do + if only_playoff_teams_amount_changed + @tournament.instant_finalists_amount, @tournament.intermediate_round_participants_amount = + TournamentService.calculate_default_amount_of_teams_advancing( + params['playoff_teams_amount'].to_i, + @tournament.stages.find_by(level: -1).groups.size + ) + end + if @tournament.update(tournament_params) + render json: @tournament + else + render json: @tournament.errors, status: :unprocessable_entity + raise ActiveRecord::Rollback + end end end