Skip to content

Commit

Permalink
Preview interactors
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Apr 16, 2019
1 parent ebd820a commit 13242b8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
24 changes: 9 additions & 15 deletions app/controllers/preview_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

class PreviewController < ApplicationController
def create
Edition.find_and_lock_current(document: params[:document]) do |edition|
begin
if Requirements::EditionChecker.new(edition).pre_preview_issues.any?
redirect_to document_path(edition.document), tried_to_preview: true
next
end
result = Preview::CreateInteractor.call(params: params, user: current_user)
issues, preview_failed = result.to_h.values_at(:issues, :preview_failed)

PreviewService.new(edition).create_preview
rescue GdsApi::BaseError => e
GovukError.notify(e)
redirect_to document_path(edition.document),
alert_with_description: t("documents.show.flashes.preview_error")
next
end

redirect_to preview_document_path(edition.document)
if issues
redirect_to document_path(params[:document]), tried_to_preview: true
elsif preview_failed
redirect_to document_path(params[:document]),
alert_with_description: t("documents.show.flashes.preview_error")
else
redirect_to preview_document_path(params[:document])
end
end

Expand Down
38 changes: 38 additions & 0 deletions app/interactors/preview/create_interactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

class Preview::CreateInteractor
include Interactor

delegate :params,
:user,
:edition,
:issues,
:preview_failed,
to: :context

def call
Edition.transaction do
find_and_lock_edition
check_for_issues
create_preview
end
end

private

def find_and_lock_edition
context.edition = Edition.lock.find_current(document: params[:document])
end

def check_for_issues
issues = Requirements::EditionChecker.new(edition).pre_preview_issues
context.fail!(issues: issues) if issues.any?
end

def create_preview
PreviewService.new(edition).create_preview
rescue GdsApi::BaseError => e
GovukError.notify(e)
context.fail!(preview_failed: true)
end
end

0 comments on commit 13242b8

Please sign in to comment.