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

Update design of first published at fields on the new and edit edition pages #8338

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<div class="govuk-!-margin-bottom-4">
<% if document_has_never_been_published? %>
<%= hidden_field_tag "edition[previously_published]", false %>

<%= render "govuk_publishing_components/components/checkboxes", {
name: "edition[previously_published]",
id: "edition_previously_published",
heading: "First published date",
heading_size: "l",
hint_text: "If this document has been published on another webpage, you must provide the date it was first published online." +
" This only applies to online content. Do not use this for file attachments.",
items: [
{
label: "This document has previously been published on another website",
value: true,
checked: previously_published,
data_attributes: {
track_category: "checkboxClicked",
track_label: "This document has previously been published on another website",
track_action: "first_published_at",
},
conditional: capture do
first_published_at_fields
end,
},
],
} %>
<% else %>
<%= render "govuk_publishing_components/components/fieldset", {
legend_text: "First published",
heading_size: "l",
hint: "For example, 31 3 2000",
id: "edition_first_published_at",
error_message: errors_for_input(edition.errors, :first_published_at),
} do %>
<% capture do %>
<%= first_published_at_fields %>
<% end %>
<% end %>
<% end %>
</div>
58 changes: 58 additions & 0 deletions app/components/admin/editions/first_published_at_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

class Admin::Editions::FirstPublishedAtComponent < ViewComponent::Base
include ErrorsHelper

def initialize(edition:, previously_published:, day: nil, month: nil, year: nil)
@edition = edition
@previously_published = previously_published
@day = day
@month = month
@year = year
end

def render?
!edition.is_a?(Consultation)
end

private

attr_reader :edition, :previously_published, :day, :month, :year

def document_has_never_been_published?
edition.published_major_version.nil?
end

def first_published_at_fields
render("components/datetime_fields", {
field_name: "first_published_at",
prefix: "edition",
date_heading: "Date (required)",
date_only: true,
date_hint: "For example, 01 08 2022",
error_items: errors_for(edition.errors, :first_published_at),
id: "edition_first_published_at",
year: {
id: "edition_first_published_at_1i",
value: year,
name: "edition[first_published_at(1i)]",
label: "Year",
width: 4,
},
month: {
id: "edition_first_published_at_2i",
value: month,
name: "edition[first_published_at(2i)]",
label: "Month",
width: 2,
},
day: {
id: "edition_first_published_at_3i",
value: day,
name: "edition[first_published_at(3i)]",
label: "Day",
width: 2,
},
})
end
end
2 changes: 0 additions & 2 deletions app/controllers/admin/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,6 @@ def clean_edition_parameters
edition_params["first_published_at(1i)"] = ""
edition_params["first_published_at(2i)"] = ""
edition_params["first_published_at(3i)"] = ""
edition_params["first_published_at(4i)"] = ""
edition_params["first_published_at(5i)"] = ""
end

if params[:review_reminder].blank? && edition_params.dig("document_attributes", "review_reminder_attributes").present?
Expand Down
78 changes: 0 additions & 78 deletions app/views/admin/editions/_first_published_at.html.erb

This file was deleted.

10 changes: 7 additions & 3 deletions app/views/admin/editions/_standard_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
<% end %>
</div>

<% unless edition.is_a?(Consultation) %>
<%= render "first_published_at", form: form, edition: edition %>
<% end %>
<%= render Admin::Editions::FirstPublishedAtComponent.new(
edition:,
previously_published: params.dig("edition", "previously_published") == "true" || edition.previously_published == true,
year: @edition_params.try(:dig, "first_published_at(1i)") || edition.first_published_at&.year,
month: @edition_params.try(:dig, "first_published_at(2i)") || edition.first_published_at&.month,
day: @edition_params.try(:dig, "first_published_at(3i)") || edition.first_published_at&.day,
) %>
2 changes: 1 addition & 1 deletion features/previously_published.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Feature: Previously published options
Given I am a writer in the organisation "Department of Examples"
When I start a new case study
And I click save
Then I see a validation error for the 'previously published' option
Then I do not see a validation error for the 'previously published' option

@javascript
Scenario: Creating a new case study and selecting a previously published date in the future
Expand Down
10 changes: 5 additions & 5 deletions features/step_definitions/previously_published_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
end

And(/^I select a previously published date in the future$/) do
choose "has previously been published on another website."
check "This document has previously been published on another website"
within "#edition_previously_published" do
fill_in_date_fields("1 July 2018")
end
end

And(/^I select that this document has been previously published$/) do
choose "has previously been published on another website."
check "This document has previously been published on another website"
end

And(/^I select a previously published date in the past$/) do
choose "has previously been published on another website."
check "This document has previously been published on another website"
within "#edition_previously_published" do
fill_in_date_fields("1 February 2017")
end
end

Then(/^I see a validation error for the 'previously published' option$/) do
expect(page).to have_content("You must specify whether the document has been published before")
Then(/^I do not see a validation error for the 'previously published' option$/) do
expect(page).not_to have_content("You must specify whether the document has been published before")
end

Then(/^I see a validation error for the future date$/) do
Expand Down
16 changes: 8 additions & 8 deletions features/support/document_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def begin_drafting_document(options)

case options[:previously_published]
when false
choose "has never been published before."
uncheck "This document has previously been published on another website"
when true
choose "has previously been published on another website."
check "This document has previously been published on another website"
end

if options[:all_nation_applicability]
Expand Down Expand Up @@ -110,17 +110,17 @@ def pdf_attachment

