-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(699) Allow Content Block schedule to be edited #9699
Merged
pezholio
merged 8 commits into
main
from
content-modelling/699-allow-schedule-to-be-edited
Dec 5, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
684e9d8
Move publish / schedule methods out of Publishable
pezholio 8fb17eb
Dequeue existing jobs if already scheduled
pezholio 091406e
Dequeue any existing jobs when publishing
pezholio b606fe2
Share schedule or publish behaviour
pezholio 2b27da5
Allow schedule to be edited
pezholio 2e300be
Add a `superseded` state to an edition
pezholio 46e10b9
Supersede previously scheduled editions
pezholio 0d55ff1
Move remaining dequeueing behaviour to concern
pezholio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
lib/engines/content_block_manager/app/controllers/concerns/can_schedule_or_publish.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module CanScheduleOrPublish | ||
extend ActiveSupport::Concern | ||
|
||
def schedule_or_publish(template = "content_block_manager/content_block/editions/workflow/schedule_publishing") | ||
@schema = ContentBlockManager::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type) | ||
|
||
if params[:schedule_publishing].blank? | ||
@content_block_edition.errors.add(:schedule_publishing, "cannot be blank") | ||
raise ActiveRecord::RecordInvalid, @content_block_edition | ||
elsif params[:schedule_publishing] == "schedule" | ||
ContentBlockManager::ScheduleEditionService.new(@schema).call(@content_block_edition, scheduled_publication_params) | ||
else | ||
publish and return | ||
end | ||
|
||
redirect_to content_block_manager.content_block_manager_content_block_workflow_path(id: @content_block_edition.id, | ||
step: :confirmation, | ||
is_scheduled: true) | ||
rescue ActiveRecord::RecordInvalid | ||
render template | ||
end | ||
|
||
def publish | ||
new_edition = ContentBlockManager::PublishEditionService.new.call(@content_block_edition) | ||
redirect_to content_block_manager.content_block_manager_content_block_workflow_path(id: new_edition.id, step: :confirmation) | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
...ager/app/controllers/content_block_manager/content_block/documents/schedule_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class ContentBlockManager::ContentBlock::Documents::ScheduleController < ContentBlockManager::BaseController | ||
include CanScheduleOrPublish | ||
|
||
def edit | ||
document = ContentBlockManager::ContentBlock::Document.find(params[:document_id]) | ||
@content_block_edition = document.latest_edition | ||
end | ||
|
||
def update | ||
document = ContentBlockManager::ContentBlock::Document.find(params[:document_id]) | ||
@content_block_edition = document.latest_edition | ||
schedule_or_publish("content_block_manager/content_block/documents/schedule/edit") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 0 additions & 85 deletions
85
lib/engines/content_block_manager/app/lib/content_block_manager/publishable.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
lib/engines/content_block_manager/app/services/content_block_manager/concerns/dequeueable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module ContentBlockManager | ||
module Concerns | ||
module Dequeueable | ||
extend ActiveSupport::Concern | ||
|
||
def dequeue_all_previously_queued_editions(content_block_edition) | ||
content_block_edition.document.editions.where(state: :scheduled).find_each do |edition| | ||
next if content_block_edition.id == edition.id | ||
|
||
ContentBlockManager::SchedulePublishingWorker.dequeue(edition) | ||
edition.supersede! | ||
end | ||
end | ||
end | ||
end | ||
end |
2 changes: 0 additions & 2 deletions
2
...ngines/content_block_manager/app/services/content_block_manager/create_edition_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 66 additions & 1 deletion
67
...gines/content_block_manager/app/services/content_block_manager/publish_edition_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,74 @@ | ||
module ContentBlockManager | ||
class PublishEditionService | ||
include Publishable | ||
class PublishingFailureError < StandardError; end | ||
|
||
include Concerns::Dequeueable | ||
|
||
def call(edition) | ||
publish_with_rollback(edition) | ||
end | ||
|
||
private | ||
|
||
def publish_with_rollback(content_block_edition) | ||
document = content_block_edition.document | ||
schema = ContentBlockManager::ContentBlock::Schema.find_by_block_type(document.block_type) | ||
content_id = document.content_id | ||
content_id_alias = document.content_id_alias | ||
|
||
create_publishing_api_edition( | ||
content_id:, | ||
content_id_alias:, | ||
schema_id: schema.id, | ||
title: content_block_edition.title, | ||
details: content_block_edition.details, | ||
instructions_to_publishers: content_block_edition.instructions_to_publishers, | ||
links: { | ||
primary_publishing_organisation: [ | ||
content_block_edition.lead_organisation.content_id, | ||
], | ||
}, | ||
) | ||
ContentBlockManager::SchedulePublishingWorker.dequeue(content_block_edition) if content_block_edition.scheduled? | ||
dequeue_all_previously_queued_editions(content_block_edition) | ||
publish_publishing_api_edition(content_id:) | ||
update_content_block_document_with_latest_edition(content_block_edition) | ||
content_block_edition.public_send(:publish!) | ||
content_block_edition | ||
rescue PublishingFailureError => e | ||
discard_publishing_api_edition(content_id:) | ||
raise e | ||
end | ||
|
||
def create_publishing_api_edition(content_id:, content_id_alias:, schema_id:, title:, instructions_to_publishers:, details:, links:) | ||
Services.publishing_api.put_content(content_id, { | ||
schema_name: schema_id, | ||
document_type: schema_id, | ||
publishing_app: Whitehall::PublishingApp::WHITEHALL, | ||
title:, | ||
instructions_to_publishers:, | ||
content_id_alias:, | ||
details:, | ||
links:, | ||
update_type: "major", | ||
}) | ||
end | ||
|
||
def publish_publishing_api_edition(content_id:) | ||
Services.publishing_api.publish(content_id, "content_block") | ||
rescue GdsApi::HTTPErrorResponse => e | ||
raise PublishingFailureError, "Could not publish #{content_id} because: #{e.message}" | ||
end | ||
|
||
def update_content_block_document_with_latest_edition(content_block_edition) | ||
content_block_edition.document.update!( | ||
latest_edition_id: content_block_edition.id, | ||
live_edition_id: content_block_edition.id, | ||
) | ||
end | ||
|
||
def discard_publishing_api_edition(content_id:) | ||
Services.publishing_api.discard_draft(content_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ck_manager/app/views/content_block_manager/content_block/documents/schedule/edit.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= render "content_block_manager/content_block/shared/schedule_publishing", { | ||
context: "Edit a content block", | ||
back_link: content_block_manager.content_block_manager_content_block_document_path(id: @content_block_edition.document.id), | ||
form_url: content_block_manager.content_block_manager_content_block_document_update_schedule_path(document_id: @content_block_edition.document.id), | ||
} %> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think this line could be part of the dequeuing concern? or is the dequeuing concern already doing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or could it have it's own named function to show what it's doing like
dequeue_current_edition_if_previously_scheduled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good plan! Have done 👍