Skip to content

Commit

Permalink
Merge pull request #9496 from alphagov/content-modelling/refactor-con…
Browse files Browse the repository at this point in the history
…trollers

Refactor create/edit controllers
  • Loading branch information
pezholio authored Oct 8, 2024
2 parents e801394 + 371bd55 commit 019641a
Show file tree
Hide file tree
Showing 35 changed files with 744 additions and 1,134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def scheduled_item

def edit_action
{
href: helpers.content_object_store.edit_content_object_store_content_block_edition_path(content_block_document.latest_edition, step: ContentObjectStore::ContentBlock::EditionsController::EDIT_FORM_STEPS[:edit_block]),
href: helpers.content_object_store.new_content_object_store_content_block_document_edition_path(content_block_document),
link_text: "Change",
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@ def show
content_block_document: @content_block_document,
)
end

def new
if params[:block_type].blank?
@schemas = ContentObjectStore::ContentBlock::Schema.all
else
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(params[:block_type].underscore)
@form = ContentObjectStore::ContentBlock::DocumentForm.new(schema: @schema)
end
end

def create
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(params[:block_type].underscore)
new_edition = ContentObjectStore::CreateEditionService.new(@schema).call(edition_params)
redirect_to content_object_store.content_object_store_content_block_workflow_path(new_edition, step: ContentObjectStore::ContentBlock::Editions::WorkflowController::NEW_BLOCK_STEPS[:review])
rescue ActiveRecord::RecordInvalid => e
@form = ContentObjectStore::ContentBlock::DocumentForm.new(content_block_edition: e.record, schema: @schema)
render "content_object_store/content_block/documents/new"
end

def new_document_options_redirect
if params[:block_type].present?
redirect_to content_object_store.new_content_object_store_content_block_document_path(block_type: params.require(:block_type))
else
redirect_to content_object_store.new_content_object_store_content_block_document_path, flash: { error: "You must select a block type" }
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class ContentObjectStore::ContentBlock::Editions::WorkflowController < ContentObjectStore::BaseController
NEW_BLOCK_STEPS = {
review: "review",
}.freeze

UPDATE_BLOCK_STEPS = {
review_links: "review_links",
schedule_publishing: "schedule_publishing",
}.freeze

def show
step = params[:step]
@content_block_edition = ContentObjectStore::ContentBlock::Edition.find(params[:id])
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type)

case step
when UPDATE_BLOCK_STEPS[:review_links]
review_links
when UPDATE_BLOCK_STEPS[:schedule_publishing]
schedule_publishing
when NEW_BLOCK_STEPS[:review]
review
end
end

def update
step = params[:step]
@content_block_edition = ContentObjectStore::ContentBlock::Edition.find(params[:id])
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type)

case step
when UPDATE_BLOCK_STEPS[:review_links]
redirect_to content_object_store.content_object_store_content_block_workflow_path(id: @content_block_edition.id, step: :schedule_publishing)
when UPDATE_BLOCK_STEPS[:schedule_publishing]
schedule_or_publish
when NEW_BLOCK_STEPS[:review]
publish
end
end

private

def review
@content_block_edition = ContentObjectStore::ContentBlock::Edition.find(params[:id])

render :review
end

def review_links
@content_block_document = @content_block_edition.document
@host_content_items = ContentObjectStore::GetHostContentItems.by_embedded_document(
content_block_document: @content_block_document,
)

render :review_links
end

def schedule_publishing
@content_block_document = @content_block_edition.document

render :schedule_publishing
end

def schedule_or_publish
@content_block_edition = ContentObjectStore::ContentBlock::Edition.find(params[:id])
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type)

if params[:schedule_publishing].blank?
@content_block_edition.errors.add(:schedule_publishing, "cannot be blank")
raise ActiveRecord::RecordInvalid, @content_block_edition
elsif params[:schedule_publishing] == "schedule"
ContentObjectStore::ScheduleEditionService.new(@schema).call(@content_block_edition, scheduled_publication_params)
message = "#{@content_block_edition.block_type.humanize} scheduled successfully"
else
publish and return
end

redirect_to content_object_store.content_object_store_content_block_document_path(@content_block_edition.document),
flash: { notice: message }
rescue ActiveRecord::RecordInvalid
render "content_object_store/content_block/editions/workflow/schedule_publishing"
end

def publish
new_edition = ContentObjectStore::PublishEditionService.new(@schema).call(@content_block_edition)
redirect_to content_object_store.content_object_store_content_block_document_path(new_edition.document),
flash: { notice: "#{new_edition.block_type.humanize} created successfully" }
end
end
Original file line number Diff line number Diff line change
@@ -1,95 +1,31 @@
class ContentObjectStore::ContentBlock::EditionsController < ContentObjectStore::BaseController
def new
if params[:block_type].blank?
@error_message = "You must select a block type" if params[:block_type] == ""
@schemas = ContentObjectStore::ContentBlock::Schema.all
else
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(params[:block_type].underscore)
@form = ContentObjectStore::ContentBlock::EditionForm::Create.new(
content_block_edition: ContentObjectStore::ContentBlock::Edition.new,
schema: @schema,
)
end
@content_block_document = ContentObjectStore::ContentBlock::Document.find(params[:document_id])
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(@content_block_document.block_type)
@form = ContentObjectStore::ContentBlock::EditionForm.new(
content_block_edition: @content_block_document.latest_edition,
schema: @schema,
)
end

def create
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(block_type_param)

