Skip to content

Commit

Permalink
Merge pull request #9841 from alphagov/content-modelling/828-addition…
Browse files Browse the repository at this point in the history
…al-workflow-iterations

(828) Additional workflow iterations
  • Loading branch information
pezholio authored Jan 24, 2025
2 parents d5786bc + 19e3d4b commit 4b12497
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ def scheduled_publication_date_params
]
end

def review_update_url
content_block_manager.content_block_manager_content_block_workflow_path(
@content_block_edition,
step: :review,
)
end

def is_scheduling?
@content_block_edition.scheduled_publication.present?
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Workflow
class Step < Data.define(:name, :show_action, :update_action)
ALL = [
Step.new(:edit_draft, :edit_draft, nil),
Step.new(:review_links, :review_links, :redirect_to_next_step),
Step.new(:schedule_publishing, :schedule_publishing, :validate_schedule),
Step.new(:internal_note, :internal_note, :update_internal_note),
Step.new(:change_note, :change_note, :update_change_note),
Step.new(:review, :review, :validate_review_page),
Step.new(:confirmation, :confirmation, nil),
].freeze

def self.by_name(name)
ALL.find { |step| step.name == name.to_sym }
end

def previous_step
ALL[index - 1]
end

def next_step
ALL[index + 1]
end

private

def index
ALL.find_index { |step| step.name == name.to_sym }
end
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
module Workflow::ShowMethods
extend ActiveSupport::Concern

SHOW_ACTIONS = {
edit_draft: :edit_draft,
review_links: :review_links,
schedule_publishing: :schedule_publishing,
internal_note: :internal_note,
change_note: :change_note,
review: :review,
review_update: :review_update,
confirmation: :confirmation,
}.freeze

def edit_draft
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])
@form = ContentBlockManager::ContentBlock::EditionForm.for(
Expand Down Expand Up @@ -44,41 +33,18 @@ def schedule_publishing

def internal_note
@content_block_document = @content_block_edition.document
@back_path = content_block_manager.content_block_manager_content_block_workflow_path(
@content_block_edition,
step: :schedule_publishing,
)

render :internal_note
end

def change_note
@content_block_document = @content_block_edition.document
@back_path = content_block_manager.content_block_manager_content_block_workflow_path(
@content_block_edition,
step: :internal_note,
)

render :change_note
end

def review_update
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])

@url = review_update_url
@back_path = content_block_manager.content_block_manager_content_block_workflow_path(
@content_block_edition,
step: :change_note,
)

render :review
end

def review
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])
@back_path = content_block_manager.content_block_manager_content_block_documents_path

@url = review_url

render :review
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,27 @@ module Workflow::UpdateMethods

REVIEW_ERROR = Data.define(:attribute, :full_message)

UPDATE_ACTIONS = {
review_links: :redirect_to_schedule,
schedule_publishing: :validate_schedule,
internal_note: :update_internal_note,
change_note: :update_change_note,
review_update: :validate_review_page,
review: :validate_review_page,
}.freeze

def redirect_to_schedule
redirect_to content_block_manager.content_block_manager_content_block_workflow_path(
id: @content_block_edition.id,
step: :schedule_publishing,
)
end

def validate_schedule
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])

validate_scheduled_edition

redirect_to content_block_manager.content_block_manager_content_block_workflow_path(
id: @content_block_edition.id,
step: :internal_note,
)
redirect_to_next_step
rescue ActiveRecord::RecordInvalid
render "content_block_manager/content_block/editions/workflow/schedule_publishing"
end

def update_internal_note
@content_block_edition.update!(internal_change_note: edition_params[:internal_change_note])

redirect_to content_block_manager.content_block_manager_content_block_workflow_path(
id: @content_block_edition.id,
step: :change_note,
)
redirect_to_next_step
end

def update_change_note
@content_block_edition.assign_attributes(change_note: edition_params[:change_note], major_change: edition_params[:major_change])
@content_block_edition.save!(context: :change_note)

redirect_to content_block_manager.content_block_manager_content_block_workflow_path(
id: @content_block_edition.id,
step: :review_update,
)
redirect_to_next_step
rescue ActiveRecord::RecordInvalid
render :change_note
end
Expand All @@ -57,20 +32,20 @@ def validate_review_page
if params[:is_confirmed].blank?
@confirm_error_copy = I18n.t("content_block_edition.review_page.errors.confirm")
@error_summary_errors = [{ text: @confirm_error_copy, href: "#is_confirmed-0" }]
@url = on_review_page? ? review_url : review_update_url
render "content_block_manager/content_block/editions/workflow/review"
render :review
else
schedule_or_publish
end
end

private

def on_review_page?
params[:step] == :review
end
def redirect_to_next_step
next_step = Workflow::Step.by_name(params[:step])&.next_step

