Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image controller with interactors #893

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem "govuk_publishing_components", "~> 16.10"
gem "govuk_sidekiq", "~> 3"
gem "hashdiff", "~> 0.3"
gem "image_processing", "~> 1"
gem "interactor", "~> 3"
gem "kaminari", "~> 1"
gem "notifications-ruby-client", "~> 3.1"
gem "pg", "~> 1"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ GEM
image_processing (1.9.0)
mini_magick (>= 4.9.3, < 5)
ruby-vips (>= 2.0.13, < 3)
interactor (3.1.1)
io-like (0.3.0)
jaro_winkler (1.5.2)
jasmine (3.4.0)
Expand Down Expand Up @@ -464,6 +465,7 @@ DEPENDENCIES
govuk_test (~> 0.3)
hashdiff (~> 0.3)
image_processing (~> 1)
interactor (~> 3)
jasmine (~> 3.4)
jasmine_selenium_runner (~> 3)
kaminari (~> 1)
Expand Down
30 changes: 2 additions & 28 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,8 @@ def search
end

def insert
Edition.find_and_lock_current(document: params[:document]) do |edition|
redirect_location = edit_document_path(edition.document) + "#body"
Contacts::InsertInteractor.call(params: params, user: current_user)

if params[:contact_id].empty?
redirect_to redirect_location
next
end

contact_markdown = "[Contact:#{params[:contact_id]}]\n"
revision = edition.revision

body = revision.contents.fetch("body", "").chomp
updated_body = if body.present?
"#{body}\n\n#{contact_markdown}"
else
contact_markdown
end

updater = Versioning::RevisionUpdater.new(revision, current_user)
updater.assign(contents: revision.contents.merge("body" => updated_body))

if updater.changed?
edition.assign_revision(updater.next_revision, current_user).save!
TimelineEntry.create_for_revision(entry_type: :updated_content, edition: edition)
PreviewService.new(edition).try_create_preview
end

redirect_to redirect_location
end
redirect_to edit_document_path(params[:document], anchor: "body")
end
end
78 changes: 22 additions & 56 deletions app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,31 @@ def confirm_delete_draft
end

def destroy
Edition.find_and_lock_current(document: params[:document]) do |edition|
begin
DeleteDraftService.new(edition.document, current_user).delete

TimelineEntry.create_for_status_change(entry_type: :draft_discarded,
status: edition.status)

redirect_to documents_path
rescue GdsApi::BaseError => e
GovukError.notify(e)
redirect_to edition.document, alert_with_description: t("documents.show.flashes.delete_draft_error")
end
end
Documents::DestroyInteractor.call(params: params, user: current_user)
redirect_to documents_path
rescue GdsApi::BaseError => e
GovukError.notify(e)
redirect_to document_path(params[:document]),
alert_with_description: t("documents.show.flashes.delete_draft_error")
end

def update
Edition.find_and_lock_current(document: params[:document]) do |edition|
updater = Versioning::RevisionUpdater.new(edition.revision, current_user)
updater.assign(update_params(edition.document))

add_contact_request = params[:submit] == "add_contact"
@issues = Requirements::EditPageChecker.new(edition, updater.next_revision)
.pre_preview_issues

if @issues.any?
flash.now["alert_with_items"] = {
"title" => I18n.t!("documents.edit.flashes.requirements"),
"items" => @issues.items,
}

render :edit,
assigns: { edition: edition, revision: updater.next_revision },
status: :unprocessable_entity
next
end

if updater.changed?
edition.assign_revision(updater.next_revision, current_user).save!
TimelineEntry.create_for_revision(entry_type: :updated_content, edition: edition)
PreviewService.new(edition).try_create_preview
end

if add_contact_request
redirect_to search_contacts_path(edition.document)
else
redirect_to edition.document
end
result = Documents::UpdateInteractor.call(params: params, user: current_user)
edition, revision, issues, = result.to_h.values_at(:edition, :revision, :issues)

if issues
flash.now["alert_with_items"] = {
"title" => I18n.t!("documents.edit.flashes.requirements"),
"items" => issues.items,
}

render :edit,
assigns: { edition: edition, revision: revision },
status: :unprocessable_entity
elsif params[:submit] == "add_contact"
redirect_to search_contacts_path(edition.document)
else
redirect_to edition.document
end
end

Expand All @@ -94,16 +72,4 @@ def filter_params
per_page: 50,
}
end

def update_params(document)
contents_params = document.document_type.contents.map(&:id)

params.require(:revision)
.permit(:update_type, :change_note, :title, :summary, contents: contents_params)
.tap do |p|
p[:title] = p[:title]&.strip
p[:summary] = p[:summary]&.strip
p[:base_path] = PathGeneratorService.new.path(document, p[:title])
end
end
end
36 changes: 7 additions & 29 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,13 @@

class EditionsController < ApplicationController
def create
Edition.find_and_lock_current(document: params[:document]) do |edition|
unless edition.live?
# FIXME: this shouldn't be an exception but we've not worked out the
# right response - maybe bad request or a redirect with flash?
raise "Can't create a new edition when the current edition is a draft"
end

edition.update!(current: false)

next_edition = Edition.find_by(
document: edition.document,
number: edition.number + 1,
)

if next_edition
next_edition.resume_discarded(edition, current_user)

TimelineEntry.create_for_status_change(entry_type: :draft_reset,
status: next_edition.status)
else
next_edition = Edition.create_next_edition(edition, current_user)

TimelineEntry.create_for_status_change(entry_type: :new_edition,
status: next_edition.status)
end

PreviewService.new(next_edition).try_create_preview

redirect_to edit_document_path(next_edition.document)
result = Editions::CreateInteractor.call(params: params, user: current_user)
if result.draft_current_edition
# FIXME: this shouldn't be an exception but we've not worked out the
# right response - maybe bad request or a redirect with flash?
raise "Can't create a new edition when the current edition is a draft"
else
redirect_to edit_document_path(params[:document])
end
end
end
Loading