Skip to content

Commit 91995c0

Browse files
committed
Extract Page upsert method
1 parent d1d324c commit 91995c0

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

app/jobs/pages/batch_upsert_pages_job.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ module Pages
22
class BatchUpsertPagesJob < ApplicationJob
33
queue_as :default
44

5-
def perform
6-
[
7-
SitepressArticle,
8-
SitepressSlashPage
9-
].each do |sitepress_collection|
10-
sitepress_collection.all.each do |sitepress_resource|
11-
Page.find_or_create_by!(request_path: sitepress_resource.request_path)
12-
end
13-
end
5+
def perform(limit: nil)
6+
Page.upsert_from_sitepress!(limit: limit)
147
end
158
end
169
end

app/models/page.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,35 @@ def data = NullData.new(title: nil, description: nil)
3030
# def resource_data
3131
delegate :data, to: :resource, allow_nil: true, prefix: true
3232

33+
# We currently have a split system of Sitepress and Page models for handling static pages
34+
# While not ideal, it currently allows us to live in both worlds depending on the context.
35+
# Ultimately, migrating away from Sitepress for indexed content may be what‘s needed, but
36+
# keeping the split personality for now.
3337
def self.as_published_articles
3438
SitepressArticle.take_published(all.map { |page| SitepressArticle.new(page.resource) })
3539
end
3640

41+
def self.upsert_from_sitepress!(limit: nil)
42+
# Targeting specific Sitepress models until we have a better way to make
43+
# Page model aware of published state
44+
enum = [
45+
SitepressArticle,
46+
SitepressSlashPage
47+
].lazy.flat_map { |model| model.all.resources }
48+
49+
enum = enum.filter do |sitepress_resource|
50+
Page.find_by(request_path: sitepress_resource.request_path).nil?
51+
end.map do |sitepress_resource|
52+
Page.create!(request_path: sitepress_resource.request_path)
53+
end
54+
55+
if limit
56+
enum = enum.take(limit)
57+
end
58+
59+
enum.to_a
60+
end
61+
3762
def sitepress_article
3863
SitepressArticle.new(resource)
3964
end

spec/jobs/pages/batch_upsert_pages_job_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@
1919
described_class.perform_now
2020
}.not_to change(Page, :count)
2121
end
22+
23+
it "handles limit argument" do
24+
expect {
25+
described_class.perform_now(limit: 3)
26+
}.to change(Page, :count).by(3)
27+
28+
expect {
29+
described_class.perform_now(limit: 3)
30+
}.to change(Page, :count).by(3)
31+
end
2232
end

0 commit comments

Comments
 (0)