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

Add confirm discard page as an extra step when discarding a draft (un… #2134

Merged
merged 2 commits into from
Aug 23, 2023
Merged
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
27 changes: 25 additions & 2 deletions app/controllers/manuals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ def publish
)
end

def confirm_discard
service = Manual::ShowService.new(
manual_id:,
user: current_user,
)
manual = service.call

if !manual.has_ever_been_published?
render(
:confirm_discard,
layout: "design_system",
locals: {
manual:,
},
)
else
redirect_to(
manual_path(manual_id),
flash: { error: "#{manual.title} cannot be discarded as it has already been published" },
)
end
end

def discard_draft
service = Manual::DiscardDraftService.new(
user: current_user,
Expand All @@ -171,12 +194,12 @@ def discard_draft
if result.successful?
redirect_to(
manuals_path,
flash: { notice: "Discarded draft of #{result.manual_title}" },
flash: { success: "Discarded draft of #{result.manual_title}" },
)
else
redirect_to(
manual_path(manual_id),
flash: { notice: "Unable to discard draft of #{result.manual_title}" },
flash: { error: "Unable to discard draft of #{result.manual_title}" },
)
end
end
Expand Down
39 changes: 39 additions & 0 deletions app/views/manuals/confirm_discard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% content_for :title, "Discard #{manual.title}" %>

<% content_for :title_margin_bottom, 6 %>

<%= render "govuk_publishing_components/components/breadcrumbs", {
collapse_on_mobile: true,
breadcrumbs: [
{
title: "Your manuals",
url: manuals_path
},
{
title: manual.title,
url: manual_path(manual)
},
{
title: "Discard"
},
]
} %>

<%= render "govuk_publishing_components/components/title", {
title: "Discard #{manual.title}"
} %>

<p class="govuk-body">You are about to discard "<%= manual.title %>".</p>
<p class="govuk-body">Are you sure you want to discard this draft manual?</p>

<%= form_tag(discard_draft_manual_path(manual), method: :delete) do %>
<div class="govuk-button-group govuk-!-margin-bottom-6">
<%= render "govuk_publishing_components/components/button", {
text: "Discard manual",
name: "submit",
destructive: true
} %>

<%= link_to("Cancel", manual_path(manual), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
4 changes: 1 addition & 3 deletions app/views/manuals/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@
<div class="panel panel-default">
<div class="panel-heading"><h3>Discard draft manual</h3></div>
<div class="panel-body">
<%= form_tag(discard_draft_manual_path(manual), method: :delete) do %>
<button name="submit" class="btn btn-danger" data-module="confirm" data-message="Are you sure you want to discard this draft manual?">Discard draft manual</button>
<% end -%>
<%= link_to 'Discard draft', confirm_discard_manual_path(manual), class: 'btn btn-danger' %>
</div>
</div>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
delete :discard_draft, on: :member

get :confirm_publish, on: :member
get :confirm_discard, on: :member

get :original_publication_date, on: :member, action: :edit_original_publication_date
put :original_publication_date, on: :member, action: :update_original_publication_date
Expand Down
1 change: 1 addition & 0 deletions features/deleting-a-manual.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Feature: Rake task to delete a manual
Given a draft manual exists without any sections
And a draft section exists for the manual
When I discard the draft manual
And I confirm draft deletion
Then the manual and its sections are deleted

Scenario: Deleting a published manual
Expand Down
5 changes: 5 additions & 0 deletions features/step_definitions/deleting_manuals_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@
When(/^I discard the draft manual$/) do
discard_draft_manual(@manual.title)
end

When(/^I confirm draft deletion$/) do
expect(page).to have_button("Discard manual")
click_on "Discard manual"
end
46 changes: 44 additions & 2 deletions spec/controllers/manuals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,48 @@
end
end

describe "#confirm_discard" do
let(:manual_id) { "manual-1" }
let(:service) { double(Manual::ShowService, call: manual) }
let(:manual) { instance_double(Manual, title: manual_title) }
let(:manual_title) { "My manual title" }

before do
login_as_stub_user
allow(Manual::ShowService).to receive(:new).and_return(service)
end

context "when the manual has been previously published" do
before do
allow(manual).to receive(:has_ever_been_published?).and_return(true)
get :confirm_discard, params: { id: manual_id }
end

it "sets a flash message indicating failure" do
expect(flash[:error]).to include("#{manual_title} cannot be discarded as it has already been published")
end

it "redirects to the show page for the manual" do
expect(response).to redirect_to manual_path(manual_id)
end
end

context "when the manual has not been previously published" do
before do
allow(manual).to receive(:has_ever_been_published?).and_return(false)
get :confirm_discard, params: { id: manual_id }
end

it "renders the discard confirmation page" do
expect(response).to render_template(:confirm_discard)
end

it "renders with the design system layout" do
expect(response).to render_template("design_system")
end
end
end

describe "#discard_draft" do
let(:manual_id) { "manual-1" }
let(:service) { double(Manual::DiscardDraftService, call: result) }
Expand All @@ -49,7 +91,7 @@
let(:discard_success) { true }

it "sets a flash message indicating success" do
expect(flash[:notice]).to include("Discarded draft of My manual")
expect(flash[:success]).to include("Discarded draft of My manual")
end

it "redirects to the manuals index" do
Expand All @@ -61,7 +103,7 @@
let(:discard_success) { false }

it "sets a flash message indicating failure" do
expect(flash[:notice]).to include("Unable to discard draft of My manual")
expect(flash[:error]).to include("Unable to discard draft of My manual")
end

it "redirects to the show page for the manual" do
Expand Down
26 changes: 26 additions & 0 deletions spec/views/manuals/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "spec_helper"

describe "manuals/show", type: :view do
before do
allow(view).to receive(:current_user_is_gds_editor?).and_return(true)
allow(view).to receive(:current_user_can_publish?).and_return(true)
end

it "does not render the discard button for a published manual" do
manual = FactoryBot.build_stubbed(:manual, ever_been_published: true)
manual.publish_tasks = []

render template: "manuals/show", locals: { manual:, slug_unique: true, clashing_sections: [] }

expect(rendered).not_to match(/Discard draft/)
end

it "renders the discard button for an unpublished manual" do
manual = FactoryBot.build_stubbed(:manual, ever_been_published: false)
manual.publish_tasks = []

render template: "manuals/show", locals: { manual:, slug_unique: true, clashing_sections: [] }

expect(rendered).to match(/Discard draft/)
end
end