Skip to content

Commit 075089e

Browse files
committed
Add/update batch page tasks
1 parent a03381b commit 075089e

File tree

7 files changed

+79
-11
lines changed

7 files changed

+79
-11
lines changed

app/jobs/pages/batch_analyze_topics_job.rb

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

55
def perform
6-
Page.where(request_path: SitepressArticle.published.map(&:request_path)).missing(:topics).find_each do |page|
6+
scope = Page.where.missing(:topics)
7+
.and(Page.where(request_path: SitepressArticle.published.map(&:request_path)))
8+
9+
scope.find_each do |page|
710
Pages::AnalyzeTopicsJob.perform_later(page)
811
end
912
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Pages
2+
class BatchUpsertPagesJob < ApplicationJob
3+
queue_as :default
4+
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
14+
end
15+
end
16+
end

app/models/page.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@
1717
class Page < ApplicationRecord
1818
include Page::Searchable
1919

20+
NullResource = Data.define(:request_path) do
21+
def data = NullData.new(title: nil, description: nil)
22+
end
23+
24+
NullData = Data.define(:title, :description)
25+
2026
has_many :page_topics, dependent: :destroy
2127
has_many :topics, through: :page_topics
2228
has_many :approved_topics, -> { approved }, through: :page_topics, source: :topic, inverse_of: :pages
2329

30+
# def resource_data
31+
delegate :data, to: :resource, allow_nil: true, prefix: true
32+
2433
def self.as_published_articles
2534
SitepressArticle.take_published(all.map { |page| SitepressArticle.new(page.resource) })
2635
end
@@ -29,7 +38,8 @@ def sitepress_article
2938
SitepressArticle.new(resource)
3039
end
3140

32-
def resource = Sitepress.site.get(request_path)
41+
def resource = Sitepress.site.get(request_path) ||
42+
NullResource.new(request_path: request_path)
3343

3444
def body_text = Nokogiri::HTML(SitepressPage.render_html(resource)).text.squish
3545

db/seeds/default/pages.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
[
2-
SitepressArticle,
3-
SitepressSlashPage
4-
].each do |sitepress_collection|
5-
sitepress_collection.all.each do |sitepress_resource|
6-
Page.find_or_create_by!(request_path: sitepress_resource.request_path)
7-
end
8-
end
1+
Pages::BatchUpsertPagesJob.perform_now

spec/factories/pages.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
#
1414
FactoryBot.define do
1515
factory :page do
16-
request_path { "/" }
16+
request_path { "/" + Faker::Internet.slug }
1717
end
1818
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Pages::BatchAnalyzeTopicsJob, type: :job do
4+
it "doesn’t blow up" do
5+
allow(SitepressPage).to receive(:render_html).and_return(Faker::HTML.random)
6+
7+
topics = FactoryBot.create_list(:topic, 2)
8+
9+
# articles without topics
10+
Page.find_or_create_by!(request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
11+
Page.find_or_create_by!(request_path: "/articles/mastering-custom-configuration-in-rails")
12+
13+
# articles with topics
14+
Page.find_or_create_by!(request_path: "/articles/web-push-notifications-from-rails")
15+
.update!(topics: topics)
16+
17+
# not articles
18+
FactoryBot.create_list(:page, 3)
19+
20+
expect(Pages::AnalyzeTopicsJob).to receive(:perform_later).exactly(2).times
21+
22+
described_class.perform_now
23+
end
24+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Pages::BatchUpsertPagesJob, type: :job do
4+
it "creates pages for sitepress resources" do
5+
described_class.perform_now
6+
7+
expect(Page.count).to eq(
8+
SitepressArticle.all.resources.size +
9+
SitepressSlashPage.all.resources.size
10+
)
11+
12+
expect(Page.pluck(:request_path)).to include("/articles/custom-color-schemes-with-ruby-on-rails")
13+
end
14+
15+
it "is idempotent" do
16+
described_class.perform_now
17+
18+
expect {
19+
described_class.perform_now
20+
}.not_to change(Page, :count)
21+
end
22+
end

0 commit comments

Comments
 (0)