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
groups = organize_teams_in_groups(teams)
# add groups to tournament
result = AddGroupStageToTournamentAndSaveTournamentToDatabase.call(tournament: tournament, groups: groups)
result = AddGroupStageToTournamentAndSave.call(tournament: tournament, groups: groups)
else
# convert teams parameter into Team objects
teams = teams.map(&method(:find_or_create_team))
# associate provided teams with tournament
tournament.teams = teams
# add playoff stage to tournament
result = AddPlayoffsToTournamentAndSaveTournamentToDatabase.call(tournament: tournament)
result = AddPlayoffsToTournamentAndSave.call(tournament: tournament)
end
# validate tournament
unless tournament.valid?

View File

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

View File

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

View File

@ -1,10 +1,10 @@
# frozen_string_literal: true
class SaveTournamentToDatabase
class SaveApplicationRecordObject
include Interactor
def call
if context.tournament.save
if context.object_to_save.save
nil
else
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
RSpec.describe SaveTournamentToDatabase do
RSpec.describe SaveApplicationRecordObject do
before do
@tournament = create(:tournament)
end
context 'save succeeds' do
let(:context) do
SaveTournamentToDatabase.call(tournament: @tournament)
SaveApplicationRecordObject.call(object_to_save: @tournament)
end
before do
expect_any_instance_of(Tournament)
@ -19,13 +19,13 @@ RSpec.describe SaveTournamentToDatabase do
end
it 'provides the tournament' do
expect(context.tournament).to eq(@tournament)
expect(context.object_to_save).to eq(@tournament)
end
end
context 'save fails' do
let(:context) do
SaveTournamentToDatabase.call(tournament: @tournament)
SaveApplicationRecordObject.call(object_to_save: @tournament)
end
before do
expect_any_instance_of(Tournament)