Skip to content

Instantly share code, notes, and snippets.

@kevindew
Created March 19, 2019 19:01
Show Gist options
  • Save kevindew/4ab20f0d861a83267395ff7d0c0d608f to your computer and use it in GitHub Desktop.
Save kevindew/4ab20f0d861a83267395ff7d0c0d608f to your computer and use it in GitHub Desktop.
class Images::Create
include Interactor
def initialize(params:, user:)
super
end
def call
find_and_lock_edition do
check_issues(context.params[:image])
upload_image(context.params[:image])
update_edition
create_preview
end
end
private
def find_and_lock_edition
Edition.find_and_lock_current(document: context.params[:document]) do |edition|
context.edition = edition
yield
end
end
def check_issues(image_params)
issues = ::Requirements::ImageUploadChecker.new(image_params).issues
context.fail!(issues: issues) if issues.any?
end
def upload_image(image_params)
upload_service = ImageUploadService.new(image_params, context.edition.revision)
context.image_revision = upload_service.call(context.user)
end
def update_edition
updater = Versioning::RevisionUpdater.new(context.edition.revision,
context.user)
updater.update_image(context.image_revision, false)
context.edition.assign_revision(updater.next_revision, context.user).save!
end
def create_preview
PreviewService.new(context.edition).try_create_preview
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment