Skip to content

First published at validation 3.0 [WHIT-3076]#11473

Open
davidsauntson wants to merge 8 commits into
mainfrom
first-published-at-validation-3.0
Open

First published at validation 3.0 [WHIT-3076]#11473
davidsauntson wants to merge 8 commits into
mainfrom
first-published-at-validation-3.0

Conversation

@davidsauntson
Copy link
Copy Markdown
Contributor

@davidsauntson davidsauntson commented May 19, 2026

Users trying to edit previously published, historic documents are unable to do so if the first_published_at date was (correctly) updated in the past, since the validation prevents them from saving. This caused a minor production incident.

These changes fix that by building on #11429 and #11393 to ensure the validation is only applied when:

  • the first_published_at date has actually changed
  • the edition being edited is not political / historic

These changes are in the last two commits: b7f9001 and 0c049b3

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

This application is owned by the Whitehall Experience team. Please let us know in #govuk-whitehall-experience-tech when you raise any PRs.

Follow these steps if you are doing a Rails upgrade.

It was possible to set the `first_published_date` to any time before the present, which allowed users to backdate the `first_published_at` date so it came _after_ the first change note.  The causes some undesireable behaviour, including a confusing ordering in the list of events in the 'See all updates' section of a rendered page.

Adds validation to the `Edition` model so that the `first_published_at` date cannot be overriden to come after the date of the earliest change note (`major_change_published_at`) on associated editions.

(cherry picked from commit c1ea53c)
In some validation cases, calling `edition.other_editions` raises a `NoMethodError`  exception because there is no associated `document` object
for that `edition`.

This commit adds a guard clause to the `#other_editions` method so that it returns an empty array instead of raising an exception.

(cherry picked from commit b47e065)
…ror messages

The `first_published_preceeds_change_notes` validation caused two contradictory error messages when the `first_published_at` date was set to the future on editions with `change_notes`, ie:

- "First published date must be between 01/01/1900 and the present"
- "First published must be before the first change note (<change note date>)"

This change fixes this by only running the `first_published_at, inclusion` validation if there are no `other_editions` present for the draft edition in question.

The guard clause was added to `#other_editions` that returns an empty array instead of `nil`, so that `.empty?` can be used in the inline attribute validation.

(cherry picked from commit 997ab2b)
The `#first_published_preceeds_change_notes` error message presents the date of the earliest change note to users.  In production, this caused a problem where a user backdated an edition into a previous government, entered history mode and then did not have permissions to edit or delete the draft edition (see https://govuk.zendesk.com/agent/tickets/6565061).

This change introduces validation that ensures the `first_published_at` date is after the start of the current government.  This is only applied to non-political and non-historic editions so that users with the correct privileges can still edit those editions as required.

(cherry picked from commit 54ed806)
Since we can no longer create editions with a `first_published_at` before the start of the current government, some tests that use FactoryBot's `create` method fail validation.

This change makes these tests use `#build` instead of `#create`, since this does not run validation.  The tests don't need the validation or persistence of the `#create` brings.

(cherry picked from commit 69d3043)
Since we can no longer create editions with `first_published_at` dates before the start of the current government, and the behavioural tests create governments starting when the tests are run, some of the behavioural tests fail.

This change allows the tests to pass by changing the default `first_published_at` date to `today`.

(cherry picked from commit 78bd24b)
Users trying to edit previously published, historic documents are unable to do so if the `first_published_at` date was (correctly) updated in the past, since the validation prevents them from saving.  This caused a minor production incident.

This change partially fixes that by ensuring we  check `first_published_at` validity compared to government and change note dates when the attribute has been changed.
…or political editions

Users trying to edit previously published, historic documents are unable to do so if the `first_published_at` date was (correctly) updated in the past, since the validation prevents them from saving.  This caused a minor production incident.

This commit (along with the previous one) fixes this by ensuring the `first_published_preceeds_change_notes` validation doesn't run on historic or political editions.
@davidsauntson davidsauntson force-pushed the first-published-at-validation-3.0 branch from 0c049b3 to 7499523 Compare May 19, 2026 15:58
@davidsauntson davidsauntson changed the title First published at validation 3.0 First published at validation 3.0 [WHIT-3076] May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants