Generalize tournament save interactor

It now is responsible for saving all ApplicationRecord objects to the
database. This will reduce code duplication one we have other objects
that need to be saved. (As we will soon need to save individual matches)
This commit is contained in:
Daniel Schädler 2019-05-07 09:51:37 +02:00
parent be24b1bc39
commit b5ab7731b0
9 changed files with 24 additions and 24 deletions

View File

@ -38,14 +38,14 @@ class TournamentsController < ApplicationController
if group_stage if group_stage
groups = organize_teams_in_groups(teams) groups = organize_teams_in_groups(teams)
# add groups to tournament # add groups to tournament
result = AddGroupStageToTournamentAndSaveTournamentToDatabase.call(tournament: tournament, groups: groups) result = AddGroupStageToTournamentAndSave.call(tournament: tournament, groups: groups)
else else
# convert teams parameter into Team objects # convert teams parameter into Team objects
teams = teams.map(&method(:find_or_create_team)) teams = teams.map(&method(:find_or_create_team))
# associate provided teams with tournament # associate provided teams with tournament
tournament.teams = teams tournament.teams = teams
# add playoff stage to tournament # add playoff stage to tournament
result = AddPlayoffsToTournamentAndSaveTournamentToDatabase.call(tournament: tournament) result = AddPlayoffsToTournamentAndSave.call(tournament: tournament)
end end
# validate tournament # validate tournament
unless tournament.valid? unless tournament.valid?

View File

@ -10,7 +10,7 @@ class AddGroupStageToTournament
begin begin
group_stage = GroupStageService.generate_group_stage(groups) group_stage = GroupStageService.generate_group_stage(groups)
tournament.stages = [group_stage] tournament.stages = [group_stage]
context.tournament = tournament context.object_to_save = tournament
rescue StandardError rescue StandardError
context.fail! context.fail!
end end

View File

@ -12,7 +12,7 @@ class AddPlayoffsToTournament
else else
tournament.stages.concat playoff_stages tournament.stages.concat playoff_stages
end end
context.tournament = tournament context.object_to_save = tournament
else else
context.fail! context.fail!
end end

View File

@ -1,10 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
class SaveTournamentToDatabase class SaveApplicationRecordObject
include Interactor include Interactor
def call def call
if context.tournament.save if context.object_to_save.save
nil nil
else else
context.fail! context.fail!

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddGroupStageToTournamentAndSave
include Interactor::Organizer
organize AddGroupStageToTournament, SaveApplicationRecordObject
end

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
class AddGroupStageToTournamentAndSaveTournamentToDatabase
include Interactor::Organizer
organize AddGroupStageToTournament, SaveTournamentToDatabase
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddPlayoffsToTournamentAndSave
include Interactor::Organizer
organize AddPlayoffsToTournament, SaveApplicationRecordObject
end

View File

@ -1,7 +0,0 @@
# frozen_string_literal: true
class AddPlayoffsToTournamentAndSaveTournamentToDatabase
include Interactor::Organizer
organize AddPlayoffsToTournament, SaveTournamentToDatabase
end

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe SaveTournamentToDatabase do RSpec.describe SaveApplicationRecordObject do
before do before do
@tournament = create(:tournament) @tournament = create(:tournament)
end end
context 'save succeeds' do context 'save succeeds' do
let(:context) do let(:context) do
SaveTournamentToDatabase.call(tournament: @tournament) SaveApplicationRecordObject.call(object_to_save: @tournament)
end end
before do before do
expect_any_instance_of(Tournament) expect_any_instance_of(Tournament)
@ -19,13 +19,13 @@ RSpec.describe SaveTournamentToDatabase do
end end
it 'provides the tournament' do it 'provides the tournament' do
expect(context.tournament).to eq(@tournament) expect(context.object_to_save).to eq(@tournament)
end end
end end
context 'save fails' do context 'save fails' do
let(:context) do let(:context) do
SaveTournamentToDatabase.call(tournament: @tournament) SaveApplicationRecordObject.call(object_to_save: @tournament)
end end
before do before do
expect_any_instance_of(Tournament) expect_any_instance_of(Tournament)