Skip to content

Commit

Permalink
Merge pull request #8904 from alphagov/change-ww-office-publishing
Browse files Browse the repository at this point in the history
Change publishing of offices associated with editionable worldwide organisations
  • Loading branch information
brucebolt authored Mar 28, 2024
2 parents 5438a2c + a585bc6 commit eef82b8
Show file tree
Hide file tree
Showing 22 changed files with 565 additions and 64 deletions.
12 changes: 12 additions & 0 deletions app/controllers/admin/worldwide_offices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def update
worldwide_office_params[:service_ids] ||= []
if @worldwide_office.update(worldwide_office_params)
handle_show_on_home_page_param
republish_draft_worldwide_organisation
redirect_to admin_worldwide_organisation_worldwide_offices_path(@worldwide_organisation), notice: "#{@worldwide_office.title} has been edited"
else
@worldwide_office.contact.contact_numbers.build if @worldwide_office.contact.contact_numbers.blank?
Expand All @@ -31,6 +32,7 @@ def create
@worldwide_office = @worldwide_organisation.offices.build(worldwide_office_params)
if @worldwide_office.save
handle_show_on_home_page_param
republish_draft_worldwide_organisation
redirect_to admin_worldwide_organisation_worldwide_offices_path(@worldwide_organisation), notice: "#{@worldwide_office.title} has been added"
else
@worldwide_office.contact.contact_numbers.build if @worldwide_office.contact.contact_numbers.blank?
Expand All @@ -48,6 +50,12 @@ def destroy
title = @worldwide_office.title

if @worldwide_office.destroy
if @worldwide_office.edition
PublishingApiDiscardDraftWorker.perform_async(@worldwide_office.content_id, I18n.default_locale.to_s)
PublishingApiDiscardDraftWorker.perform_async(@worldwide_office.contact.content_id, I18n.default_locale.to_s)
end

republish_draft_worldwide_organisation
redirect_to admin_worldwide_organisation_worldwide_offices_path(@worldwide_organisation), notice: "#{title} has been deleted"
else
render :edit
Expand Down Expand Up @@ -126,4 +134,8 @@ def worldwide_office_params
{ contact_numbers_attributes: %i[id label number _destroy] },
])
end

def republish_draft_worldwide_organisation
Whitehall.edition_services.draft_updater(@worldwide_office.edition).perform! if @worldwide_office.edition
end
end
8 changes: 8 additions & 0 deletions app/models/call_for_evidence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def allows_html_attachments?
true
end

def associated_documents
attachables.flat_map(&:html_attachments)
end

def deleted_associated_documents
attachables.flat_map(&:deleted_html_attachments)
end

def previously_published
false
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/call_for_evidence_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def allows_html_attachments?
true
end

def associated_documents
attachables.flat_map(&:html_attachments)
end

def deleted_associated_documents
attachables.flat_map(&:deleted_html_attachments)
end

def path_name
to_model.class.name.underscore
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/consultation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ def allows_html_attachments?
true
end

def associated_documents
attachables.flat_map(&:html_attachments)
end

def deleted_associated_documents
attachables.flat_map(&:deleted_html_attachments)
end

def previously_published
false
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/consultation_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def allows_html_attachments?
true
end

def associated_documents
attachables.flat_map(&:html_attachments)
end

def deleted_associated_documents
attachables.flat_map(&:deleted_html_attachments)
end

def path_name
to_model.class.name.underscore
end
Expand Down
14 changes: 13 additions & 1 deletion app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ class Contact < ApplicationRecord
extend HomePageList::ContentItem
is_stored_on_home_page_lists

def can_publish_to_publishing_api?
return false if contactable.is_a?(WorldwideOffice) && contactable.edition

super
end

def can_publish_gone_to_publishing_api?
return false if contactable.is_a?(WorldwideOffice) && contactable.edition

super
end

