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:
parent
3ed8165501
commit
8388af20f0
|
|
@ -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?
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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!
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddGroupStageToTournamentAndSave
|
||||
include Interactor::Organizer
|
||||
|
||||
organize AddGroupStageToTournament, SaveApplicationRecordObject
|
||||
end
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddGroupStageToTournamentAndSaveTournamentToDatabase
|
||||
include Interactor::Organizer
|
||||
|
||||
organize AddGroupStageToTournament, SaveTournamentToDatabase
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddPlayoffsToTournamentAndSave
|
||||
include Interactor::Organizer
|
||||
|
||||
organize AddPlayoffsToTournament, SaveApplicationRecordObject
|
||||
end
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddPlayoffsToTournamentAndSaveTournamentToDatabase
|
||||
include Interactor::Organizer
|
||||
|
||||
organize AddPlayoffsToTournament, SaveTournamentToDatabase
|
||||
end
|
||||
|
|
@ -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)
|
||||
Loading…
Reference in New Issue