Skip to content

Commit

Permalink
Merge pull request #2374 from alphagov/unpublish-edition-edit
Browse files Browse the repository at this point in the history
Unpublish edition edit
  • Loading branch information
syed-ali-tw authored Oct 18, 2024
2 parents 28522ea + d3cea9f commit 25fcb87
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 52 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $govuk-page-width: 1140px;
@import 'govuk_publishing_components/components/heading';
@import 'govuk_publishing_components/components/hint';
@import 'govuk_publishing_components/components/input';
@import 'govuk_publishing_components/components/inset-text';
@import 'govuk_publishing_components/components/label';
@import 'govuk_publishing_components/components/layout-footer';
@import 'govuk_publishing_components/components/layout-for-admin';
Expand Down
61 changes: 59 additions & 2 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ class EditionsController < InheritedResources::Base
layout "design_system"

defaults resource_class: Edition, collection_name: "editions", instance_name: "resource"

before_action :setup_view_paths, except: %i[index]
before_action except: %i[index] do
require_user_accessibility_to_edition(@resource)
end
before_action only: %i[unpublish confirm_unpublish process_unpublish] do
require_govuk_editor(redirect_path: edition_path(resource))
end

helper_method :locale_to_language

Expand All @@ -21,6 +25,7 @@ def show
end

alias_method :metadata, :show
alias_method :unpublish, :show

def history
render action: "show"
Expand All @@ -34,8 +39,30 @@ def linking
render action: "show"
end

def unpublish
render action: "show"
def confirm_unpublish
if redirect_url.blank? || validate_redirect(redirect_url)
render "secondary_nav_tabs/confirm_unpublish"
else
error_message = "Redirect path is invalid. #{description(resource)} can not be unpublished."
@resource.errors.add(:redirect_url, error_message)
render "show"
end
end

def process_unpublish
artefact = @resource.artefact

success = unpublish_edition(artefact)
if success
notice = "Content unpublished"
notice << " and redirected" if redirect_url.present?
flash[:success] = notice
redirect_to root_path
else
render_confirm_page_with_error
end
rescue StandardError
render_confirm_page_with_error
end

protected
Expand All @@ -46,6 +73,19 @@ def setup_view_paths

private

def unpublish_edition(artefact)
params["redirect_url"].strip.empty? ? UnpublishService.call(artefact, current_user) : UnpublishService.call(artefact, current_user, redirect_url)
end

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

def downstream_error_message
"Due to a service problem, the edition couldn't be unpublished"
end

def setup_view_paths_for(publication)
prepend_view_path "app/views/editions"
prepend_view_path template_folder_for(publication)
Expand All @@ -61,4 +101,21 @@ def locale_to_language(locale)
""
end
end

def validate_redirect(redirect_url)
regex = /(\/([a-z0-9]+-)*[a-z0-9]+)+/
redirect_url =~ regex
end

