-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8338 from alphagov/1781-update-design-of-first-pu…
…blished-section-for-editions Update design of first published at fields on the new and edit edition pages
- Loading branch information
Showing
11 changed files
with
208 additions
and
151 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
app/components/admin/editions/first_published_at_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
app/components/admin/editions/first_published_at_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
test/components/admin/editions/first_published_at_component_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.