def fill_in_news_article_fields(first_published: "2010-01-01", announcement_type: "News story")
select announcement_type, from: "News article type"
radio_label = "This document has previously been published on another website."
choose radio_label
within_conditional_reveal radio_label do
checkbox_label = "This document has previously been published on another website"
check checkbox_label
within_conditional_reveal checkbox_label do
fill_in_date_fields(first_published)
end
end

def fill_in_publication_fields(first_published: "2010-01-01", publication_type: "Research and analysis")
radio_label = "This document has previously been published on another website."
choose radio_label
within_conditional_reveal radio_label do
checkbox_label = "This document has previously been published on another website"
check checkbox_label
within_conditional_reveal checkbox_label do
fill_in_date_fields(first_published)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require "test_helper"

class Admin::Editions::FirstPublishedAtComponentTest < ViewComponent::TestCase
test "doesn't render when the edition is a consultation" do
edition = build(:consultation)

render_inline(Admin::Editions::FirstPublishedAtComponent.new(edition:, previously_published: true))

assert page.text.blank?
end

test "when the document has never been published it renders with the correct fields" do
edition = build(:publication)

render_inline(Admin::Editions::FirstPublishedAtComponent.new(edition:, previously_published: false))

assert_selector "input[type='hidden'][name='edition[previously_published]'][value='false']", visible: :hidden
assert_selector ".govuk-checkboxes" do
assert_selector "input[type='checkbox'][name='edition[previously_published]'][value='true']"
assert_selector "input[type='checkbox'][name='edition[previously_published]'][value='true'][checked]", count: 0
assert_selector "input[type='text'][name='edition[first_published_at(3i)]']"
assert_selector "label[for=edition_first_published_at_3i]", text: "Day"
assert_selector "input[type='text'][name='edition[first_published_at(2i)]']"
assert_selector "label[for=edition_first_published_at_2i]", text: "Month"
assert_selector "input[type='text'][name='edition[first_published_at(1i)]']"
assert_selector "label[for=edition_first_published_at_1i]", text: "Year"
end
end

test "when the document has not been published on GOV.UK but has been published on another website it checks the checkbox" do
edition = build(:publication)

render_inline(Admin::Editions::FirstPublishedAtComponent.new(edition:, previously_published: true))

assert_selector "input[type='hidden'][name='edition[previously_published]'][value='false']", visible: :hidden
assert_selector ".govuk-checkboxes" do
assert_selector "input[type='checkbox'][name='edition[previously_published]'][value='true'][checked]"
assert_selector "input[type='text'][name='edition[first_published_at(3i)]']"
assert_selector "input[type='text'][name='edition[first_published_at(2i)]']"
assert_selector "input[type='text'][name='edition[first_published_at(1i)]']"
end
end

test "when a first published at date is passed into the component it assigns the correct values to the date inputs" do
edition = build(:publication)

render_inline(Admin::Editions::FirstPublishedAtComponent.new(
edition:,
previously_published: true,
year: 2022,
month: 10,
day: 1,
))

assert_selector "input[type='hidden'][name='edition[previously_published]'][value='false']", visible: :hidden
assert_selector ".govuk-checkboxes" do
assert_selector "input[type='checkbox'][name='edition[previously_published]'][value='true'][checked]"
assert_selector "input[type='text'][name='edition[first_published_at(3i)]'][value='1']"
assert_selector "input[type='text'][name='edition[first_published_at(2i)]'][value='10']"
assert_selector "input[type='text'][name='edition[first_published_at(1i)]'][value='2022']"
end
end

test "when a document has been published on GOV.UK it doesn't render a checkbox and assigns the correct values to the date inputs" do
edition = build(:published_publication)

render_inline(Admin::Editions::FirstPublishedAtComponent.new(
edition:,
previously_published: true,
year: 2022,
month: 10,
day: 1,
))

assert_selector "input[type='hidden'][name='edition[previously_published]'][value='false']", visible: :hidden, count: 0
assert_selector ".govuk-fieldset", text: "First published" do
assert_selector "input[type='text'][name='edition[first_published_at(3i)]'][value='1']"
assert_selector "label[for=edition_first_published_at_3i]", text: "Day"
assert_selector "input[type='text'][name='edition[first_published_at(2i)]'][value='10']"
assert_selector "label[for=edition_first_published_at_2i]", text: "Month"
assert_selector "input[type='text'][name='edition[first_published_at(1i)]'][value='2022']"
assert_selector "label[for=edition_first_published_at_1i]", text: "Year"
end
end
end
3 changes: 1 addition & 2 deletions test/functional/admin/publications_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class Admin::PublicationsControllerTest < ActionController::TestCase
should_allow_scheduled_publication_of :publication
should_allow_access_limiting_of :publication
should_render_govspeak_history_and_fact_checking_tabs_for :publication
should_allow_overriding_of_first_published_at_for :publication

view_test "new displays publication fields" do
get :new

assert_select "form#new_edition" do
assert_select "input[name*='edition[first_published_at']", count: 3
assert_select "select[name*='edition[first_published_at']", count: 2
assert_select "select[name='edition[publication_type_id]']"
assert_select "input[name='edition[access_limited]']"
end
Expand Down Expand Up @@ -99,7 +99,6 @@ class Admin::PublicationsControllerTest < ActionController::TestCase
assert_select "form#edit_edition" do
assert_select "select[name='edition[publication_type_id]']"
assert_select "input[name*='edition[first_published_at']", count: 3
assert_select "select[name*='edition[first_published_at']", count: 2
assert_select ".js-app-view-edition-form__subtype-format-advice", text: "Use this subformat for… A policy paper explains the government's position on something. It doesn’t include instructions on how to carry out a task, only the policy itself and how it’ll be implemented.Read the policy papers guidance in full."
end
end
Expand Down
Loading