Skip to content

Commit

Permalink
Batch publish now tries to publish all that it can
Browse files Browse the repository at this point in the history
It will log errors for bad data/unexpected edition states, but still try to
publish everything else.
  • Loading branch information
jamiecobbett committed Mar 27, 2013
1 parent fb751ab commit 4d82b2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
11 changes: 6 additions & 5 deletions lib/batch_publish.rb
Expand Up @@ -10,15 +10,16 @@ def call
slug: identifier.fetch(:slug),
version_number: identifier.fetch(:edition)
).first
unless edition
raise "Edition couldn't be found for #{identifier}"
end
unless edition.ready? || edition.published?
raise "Edition #{identifier} isn't 'published' or 'ready'. It's #{edition.state}"
if edition.nil?
Rails.logger.error "Edition couldn't be found for #{identifier}"
elsif edition.ready? || edition.published?
Rails.logger.error "Edition #{identifier} isn't 'published' or 'ready'. It's #{edition.state}"

This comment has been minimized.

Copy link
@alext

alext Mar 27, 2013

Contributor

I think the logic is inverted here.

This comment has been minimized.

Copy link
@jamiecobbett

jamiecobbett Mar 27, 2013

Author Contributor

Ah yes. Curse you, unless!

end
edition
end

editions = editions.compact

editions.each do |edition|
published = user.progress(edition, { request_type: "publish", comment: "" })
if published
Expand Down
39 changes: 19 additions & 20 deletions test/unit/lib/batch_publish_test.rb
Expand Up @@ -28,42 +28,41 @@ class BatchPublishTest < ActiveSupport::TestCase
end

context "edition doesn't exist" do
should "fail noisily" do
should "still try to publish the other editions" do
edition = FactoryGirl.create(:edition, slug: "foo", version_number: 1, state: "ready", body: "x")
edition_identifiers = [
{ slug: "not-foo", edition: 1 }
{ slug: "not-foo", edition: 1 },
{ slug: "foo", edition: 1 }
]
assert_raises RuntimeError do
BatchPublish.new(edition_identifiers, @user.email).call
end
BatchPublish.new(edition_identifiers, @user.email).call
assert_equal "published", edition.reload.state
end
end

context "an edition is not 'Ready' yet" do
should "fail noisily" do
edition = FactoryGirl.create(:edition, slug: "foo", version_number: 1, state: "draft", body: "x")
edition.expects(:publish).never
should "still try to publish the other editions" do
draft = FactoryGirl.create(:edition, slug: "foo-draft", version_number: 1, state: "draft", body: "x")
ready = FactoryGirl.create(:edition, slug: "foo", version_number: 1, state: "ready", body: "x")
draft.expects(:publish).never
edition_identifiers = [
{ slug: "foo-draft", edition: 1 },
{ slug: "foo", edition: 1 }
]
assert_raises RuntimeError do
BatchPublish.new(edition_identifiers, @user.email).call
end
assert_equal "draft", edition.reload.state
BatchPublish.new(edition_identifiers, @user.email).call
assert_equal "draft", draft.reload.state
assert_equal "published", ready.reload.state
end
end

context "one of the editions cannot be found" do
should "not try to publish any of the editions" do
should "still try to publish the other editions" do
edition = FactoryGirl.create(:edition, slug: "foo", version_number: 1, state: "ready", body: "x")
Edition.any_instance.expects(:publish).never
edition_identifiers = [
{ slug: "foo", edition: 1 },
{ slug: "not-foo", edition: 1 }
{ slug: "not-foo", edition: 1 },
{ slug: "foo", edition: 1 }
]
assert_raises RuntimeError do
BatchPublish.new(edition_identifiers, @user.email).call
end
assert_equal "ready", edition.reload.state
BatchPublish.new(edition_identifiers, @user.email).call
assert_equal "published", edition.reload.state
end
end
end

0 comments on commit 4d82b2c

Please sign in to comment.