Skip to content

Commit

Permalink
Merge pull request #9509 from alphagov/content-modelling/613-publish-…
Browse files Browse the repository at this point in the history
…to-draft-content-store-when-creating-a-draft

(613) Publish to draft content store when creating a draft
  • Loading branch information
pezholio authored Oct 10, 2024
2 parents d105610 + c1bc7ef commit 453a4db
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ContentObjectStore
module Publishable
class PublishingFailureError < StandardError; end

def publish_with_rollback(schema:, title:, details:)
def publish_with_rollback(schema)
raise ArgumentError, "Local database changes not given" unless block_given?

ActiveRecord::Base.transaction do
Expand All @@ -13,8 +13,8 @@ def publish_with_rollback(schema:, title:, details:)
create_publishing_api_edition(
content_id:,
schema_id: schema.id,
title:,
details: details.to_h,
title: content_block_edition.title,
details: content_block_edition.details,
links: {
primary_publishing_organisation: [
organisation_id,
Expand All @@ -41,6 +41,28 @@ def schedule_with_rollback
end
end

def create_draft_edition(schema)
raise ArgumentError, "Local database changes not given" unless block_given?

ActiveRecord::Base.transaction do
content_block_edition = yield
content_id = content_block_edition.document.content_id
organisation_id = content_block_edition.lead_organisation.content_id

create_publishing_api_edition(
content_id:,
schema_id: schema.id,
title: content_block_edition.title,
details: content_block_edition.details.to_h,
links: {
primary_publishing_organisation: [
organisation_id,
],
},
)
end
end

def update_content_block_document(new_content_block_edition:, update_document_params:)
# Updates to a Document should never change its block type
update_document_params.delete(:block_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ def initialize(schema)
end

def call(edition_params, document_id: nil)
@new_edition = build_edition(edition_params, document_id)
@new_edition.assign_attributes(edition_params)
@new_edition.save!
create_draft_edition(@schema) do
@new_edition = build_edition(edition_params, document_id)
@new_edition.assign_attributes(edition_params)
@new_edition.save!
@new_edition
end
@new_edition
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ def initialize(schema)
end

def call(edition)
title = edition.title
details = edition.details
publish_with_rollback(schema: @schema, title:, details:) do
publish_with_rollback(@schema) do
edition
end
edition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
schema { build(:content_block_schema) }
creator

organisation { FactoryBot.create(:organisation) }
organisation { FactoryBot.build(:organisation) }

document_id { nil }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,51 @@ class PublishableTestClass
include ContentObjectStore::Publishable
end

test "it raises an error if a block isn't passed since changes need to be made locally" do
anything = Object.new
test_instance = PublishableTestClass.new
describe "#publish_with_rollback" do
it "raises an error if a block isn't passed since changes need to be made locally" do
anything = Object.new
test_instance = PublishableTestClass.new

assert_raises ArgumentError, "Local database changes not given" do
test_instance.publish_with_rollback(
schema: anything, title: anything, details: anything,
assert_raises ArgumentError, "Local database changes not given" do
test_instance.publish_with_rollback(
schema: anything, title: anything, details: anything,
)
end
end
end

describe "#create_draft_edition" do
let(:schema) { build(:content_block_schema) }
let(:content_block_edition) { build(:content_block_edition, :email_address) }

let(:publishable_test_instance) { PublishableTestClass.new }

it "raises an error if a block isn't passed since changes need to be made locally" do
assert_raises ArgumentError, "Local database changes not given" do
publishable_test_instance.create_draft_edition(schema)
end
end

it "creates a draft edition" do
Services.publishing_api.expects(:put_content).with(
content_block_edition.document.content_id,
{
schema_name: schema.id,
document_type: schema.id,
publishing_app: Whitehall::PublishingApp::WHITEHALL,
title: content_block_edition.title,
details: content_block_edition.details,
links: {
primary_publishing_organisation: [
content_block_edition.lead_organisation.content_id,
],
},
},
)

publishable_test_instance.create_draft_edition(schema) do
content_block_edition
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class ContentObjectStore::CreateEditionServiceTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

describe "#call" do
let!(:organisation) { create(:organisation) }

let(:content_id) { "49453854-d8fd-41da-ad4c-f99dbac601c3" }
let(:organisation_id) { "f67b4350-35c2-46a8-babf-e39f5a4f2a7e" }
let(:schema) { build(:content_block_schema, block_type: "content_block_type", body: { "properties" => { "foo" => "", "bar" => "" } }) }
let(:new_title) { "New Title" }
let(:edition_params) do
Expand All @@ -19,7 +20,7 @@ class ContentObjectStore::CreateEditionServiceTest < ActiveSupport::TestCase
"bar" => "Bar text",
},
creator: build(:user),
organisation_id:,
organisation_id: organisation.id.to_s,
}
end

Expand Down Expand Up @@ -52,6 +53,13 @@ class ContentObjectStore::CreateEditionServiceTest < ActiveSupport::TestCase
assert_equal edition_params[:document_attributes][:block_type], new_document.block_type
assert_equal edition_params[:details], new_edition.details
assert_equal new_edition.document_id, new_document.id
assert_equal new_edition.lead_organisation.id, organisation.id
end

it "sends the content block to the Publishing API as a draft" do
assert_draft_created_in_publishing_api(content_id) do
ContentObjectStore::CreateEditionService.new(schema).call(edition_params)
end
end

describe "when a document id is provided" do
Expand All @@ -70,7 +78,34 @@ class ContentObjectStore::CreateEditionServiceTest < ActiveSupport::TestCase
assert_equal new_title, document.title
assert_equal edition_params[:details], new_edition.details
assert_equal new_edition.document_id, document.id
assert_equal new_edition.lead_organisation.id, organisation.id
end

it "sends the content block to the Publishing API as a draft" do
assert_draft_created_in_publishing_api(document.content_id) do
ContentObjectStore::CreateEditionService.new(schema).call(edition_params, document_id: document.id)
end
end
end
end
end

def assert_draft_created_in_publishing_api(content_id, &block)
Services.publishing_api.expects(:put_content).with(
content_id,
{
schema_name: schema.id,
document_type: schema.id,
publishing_app: Whitehall::PublishingApp::WHITEHALL,
title: new_title,
details: edition_params[:details],
links: {
primary_publishing_organisation: [
organisation.content_id,
],
},
},
)

block.call
end

0 comments on commit 453a4db

Please sign in to comment.