-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iterate interactors following feedback
Following feedback on the first draft of interactors for images this makes a number of changes: - Changes the class naming to be suffixed with interactor. As these are mostly named with verbs this is a bit weird as it sounds like they create an interactor, however it is consistent with most other app level naming patterns. - Makes clearer the properties in play on a context by delegating each of them to context in the interactor class. - Drops using an update_context method to set context at the end of an interactor and instead uses an approach where context properties are set in the call method. - Run an interactor in an explicit transaction rather than using the `Edition.find_and_lock_current` method. Since we're setting context.edition we'd still have a line of code so we don't really benefit from the yield. - Uses a suggestion from Ben to define each instance variable we use from a result in a controller, by using values_at method on the result object (once converted to a hash) - Rather than checking on failure of a result check on a particular problem, such as issues.
- Loading branch information
Showing
13 changed files
with
319 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
class Images::CreateInteractor | ||
include Interactor | ||
delegate :params, | ||
:user, | ||
:edition, | ||
:image_revision, | ||
:issues, | ||
to: :context | ||
|
||
def initialize(params:, user:) | ||
super | ||
end | ||
|
||
def call | ||
Edition.transaction do | ||
context.edition = Edition.lock.find_current(document: params[:document]) | ||
check_for_issues(params[:image]) | ||
|
||
context.image_revision = upload_image(params[:image]) | ||
update_edition | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_for_issues(image_params) | ||
issues = Requirements::ImageUploadChecker.new(image_params).issues | ||
context.fail!(issues: issues) if issues.any? | ||
end | ||
|
||
def upload_image(image_params) | ||
ImageUploadService.new(image_params, edition.revision).call(user) | ||
end | ||
|
||
def update_edition | ||
Versioning::RevisionUpdater.new(edition.revision, user).tap do |updater| | ||
updater.update_image(image_revision, false) | ||
edition.assign_revision(updater.next_revision, user).save! | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
class Images::DestroyInteractor | ||
include Interactor | ||
delegate :params, | ||
:user, | ||
:edition, | ||
:image_revision, | ||
:removed_lead_image, | ||
to: :context | ||
|
||
def initialize(params:, user:) | ||
super | ||
end | ||
|
||
def call | ||
Edition.transaction do | ||
context.edition = Edition.lock.find_current(document: params[:document]) | ||
context.image_revision = edition.image_revisions.find_by!(image_id: params[:image_id]) | ||
|
||
updater = remove_image(image_revision) | ||
create_timeline_entry | ||
update_preview | ||
|
||
context.removed_lead_image = updater.removed_lead_image? | ||
end | ||
end | ||
|
||
private | ||
|
||
def remove_image(image_revision) | ||
Versioning::RevisionUpdater.new(edition.revision, user).tap do |updater| | ||
updater.remove_image(image_revision) | ||
edition.assign_revision(updater.next_revision, user).save! | ||
end | ||
end | ||
|
||
def create_timeline_entry | ||
TimelineEntry.create_for_revision(entry_type: :image_deleted, edition: edition) | ||
end | ||
|
||
def update_preview | ||
PreviewService.new(edition).try_create_preview | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.