diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index c14949e5f..e3e12a967 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -7,6 +7,7 @@ $govuk-page-width: 1140px; @import 'govuk_publishing_components/components/button'; @import 'govuk_publishing_components/components/checkboxes'; @import 'govuk_publishing_components/components/date-input'; +@import 'govuk_publishing_components/components/details'; @import 'govuk_publishing_components/components/document-list'; @import 'govuk_publishing_components/components/error-alert'; @import 'govuk_publishing_components/components/error-message'; @@ -37,6 +38,6 @@ $govuk-page-width: 1140px; @import 'govuk_publishing_components/components/title'; @import "tags"; @import "downtimes"; +@import "options_sidebar"; @import "publications"; -@import "popular_links"; @import "editions"; diff --git a/app/assets/stylesheets/popular_links.scss b/app/assets/stylesheets/options_sidebar.scss similarity index 89% rename from app/assets/stylesheets/popular_links.scss rename to app/assets/stylesheets/options_sidebar.scss index 4ce310c3e..69a301522 100644 --- a/app/assets/stylesheets/popular_links.scss +++ b/app/assets/stylesheets/options_sidebar.scss @@ -1,4 +1,4 @@ -.popular-links__sidebar { +.options-sidebar { .govuk-button { width: 100%; } diff --git a/app/controllers/editions_controller.rb b/app/controllers/editions_controller.rb index cd12f371e..7c7eb5a49 100644 --- a/app/controllers/editions_controller.rb +++ b/app/controllers/editions_controller.rb @@ -11,7 +11,7 @@ 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] do + before_action only: %i[progress admin update] do require_editor_permissions end @@ -31,6 +31,22 @@ def show alias_method :unpublish, :show alias_method :admin, :show + def update + @resource.assign_attributes(permitted_params) + + if @resource.save + UpdateWorker.perform_async(resource.id.to_s) + flash.now[:success] = "Edition updated successfully." + else + @artefact = @resource.artefact + end + rescue StandardError => e + Rails.logger.error "Error #{e.class} #{e.message}" + @resource.errors.add(:show, "Due to a service problem, the edition couldn't be updated") + ensure + render "show" + end + def history render action: "show" end @@ -133,4 +149,8 @@ def progress_action_param rescue StandardError nil end + + def permitted_params + params.require(:edition).permit(%i[title overview in_beta body major_change change_note]) + end end diff --git a/app/helpers/editions_helper.rb b/app/helpers/editions_helper.rb index 0fe663a63..cb15433f8 100644 --- a/app/helpers/editions_helper.rb +++ b/app/helpers/editions_helper.rb @@ -6,7 +6,7 @@ def activity_forms_required? params[:action] == "diff" end - def resource_form(resource, &form_definition) + def legacy_resource_form(resource, &form_definition) html_options = { id: "edition-form" } unless resource.locked_for_edits? || resource.archived? if resource.is_a?(Parted) || resource.is_a?(Varianted) diff --git a/app/helpers/tabbed_nav_helper.rb b/app/helpers/tabbed_nav_helper.rb index 07c5c4dac..22033d6ef 100644 --- a/app/helpers/tabbed_nav_helper.rb +++ b/app/helpers/tabbed_nav_helper.rb @@ -15,6 +15,8 @@ def current_tab_name current_tab = (request.path.split("/") & all_tab_names).first case current_tab + when nil + "edit" when "metadata" "metadata" when "unpublish" diff --git a/app/views/editions/secondary_nav_tabs/_edit.html.erb b/app/views/editions/secondary_nav_tabs/_edit.html.erb new file mode 100644 index 000000000..dba5656ae --- /dev/null +++ b/app/views/editions/secondary_nav_tabs/_edit.html.erb @@ -0,0 +1,128 @@ +
+
+ <%= render "govuk_publishing_components/components/heading", { + text: "Edit", + heading_level: 2, + margin_bottom: 5, + } %> +
+
+ +<%= form_for @resource, as: :edition, url: edition_path(@resource) do |f| %> +
+
+ <%= render "govuk_publishing_components/components/input", { + label: { + text: "Title", + }, + id: "title", + name: "edition[title]", + value: @resource.title, + heading_size: "m", + error_items: errors_for(@edition.errors, "title".to_sym, use_full_message: false), + } %> + + <%= render "govuk_publishing_components/components/textarea", { + label: { + heading_size: "m", + text: "Meta tag description", + }, + name: "edition[overview]", + hint: "Some search engines will display this if they cannot find what they need in the main text", + value: @resource.overview, + } %> + + <%= render "govuk_publishing_components/components/radio", { + heading: "Is this beta content?", + name: "edition[in_beta]", + inline: true, + heading_size: "m", + items: [ + { + value: 1, + text: "Yes", + checked: @resource.in_beta, + }, + { + value: 0, + text: "No", + checked: !@resource.in_beta, + }, + ], + } %> + + <%= render "govuk_publishing_components/components/textarea", { + label: { + heading_size: "m", + text: "Body", + }, + name: "edition[body]", + value: @resource.body, + hint: ("Refer to #{link_to("Refer to the Govspeak guidance (opens in new tab)", "https://govspeak-preview.publishing.service.gov.uk/guide", target: "_blank", rel: "noopener", class: "govuk-link")}").html_safe, + } %> + + <% if @resource.published_edition %> + <%= render "govuk_publishing_components/components/details", { + title: "Add a public change note", + } do %> + <%= render "govuk_publishing_components/components/radio", { + heading: "Add a public change note", + heading_level: 3, + heading_size: "m", + name: "edition[major_change]", + hint: "Telling users when published information has changed is important for transparency.", + items: [ + { + hint_text: "A change note will be published on the page and emailed to users subscribed to email alerts. The ‘last updated’ date will change.", + text: "Yes - information has been added, updated or removed.", + checked: @resource.major_change, + bold: true, + value: true, + conditional: render("govuk_publishing_components/components/textarea", { + label: { + text: "Describe the change for users", + bold: true, + }, + name: "edition[change_note]", + value: @resource.change_note, + hint: ("

Tell users what has changed, where and why. Write in full sentences, leading with the most important words. For example, \"College A has been removed from the registered sponsors list because its licence has been suspended.\"

Guidance on change notes (opens in a new tab)").html_safe, + }), + }, + { + value: false, + text: "No", + bold: true, + checked: !@resource.major_change, + }, + ], + } %> + <% end %> + <% end %> +
+ +
+ <% if @resource.locked_for_edits? %> +

<%= @resource.error_description %> can't be changed.

+ <% else %> + + <% end %> +
+
+<% end %> diff --git a/app/views/homepage/popular_links/show.html.erb b/app/views/homepage/popular_links/show.html.erb index 2cd780fcf..ebed702d3 100644 --- a/app/views/homepage/popular_links/show.html.erb +++ b/app/views/homepage/popular_links/show.html.erb @@ -15,7 +15,7 @@ - diff --git a/app/views/legacy_editions/show.html.erb b/app/views/legacy_editions/show.html.erb index b9dae652e..0aa1d214b 100644 --- a/app/views/legacy_editions/show.html.erb +++ b/app/views/legacy_editions/show.html.erb @@ -44,7 +44,7 @@ <% end %> - <%= resource_form(@resource) do |f| %> + <%= legacy_resource_form(@resource) do |f| %>
<%= render resource_fields(@resource), f: f %>
diff --git a/test/functional/editions_controller_test.rb b/test/functional/editions_controller_test.rb index c608c8d69..c2a8f6234 100644 --- a/test/functional/editions_controller_test.rb +++ b/test/functional/editions_controller_test.rb @@ -278,4 +278,55 @@ class EditionsControllerTest < ActionController::TestCase end end end + + context "#update" do + should "show update and success message and render show template when saved" do + post :update, params: { + id: @edition.id, + edition: { + title: "The changed title", + }, + } + + assert_template "show" + assert_equal "Edition updated successfully.", flash[:success] + assert_equal "The changed title", AnswerEdition.last.title + end + + should "show error message and render show template when title field is blank" do + post :update, params: { + id: @edition.id, + edition: { + title: "", + }, + } + + assert_template "show" + assert_select ".gem-c-error-summary__list-item", "Enter a title" + end + + should "show error message and render show template when the edition could not be updated" do + Edition.any_instance.stubs(:save).raises(StandardError) + post :update, params: { + id: @edition.id, + edition: { + title: "A title", + }, + } + + assert_template "show" + assert_select ".gem-c-error-summary__list-item", "Due to a service problem, the edition couldn't be updated" + end + + should "call update worker with edition id when saved" do + UpdateWorker.expects(:perform_async).with(@edition.id.to_s) + + post :update, params: { + id: @edition.id, + edition: { + title: "The changed title", + }, + } + end + end end diff --git a/test/integration/edition_edit_test.rb b/test/integration/edition_edit_test.rb index d415c32df..beb169331 100644 --- a/test/integration/edition_edit_test.rb +++ b/test/integration/edition_edit_test.rb @@ -225,10 +225,90 @@ class EditionEditTest < IntegrationTest end end + context "edit tab" do + context "draft edition of a new publication" do + setup do + visit_edition_in_draft + end + + should "show 'Metadata' header and an update button" do + within :css, "h2.gem-c-heading" do + assert page.has_text?("Edit") + end + assert page.has_button?("Save") + end + + should "show Title input box prefilled" do + assert page.has_text?("Title") + assert page.has_field?("edition[title]", with: "Edit page title") + end + + should "show Meta tag input box prefilled" do + assert page.has_text?("Meta tag description") + assert page.has_text?("Some search engines will display this if they cannot find what they need in the main text") + assert page.has_field?("edition[overview]", with: "metatags") + end + + should "show Beta content radios prechecked" do + assert page.has_text?("Is this beta content?") + assert find(".gem-c-radio input[value='0']") + assert find(".gem-c-radio input[value='1']").checked? + end + + should "show Body text field prefilled" do + assert page.has_text?("Body") + assert page.has_text?("Refer to the Govspeak guidance (opens in new tab)") + assert page.has_field?("edition[body]", with: "The body") + end + + should "not show Change Note field for an unpublished document" do + assert page.has_no_text?("Add a public change note") + assert page.has_no_text?("Telling users when published information has changed is important for transparency.") + assert page.has_no_field?("edition[change_note]") + end + + should "update and show success message" do + fill_in "edition[title]", with: "Changed Title" + fill_in "edition[overview]", with: "Changed Meta tag description" + choose("Yes") + fill_in "edition[body]", with: "Changed body" + click_button("Save") + + assert page.has_field?("edition[title]", with: "Changed Title") + assert page.has_field?("edition[overview]", with: "Changed Meta tag description") + assert find(".gem-c-radio input[value='1']").checked? + assert page.has_field?("edition[body]", with: "Changed body") + assert page.has_text?("Edition updated successfully.") + end + end + + context "draft edition of a previously published publication" do + setup do + visit_new_edition_of_published_edition + end + + should "show Change Note field for a new edition of a published document" do + find("details").click + find("input[name='edition[major_change]'][value='true']").choose + + assert page.has_text?("Add a public change note") + assert page.has_text?("Telling users when published information has changed is important for transparency.") + assert page.has_field?("edition[change_note]") + end + end + end + private def visit_edition_in_draft - @draft_edition = FactoryBot.create(:edition, title: "Edit page title", state: "draft") + @draft_edition = FactoryBot.create( + :answer_edition, + title: "Edit page title", + state: "draft", + overview: "metatags", + in_beta: 1, + body: "The body", + ) visit edition_path(@draft_edition) end @@ -250,4 +330,24 @@ def visit_edition_in_fact_check @fact_check_edition = FactoryBot.create(:edition, title: "Edit page title", state: "fact_check") visit edition_path(@fact_check_edition) end + + def visit_new_edition_of_published_edition + published_edition = FactoryBot.create( + :answer_edition, + panopticon_id: FactoryBot.create( + :artefact, + slug: "can-i-get-a-driving-licence", + ).id, + state: "published", + slug: "can-i-get-a-driving-licence", + ) + new_edition = FactoryBot.create( + :answer_edition, + panopticon_id: published_edition.artefact.id, + state: "draft", + version_number: 2, + change_note: "The change note", + ) + visit edition_path(new_edition) + end end