diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index f084f9bd0..c14949e5f 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -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'; diff --git a/app/controllers/editions_controller.rb b/app/controllers/editions_controller.rb index 349b791db..a52958e33 100644 --- a/app/controllers/editions_controller.rb +++ b/app/controllers/editions_controller.rb @@ -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 @@ -21,6 +25,7 @@ def show end alias_method :metadata, :show + alias_method :unpublish, :show def history render action: "show" @@ -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 @@ -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) @@ -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 diff --git a/app/helpers/common_components_helper.rb b/app/helpers/common_components_helper.rb new file mode 100644 index 000000000..35e63a300 --- /dev/null +++ b/app/helpers/common_components_helper.rb @@ -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 diff --git a/app/helpers/popular_links_helper.rb b/app/helpers/popular_links_helper.rb index 0bc89bb71..6c19a09ef 100644 --- a/app/helpers/popular_links_helper.rb +++ b/app/helpers/popular_links_helper.rb @@ -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 diff --git a/app/helpers/tabbed_nav_helper.rb b/app/helpers/tabbed_nav_helper.rb index 13d95b6e8..a174cf782 100644 --- a/app/helpers/tabbed_nav_helper.rb +++ b/app/helpers/tabbed_nav_helper.rb @@ -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 @@ -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 diff --git a/app/models/edition.rb b/app/models/edition.rb index 5816bf858..ca584a9aa 100644 --- a/app/models/edition.rb +++ b/app/models/edition.rb @@ -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) @@ -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 diff --git a/app/views/editions/secondary_nav_tabs/_metadata.html.erb b/app/views/editions/secondary_nav_tabs/_metadata.html.erb index 65c972f9b..50b56e9bc 100644 --- a/app/views/editions/secondary_nav_tabs/_metadata.html.erb +++ b/app/views/editions/secondary_nav_tabs/_metadata.html.erb @@ -1,10 +1,6 @@
Are you sure you want to unpublish this document?
+ + <% end %> +