Skip to content

Commit 7a55675

Browse files
committed
Refactor Page upsert methods
1 parent 92f6209 commit 7a55675

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

app/jobs/pages/batch_upsert_pages_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class BatchUpsertPagesJob < ApplicationJob
33
queue_as :default
44

55
def perform(limit: nil)
6-
Page.upsert_from_sitepress!(limit: limit)
6+
Page.upsert_collection_from_sitepress!(limit: limit)
77
end
88
end
99
end

app/models/page.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,22 @@ def self.as_published_articles
4444
SitepressArticle.take_published(all.map { |page| SitepressArticle.new(page.resource) })
4545
end
4646

47-
def self.upsert_from_sitepress!(limit: nil)
47+
def self.upsert_collection_from_sitepress!(limit: nil)
4848
# Targeting specific Sitepress models until we have a better way to make
4949
# Page model aware of published state
5050
enum = [
5151
SitepressArticle,
5252
SitepressSlashPage
5353
].lazy.flat_map { |model| model.all.resources }
5454

55-
enum = enum.filter do |sitepress_resource|
56-
Page.find_by(request_path: sitepress_resource.request_path).nil?
57-
end.map do |sitepress_resource|
58-
Page.create!(request_path: sitepress_resource.request_path)
55+
if limit
56+
enum = enum.filter do |sitepress_resource|
57+
Page.find_by(request_path: sitepress_resource.request_path).nil?
58+
end
59+
end
60+
61+
enum = enum.map do |sitepress_resource|
62+
upsert_page_from_sitepress!(sitepress_resource)
5963
end
6064

6165
if limit

spec/system/articles_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
end
99

1010
it "displays a single article" do
11-
Page.upsert_from_sitepress!(limit: 1)
11+
Page.upsert_collection_from_sitepress!(limit: 1)
1212

1313
article = Page.first
1414
article.topics << FactoryBot.create_list(:topic, 2, :approved)

spec/system/topics_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
RSpec.describe "Topics", type: :system do
44
it "displays the topics and provides navigation links" do
5-
Page.upsert_from_sitepress!(limit: 3)
5+
Page.upsert_collection_from_sitepress!(limit: 3)
66

77
expect(Page.count).to eq(3)
88

0 commit comments

Comments
 (0)