Skip to content

Commit

Permalink
Merge pull request #4 from co-cddo/add-summary-step
Browse files Browse the repository at this point in the history
Add summary step to workflow
  • Loading branch information
RobNicholsGDS authored Feb 12, 2024
2 parents 321c869 + 228c3d3 commit 0217648
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 34 deletions.
3 changes: 2 additions & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def create
# PATCH/PUT /items/1
def update
if form.save
redirect_to item_url(form.item), notice: "Item was successfully updated."
target_url = ItemWorkflow.last_step?(item:) ? item_url(item) : edit_item_url(item)
redirect_to target_url, notice: "Item was successfully updated."
else
render :edit, status: :unprocessable_entity
end
Expand Down
5 changes: 5 additions & 0 deletions app/forms/summary_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SummaryForm < BaseForm
def update_item
item.metadata["summary"] = params.dig(:item, :summary).presence
end
end
11 changes: 0 additions & 11 deletions app/views/items/_form.html.erb

This file was deleted.

9 changes: 8 additions & 1 deletion app/views/items/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<h1 class="govuk-heading-l">Editing item</h1>

<%= render "items/forms/#{@form.template}", form: @form %>
<%= form_with(model: @form.item) do |f| %>
<%= render 'shared/form_errors', model: @form %>
<%= render "items/forms/#{@form.template}", form: f %>

<div>
<%= f.govuk_submit %>
</div>
<% end %>

<div>
<%= govuk_link_to "Show this item", @item %> |
Expand Down
14 changes: 5 additions & 9 deletions app/views/items/forms/_alternative_title.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<%= form_with(model: @form.item) do |form| %>
<%= render 'shared/form_errors', model: @form %>

<%= form.govuk_text_field :alternative_title, label: { text: 'Alternative title'} %>

<div>
<%= form.govuk_submit %>
</div>
<% end %>
<%= form.govuk_text_field(
:alternative_title,
label: { text: 'Alternative title'},
value: form.object.metadata['alternativeTitle']
) %>
1 change: 1 addition & 0 deletions app/views/items/forms/_summary.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= form.govuk_text_area :summary, value: form.object.metadata['summary'] %>
10 changes: 1 addition & 9 deletions app/views/items/forms/_title.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
<%= form_with(url: items_path, model: @form.item) do |form| %>
<%= render 'shared/form_errors', model: @form %>

<%= form.govuk_text_field :title %>

<div>
<%= form.govuk_submit %>
</div>
<% end %>
<%= form.govuk_text_field :title, value: form.object.metadata['title'] %>
9 changes: 8 additions & 1 deletion app/views/items/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<h1 class="govuk-heading-l">New item</h1>

<%= render "items/forms/title", form: @form %>
<%= form_with(url: items_path, model: @form.item) do |f| %>
<%= render 'shared/form_errors', model: @form %>
<%= render "items/forms/title", form: f %>

<div>
<%= f.govuk_submit %>
</div>
<% end %>

<div>
<%= govuk_link_to "Back to items", items_path %>
Expand Down
11 changes: 10 additions & 1 deletion app/workflows/item_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ class ItemWorkflow
STEPS = {
title: TitleForm,
alternative_title: AlternativeTitleForm,
summary: SummaryForm,
}.freeze

def self.form_for(**options)
new(**options).form
end

def self.last_step?(**options)
new(**options).last_step?
end

attr_reader :item, :params

def initialize(item:, params: {})
Expand All @@ -18,7 +23,7 @@ def initialize(item:, params: {})
def current_step
return first_step if item.last_completed_step.blank?

STEPS.keys[STEPS.keys.index(item.last_completed_step.to_sym) - 1].presence || first_step
STEPS.keys[STEPS.keys.index(item.last_completed_step.to_sym) + 1].presence || first_step
end

def first_step
Expand All @@ -28,4 +33,8 @@ def first_step
def form
STEPS[current_step].new(item:, params:)
end

def last_step?
item.last_completed_step&.to_sym == STEPS.keys.last
end
end
26 changes: 26 additions & 0 deletions spec/forms/summary_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe SummaryForm, type: :model do
let(:item) { create :item }
let(:summary) { Faker::Lorem.paragraph }
let(:params) do
{ item: { summary: } }
end
let(:summary_form) { described_class.new(item:, params:) }

describe "#save" do
it "saves the summary to metadata" do
summary_form.save
expect(item.reload.metadata["summary"]).to eq(summary)
end

context "with blank entry" do
let(:summary) { "" }

it "saves an empty array to alternative title" do
summary_form.save
expect(item.reload.metadata["summary"]).to be_nil
end
end
end
end
31 changes: 31 additions & 0 deletions spec/requests/item_workflow_intergration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "rails_helper"

RSpec.describe "items workflow", type: :request do
let(:steps) do
{
title: Faker::Company.name,
alternative_title: Faker::Company.name,
summary: Faker::Lorem.paragraph,
}
end

it "visits each step in the flow" do
item = nil
steps.each do |step, input|
if step == steps.keys.first
post items_url, params: { item: { step => input } }
item = Item.last
else
patch item_url(item), params: { item: { step => input } }
end

if step == steps.keys.last
expect(response).to redirect_to(item_url(item))
else
expect(response).to redirect_to(edit_item_url(item)), "#{step} didn't redirect to next step"
end

expect(item.reload.metadata[step.to_s.camelize(:lower)]).to include(input), "#{step} in metadata was not: #{input}"
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/items_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

it "redirects to the item" do
patch item_url(item), params: { item: { alternative_title: } }
expect(response).to redirect_to(item_url(item))
expect(response).to redirect_to(edit_item_url(item))
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/workflows/item_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,20 @@
expect(item_workflow.form.params).to eq(params)
end
end

describe "#last_step?" do
it "defaults to false" do
expect(item_workflow.last_step?).to be_falsy
end

context "when items is at last step" do
# Item has completed penultimate and is currently at last
# So item last completed step is two from end
let(:item) { create :item, last_completed_step: steps.keys.last }

it "returns true" do
expect(item_workflow.last_step?).to be_truthy
end
end
end
end

0 comments on commit 0217648

Please sign in to comment.