def make_govuk_url_relative(url = "")
url.sub(%r{^(https?://)?(www\.)?gov\.uk/}, "/")
end

def redirect_url
make_govuk_url_relative params["redirect_url"]
end

def description(resource)
resource.format.underscore.humanize
end
end
36 changes: 36 additions & 0 deletions app/helpers/common_components_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module CommonComponentsHelper
def header_for(tab_name)
render "govuk_publishing_components/components/heading", {
text: tab_name,
heading_level: 2,
margin_bottom: 5,
}
end

def primary_button_for(model, url, text)
form_for model, url:, method: :post do
render "govuk_publishing_components/components/button", {
text:,
margin_bottom: 3,
}
end
end

def secondary_button_for(model, url, text)
form_for model, url:, method: :post do
render "govuk_publishing_components/components/button", {
text:,
margin_bottom: 3,
secondary_solid: true,
}
end
end

def primary_link_button_for(url, text)
render "govuk_publishing_components/components/button", {
text:,
href: url,
margin_bottom: 3,
}
end
end
27 changes: 0 additions & 27 deletions app/helpers/popular_links_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,4 @@ def popular_link_rows(item)
rows << { key: "URL", value: item[:url] }
rows.compact
end

def primary_button_for(model, url, text)
form_for model, url:, method: :post do
render "govuk_publishing_components/components/button", {
text:,
margin_bottom: 3,
}
end
end

def secondary_button_for(model, url, text)
form_for model, url:, method: :post do
render "govuk_publishing_components/components/button", {
text:,
margin_bottom: 3,
secondary_solid: true,
}
end
end

def primary_link_button_for(url, text)
render "govuk_publishing_components/components/button", {
text:,
href: url,
margin_bottom: 3,
}
end
end
12 changes: 11 additions & 1 deletion app/helpers/tabbed_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ def edition_nav_items(edition)
nav_items = []

all_tab_names.each do |item|
next if !edition.state.eql?("published") && item == "unpublish"

nav_items << standard_nav_items(item, edition)
end

Expand Down Expand Up @@ -31,7 +33,15 @@ def edit_nav_item(label, href, current)

def current_tab_name
current_tab = (request.path.split("/") & all_tab_names).first
current_tab == "metadata" ? "metadata" : "temp_nav_text"

case current_tab
when "metadata"
"metadata"
when "unpublish"
"unpublish"
else
"temp_nav_text"
end
end

private
Expand Down
7 changes: 4 additions & 3 deletions app/models/edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def cloning_between_parted_types?(new_edition)

def self.find_or_create_from_panopticon_data(panopticon_id, importing_user)
existing_publication = Edition.where(panopticon_id:)
.order_by(version_number: :desc).first
.order_by(version_number: :desc).first
return existing_publication if existing_publication

metadata = Artefact.find(panopticon_id)
Expand Down Expand Up @@ -422,8 +422,9 @@ def check_for_archived_artefact
a = Artefact.find(panopticon_id)
if (a.state == "archived") && changes.any?
# If we're only changing the state to archived, that's ok
# Any other changes are not allowed
allowed_keys = %w[state updated_at]
# We have to add owning_org_content_ids as Mongo changes the
# value from nil to an empty array on save - reads as a change
allowed_keys = %w[state updated_at owning_org_content_ids]
unless (changes.keys - allowed_keys).empty? && (state == "archived")
raise "Editing of an edition with an Archived artefact is not allowed"
end
Expand Down
6 changes: 1 addition & 5 deletions app/views/editions/secondary_nav_tabs/_metadata.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/heading", {
text: "Metadata",
heading_level: 2,
margin_bottom: 5,
} %>
<%= header_for("Metadata") %>
<% if Edition::PUBLISHING_API_DRAFT_STATES.include? publication.state %>
<%= form_for(@artefact, :html => { :class => "artefact", :id => "edit_artefact" }) do |f| %>
Expand Down
31 changes: 31 additions & 0 deletions app/views/editions/secondary_nav_tabs/_unpublish.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% @edition = @resource %>
<%= header_for("Unpublish") %>
<%= render "govuk_publishing_components/components/inset_text", {
text: "If you unpublish a page from GOV.UK it cannot be undone.",
} %>
<%= form_for @edition, url: confirm_unpublish_edition_path, method: "get" do |f| %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: "Redirect to URL",
},
id: "redirect_url",
name: "redirect_url",
value: params[:redirect_url],
hint: "For example: https://www.gov.uk/redirect-to-replacement-page",
heading_level: 3,
heading_size: "s",
error_items: errors_for(f.object.errors, :redirect_url, use_full_message: false),
} %>
<div class="govuk-button-group">
<%= render("govuk_publishing_components/components/button", {
text: "Continue",
type: "submit",
}) %>
</div>
<% end %>
</div>
</div>
39 changes: 39 additions & 0 deletions app/views/editions/secondary_nav_tabs/confirm_unpublish.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% @edition = @resource %>
<% content_for :title_context, @edition.title %>
<% content_for :page_title, "Unpublish" %>
<% content_for :title, "Unpublish" %>
<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 unpublish a page from GOV.UK it cannot be undone.",
} %>
<%= form_for @edition, url: process_unpublish_edition_path(@edition), method: :post do %>
<%= hidden_field_tag :redirect_url, params[:redirect_url] %>
<p class="govuk-body govuk-!-margin-bottom-7">Are you sure you want to unpublish this document?</p>
<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Unpublish document",
destructive: true,
} %>
<%= link_to("Cancel", unpublish_edition_path, class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
</div>
</div>
15 changes: 15 additions & 0 deletions app/views/editions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
<% content_for :page_title, @resource.title %>
<% content_for :title, @resource.title %>
<% 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-row">
<div class="govuk-grid-column-two-thirds editions__edit__summary">
<%= render "govuk_publishing_components/components/summary_list", {
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
get "related_external_links", to: "editions#linking"
get "tagging", to: "editions#linking"
get "unpublish"
get "unpublish/confirm-unpublish", to: "editions#confirm_unpublish", as: "confirm_unpublish"
post "process_unpublish"
end
end
end
Expand Down
Loading

0 comments on commit 25fcb87

Please sign in to comment.