Skip to content

Commit 3d6958b

Browse files
committed
Refactor to adapter model Page::Sitepressed::Resource
1 parent b3338bf commit 3d6958b

File tree

4 files changed

+91
-40
lines changed

4 files changed

+91
-40
lines changed

app/models/page.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,31 @@ def published? = !!published_at
4242

4343
def published_on = published_at&.to_date
4444

45-
def revised_on = resource.data.updated&.to_date
46-
4745
def indexed? = !!indexed_at
4846

49-
# alias
50-
def resource = sitepress_resource
47+
def body_text = Nokogiri::HTML(body_html(format: :atom)).text.squish
5148

52-
def title = resource.data.title
49+
def body_html(format: :html, **) = self.class.render_html(self, format:, **)
5350

54-
def body = resource.body
51+
# alias
5552

56-
def body_text = Nokogiri::HTML(body_html(format: :atom)).text.squish
53+
def revised_on = resource.updated_on
5754

58-
def body_html(format: :html, **) = self.class.render_html(self, format:, **)
55+
def title = resource.title
56+
57+
def body = resource.body
5958

60-
def description = resource.data.description
59+
def description = resource.description
6160

62-
def image = resource.data.image
61+
def image = resource.image
6362

64-
def meta_image = resource.data.meta_image
63+
def meta_image = resource.meta_image
6564

66-
def toc = resource.data.toc
65+
def toc = resource.toc
6766

68-
def enable_twitter_widgets = resource.data.enable_twitter_widgets
67+
def enable_twitter_widgets = resource.enable_twitter_widgets
6968

70-
def atom_feed_id = resource.data.uuid.presence || id
69+
def atom_feed_id = resource.uuid.presence || id
7170

72-
def author = resource.data.author
71+
def author = resource.author
7372
end

app/models/page/sitepressed.rb

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,80 @@ class Page
22
module Sitepressed
33
extend ActiveSupport::Concern
44

5-
NullResource = Data.define(:request_path) do
6-
def data = NullData.new(title: nil, description: nil)
5+
Resource = Data.define(
6+
:request_path,
7+
:body,
8+
:title,
9+
:description,
10+
:image,
11+
:meta_image,
12+
:toc,
13+
:enable_twitter_widgets,
14+
:uuid,
15+
:author,
16+
:published_on,
17+
:updated_on
18+
) do
19+
def published_at = published_on&.to_time&.middle_of_day
20+
21+
def revised_at = updated_on&.to_time&.middle_of_day
22+
23+
def self.from(sitepress_resource)
24+
Resource.new \
25+
request_path: sitepress_resource.request_path,
26+
body: sitepress_resource.body,
27+
title: sitepress_resource.data.title,
28+
description: sitepress_resource.data.description,
29+
image: sitepress_resource.data.image,
30+
meta_image: sitepress_resource.data.meta_image,
31+
toc: sitepress_resource.data.toc,
32+
enable_twitter_widgets: sitepress_resource.data.enable_twitter_widgets,
33+
uuid: sitepress_resource.data.uuid,
34+
author: sitepress_resource.data.author,
35+
published_on: sitepress_resource.data.published&.to_date,
36+
updated_on: sitepress_resource.data.updated&.to_date
37+
end
38+
end
39+
40+
NullSitepressResource = Data.define(:request_path) do
41+
def data = NullSitpressData.new
42+
43+
def handler = "html"
744

845
def body = ""
946
end
1047

11-
NullData = Data.define(:title, :description) do
48+
class NullSitpressData
49+
def title = nil
50+
51+
def description = nil
52+
53+
def toc = nil
54+
55+
def enable_twitter_widgets = nil
56+
1257
def uuid = nil
1358

59+
def author = nil
60+
61+
def published = nil
62+
63+
def updated = nil
64+
1465
def image = "articles/placeholder.jpg"
1566

16-
def author = nil
67+
def meta_image = image
1768
end
1869

19-
def sitepress_resource = Sitepress.site.get(request_path) ||
20-
NullResource.new(request_path: request_path)
70+
def resource = Resource.from(sitepress_resource)
2171