def republish_organisation_to_publishing_api
Whitehall::PublishingApi.republish_async(contactable) if contactable.is_a?(Organisation)
end

def republish_worldwide_office_to_publishing_api
Whitehall::PublishingApi.republish_async(contactable) if contactable.is_a?(WorldwideOffice)
Whitehall::PublishingApi.republish_async(contactable) if contactable.is_a?(WorldwideOffice) && !contactable.edition
end

def contactable_name
Expand Down
8 changes: 8 additions & 0 deletions app/models/edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,14 @@ def images_have_unique_filenames?
names.uniq.length == names.length
end

def associated_documents
[]
end

def deleted_associated_documents
[]
end

private

def date_for_government
Expand Down
4 changes: 4 additions & 0 deletions app/models/editionable_worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,8 @@ def republish_dependent_documents

documents.each { |d| Whitehall::PublishingApi.republish_document_async(d) }
end

def associated_documents
(offices + offices.map(&:contact)).compact.flatten
end
end
8 changes: 8 additions & 0 deletions app/models/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def allows_html_attachments?
true
end

def associated_documents
attachables.flat_map(&:html_attachments)
end

def deleted_associated_documents
attachables.flat_map(&:deleted_html_attachments)
end

def assign_statistics_announcement
if statistics_announcement_id.present?
self.statistics_announcement = StatisticsAnnouncement.find(statistics_announcement_id)
Expand Down
12 changes: 12 additions & 0 deletions app/models/worldwide_office.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class WorldwideOffice < ApplicationRecord
delegate(:non_english_translated_locales, to: :worldwide_organisation)
delegate(:embassy_office?, to: :worldwide_office_type)

def can_publish_to_publishing_api?
return false if edition

super
end

def can_publish_gone_to_publishing_api?
return false if edition

super
end

def worldwide_organisation
super || edition
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ServiceListeners
class PublishingApiHtmlAttachments
class PublishingApiAssociatedDocuments
attr_reader :edition

def self.process(edition, event)
Expand All @@ -22,27 +22,27 @@ def republish
update_draft(update_type: "republish")
elsif edition.unpublishing && edition.withdrawn?
do_publish("republish")
discard_drafts(deleted_html_attachments)
discard_drafts(deleted_associated_documents)
withdraw
elsif edition.unpublishing
update_draft(update_type: "republish")
patch_links
unpublish(allow_draft: true)
else
do_publish("republish")
discard_drafts(deleted_html_attachments)
discard_drafts(deleted_associated_documents)
end
end

def update_draft(update_type: nil)
current_html_attachments.each do |html_attachment|
current_associated_documents.each do |associated_document|
Whitehall::PublishingApi.save_draft_translation(
html_attachment,
html_attachment.locale || I18n.default_locale.to_s,
associated_document,
locale_for_document(associated_document),
update_type || (edition.minor_change? ? "minor" : "major"),
)
end
discard_drafts(deleted_html_attachments)
discard_drafts(deleted_associated_documents)
end
# We don't care whether this is a translation or the main
# document, we just send the correct html attachments regardless.
Expand All @@ -55,75 +55,83 @@ def unpublish(allow_draft: false)
edition.public_path
end

current_html_attachments.each do |html_attachment|
current_associated_documents.each do |associated_document|
PublishingApiRedirectWorker.new.perform(
html_attachment.content_id,
associated_document.content_id,
destination,
html_attachment.locale || I18n.default_locale.to_s,
locale_for_document(associated_document),
allow_draft,
)
end
end

def withdraw
current_html_attachments.each do |html_attachment|
current_associated_documents.each do |associated_document|
PublishingApiWithdrawalWorker.new.perform(
html_attachment.content_id,
associated_document.content_id,
edition.unpublishing.explanation,
html_attachment.locale || I18n.default_locale.to_s,
locale_for_document(associated_document),
false,
edition.unpublishing.unpublished_at.to_s,
)
end
end

