-
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.
Refactor ImagesController using interactor pattern
This demonstrates a demo of how the ImagesController could work using the interactor gem. There were some small changes I made here compared to the existing code: 1. We no longer preview on image creation. There isn't any point doing this as it won't succeed - as initial image is invalid 2. We round the number as part of cropping an image. This saves this frequently being off by 1 pixel, fixing this made tests easier.
- Loading branch information
Showing
9 changed files
with
592 additions
and
118 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 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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
class Images::Create | ||
include Interactor | ||
delegate :params, :user, to: :context | ||
|
||
def initialize(params:, user:) | ||
super | ||
end | ||
|
||
def call | ||
Edition.find_and_lock_current(document: params[:document]) do |edition| | ||
check_issues(edition, params[:image]) | ||
image_revision = upload_image(edition, params[:image]) | ||
update_edition(edition, image_revision) | ||
update_context(edition: edition, image_revision: image_revision) | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_issues(edition, image_params) | ||
issues = Requirements::ImageUploadChecker.new(image_params).issues | ||
context.fail!(edition: edition, issues: issues) if issues.any? | ||
end | ||
|
||
def upload_image(edition, image_params) | ||
ImageUploadService.new(image_params, edition.revision).call(user) | ||
end | ||
|
||
def update_edition(edition, image_revision) | ||
updater = Versioning::RevisionUpdater.new(edition.revision, user) | ||
updater.update_image(image_revision, false) | ||
edition.assign_revision(updater.next_revision, user).save! | ||
end | ||
|
||
def update_context(attributes) | ||
attributes.each { |k, v| context[k.to_sym] = v } | ||
end | ||
end |
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class Images::Destroy | ||
include Interactor | ||
delegate :params, :user, to: :context | ||
|
||
def initialize(params:, user:) | ||
super | ||
end | ||
|
||
def call | ||
Edition.find_and_lock_current(document: params[:document]) do |edition| | ||
image_revision = edition.image_revisions.find_by!(image_id: params[:image_id]) | ||
updater = Versioning::RevisionUpdater.new(edition.revision, user) | ||
|
||
updater.remove_image(image_revision) | ||
edition.assign_revision(updater.next_revision, user).save! | ||
|
||
TimelineEntry.create_for_revision(entry_type: :image_deleted, | ||
edition: edition) | ||
PreviewService.new(edition).try_create_preview | ||
|
||
update_context(edition: edition, | ||
image_revision: image_revision, | ||
updater: updater) | ||
end | ||
end | ||
|
||
private | ||
|
||
def update_context(attributes) | ||
attributes.each { |k, v| context[k.to_sym] = v } | ||
end | ||
end |
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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
class Images::Update | ||
include Interactor | ||
delegate :params, :user, to: :context | ||
|
||
def initialize(params:, user:) | ||
super | ||
end | ||
|
||
def call | ||
Edition.find_and_lock_current(document: params[:document]) do |edition| | ||
image_revision = update_image_revision(edition, image_params) | ||
check_issues(edition, image_revision) | ||
|
||
updater = Versioning::RevisionUpdater.new(edition.revision, user) | ||
updater.update_image(image_revision, params[:lead_image] == "on") | ||
|
||
if updater.changed? | ||
create_timeline_entry(edition, updater) | ||
edition.assign_revision(updater.next_revision, user).save! | ||
PreviewService.new(edition).try_create_preview | ||
end | ||
|
||
update_context(edition: edition, | ||
image_revision: image_revision, | ||
updater: updater) | ||
end | ||
end | ||
|
||
private | ||
|
||
def update_image_revision(edition, update_params) | ||
image_revision = edition.image_revisions.find_by!(image_id: params[:image_id]) | ||
updater = Versioning::ImageRevisionUpdater.new(image_revision, user) | ||
updater.assign(update_params) | ||
updater.next_revision | ||
end | ||
|
||
def image_params | ||
params.require(:image_revision).permit(:caption, :alt_text, :credit) | ||
end | ||
|
||
def check_issues(edition, image_revision) | ||
checker = Requirements::ImageRevisionChecker.new(image_revision) | ||
issues = checker.pre_preview_metadata_issues | ||
if issues.any? | ||
context.fail!(edition: edition, | ||
image_revision: image_revision, | ||
issues: issues) | ||
end | ||
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_timeline_entry(edition, updater) | ||
timeline_entry_type = if updater.selected_lead_image? | ||
:lead_image_selected | ||
elsif updater.removed_lead_image? | ||
:lead_image_removed | ||
else | ||
:image_updated | ||
end | ||
|
||
TimelineEntry.create_for_revision(entry_type: timeline_entry_type, edition: edition) | ||
end | ||
|
||
def update_context(attributes) | ||
attributes.each { |k, v| context[k.to_sym] = v } | ||
end | ||
end |
Oops, something went wrong.