22-
def upsert_page_from_sitepress!
23-
self.class.upsert_page_from_sitepress!(sitepress_resource)
24-
end
72+
def sitepress_resource = @sitepress_resource ||= Sitepress.site.get(request_path) || NullSitepressResource.new(request_path: request_path)
2573

26-
def resource_missing? = sitepress_resource.is_a?(NullResource)
74+
def upsert_page_from_sitepress! = upsert_page_from_resource!
75+
76+
def upsert_page_from_resource! = self.class.upsert_page_from_resource!(resource)
77+
78+
def resource_missing? = sitepress_resource.is_a?(NullSitepressResource)
2779

2880
def handler = sitepress_resource.handler
2981

@@ -35,16 +87,16 @@ def handler = sitepress_resource.handler
3587
# the split personality for now.
3688
#
3789
def upsert_collection_from_sitepress!(limit: nil)
38-
enum = SitepressPage.all.resources.lazy
90+
enum = SitepressPage.all.resources.lazy.map { |s| Resource.from(s) }
3991

4092
if limit
41-
enum = enum.filter do |sitepress_resource|
42-
find_by(request_path: sitepress_resource.request_path).nil?
93+
enum = enum.filter do |resource|
94+
find_by(request_path: resource.request_path).nil?
4395
end
4496
end
4597

46-
enum = enum.map do |sitepress_resource|
47-
upsert_page_from_sitepress!(sitepress_resource)
98+
enum = enum.map do |resource|
99+
upsert_page_from_resource!(resource)
48100
end
49101

50102
if limit
@@ -54,14 +106,14 @@ def upsert_collection_from_sitepress!(limit: nil)
54106
enum.to_a
55107
end
56108

57-
def upsert_page_by_request_path!(request_path)
58-
upsert_page_from_sitepress!(Sitepress.site.get(request_path))
59-
end
109+
def upsert_page_by_request_path!(request_path) = upsert_page_from_sitepress!(Sitepress.site.get(request_path))
110+
111+
def upsert_page_from_sitepress!(sitepress_resource) = upsert_page_from_resource! Resource.from(sitepress_resource)
60112

61-
def upsert_page_from_sitepress!(sitepress_resource)
62-
page = find_or_initialize_by(request_path: sitepress_resource.request_path)
63-
page.published_at = sitepress_resource.data.published.to_time.middle_of_day if sitepress_resource.data.published
64-
page.updated_at = sitepress_resource.data.updated.to_time.middle_of_day if sitepress_resource.data.updated
113+
def upsert_page_from_resource!(resource)
114+
page = find_or_initialize_by(request_path: resource.request_path)
115+
page.published_at = resource.published_at if resource.published_at
116+
page.updated_at = resource.revised_at if resource.revised_at # Should use a `Page#revised_at` column instead
65117
page.save!
66118
page
67119
end

spec/jobs/pages/analyze_topics_job_spec.rb

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

3232
describe Pages::AnalyzeTopicsJob::Batch do
3333
it "doesn’t blow up" do
34-
allow(SitepressPage).to receive(:render_html).and_return(Faker::HTML.random)
34+
allow(Page).to receive(:render_html).and_return(Faker::HTML.random)
3535

3636
topics = FactoryBot.create_list(:topic, 2)
3737

spec/models/page_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
it "represents a sitepress resource" do
2323
page = FactoryBot.create(:page, request_path: "/")
2424

25-
expect(page.resource).to eq Sitepress.site.get("/")
25+
expect(page.resource).to eq Page::Sitepressed::Resource.from(Sitepress.site.get("/"))
2626
end
2727
end
2828

@@ -80,7 +80,7 @@
8080

8181
%w[title description image meta_image toc author].each do |method|
8282
expect(page.send(method)).not_to be_nil, "Expected #{method} to be present"
83-
expect(page.send(method)).to eq(page.resource.data.send(method)), "Expected #{method} to match"
83+
expect(page.send(method)).to eq(page.sitepress_resource.data.send(method)), "Expected #{method} to match"
8484
end
8585
end
8686
end

0 commit comments

Comments
 (0)