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 delete to admin tab #2416

Merged
merged 1 commit into from
Nov 18, 2024
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
6 changes: 6 additions & 0 deletions app/assets/stylesheets/editions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
@include govuk-responsive-padding(4, "top");
border-top: 1px solid $govuk-border-colour;
}

.editions__admin__tab {
.govuk-link {
margin-top: govuk-spacing(3)
}
}
31 changes: 27 additions & 4 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class EditionsController < InheritedResources::Base
before_action only: %i[unpublish confirm_unpublish process_unpublish] do
require_govuk_editor(redirect_path: edition_path(resource))
end
before_action only: %i[progress admin update] do
before_action only: %i[progress admin update confirm_destroy] do
require_editor_permissions
end
before_action only: %i[confirm_destroy destroy] do
destroyable_edition?
end

helper_method :locale_to_language

Expand Down Expand Up @@ -90,6 +93,19 @@ def progress
redirect_to edition_path(resource)
end

def confirm_destroy
render "secondary_nav_tabs/confirm_destroy"
end

def destroy
@resource.destroy!
flash[:success] = "Edition deleted"
redirect_to root_url
rescue StandardError
flash[:danger] = downstream_error_message(:deleted)
render "secondary_nav_tabs/confirm_destroy"
end

protected

def setup_view_paths
Expand All @@ -108,12 +124,12 @@ def unpublish_edition(artefact)
end

def render_confirm_page_with_error
@resource.errors.add(:unpublish, downstream_error_message)
@resource.errors.add(:unpublish, downstream_error_message(:unpublished))
render "secondary_nav_tabs/confirm_unpublish"
end

def downstream_error_message
"Due to a service problem, the edition couldn't be unpublished"
def downstream_error_message(action)
"Due to a service problem, the edition couldn't be #{action}"
end

def locale_to_language(locale)
Expand Down Expand Up @@ -153,4 +169,11 @@ def progress_action_param
def permitted_params
params.require(:edition).permit(%i[title overview in_beta body major_change change_note])
end

def destroyable_edition?
return if @resource.can_destroy?

flash[:danger] = "Cannot delete a #{description(@resource).downcase} that has ever been published."
Copy link
Contributor

@ellohez ellohez Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way that we can re-word this? Just worried that we could display something grammatically incorrect such as:
"Cannot delete a answer that has ever been published"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been copied from existing error message and I think we have decided not to change error messages for now.

I have made the note of this to be added to the doc where we are maintaining list of all error messages to be reviewed.

redirect_to edition_path(@resource)
end
end
16 changes: 7 additions & 9 deletions app/views/editions/secondary_nav_tabs/_admin.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<% @edition = @resource %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<div class="govuk-grid-column-two-thirds editions__admin__tab">
<%= header_for("Admin") %>

<% if @edition.fact_check? %>
<%= form_for @edition, url: skip_fact_check_edition_path(@edition), method: "post" do %>
<%= render "govuk_publishing_components/components/button", {
text: "Skip fact check",
} %>
<% end %>
<% end %>
<%= render "govuk_publishing_components/components/list", {
items: [
( primary_button_for(@edition, skip_fact_check_edition_path(@edition), "Skip fact check") if @edition.fact_check? ),
( link_to("Delete edition #{@edition.version_number}", confirm_destroy_edition_path(@resource), class: "govuk-link gem-link--destructive") if @edition.can_destroy? ),
],
} %>
</div>
</div>
37 changes: 37 additions & 0 deletions app/views/editions/secondary_nav_tabs/confirm_destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<% @edition = @resource %>
<% content_for :title_context, @edition.title %>
<% content_for :page_title, "Delete edition" %>
<% content_for :title, "Delete edition" %>
<div class="govuk-grid-row">
<% unless @edition.errors.empty? %>
<% content_for :error_summary do %>
<%= render("govuk_publishing_components/components/error_summary", {
id: "error-summary",
title: "There is a problem",
items: @edition.errors.map do |error|
{
text: error.message,
href: "##{error.attribute.to_s}",
}
end,
}) %>
<% end %>
<% end %>

