From adef30a5e253d0e581a065ca02b482121344a71f Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Mon, 8 Apr 2019 09:54:21 +0100 Subject: [PATCH] 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. --- app/controllers/images_controller.rb | 79 ++++++++++-------- app/interactors/images/create.rb | 40 --------- app/interactors/images/create_interactor.rb | 43 ++++++++++ app/interactors/images/destroy.rb | 34 -------- app/interactors/images/destroy_interactor.rb | 45 ++++++++++ app/interactors/images/update.rb | 82 ------------------ app/interactors/images/update_crop.rb | 50 ----------- .../images/update_crop_interactor.rb | 65 +++++++++++++++ app/interactors/images/update_interactor.rb | 83 +++++++++++++++++++ ...eate_spec.rb => create_interactor_spec.rb} | 8 +- ...roy_spec.rb => destroy_interactor_spec.rb} | 10 +-- ...spec.rb => update_crop_interactor_spec.rb} | 32 +++---- ...date_spec.rb => update_interactor_spec.rb} | 26 +++--- 13 files changed, 319 insertions(+), 278 deletions(-) delete mode 100644 app/interactors/images/create.rb create mode 100644 app/interactors/images/create_interactor.rb delete mode 100644 app/interactors/images/destroy.rb create mode 100644 app/interactors/images/destroy_interactor.rb delete mode 100644 app/interactors/images/update.rb delete mode 100644 app/interactors/images/update_crop.rb create mode 100644 app/interactors/images/update_crop_interactor.rb create mode 100644 app/interactors/images/update_interactor.rb rename spec/interactors/images/{create_spec.rb => create_interactor_spec.rb} (80%) rename spec/interactors/images/{destroy_spec.rb => destroy_interactor_spec.rb} (82%) rename spec/interactors/images/{update_crop_spec.rb => update_crop_interactor_spec.rb} (67%) rename spec/interactors/images/{update_spec.rb => update_interactor_spec.rb} (80%) diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index 765b36f783..0b22db524a 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -7,20 +7,24 @@ def index end def create - result = Images::Create.call(params: params, user: current_user) - if result.failure? + result = Images::CreateInteractor.call(params: params, user: current_user) + edition, image_revision, issues = result.to_h.values_at(:edition, + :image_revision, + :issues) + + if issues flash.now["alert_with_items"] = { "title" => I18n.t!("images.index.flashes.upload_requirements"), - "items" => result.issues.items, + "items" => issues.items, } render :index, - assigns: { edition: result.edition }, + assigns: { edition: edition }, layout: rendering_context, status: :unprocessable_entity else - redirect_to crop_image_path(result.edition.document, - result.image_revision.image_id, + redirect_to crop_image_path(edition.document, + image_revision.image_id, wizard: "upload") end end @@ -32,9 +36,10 @@ def crop end def update_crop - result = Images::UpdateCrop.call(params: params, user: current_user) - redirect_to edit_image_path(result.edition.document, - result.image_revision.image_id, + result = Images::UpdateCropInteractor.call(params: params, user: current_user) + edition, image_revision = result.to_h.values_at(:edition, :image_revision) + redirect_to edit_image_path(edition.document, + image_revision.image_id, wizard: params[:wizard]) end @@ -45,49 +50,51 @@ def edit end def update - result = Images::Update.call(params: params, user: current_user) + result = Images::UpdateInteractor.call(params: params, user: current_user) + + edition, image_revision, issues, lead_selected, lead_removed = + result.to_h.values_at(:edition, + :image_revision, + :issues, + :selected_lead_image, + :removed_lead_image) - if result.failure? + if issues flash.now["alert_with_items"] = { "title" => I18n.t!("images.edit.flashes.requirements"), - "items" => result.issues.items, + "items" => issues.items, } render :edit, - assigns: { edition: result.edition, - image_revision: result.image_revision, - issues: result.issues }, + assigns: { edition: edition, + image_revision: image_revision, + issues: issues }, layout: rendering_context, status: :unprocessable_entity + elsif lead_selected + redirect_to document_path(edition.document), + notice: t("documents.show.flashes.lead_image.selected", file: image_revision.filename) + elsif lead_removed + redirect_to images_path(edition.document), + notice: t("images.index.flashes.lead_image.removed", file: image_revision.filename) else - document = result.edition.document - - if result.updater.selected_lead_image? - redirect_to document_path(document), - notice: t("documents.show.flashes.lead_image.selected", - file: result.image_revision.filename) - elsif result.updater.removed_lead_image? - redirect_to images_path(document), - notice: t("images.index.flashes.lead_image.removed", - file: result.image_revision.filename) - else - redirect_to images_path(document) - end + redirect_to images_path(edition.document) end end def destroy - result = Images::Destroy.call(params: params, user: current_user) - document = result.edition.document - - if result.updater.removed_lead_image? - redirect_to images_path(document), + result = Images::DestroyInteractor.call(params: params, user: current_user) + edition, image_revision, removed_lead = result.to_h.values_at(:edition, + :image_revision, + :removed_lead_image) + if removed_lead + redirect_to images_path(edition.document), notice: t("images.index.flashes.lead_image.deleted", - file: result.image_revision.filename) + file: image_revision.filename) else - redirect_to images_path(document), + redirect_to images_path(edition.document), notice: t("images.index.flashes.deleted", - file: result.image_revision.filename) + file: image_revision.filename) end end diff --git a/app/interactors/images/create.rb b/app/interactors/images/create.rb deleted file mode 100644 index 7d993cd897..0000000000 --- a/app/interactors/images/create.rb +++ /dev/null @@ -1,40 +0,0 @@ -# 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 diff --git a/app/interactors/images/create_interactor.rb b/app/interactors/images/create_interactor.rb new file mode 100644 index 0000000000..219de566cc --- /dev/null +++ b/app/interactors/images/create_interactor.rb @@ -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 diff --git a/app/interactors/images/destroy.rb b/app/interactors/images/destroy.rb deleted file mode 100644 index 7addeca9f0..0000000000 --- a/app/interactors/images/destroy.rb +++ /dev/null @@ -1,34 +0,0 @@ -# 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 diff --git a/app/interactors/images/destroy_interactor.rb b/app/interactors/images/destroy_interactor.rb new file mode 100644 index 0000000000..9de2e03c90 --- /dev/null +++ b/app/interactors/images/destroy_interactor.rb @@ -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 diff --git a/app/interactors/images/update.rb b/app/interactors/images/update.rb deleted file mode 100644 index 025e7fbcef..0000000000 --- a/app/interactors/images/update.rb +++ /dev/null @@ -1,82 +0,0 @@ -# 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 diff --git a/app/interactors/images/update_crop.rb b/app/interactors/images/update_crop.rb deleted file mode 100644 index d777c77cd1..0000000000 --- a/app/interactors/images/update_crop.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -class Images::UpdateCrop - 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::ImageRevisionUpdater.new(image_revision, user) - updater.assign(crop_params) - - if updater.changed? - update_edition(edition, updater.next_revision) - TimelineEntry.create_for_revision(entry_type: :image_updated, - edition: edition) - PreviewService.new(edition).try_create_preview - end - - update_context(edition: edition, image_revision: updater.next_revision) - end - end - -private - - def crop_params - image_aspect_ratio = Image::HEIGHT.to_f / Image::WIDTH - - params - .require(:image_revision) - .permit(:crop_x, :crop_y, :crop_width, :crop_width) - .tap { |p| p[:crop_height] = (p[:crop_width].to_i * image_aspect_ratio).round - 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 diff --git a/app/interactors/images/update_crop_interactor.rb b/app/interactors/images/update_crop_interactor.rb new file mode 100644 index 0000000000..056cade261 --- /dev/null +++ b/app/interactors/images/update_crop_interactor.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +class Images::UpdateCropInteractor + include Interactor + delegate :params, + :user, + :edition, + :image_revision, + to: :context + + def initialize(params:, user:) + super + end + + def call + Edition.transaction do + context.edition = Edition.lock.find_current(document: params[:document]) + + updater = update_image(find_image_revision(params[:image_id]), crop_params) + + if updater.changed? + create_timeline_entry + update_preview + end + + context.image_revision = updater.next_revision + end + end + +private + + def find_image_revision(image_id) + edition.image_revisions.find_by!(image_id: image_id) + end + + def crop_params + image_aspect_ratio = Image::HEIGHT.to_f / Image::WIDTH + + params + .require(:image_revision) + .permit(:crop_x, :crop_y, :crop_width, :crop_width) + .tap { |p| p[:crop_height] = (p[:crop_width].to_i * image_aspect_ratio).round } + end + + def update_image(image_revision, params) + Versioning::ImageRevisionUpdater.new(image_revision, user).tap do |updater| + updater.assign(params) + update_edition(updater.next_revision) if updater.changed? + end + end + + def update_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 create_timeline_entry + TimelineEntry.create_for_revision(entry_type: :image_updated, edition: edition) + end + + def update_preview + PreviewService.new(edition).try_create_preview + end +end diff --git a/app/interactors/images/update_interactor.rb b/app/interactors/images/update_interactor.rb new file mode 100644 index 0000000000..335f321d9a --- /dev/null +++ b/app/interactors/images/update_interactor.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +class Images::UpdateInteractor + include Interactor + delegate :params, + :user, + :edition, + :image_revision, + :issues, + :selected_lead_image, + :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 = update_image_revision(image_params) + + check_for_issues + + updater = update_image(params[:lead_image] == "on") + + context.selected_lead_image = updater.selected_lead_image? + context.removed_lead_image = updater.removed_lead_image? + + if updater.changed? + create_timeline_entry + update_preview + end + end + end + +private + + def update_image_revision(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_for_issues + checker = Requirements::ImageRevisionChecker.new(image_revision) + issues = checker.pre_preview_metadata_issues + context.fail!(issues: issues) if issues.any? + end + + def update_image(lead_image) + Versioning::RevisionUpdater.new(edition.revision, user).tap do |updater| + updater.update_image(image_revision, lead_image) + edition.assign_revision(updater.next_revision, user).save! if updater.changed? + 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 create_timeline_entry + timeline_entry_type = if selected_lead_image + :lead_image_selected + elsif removed_lead_image + :lead_image_removed + else + :image_updated + end + + TimelineEntry.create_for_revision(entry_type: timeline_entry_type, edition: edition) + end + + def update_preview + PreviewService.new(edition).try_create_preview + end +end diff --git a/spec/interactors/images/create_spec.rb b/spec/interactors/images/create_interactor_spec.rb similarity index 80% rename from spec/interactors/images/create_spec.rb rename to spec/interactors/images/create_interactor_spec.rb index ace3183953..68332e7abe 100644 --- a/spec/interactors/images/create_spec.rb +++ b/spec/interactors/images/create_interactor_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Images::Create do +RSpec.describe Images::CreateInteractor do def strong_params(**params) ActionController::Parameters.new(params) end @@ -11,7 +11,7 @@ def strong_params(**params) context "when an image upload has issues" do it "fails with issues" do - result = Images::Create.call( + result = Images::CreateInteractor.call( params: strong_params( document: edition.document.to_param, image: nil, @@ -32,13 +32,13 @@ def strong_params(**params) end it "uploads an image" do - expect { Images::Create.call(params: params, user: user) } + expect { Images::CreateInteractor.call(params: params, user: user) } .to change { Image.count } .by(1) end it "updates the edition revision" do - expect { Images::Create.call(params: params, user: user) } + expect { Images::CreateInteractor.call(params: params, user: user) } .to(change { edition.reload.revision }) end end diff --git a/spec/interactors/images/destroy_spec.rb b/spec/interactors/images/destroy_interactor_spec.rb similarity index 82% rename from spec/interactors/images/destroy_spec.rb rename to spec/interactors/images/destroy_interactor_spec.rb index e392ca42e0..bdd4f3ed98 100644 --- a/spec/interactors/images/destroy_spec.rb +++ b/spec/interactors/images/destroy_interactor_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Images::Destroy do +RSpec.describe Images::DestroyInteractor do def strong_params(**params) ActionController::Parameters.new(params) end @@ -24,7 +24,7 @@ def strong_params(**params) end it "creates a revision without the image revision" do - Images::Destroy.call(params: params, user: user) + Images::DestroyInteractor.call(params: params, user: user) revision = edition.reload.revision expect(revision.image_revisions).not_to include(image_revision) end @@ -33,12 +33,12 @@ def strong_params(**params) expect(TimelineEntry) .to receive(:create_for_revision) .with(entry_type: :image_deleted, edition: edition) - Images::Destroy.call(params: params, user: user) + Images::DestroyInteractor.call(params: params, user: user) end it "creates a preview" do expect(preview_service).to receive(:try_create_preview) - Images::Destroy.call(params: params, user: user) + Images::DestroyInteractor.call(params: params, user: user) end context "when the image does not exist" do @@ -50,7 +50,7 @@ def strong_params(**params) image_id: Image.maximum(:id).to_i + 1, ) - expect { Images::Destroy.call(params: params, user: user) } + expect { Images::DestroyInteractor.call(params: params, user: user) } .to raise_error(ActiveRecord::RecordNotFound) end end diff --git a/spec/interactors/images/update_crop_spec.rb b/spec/interactors/images/update_crop_interactor_spec.rb similarity index 67% rename from spec/interactors/images/update_crop_spec.rb rename to spec/interactors/images/update_crop_interactor_spec.rb index d3b8fd8f7d..7a41ce8ac9 100644 --- a/spec/interactors/images/update_crop_spec.rb +++ b/spec/interactors/images/update_crop_interactor_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Images::UpdateCrop do +RSpec.describe Images::UpdateCropInteractor do def strong_params(**params) ActionController::Parameters.new(params) end @@ -21,14 +21,16 @@ def strong_params(**params) strong_params( document: edition.document.to_param, image_id: image_revision.image_id, - crop_x: image_revision.crop_x + 10, - crop_y: image_revision.crop_y + 10, - crop_width: image_revision.crop_width, + image_revision: { + crop_x: image_revision.crop_x + 10, + crop_y: image_revision.crop_y + 10, + crop_width: image_revision.crop_width, + }, ) end it "creates a new revision" do - expect { Images::UpdateCrop.call(params: params, user: user) } + expect { Images::UpdateCropInteractor.call(params: params, user: user) } .to(change { edition.reload.revision }) end @@ -36,12 +38,12 @@ def strong_params(**params) expect(TimelineEntry) .to receive(:create_for_revision) .with(entry_type: :image_updated, edition: edition) - Images::UpdateCrop.call(params: params, user: user) + Images::UpdateCropInteractor.call(params: params, user: user) end it "creates a preview" do expect(preview_service).to receive(:try_create_preview) - Images::UpdateCrop.call(params: params, user: user) + Images::UpdateCropInteractor.call(params: params, user: user) end end @@ -50,25 +52,27 @@ def strong_params(**params) strong_params( document: edition.document.to_param, image_id: image_revision.image_id, - crop_x: image_revision.crop_x, - crop_y: image_revision.crop_y, - crop_width: image_revision.crop_width, + image_revision: { + crop_x: image_revision.crop_x, + crop_y: image_revision.crop_y, + crop_width: image_revision.crop_width, + }, ) end it "doesn't create a new revision" do - expect { Images::UpdateCrop.call(params: params, user: user) } + expect { Images::UpdateCropInteractor.call(params: params, user: user) } .not_to(change { edition.reload.revision }) end it "doesn't create a timeline entry" do - expect { Images::UpdateCrop.call(params: params, user: user) } + expect { Images::UpdateCropInteractor.call(params: params, user: user) } .not_to change(TimelineEntry, :count) end it "creates a preview" do expect(preview_service).not_to receive(:try_create_preview) - Images::UpdateCrop.call(params: params, user: user) + Images::UpdateCropInteractor.call(params: params, user: user) end end @@ -79,7 +83,7 @@ def strong_params(**params) image_id: Image.maximum(:id).to_i + 1, ) - expect { Images::UpdateCrop.call(params: params, user: user) } + expect { Images::UpdateCropInteractor.call(params: params, user: user) } .to raise_error(ActiveRecord::RecordNotFound) end end diff --git a/spec/interactors/images/update_spec.rb b/spec/interactors/images/update_interactor_spec.rb similarity index 80% rename from spec/interactors/images/update_spec.rb rename to spec/interactors/images/update_interactor_spec.rb index 646596f3e6..ff55b36c09 100644 --- a/spec/interactors/images/update_spec.rb +++ b/spec/interactors/images/update_interactor_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Images::Update do +RSpec.describe Images::UpdateInteractor do def strong_params(**params) ActionController::Parameters.new(params) end @@ -25,7 +25,7 @@ def strong_params(**params) context "when an image is updated" do it "creates a new revision" do - expect { Images::Update.call(params: strong_params(success_params), user: user) } + expect { Images::UpdateInteractor.call(params: strong_params(success_params), user: user) } .to(change { edition.reload.revision }) image_revision = edition.image_revisions.first alt_text = success_params[:image_revision][:alt_text] @@ -34,7 +34,7 @@ def strong_params(**params) it "creates a preview" do expect(preview_service).to receive(:try_create_preview) - Images::Update.call(params: strong_params(success_params), user: user) + Images::UpdateInteractor.call(params: strong_params(success_params), user: user) end end @@ -43,7 +43,7 @@ def strong_params(**params) expect(TimelineEntry) .to receive(:create_for_revision) .with(entry_type: :image_updated, edition: edition) - Images::Update.call(params: strong_params(success_params), user: user) + Images::UpdateInteractor.call(params: strong_params(success_params), user: user) end end @@ -53,7 +53,7 @@ def strong_params(**params) end it "sets the edition lead image" do - expect { Images::Update.call(params: params, user: user) } + expect { Images::UpdateInteractor.call(params: params, user: user) } .to change { edition.reload.lead_image_revision } .from(nil) end @@ -62,7 +62,7 @@ def strong_params(**params) expect(TimelineEntry) .to receive(:create_for_revision) .with(entry_type: :lead_image_selected, edition: edition) - Images::Update.call(params: params, user: user) + Images::UpdateInteractor.call(params: params, user: user) end end @@ -73,7 +73,7 @@ def strong_params(**params) end it "removes the edition lead image" do - expect { Images::Update.call(params: params, user: user) } + expect { Images::UpdateInteractor.call(params: params, user: user) } .to change { edition.reload.lead_image_revision } .to(nil) end @@ -82,7 +82,7 @@ def strong_params(**params) expect(TimelineEntry) .to receive(:create_for_revision) .with(entry_type: :lead_image_removed, edition: edition) - Images::Update.call(params: params, user: user) + Images::UpdateInteractor.call(params: params, user: user) end end @@ -93,24 +93,24 @@ def strong_params(**params) end it "doesn't create a timeline entry" do - expect { Images::Update.call(params: params, user: user) } + expect { Images::UpdateInteractor.call(params: params, user: user) } .not_to change(TimelineEntry, :count) end it "doesn't update the editions revision" do - expect { Images::Update.call(params: params, user: user) } + expect { Images::UpdateInteractor.call(params: params, user: user) } .not_to(change { edition.reload.revision }) end it "doesn't preview" do expect(preview_service).not_to receive(:try_create_preview) - Images::Update.call(params: params, user: user) + Images::UpdateInteractor.call(params: params, user: user) end end context "when there are issues" do it "fails with issues" do - result = Images::Update.call( + result = Images::UpdateInteractor.call( params: strong_params( document: edition.document.to_param, image_id: image_revision.image_id, @@ -133,7 +133,7 @@ def strong_params(**params) image_revision: { caption: "Caption" }, ) - expect { Images::Update.call(params: params, user: user) } + expect { Images::UpdateInteractor.call(params: params, user: user) } .to raise_error(ActiveRecord::RecordNotFound) end end