Skip to content

Commit

Permalink
Add route for submitting/publishing the retagging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBAshton committed Jan 23, 2025
1 parent 7f41796 commit 1253635
Showing 5 changed files with 71 additions and 5 deletions.
21 changes: 19 additions & 2 deletions app/controllers/admin/retagging_controller.rb
Original file line number Diff line number Diff line change
@@ -11,17 +11,34 @@ def preview
updater.validate

if updater.errors.any?
sanitized_errors = updater.errors.map { |err| sanitize(err) }
flash[:alert] = "Errors with CSV input: <br>#{sanitized_errors.join('<br>')}".html_safe
set_flash_alert(updater.errors)
render :index
else
@docs_to_update = updater.summarise_changes
end
end

def publish
updater = DataHygiene::BulkOrganisationUpdater.new(params[:csv_input])
updater.validate

if updater.errors.any?
set_flash_alert(updater.errors)
else
updater.call
flash[:notice] = "Retagging in progress."
end
redirect_to(admin_retagging_index_path)
end

private

def enforce_permissions!
enforce_permission!(:administer, :retag_content)
end

def set_flash_alert(errors)
sanitized_errors = errors.map { |err| sanitize(err) }
flash[:alert] = "Errors with CSV input: <br>#{sanitized_errors.join('<br>')}".html_safe
end
end
2 changes: 1 addition & 1 deletion app/views/admin/retagging/preview.html.erb
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
end,
} %>

<%= form_with(url: "#", method: :post) do %>
<%= form_with(url: admin_retagging_publish_path, method: :post) do %>
<input type="hidden" name="csv_input" value="<%= params[:csv_input] %>">
<p class="govuk-body">
<%= render "govuk_publishing_components/components/button", {
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })

get "/retagging" => "retagging#index", as: :retagging_index
post "/retagging" => "retagging#preview", as: :retagging_preview
post "/retagging/publish" => "retagging#publish", as: :retagging_publish

resources :documents, only: [] do
resources :review_reminders, only: %i[new create edit update destroy] do
11 changes: 11 additions & 0 deletions features/retagging-content.feature
Original file line number Diff line number Diff line change
@@ -19,3 +19,14 @@ Feature: Retagging documents to different organisations
And I submit my CSV of documents to be retagged
Then I can see a summary of the proposed changes
And my CSV input should be in a hidden field ready to confirm retagging

Scenario: Full run of the retagging
Given the documents and organisations I am retagging exist
When I visit the retagging page
And I submit my CSV of documents to be retagged
Then I can see a summary of the proposed changes
And my CSV input should be in a hidden field ready to confirm retagging
And when I click "Publish changes" on this retagging screen
Then I am redirected to the retagging index page
And I see a confirmation message that my documents are being retagged
And the changes should have been actioned
41 changes: 39 additions & 2 deletions features/step_definitions/retagging_content_steps.rb
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
Given("the documents and organisations I am retagging exist") do
$csv_to_submit = <<~CSV
Slug,New lead organisations,New supporting organisations,Document type
/government/publications/linked-identifier-schemes-best-practice-guide,government-digital-service,geospatial-commission,Publication
/government/publications/linked-identifier-schemes-best-practice-guide,"cabinet-office,government-digital-service",geospatial-commission,Publication
/government/publications/search-engine-optimisation-for-publishers-best-practice-guide,government-digital-service,"cabinet-office, geospatial-commission",Publication
CSV

@@ -61,7 +61,7 @@
expect(page).to have_content("Retag content - preview changes")
table_row = find(".govuk-table__body .govuk-table__row:first")
expect(table_row).to have_selector(".govuk-table__cell", text: "linked-identifier-schemes-best-practice-guide")
expect(table_row).to have_selector(".govuk-table__cell", text: "Added government-digital-service, Removed some-other-org. Result: government-digital-service")
expect(table_row).to have_selector(".govuk-table__cell", text: "Added cabinet-office, government-digital-service, Removed some-other-org. Result: cabinet-office, government-digital-service")
expect(table_row).to have_selector(".govuk-table__cell", text: "Added geospatial-commission. Result: geospatial-commission")
table_row = find(".govuk-table__body .govuk-table__row:last")
expect(table_row).to have_selector(".govuk-table__cell", text: "search-engine-optimisation-for-publishers-best-practice-guide")
@@ -73,3 +73,40 @@
expect(find("[name=csv_input]", visible: false).value.gsub("\r\n", "\n")).to eq($csv_to_submit)
end
# rubocop:enable Style/GlobalVars

Then(/when I click "(.+)" on this retagging screen/) do |button_text|
click_button button_text
end

Then("I am redirected to the retagging index page") do
expect(current_url).to eq(admin_retagging_index_url)
end

Then("I see a confirmation message that my documents are being retagged") do
expect(page.find(".govuk-notification-banner--success")).to have_text("Retagging in progress.")
end

And("the changes should have been actioned") do
doc1 = Document.find_by(slug: "linked-identifier-schemes-best-practice-guide")
doc2 = Document.find_by(slug: "search-engine-optimisation-for-publishers-best-practice-guide")

expect(doc1.latest_edition.lead_organisations.map(&:slug)).to eq(%w[cabinet-office government-digital-service])
expect(doc1.latest_edition.supporting_organisations.map(&:slug)).to eq(%w[geospatial-commission])
expect(doc2.latest_edition.lead_organisations.map(&:slug)).to eq(%w[government-digital-service])
# The `.sort` below is needed as, without it, the response is
# `geospatial-commission, cabinet-office` despite the order specified in the CSV.
#
# This may not be desired behaviour but it has been the behaviour to date (in the rake task,
# which our new form replaces) so as far as we know, has not been an issue in the past.
# Just noting here for reference.
#
# NB, the `new_supporting_organisations` parameter in `BulkOrganisationUpdater`'s `update_edition`
# method DOES have the organisations coming through in the correct order, so it only seems to
# go out of order in the `edition.update(supporting_organisations: new_supporting_organisations)`
# call.
#
# NB, the same issue does not affect `lead_organisations`, presumably because the
# "edition_organisations" table has a `lead_ordering` integer to track the order. There is no
# equivalent ordering tracking for supporting organisations.
expect(doc2.latest_edition.supporting_organisations.map(&:slug).sort).to eq(%w[cabinet-office geospatial-commission])
end

0 comments on commit 1253635

Please sign in to comment.