new_edition = ContentObjectStore::CreateEditionService.new(@schema).call(edition_params)

redirect_to content_object_store.review_content_object_store_content_block_edition_path(new_edition)
rescue ActiveRecord::RecordInvalid => e
@form = ContentObjectStore::ContentBlock::EditionForm::Create.new(content_block_edition: e.record, schema: @schema)
render :new
end

def review
@content_block_edition = ContentObjectStore::ContentBlock::Edition.find(params[:id])
end

EDIT_FORM_STEPS = {
edit_block: "edit_block",
review_links: "review_links",
schedule_publishing: "schedule_publishing",
}.freeze

def edit
step = params[:step]
@content_block_edition = ContentObjectStore::ContentBlock::Edition.find(params[:id])
@schema = ContentObjectStore::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type)

case step
when EDIT_FORM_STEPS[:edit_block]
edit_block
when EDIT_FORM_STEPS[:review_links]
review_links
when EDIT_FORM_STEPS[:schedule_publishing]
schedule_publishing
end
end

def edit_block
@form = ContentObjectStore::ContentBlock::EditionForm::Update.new(
content_block_edition: @content_block_edition, schema: @schema, edition_to_update_id: @content_block_edition.id,
)
end

def review_links
@content_block_document = @content_block_edition.document
@edition_params = edition_params

new_edition = ContentObjectStore::ContentBlock::Edition.new(edition_params)
new_edition.document.id = @content_block_document.id

if new_edition.valid?
@host_content_items = ContentObjectStore::GetHostContentItems.by_embedded_document(
content_block_document: @content_block_document,
)
new_edition.document_id = params[:document_id]
new_edition.document.assign_attributes(edition_params[:document_attributes].except(:block_type))

render :review_links
if new_edition.valid? && new_edition.document.valid?
new_edition.save!
new_edition.document.save!
redirect_to content_object_store.content_object_store_content_block_workflow_path(id: new_edition.id, step: "review_links")
else
@form = ContentObjectStore::ContentBlock::EditionForm::Update.new(
content_block_edition: new_edition,
schema: @schema, edition_to_update_id: @content_block_edition.id
)

render :edit
@form = ContentObjectStore::ContentBlock::EditionForm.new(content_block_edition: new_edition, schema: @schema)
render "content_object_store/content_block/documents/new"
end
end

def schedule_publishing
@content_block_document = @content_block_edition.document
@edition_params = edition_params

render :schedule_publishing
end

private

def root_params
params.require(:content_object_store_content_block_edition)
end

def block_type_param
params.require("content_block/edition").require("document_attributes").require(:block_type)
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ContentObjectStore::ContentBlock::DocumentForm
include ContentObjectStore::Engine.routes.url_helpers

attr_reader :schema, :content_block_edition

def initialize(schema:, content_block_edition: ContentObjectStore::ContentBlock::Edition.new)
@schema = schema
@content_block_edition = content_block_edition
end

def url
content_object_store_content_block_documents_path(block_type: schema.block_type)
end

def attributes
schema.fields.each_with_object({}) do |field, hash|
hash[field] = nil
hash
end
end

def back_path
content_object_store_content_block_documents_path
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,25 @@
class ContentObjectStore::ContentBlock::EditionForm
include ContentObjectStore::Engine.routes.url_helpers

attr_reader :content_block_edition, :schema, :url
attr_reader :content_block_edition, :schema

def initialize(content_block_edition:, schema:)
@content_block_edition = content_block_edition
@schema = schema
end

class Create < ContentObjectStore::ContentBlock::EditionForm
def url
content_object_store_content_block_editions_path
end

def attributes
@schema.fields.each_with_object({}) do |field, hash|
hash[field] = nil
hash
end
end

def back_path
content_object_store_content_block_documents_path
end
def url
content_object_store_content_block_document_editions_path(document_id: @content_block_edition.document.id)
end

class Update < ContentObjectStore::ContentBlock::EditionForm
def initialize(edition_to_update_id:, **args)
@edition_to_update_id = edition_to_update_id
super(**args)
end

def url
edit_content_object_store_content_block_edition_path(id: @edition_to_update_id, step: ContentObjectStore::ContentBlock::EditionsController::EDIT_FORM_STEPS[:review_links])
end

def attributes
@content_block_edition.details
def attributes
@schema.fields.each_with_object({}) do |field, hash|
hash[field] = nil
hash
end
end

def back_path
content_object_store_content_block_document_path(@content_block_edition.document)
end
def back_path
content_object_store_content_block_document_path(@content_block_edition.document)
end
end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def publish_with_rollback(schema:, title:, details:)
},
)
publish_publishing_api_edition(content_id:)
update_content_block_document_with_live_edition(content_block_edition)
update_content_block_document_with_latest_edition(content_block_edition)
content_block_edition.public_send(:publish!)
rescue PublishingFailureError => e
discard_publishing_api_edition(content_id:)
Expand Down Expand Up @@ -72,8 +72,11 @@ def discard_publishing_api_edition(content_id:)
Services.publishing_api.discard_draft(content_id)
end

def update_content_block_document_with_live_edition(content_block_edition)
content_block_edition.document.update!(live_edition_id: content_block_edition.id)
def update_content_block_document_with_latest_edition(content_block_edition)
content_block_edition.document.update!(
latest_edition_id: content_block_edition.id,
live_edition_id: content_block_edition.id,
)
end
end
end
Loading

0 comments on commit 019641a

Please sign in to comment.