diff --git a/app/interactors/populate_match_below.rb b/app/interactors/populate_match_below.rb index 55539f0..96a58c3 100644 --- a/app/interactors/populate_match_below.rb +++ b/app/interactors/populate_match_below.rb @@ -6,8 +6,8 @@ class PopulateMatchBelow def call match = context.match begin - PlayoffStageService.populate_match_below(match) - context.object_to_save = match + objects_to_save = PlayoffStageService.populate_match_below(match) + context.object_to_save = objects_to_save rescue StandardError context.fail! end diff --git a/app/services/playoff_stage_service.rb b/app/services/playoff_stage_service.rb index f6beb54..60d236b 100644 --- a/app/services/playoff_stage_service.rb +++ b/app/services/playoff_stage_service.rb @@ -71,6 +71,10 @@ class PlayoffStageService end end + # Populates the match below given match with the winners of the matches above + # + # @param current_match [Match] The Match which finished, the match below it gets populated + # @return [Array] the objects that changed and need to be saved def self.populate_match_below(current_match) current_stage = current_match.stage next_stage = current_stage.tournament.stages.find { |s| s.level == current_stage.level - 1 } @@ -95,10 +99,8 @@ class PlayoffStageService # If a match is not decided yet, it will return nil as winner. # This is not allowed in Database. The following code filters out MatchScores that contain nil as team. match_scores = match_scores.select { |ms| ms.team.present? } - match_scores.each(&:save) match_below.match_scores = match_scores - match_below.save - match_below + [match_below, match_scores].flatten end class << self