<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/inset_text", {
text: "If you delete this edition it cannot be undone.",
} %>

<%= form_for @edition, url: admin_delete_edition_path(@edition), method: :delete do %>
<p class="govuk-body govuk-!-margin-bottom-7">Are you sure you want to delete this edition?</p>
<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Delete edition",
destructive: true,
} %>
<%= link_to("Cancel", admin_edition_path, class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,3 @@
<% end %>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
get "unpublish"
get "unpublish/confirm-unpublish", to: "editions#confirm_unpublish", as: "confirm_unpublish"
post "process_unpublish"
get "admin/confirm-destroy", to: "editions#confirm_destroy", as: "confirm_destroy"
delete "admin/delete-edition", to: "editions#destroy", as: "admin_delete"
post "progress"
post "skip_fact_check",
to: "editions#progress",
Expand Down
100 changes: 88 additions & 12 deletions test/functional/editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,98 @@ class EditionsControllerTest < ActionController::TestCase
end

context "#admin" do
should "show the admin page for the edition" do
get :admin, params: { id: @edition.id }
context "do not have required permissions" do
context "Welsh editors and non Welsh edition" do
setup do
login_as_welsh_editor
end

assert_response :success
%i[admin confirm_destroy].each do |url_path|
should "show permission error and redirects to edition path for #{url_path} path" do
get url_path, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
end
end
end

context "nor Welsh nor Govuk editors" do
setup do
user = FactoryBot.create(:user, name: "Stub User")
login_as(user)
end

%i[admin confirm_destroy].each do |url_path|
should "show permission error and redirects to edition path for #{url_path} path" do
get url_path, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
end
end
end
end

context "Welsh editors" do
setup do
login_as_welsh_editor
context "has required permissions" do
context "Welsh editors and welsh edition" do
setup do
login_as_welsh_editor
end

%i[admin confirm_destroy].each do |url_path|
should "be able to navigate successfully to #{url_path} path" do
get url_path, params: { id: @welsh_edition.id }

assert_response :success
end
end
end

should "be able to see the admin page for Welsh editions" do
get :admin, params: { id: @welsh_edition.id }
should "be able to navigate to the admin path" do
get :admin, params: { id: @edition.id }

assert_response :success
end

should "not be able to see the admin page for non-Welsh editions" do
get :admin, params: { id: @edition.id }
context "#confirm_destroy" do
should "be able to navigate to the confirm destroy path" do
get :confirm_destroy, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
assert_response :success
end

should "delete the edition from the database and display success message with redirection to root" do
delete :destroy, params: { id: @edition.id }

assert_equal 0, Edition.where(id: @edition.id).count
assert_equal "Edition deleted", flash[:success]
assert_redirected_to root_path
end

%i[published scheduled_for_publishing archived].each do |edition_state|
context "edition with state '#{edition_state}' can not be deleted" do
setup do
@edition = FactoryBot.create(:edition, state: edition_state, publish_at: Time.zone.now + 1.hour)
end

should "redirect to edition path with error message" do
delete :destroy, params: { id: @edition.id }

assert_redirected_to edition_path
assert_equal "Cannot delete a #{description(@edition)} that has ever been published.", flash[:danger]
end
end
end

should "render confirm destroy page with error if deleting from database fails" do
Edition.any_instance.stubs(:destroy!).raises(Mongoid::Errors::MongoidError.new)

delete :destroy, params: { id: @edition.id }

assert_template "secondary_nav_tabs/confirm_destroy"
assert_equal "Due to a service problem, the edition couldn't be deleted", flash[:danger]
end
end
end
end
Expand Down Expand Up @@ -330,4 +400,10 @@ class EditionsControllerTest < ActionController::TestCase
}
end
end

private

def description(edition)
edition.format.underscore.humanize.downcase
end
end
Loading
Loading