def on_review_update_page?
params[:step] == :review_update
redirect_to content_block_manager.content_block_manager_content_block_workflow_path(
id: @content_block_edition.id,
step: next_step&.name,
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ContentBlockManager::ContentBlock::Editions::WorkflowController < ContentB

def show
step = params[:step].to_sym
action = SHOW_ACTIONS[step]
action = Workflow::Step.by_name(step)&.show_action

if action
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])
Expand All @@ -18,7 +18,7 @@ def show

def update
step = params[:step].to_sym
action = UPDATE_ACTIONS[step]
action = Workflow::Step.by_name(step)&.update_action

if action
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])
Expand Down
15 changes: 15 additions & 0 deletions lib/engines/content_block_manager/app/helpers/workflow_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module WorkflowHelper
def back_path(content_block_edition, current_step)
if current_step == "review" && content_block_edition.document.is_new_block?
content_block_manager.content_block_manager_content_block_documents_path
else
step = Workflow::Step.by_name(current_step)&.previous_step
return nil unless step

content_block_manager.content_block_manager_content_block_workflow_path(
content_block_edition,
step: step.name,
)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def embed_code
def title
@title ||= latest_edition&.title
end

def is_new_block?
editions.count == 1
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% content_for :title_margin_bottom, 4 %>
<% content_for :back_link do %>
<%= render "govuk_publishing_components/components/back_link", {
href: @back_path,
href: back_path(@content_block_edition, "change_note"),
} %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% content_for :title_margin_bottom, 4 %>
<% content_for :back_link do %>
<%= render "govuk_publishing_components/components/back_link", {
href: @back_path,
href: back_path(@content_block_edition, "internal_note"),
} %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<% content_for :context, "Create a content block" %>
<% content_for :title, "Review #{@content_block_edition.block_type.humanize.downcase}" %>

<% content_for :back_link do %>
<%= render "govuk_publishing_components/components/back_link", {
href: @back_path,
href: back_path(@content_block_edition, "review"),
} %>
<% end %>

Expand All @@ -23,7 +24,10 @@
</div>
</div>

<%= form_with(url: @url, method: :put) do %>
<%= form_with(
url: content_block_manager.content_block_manager_content_block_workflow_path(@content_block_edition, step: :review),
method: :put,
) do %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<%= render "govuk_publishing_components/components/checkboxes", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% content_for :title_margin_bottom, 4 %>
<% content_for :back_link do %>
<%= render "govuk_publishing_components/components/back_link", {
href: content_block_manager.new_content_block_manager_content_block_document_edition_path(@content_block_document),
href: back_path(@content_block_edition, "review_links"),
} %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
content_block_edition: @content_block_edition,
params:,
context:,
back_link: content_block_manager.content_block_manager_content_block_workflow_path(
@content_block_edition,
step: :review_links,
),
back_link: back_path(@content_block_edition, "schedule_publishing"),
form_url: content_block_manager.content_block_manager_content_block_workflow_path(
@content_block_edition,
step: :schedule_publishing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Feature: Edit a content object
And I should see a back link to the document page
When I fill out the form
Then I should be on the "review_links" step
And I should see a back link to the "edit" step
And I should see a back link to the "edit_draft" step
When I continue after reviewing the links
Then I should be on the "schedule_publishing" step
And I should see a back link to the "review_links" step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ContentBlockManager::ContentBlock::WorkflowTest < ActionDispatch::Integrat
assert_equal edition.reload.change_note, change_note
assert_equal edition.reload.major_change, true

assert_redirected_to content_block_manager_content_block_workflow_path(id: edition.id, step: :review_update)
assert_redirected_to content_block_manager_content_block_workflow_path(id: edition.id, step: :review)
end

it "shows an error if the change is major and the change note is blank" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "test_helper"

class ContentBlockManager::ContentBlock::WorkflowTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

describe "Step" do
describe ".by_name" do
it "returns a step by its name" do
step = Workflow::Step.by_name("review_links")

assert_equal step&.name, :review_links
end
end

describe "#next_step" do
[
%i[edit_draft review_links],
%i[review_links schedule_publishing],
%i[schedule_publishing internal_note],
%i[internal_note change_note],
%i[change_note review],
%i[review confirmation],
].each do |current_step, expected_step|
it "returns #{expected_step} step when the current step is #{current_step}" do
step = Workflow::Step.by_name(current_step)
assert_equal step&.next_step&.name, expected_step
end
end
end

describe "#previous_step" do
[
%i[review_links edit_draft],
%i[schedule_publishing review_links],
%i[internal_note schedule_publishing],
%i[change_note internal_note],
%i[review change_note],
].each do |current_step, expected_step|
it "returns #{expected_step} step when the current step is #{current_step}" do
step = Workflow::Step.by_name(current_step)
assert_equal step&.previous_step&.name, expected_step
end
end
end
end
end
Loading

0 comments on commit 4b12497

Please sign in to comment.