Skip to content

Commit

Permalink
Only publish manuals if its the correct version
Browse files Browse the repository at this point in the history
As we are now queueing manuals for publishing we need to protect against changes happening between an editor clicking Publish and the worker actually performing the publish.

We also add the version number to the logs as its useful information to know about.
  • Loading branch information
Andrew Mitchell committed Aug 14, 2014
1 parent 947126a commit 83dbfcd
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app/loggers/publication_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def call
manual.documents.each do |doc|
PublicationLog.create!(
title: doc.title,
manual_version_number: manual.version_number,
version_number: doc.version_number,
slug: doc.slug,
change_note: doc.change_note,
document_state: state,
Expand Down
3 changes: 3 additions & 0 deletions app/models/manual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class Manual
:summary,
:organisation_slug,
:state,
:version_number,
:updated_at,
)

def initialize(attributes)
@id = attributes.fetch(:id)
@updated_at = attributes.fetch(:updated_at, nil)
@version_number = attributes.fetch(:version_number, 0)

update(attributes)
end
Expand All @@ -28,6 +30,7 @@ def attributes
summary: summary,
organisation_slug: organisation_slug,
state: state,
version_number: version_number,
updated_at: updated_at,
}
end
Expand Down
1 change: 1 addition & 0 deletions app/repositories/manual_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def build_manual_for(manual_record)
summary: edition.summary,
organisation_slug: manual_record.organisation_slug,
state: edition.state,
version_number: edition.version_number,
updated_at: edition.updated_at,
)

Expand Down
3 changes: 2 additions & 1 deletion app/services/manual_service_registry.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class ManualServiceRegistry
def publish(manual_id)
def publish(manual_id, version_number)
PublishManualService.new(
manual_repository: manual_repository,
listeners: observers.publication,
manual_id: manual_id,
version_number: version_number,
)
end

Expand Down
8 changes: 0 additions & 8 deletions app/services/organisational_manual_service_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ def show(manual_id)
)
end

def publish(context)
PublishManualService.new(
manual_repository: manual_repository,
listeners: observers.publication,
context: context,
)
end

def queue_publish(manual_id)
QueuePublishManualService.new(
async_services,
Expand Down
20 changes: 16 additions & 4 deletions app/services/publish_manual_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@ def initialize(dependencies)
@manual_id = dependencies.fetch(:manual_id)
@manual_repository = dependencies.fetch(:manual_repository)
@listeners = dependencies.fetch(:listeners)
@version_number = dependencies.fetch(:version_number)
end

def call
publish
persist
if versions_match?
publish
persist
end

manual
end

private
private

attr_reader(
:manual_id,
:manual_repository,
:listeners,
:version_number,
)

attr_reader :manual_id, :manual_repository, :listeners
def versions_match?
version_number == manual.version_number
end

def publish
manual.publish
Expand Down
2 changes: 1 addition & 1 deletion app/services/queue_publish_manual_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(async_services, repository, publication_logger, manual_id)
end

def call
async_services.publish(manual.id)
async_services.publish(manual.id, manual.version_number)
publication_logger.call(manual).call
manual
end
Expand Down
4 changes: 2 additions & 2 deletions app/workers/asynchronous_manual_service_registry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class AsynchronousManualServiceRegistry

def publish(id)
PublishManualWorker.perform_async(id)
def publish(id, version_number)
PublishManualWorker.perform_async(id, version_number)
end
end
4 changes: 2 additions & 2 deletions app/workers/publish_manual_worker.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class PublishManualWorker
include Sidekiq::Worker

def perform(manual_id)
services.publish(manual_id).call
def perform(manual_id, version_number)
services.publish(manual_id, version_number).call
end

private
Expand Down
1 change: 1 addition & 0 deletions spec/models/manual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
organisation_slug: organisation_slug,
state: state,
updated_at: updated_at,
version_number: 0,
)
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/repositories/manual_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
organisation_slug: "organisation_slug",
state: "draft",
slug: manual_slug,
version_number: 1,
}
}

Expand Down
43 changes: 43 additions & 0 deletions spec/services/publish_manual_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "fast_spec_helper"
require "publish_manual_service"

RSpec.describe PublishManualService do

let(:manual_id) { double(:manual_id) }
let(:manual_repository) { double(:manual_repository) }
let(:listeners) { [] }
let(:manual) { double(:manual, version_number: 3) }

subject {
PublishManualService.new(
manual_id: manual_id,
manual_repository: manual_repository,
listeners: listeners,
version_number: version_number,
)
}

before do
allow(manual_repository).to receive(:fetch) { manual }
allow(manual_repository).to receive(:store)
allow(manual).to receive(:publish)
end

context "when the version number is up to date" do
let(:version_number) { 3 }

it "publishes the manual" do
subject.call
expect(manual).to have_received(:publish)
end
end

context "when the version numbers differ" do
let(:version_number) { 4 }

it "does not publish the manual" do
subject.call
expect(manual).to_not have_received(:publish)
end
end
end

0 comments on commit 83dbfcd

Please sign in to comment.