Skip to content

Commit

Permalink
Merge pull request #2435 from alphagov/manuals-publisher-refactoring
Browse files Browse the repository at this point in the history
Manuals publisher refactoring
  • Loading branch information
minhngocd committed Jul 22, 2024
2 parents 8625183 + b01f4ff commit 7c5dd48
Show file tree
Hide file tree
Showing 35 changed files with 379 additions and 440 deletions.
7 changes: 2 additions & 5 deletions app/adapters/organisations_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
class OrganisationsAdapter
def initialize
@cache = {}
end

def find(slug)
def self.find(slug)
@cache ||= {}
@cache.fetch(slug) do
response = Services.organisations.organisation(slug)
organisation = Organisation.new(
Expand Down
50 changes: 22 additions & 28 deletions app/adapters/publishing_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "securerandom"

class PublishingAdapter
def save_draft(manual, republish: false, include_sections: true, include_links: true)
def self.save_draft(manual, republish: false, include_sections: true, include_links: true)
save_manual(manual, republish:, include_links:)

if include_sections
Expand All @@ -11,7 +11,7 @@ def save_draft(manual, republish: false, include_sections: true, include_links:
end
end

def unpublish_and_redirect_manual_and_sections(manual, redirect:, discard_drafts:)
def self.unpublish_and_redirect_manual_and_sections(manual, redirect:, discard_drafts:)
Services.publishing_api.unpublish(
manual.id,
type: "redirect",
Expand All @@ -27,7 +27,7 @@ def unpublish_and_redirect_manual_and_sections(manual, redirect:, discard_drafts
end
end

def unpublish_section(section, redirect:, republish: false, discard_drafts: true)
def self.unpublish_section(section, redirect:, republish: false, discard_drafts: true)
if !section.withdrawn? || republish
begin
Services.publishing_api.unpublish(
Expand All @@ -40,15 +40,15 @@ def unpublish_section(section, redirect:, republish: false, discard_drafts: true
end
end

def unpublish(manual)
def self.unpublish(manual)
Services.publishing_api.unpublish(manual.id, type: "gone")

manual.sections.each do |section|
Services.publishing_api.unpublish(section.uuid, type: "gone")
end
end

def publish(manual, republish: false)
def self.publish(manual, republish: false)
publish_manual(manual, republish:)

manual.sections.each do |section|
Expand All @@ -60,21 +60,21 @@ def publish(manual, republish: false)
end
end

def discard(manual)
def self.discard(manual)
manual.sections.each do |section|
discard_section(section)
end
Services.publishing_api.discard_draft(manual.id)
end

def save_section(section, manual, republish: false, include_links: true)
def self.save_section(section, manual, republish: false, include_links: true)
if section.needs_exporting? || republish
save_section_links(section, manual) if include_links
save_section_content(section, manual, republish:)
end
end

def redirect_section(section, to:)
def self.redirect_section(section, to:)
Services.publishing_api.put_content(
SecureRandom.uuid,
document_type: "redirect",
Expand All @@ -91,23 +91,17 @@ def redirect_section(section, to:)
)
end

def discard_section(section)
def self.discard_section(section)
Services.publishing_api.discard_draft(section.uuid)
end

private

def organisation_for(manual)
Adapters.organisations.find(manual.organisation_slug)
end

def save_manual(manual, republish:, include_links:)
def self.save_manual(manual, republish:, include_links:)
save_manual_links(manual) if include_links
save_manual_content(manual, republish:)
end

def save_manual_links(manual)
organisation = organisation_for(manual)
def self.save_manual_links(manual)
organisation = OrganisationsAdapter.find(manual.organisation_slug)

Services.publishing_api.patch_links(
manual.id,
Expand All @@ -119,8 +113,8 @@ def save_manual_links(manual)
)
end

def save_manual_content(manual, republish: false)
organisation = organisation_for(manual)
def self.save_manual_content(manual, republish: false)
organisation = OrganisationsAdapter.find(manual.organisation_slug)

update_type = case version_type(republish) || manual.version_type
when :new, :major
Expand Down Expand Up @@ -213,12 +207,12 @@ def save_manual_content(manual, republish: false)
Services.publishing_api.put_content(manual.id, attributes)
end

def publish_manual(manual, republish:)
def self.publish_manual(manual, republish:)
Services.publishing_api.publish(manual.id, update_type(republish))
end

def save_section_links(section, manual)
organisation = organisation_for(manual)
def self.save_section_links(section, manual)
organisation = OrganisationsAdapter.find(manual.organisation_slug)

Services.publishing_api.patch_links(
section.uuid,
Expand All @@ -230,8 +224,8 @@ def save_section_links(section, manual)
)
end

def save_section_content(section, manual, republish: false)
organisation = organisation_for(manual)
def self.save_section_content(section, manual, republish: false)
organisation = OrganisationsAdapter.find(manual.organisation_slug)

update_type = case version_type(republish) || section.version_type
when :new, :major
Expand Down Expand Up @@ -306,18 +300,18 @@ def save_section_content(section, manual, republish: false)
Services.publishing_api.put_content(section.uuid, attributes)
end

def publish_section(section, republish:)
def self.publish_section(section, republish:)
if section.needs_exporting? || republish
Services.publishing_api.publish(section.uuid, update_type(republish))
section.mark_as_exported! unless republish
end
end

def update_type(republish)
def self.update_type(republish)
republish ? GdsApiConstants::PublishingApi::REPUBLISH_UPDATE_TYPE : nil
end

def version_type(republish)
def self.version_type(republish)
republish ? :republish : nil
end
end
9 changes: 0 additions & 9 deletions app/lib/adapters.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/lib/manual_relocator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def redraft_and_republish

def send_draft(manual)
logger.info "Sending a draft of manual #{manual.id} (version: #{manual.version_number}) and its sections"
Adapters.publishing.save_draft(manual, include_links: false, republish: true)
PublishingAdapter.save_draft(manual, include_links: false, republish: true)
end

def send_gone(section_uuid, slug)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/manual_withdrawer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def execute(manual_id)
logger.error "FAILURE: #{message}"
raise message
end
rescue Manual::WithdrawService::ManualNotFoundError
rescue Manual::NotFoundError
message = "Manual not found for manual_id `#{manual_id}`"
warn "ERROR: #{message}"
raise message
Expand Down
2 changes: 1 addition & 1 deletion app/lib/section_reslugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def validate_new_section_in_content_store
end

def redirect_section(section)
Adapters.publishing.redirect_section(section, to: "/#{new_section_slug}")
PublishingAdapter.redirect_section(section, to: "/#{new_section_slug}")
end

def update_slug
Expand Down
2 changes: 1 addition & 1 deletion app/lib/withdraw_and_redirect_manual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def execute

raise ManualNotPublishedError, manual.slug unless manual.can_withdraw?

Adapters.publishing.unpublish_and_redirect_manual_and_sections(
PublishingAdapter.unpublish_and_redirect_manual_and_sections(
manual,
redirect:,
discard_drafts:,
Expand Down
4 changes: 2 additions & 2 deletions app/lib/withdraw_and_redirect_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def execute
raise SectionNotPublishedError, section.slug unless section.published?

if discard_draft && section.draft?
Adapters.publishing.unpublish_section(section, redirect:, discard_drafts: true)
PublishingAdapter.unpublish_section(section, redirect:, discard_drafts: true)
else
Adapters.publishing.unpublish_section(section, redirect:, discard_drafts: false)
PublishingAdapter.unpublish_section(section, redirect:, discard_drafts: false)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/manual/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def call
manual = Manual.new(attributes)

if manual.valid?
Adapters.publishing.save_draft(manual)
PublishingAdapter.save_draft(manual)
manual.save!(user)
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/manual/discard_draft_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call
Result.failure(manual)
else
begin
Adapters.publishing.discard(manual)
PublishingAdapter.discard(manual)
rescue GdsApi::HTTPNotFound
# this is fine, the manual has already been discarded from the
# publishing API and the next line will clean it up in our DB
Expand Down
4 changes: 2 additions & 2 deletions app/services/manual/publish_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def call
if version_number == manual.version_number
manual.publish
PublicationLogger.new.call(manual)
Adapters.publishing.save_draft(manual)
Adapters.publishing.publish(manual)
PublishingAdapter.save_draft(manual)
PublishingAdapter.publish(manual)
manual.save!(user)
else
raise VersionMismatchError,
Expand Down
6 changes: 3 additions & 3 deletions app/services/manual/republish_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ def self.call(user:, manual_id:)
draft_manual_version = manual_versions[:draft]

if published_manual_version.present?
Adapters.publishing.save_draft(published_manual_version, republish: true)
Adapters.publishing.publish(published_manual_version, republish: true)
PublishingAdapter.save_draft(published_manual_version, republish: true)
PublishingAdapter.publish(published_manual_version, republish: true)
end

if draft_manual_version.present?
Adapters.publishing.save_draft(draft_manual_version, republish: true)
PublishingAdapter.save_draft(draft_manual_version, republish: true)
end

manual_versions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call
manual.save!(user)
manual = Manual.find(manual_id, user)

Adapters.publishing.save_draft(manual)
PublishingAdapter.save_draft(manual)

manual
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/manual/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def call
if manual.valid?
manual.save!(user)
reloaded_manual = Manual.find(manual.id, user)
Adapters.publishing.save_draft(reloaded_manual)
PublishingAdapter.save_draft(reloaded_manual)
end

manual
Expand Down
11 changes: 2 additions & 9 deletions app/services/manual/withdraw_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ def initialize(user:, manual_id:)
end

def call
begin
manual = Manual.find(manual_id, user)
rescue KeyError => e
raise ManualNotFoundError, e
end

manual = Manual.find(manual_id, user)
manual.withdraw

if manual.withdrawn?
manual.save!(user)
Adapters.publishing.unpublish(manual)
PublishingAdapter.unpublish(manual)
end

manual
Expand All @@ -24,6 +19,4 @@ def call
private

attr_reader :user, :manual_id

class ManualNotFoundError < StandardError; end
end
4 changes: 2 additions & 2 deletions app/services/section/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def call

if new_section.valid?
manual.draft
Adapters.publishing.save_draft(manual, include_sections: false)
Adapters.publishing.save_section(new_section, manual)
PublishingAdapter.save_draft(manual, include_sections: false)
PublishingAdapter.save_section(new_section, manual)
manual.save!(user)
end

Expand Down
14 changes: 4 additions & 10 deletions app/services/section/remove_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ def initialize(user:, manual_id:, section_uuid:, attributes:)
end

def call
begin
manual = Manual.find(manual_id, user)
rescue KeyError
raise ManualNotFoundError, manual_id
end
manual = Manual.find(manual_id, user)

section = manual.find_section(section_uuid)
raise SectionNotFoundError, section_uuid if section.blank?
Expand All @@ -24,7 +20,7 @@ def call
# We need to capture the state of the section before assigning attributes.
# The Section#assign_attributes method always creates a new draft section if
# the latest edition is published.
# This causes Adapters.publishing.discard_section(section) to be called which
# This causes PublishingAdapter.discard_section(section) to be called which
# blows up as there is no draft section in the Publishing API database.
draft_section = section.draft?

Expand All @@ -36,10 +32,10 @@ def call

manual.remove_section(section_uuid)
manual.save!(user)
Adapters.publishing.save_draft(manual, include_sections: false)
PublishingAdapter.save_draft(manual, include_sections: false)

if draft_section
Adapters.publishing.discard_section(section)
PublishingAdapter.discard_section(section)
end
end

Expand All @@ -50,7 +46,5 @@ def call

attr_reader :user, :manual_id, :section_uuid, :attributes

class ManualNotFoundError < StandardError; end

class SectionNotFoundError < StandardError; end
end
2 changes: 1 addition & 1 deletion app/services/section/reorder_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def call
manual.draft
manual.reorder_sections(section_order)
manual.save!(user)
Adapters.publishing.save_draft(manual, include_sections: false)
PublishingAdapter.save_draft(manual, include_sections: false)

[manual, manual.sections]
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/section/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def call

if section.valid?
manual.draft
Adapters.publishing.save_draft(manual, include_sections: false)
Adapters.publishing.save_section(section, manual)
PublishingAdapter.save_draft(manual, include_sections: false)
PublishingAdapter.save_section(section, manual)
manual.save!(user)
end

Expand Down
Loading

0 comments on commit 7c5dd48

Please sign in to comment.