From e0da9ff7b5f9f92dd30f1730377678ace0d5c361 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Thu, 13 Jun 2019 00:19:49 +0200 Subject: [PATCH] Assign empty array if object_to_save is nil before pushing values to it https://stackoverflow.com/questions/12163625/create-or-append-to-array-in-ruby --- app/interactors/add_group_stage_to_tournament.rb | 2 +- app/interactors/add_playoffs_to_tournament.rb | 2 +- app/interactors/advance_teams_in_intermediate_stage.rb | 2 +- app/interactors/populate_match_below.rb | 2 +- app/interactors/update_groups_group_scores.rb | 2 +- spec/interactors/populate_match_below_interactor_spec.rb | 2 +- spec/interactors/update_groups_group_scores_interactor_spec.rb | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/interactors/add_group_stage_to_tournament.rb b/app/interactors/add_group_stage_to_tournament.rb index 23e81de..e9866c2 100644 --- a/app/interactors/add_group_stage_to_tournament.rb +++ b/app/interactors/add_group_stage_to_tournament.rb @@ -13,7 +13,7 @@ class AddGroupStageToTournament tournament.instant_finalists_amount, tournament.intermediate_round_participants_amount = TournamentService.calculate_default_amount_of_teams_advancing(tournament.playoff_teams_amount, group_stage.groups.size) - context.object_to_save = tournament + (context.object_to_save ||= []) << tournament rescue StandardError context.fail! end diff --git a/app/interactors/add_playoffs_to_tournament.rb b/app/interactors/add_playoffs_to_tournament.rb index 59753b0..ce530cb 100644 --- a/app/interactors/add_playoffs_to_tournament.rb +++ b/app/interactors/add_playoffs_to_tournament.rb @@ -13,7 +13,7 @@ class AddPlayoffsToTournament tournament.stages.concat playoff_stages end context.intermediate_stage = tournament.stages.find(&:intermediate_stage?) - context.object_to_save = [tournament] + (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 index cf060b6..a85ba63 100644 --- a/app/interactors/advance_teams_in_intermediate_stage.rb +++ b/app/interactors/advance_teams_in_intermediate_stage.rb @@ -9,6 +9,6 @@ class AdvanceTeamsInIntermediateStage intermediate_stage.matches.select { |m| m.state == 'single_team' } .each { |match| PopulateMatchBelowAndSave.call(match: match) } - context.object_to_save << intermediate_stage + (context.object_to_save ||= []) << intermediate_stage end end diff --git a/app/interactors/populate_match_below.rb b/app/interactors/populate_match_below.rb index 96a58c3..e224639 100644 --- a/app/interactors/populate_match_below.rb +++ b/app/interactors/populate_match_below.rb @@ -7,7 +7,7 @@ class PopulateMatchBelow match = context.match begin objects_to_save = PlayoffStageService.populate_match_below(match) - context.object_to_save = objects_to_save + (context.object_to_save ||= []) << objects_to_save rescue StandardError context.fail! end diff --git a/app/interactors/update_groups_group_scores.rb b/app/interactors/update_groups_group_scores.rb index 3ea49e2..fbb05fc 100644 --- a/app/interactors/update_groups_group_scores.rb +++ b/app/interactors/update_groups_group_scores.rb @@ -4,7 +4,7 @@ class UpdateGroupsGroupScores include Interactor def call - context.object_to_save = GroupStageService.update_group_scores(context.group) + (context.object_to_save ||= []) << GroupStageService.update_group_scores(context.group) rescue StandardError context.fail! end diff --git a/spec/interactors/populate_match_below_interactor_spec.rb b/spec/interactors/populate_match_below_interactor_spec.rb index 22ef725..2201c91 100644 --- a/spec/interactors/populate_match_below_interactor_spec.rb +++ b/spec/interactors/populate_match_below_interactor_spec.rb @@ -21,7 +21,7 @@ RSpec.describe PopulateMatchBelow, type: :interactor do end it 'provides the objects to save' do - expect(context.object_to_save).to match_array(@objects_to_save) + expect(context.object_to_save.flatten).to match_array(@objects_to_save.flatten) end end diff --git a/spec/interactors/update_groups_group_scores_interactor_spec.rb b/spec/interactors/update_groups_group_scores_interactor_spec.rb index e664ffc..bc9a3d9 100644 --- a/spec/interactors/update_groups_group_scores_interactor_spec.rb +++ b/spec/interactors/update_groups_group_scores_interactor_spec.rb @@ -22,7 +22,7 @@ RSpec.describe UpdateGroupsGroupScores, type: :interactor do end it 'provides the objects to save' do - expect(context.object_to_save).to eq(@group_scores) + expect(context.object_to_save.flatten).to eq(@group_scores) end end