def delete
discard_drafts(current_html_attachments + deleted_html_attachments)
discard_drafts(current_associated_documents + deleted_associated_documents)
end

private

def discard_drafts(html_attachments)
html_attachments.each do |html_attachment|
def locale_for_document(document)
if document.respond_to?(:locale) && document.locale
document.locale
else
I18n.default_locale.to_s
end
end

def discard_drafts(associated_documents)
associated_documents.each do |associated_document|
PublishingApiDiscardDraftWorker.perform_async(
html_attachment.content_id,
associated_document.content_id,
edition.primary_locale,
)
end
end

def patch_links
current_html_attachments.each do |html_attachment|
Whitehall::PublishingApi.patch_links(html_attachment, bulk_publishing: false)
current_associated_documents.each do |associated_document|
Whitehall::PublishingApi.patch_links(associated_document, bulk_publishing: false)
end
end

def previous_edition
@previous_edition ||= edition.previous_edition
end

def current_html_attachments
edition.attachables.flat_map(&:html_attachments)
def current_associated_documents
edition.associated_documents
end

def previous_html_attachments
def previous_associated_documents
return [] unless previous_edition

previous_edition.attachables.flat_map(&:html_attachments)
previous_edition.associated_documents
end

def content_ids_to_remove
return Set[] unless previous_edition

deleted_content_ids = deleted_html_attachments.map(&:content_id).to_set
old_content_ids = previous_html_attachments.map(&:content_id).to_set
new_content_ids = current_html_attachments.map(&:content_id).to_set
deleted_content_ids = deleted_associated_documents.map(&:content_id).to_set
old_content_ids = previous_associated_documents.map(&:content_id).to_set
new_content_ids = current_associated_documents.map(&:content_id).to_set

deleted_content_ids + old_content_ids - new_content_ids
end

def deleted_html_attachments
edition.attachables.flat_map(&:deleted_html_attachments)
def deleted_associated_documents
edition.deleted_associated_documents
end

def do_publish(update_type)
Expand All @@ -135,12 +143,12 @@ def do_publish(update_type)
)
end

current_html_attachments.each do |html_attachment|
current_associated_documents.each do |associated_document|
PublishingApiWorker.new.perform(
html_attachment.class.name,
html_attachment.id,
associated_document.class.name,
associated_document.id,
update_type,
html_attachment.locale || I18n.default_locale.to_s,
locale_for_document(associated_document),
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/services/service_listeners/publishing_api_pusher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def push(event:, options: {})
api.discard_draft_async(edition)
end

handle_html_attachments(event)
handle_associated_documents(event)
end

private

def handle_html_attachments(event)
if edition.respond_to?(:html_attachments)
PublishingApiHtmlAttachments.process(edition, event)
def handle_associated_documents(event)
if edition.respond_to?(:associated_documents) || edition.respond_to?(:deleted_associated_documents)
PublishingApiAssociatedDocuments.process(edition, event)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/workers/publishing_api_document_republishing_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def locales_for(edition)
end

def handle_attachments_for(edition)
ServiceListeners::PublishingApiHtmlAttachments.process(
ServiceListeners::PublishingApiAssociatedDocuments.process(
edition,
"republish",
)
Expand Down
6 changes: 5 additions & 1 deletion lib/publishes_to_publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ module PublishesToPublishingApi

included do
after_commit :publish_to_publishing_api, if: :can_publish_to_publishing_api?
after_commit :publish_gone_to_publishing_api, on: :destroy
after_commit :publish_gone_to_publishing_api, on: :destroy, if: :can_publish_gone_to_publishing_api?
define_callbacks :published, :published_gone
end

def can_publish_to_publishing_api?
persisted?
end

def can_publish_gone_to_publishing_api?
true
end

def republish_to_publishing_api_async
if can_publish_to_publishing_api?
Whitehall::PublishingApi.republish_async(self)
Expand Down
Loading

0 comments on commit eef82b8

Please sign in to comment.