Skip to content

Commit

Permalink
Deprecate passing a Page into PublishPageJob
Browse files Browse the repository at this point in the history
Passing an page "object" here through `GlobalID` and then reloading will
lead to double page loads.
  • Loading branch information
mamhoff committed Apr 26, 2022
1 parent 4ea6672 commit b8dd989
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
10 changes: 8 additions & 2 deletions app/jobs/alchemy/publish_page_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ module Alchemy
class PublishPageJob < BaseJob
queue_as :default

def perform(page, public_on:)
def perform(page_or_id, public_on:)
if page_or_id.is_a? Alchemy::Page
Alchemy::Deprecation.warn("Passing an Alchemy Page into the publish Job is deprecated. Please pass a page ID instead.")
page_id = page_or_id.id
else
page_id = page_or_id
end
page = Alchemy::Page.includes(
draft_version: {
elements: [
{ ingredients: :related_object },
{ contents: :essence },
],
},
).find(page.id)
).find(page_id)
Alchemy::Page::Publisher.new(page).publish!(public_on: public_on)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def copy_children_to(new_parent)
#
def publish!(current_time = Time.current)
update(published_at: current_time)
PublishPageJob.perform_later(self, public_on: current_time)
PublishPageJob.perform_later(id, public_on: current_time)
end

# Sets the public_on date on the published version
Expand Down
16 changes: 14 additions & 2 deletions spec/jobs/alchemy/publish_page_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@

it "enqueues job" do
expect {
described_class.perform_later(page, public_on: public_on)
described_class.perform_later(page.id, public_on: public_on)
}.to have_enqueued_job
end

it "calls the page publisher" do
expect_any_instance_of(Alchemy::Page::Publisher).to receive(:publish!).with(
public_on: public_on,
)
described_class.new.perform(page, public_on: public_on)
described_class.new.perform(page.id, public_on: public_on)
end

context "when called with a page object" do
subject { described_class.new.perform(page, public_on: public_on) }

it "warns about the changing API and calls the page publisher" do
expect(Alchemy::Deprecation).to receive(:warn)
expect_any_instance_of(Alchemy::Page::Publisher).to receive(:publish!).with(
public_on: public_on,
)
subject
end
end
end
end

0 comments on commit b8dd989

Please sign in to comment.