Skip to content

Commit

Permalink
Merge pull request #9065 from alphagov/capture-content-id-when-republ…
Browse files Browse the repository at this point in the history
…ishing

Capture `content_id` when republishing content
  • Loading branch information
Gweaton authored May 22, 2024
2 parents 316193d + 25899db commit 00ae5b3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
14 changes: 7 additions & 7 deletions app/controllers/admin/republishing_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def republish_page

action = "The page '#{page_to_republish[:title]}' has been scheduled for republishing"

@republishing_event = build_republishing_event(action)
@republishing_event = build_republishing_event(action:, content_id: page_to_republish[:presenter].constantize.new.content_id)

if @republishing_event.save
PresentPageToPublishingApiWorker.perform_async(page_to_republish[:presenter])
Expand Down Expand Up @@ -65,7 +65,7 @@ def republish_organisation
end

action = "The organisation '#{@organisation.name}' has been republished"
@republishing_event = build_republishing_event(action)
@republishing_event = build_republishing_event(action:, content_id: @organisation.content_id)

if @republishing_event.save
@organisation.publish_to_publishing_api
Expand Down Expand Up @@ -105,7 +105,7 @@ def republish_person
end

action = "The person '#{@person.name}' has been republished"
@republishing_event = build_republishing_event(action)
@republishing_event = build_republishing_event(action:, content_id: @person.content_id)

if @republishing_event.save
@person.publish_to_publishing_api
Expand Down Expand Up @@ -145,7 +145,7 @@ def republish_role
end

action = "The role '#{@role.name}' has been republished"
@republishing_event = build_republishing_event(action)
@republishing_event = build_republishing_event(action:, content_id: @role.content_id)

if @republishing_event.save
@role.publish_to_publishing_api
Expand Down Expand Up @@ -185,7 +185,7 @@ def republish_document
end

action = "Editions for the document with slug '#{@document.slug}' have been republished"
@republishing_event = build_republishing_event(action)
@republishing_event = build_republishing_event(action:, content_id: @document.content_id)

if @republishing_event.save
PublishingApiDocumentRepublishingWorker.new.perform(@document.id)
Expand Down Expand Up @@ -223,7 +223,7 @@ def republishable_pages
end
end

def build_republishing_event(action)
RepublishingEvent.new(user: current_user, reason: params.fetch(:reason), action:)
def build_republishing_event(action:, content_id:)
RepublishingEvent.new(user: current_user, reason: params.fetch(:reason), action:, content_id:)
end
end
1 change: 1 addition & 0 deletions app/models/republishing_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class RepublishingEvent < ApplicationRecord

validates :action, presence: true
validates :reason, presence: true
validates :content_id, presence: true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddContentIdToRepublishingEvents < ActiveRecord::Migration[7.1]
def change
add_column :republishing_events, :content_id, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_05_20_155856) do
ActiveRecord::Schema[7.1].define(version: 2024_05_21_084537) do
create_table "assets", charset: "utf8mb3", force: :cascade do |t|
t.string "asset_manager_id", null: false
t.string "variant", null: false
Expand Down Expand Up @@ -864,6 +864,7 @@
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "content_id"
t.index ["user_id"], name: "index_republishing_events_on_user_id"
end

Expand Down
16 changes: 12 additions & 4 deletions test/functional/admin/republishing_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
assert_equal newly_created_event.user, current_user
assert_equal newly_created_event.reason, "this needs republishing"
assert_equal newly_created_event.action, "The page 'Past Prime Ministers' has been scheduled for republishing"
assert_equal newly_created_event.content_id, PublishingApi::HistoricalAccountsIndexPresenter.new.content_id

assert_redirected_to admin_republishing_index_path
assert_equal "The page 'Past Prime Ministers' has been scheduled for republishing", flash[:notice]
Expand Down Expand Up @@ -144,7 +145,7 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
end

test "GDS Admin users should be able to POST :republish_organisation with an existing organisation slug, creating a RepublishingEvent for the current user" do
create(:organisation, slug: "an-existing-organisation", name: "An Existing Organisation")
create(:organisation, slug: "an-existing-organisation", name: "An Existing Organisation", content_id: "6de2fd22-4a87-49b7-be49-915f12dfe6fe")

Organisation.any_instance.expects(:publish_to_publishing_api).once

Expand All @@ -154,6 +155,8 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
assert_equal newly_created_event.user, current_user
assert_equal newly_created_event.reason, "this needs republishing"
assert_equal newly_created_event.action, "The organisation 'An Existing Organisation' has been republished"
assert_equal newly_created_event.content_id, "6de2fd22-4a87-49b7-be49-915f12dfe6fe"

assert_redirected_to admin_republishing_index_path
assert_equal "The organisation 'An Existing Organisation' has been republished", flash[:notice]
end
Expand Down Expand Up @@ -246,7 +249,7 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
end

test "GDS Admin users should be able to POST :republish_person with an existing person slug, creating a RepublishingEvent for the current user" do
create(:person, slug: "existing-person", forename: "Existing", surname: "Person")
create(:person, slug: "existing-person", forename: "Existing", surname: "Person", content_id: "6de2fd22-4a87-49b7-be49-915f12dfe6fe")

Person.any_instance.expects(:publish_to_publishing_api).once

Expand All @@ -256,6 +259,8 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
assert_equal newly_created_event.user, current_user
assert_equal newly_created_event.reason, "this needs republishing"
assert_equal newly_created_event.action, "The person 'Existing Person' has been republished"
assert_equal newly_created_event.content_id, "6de2fd22-4a87-49b7-be49-915f12dfe6fe"

assert_redirected_to admin_republishing_index_path
assert_equal "The person 'Existing Person' has been republished", flash[:notice]
end
Expand Down Expand Up @@ -348,7 +353,7 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
end

test "GDS Admin users should be able to POST :republish_role with an existing role slug, creating a RepublishingEvent for the current user" do
create(:role, slug: "an-existing-role", name: "An Existing Role")
create(:role, slug: "an-existing-role", name: "An Existing Role", content_id: "6de2fd22-4a87-49b7-be49-915f12dfe6fe")

Role.any_instance.expects(:publish_to_publishing_api).once

Expand All @@ -358,6 +363,8 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
assert_equal newly_created_event.user, current_user
assert_equal newly_created_event.reason, "this needs republishing"
assert_equal newly_created_event.action, "The role 'An Existing Role' has been republished"
assert_equal newly_created_event.content_id, "6de2fd22-4a87-49b7-be49-915f12dfe6fe"

assert_redirected_to admin_republishing_index_path
assert_equal "The role 'An Existing Role' has been republished", flash[:notice]
end
Expand Down Expand Up @@ -450,7 +457,7 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
end

test "GDS Admin users should be able to POST :republish_document with an existing document slug, creating a RepublishingEvent for the current user" do
document = create(:document, slug: "an-existing-document")
document = create(:document, slug: "an-existing-document", content_id: "6de2fd22-4a87-49b7-be49-915f12dfe6fe")

PublishingApiDocumentRepublishingWorker.any_instance.expects(:perform).with(document.id).once

Expand All @@ -460,6 +467,7 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
assert_equal newly_created_event.user, current_user
assert_equal newly_created_event.reason, "this needs republishing"
assert_equal newly_created_event.action, "Editions for the document with slug 'an-existing-document' have been republished"
assert_equal newly_created_event.content_id, "6de2fd22-4a87-49b7-be49-915f12dfe6fe"

assert_redirected_to admin_republishing_index_path
assert_equal "Editions for the document with slug 'an-existing-document' have been republished", flash[:notice]
Expand Down

0 comments on commit 00ae5b3

Please sign in to comment.