Skip to content

Commit f1980f0

Browse files
committed
Refactor methods for rendering Page text and html inline
1 parent ba9763a commit f1980f0

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

app/content/models/sitepress_page.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
class SitepressPage < Sitepress::Model
22
collection glob: "**/*.html*"
33
data :title
4-
5-
def self.render_html(resource, format: :atom)
6-
type = (format == :atom && resource.handler.to_sym == :mdrb) ? :"mdrb-atom" : resource.handler
7-
content_type = (format == :atom) ? "application/atom+xml" : "text/html"
8-
9-
ApplicationController.render(
10-
inline: resource.body,
11-
type:,
12-
content_type:,
13-
layout: false,
14-
assigns: {format:}
15-
)
16-
end
174
end

app/models/page.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def title = resource.data.title
5353

5454
def body = resource.body
5555

56-
def body_text = Nokogiri::HTML(SitepressPage.render_html(resource)).text.squish
56+
def body_text = Nokogiri::HTML(body_html(format: :atom)).text.squish
57+
58+
def body_html(format: :html, **) = self.class.render_html(self, format:, **)
5759

5860
def description = resource.data.description
5961

@@ -65,9 +67,7 @@ def toc = resource.data.toc
6567

6668
def enable_twitter_widgets = resource.data.enable_twitter_widgets
6769

68-
def atom_feed_id
69-
resource.data.uuid.presence || id
70-
end
70+
def atom_feed_id = resource.data.uuid.presence || id
7171

7272
def author = resource.data.author
7373
end

app/models/page/sitepressed.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def upsert_page_from_sitepress!
2525

2626
def resource_missing? = sitepress_resource.is_a?(NullResource)
2727

28+
def handler = sitepress_resource.handler
29+
2830
class_methods do
2931
# We currently have a dual system of content management between Sitepress and
3032
# Page models for handling static pages While not ideal, it currently allows
@@ -63,6 +65,19 @@ def upsert_page_from_sitepress!(sitepress_resource)
6365
page.save!
6466
page
6567
end
68+
69+
def render_html(page, format: :html, assigns: {})
70+
type = (format == :atom && page.handler.to_sym == :mdrb) ? :"mdrb-atom" : page.handler
71+
content_type = (format == :atom) ? "application/atom+xml" : "text/html"
72+
73+
ApplicationController.render(
74+
inline: page.body,
75+
type:,
76+
content_type:,
77+
layout: false,
78+
assigns: {format:, current_page: page}.merge(assigns)
79+
)
80+
end
6681
end
6782
end
6883
end

spec/models/page_spec.rb

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,48 @@
2929
describe "#body" do
3030
it "delegates to resource" do
3131
page = FactoryBot.build(:page, request_path: "/articles/introducing-joy-of-rails")
32+
body = page.body
3233

33-
expect(page.body).to be_present
34-
expect(page.body).to eq page.resource.body
34+
expect(body).to be_present
35+
expect(body).to eq page.resource.body
36+
37+
expect(body).to match(/##/)
38+
end
39+
end
40+
41+
describe "#body_text" do
42+
it "renders processed text from body" do
43+
page = FactoryBot.build(:page, request_path: "/articles/introducing-joy-of-rails")
44+
45+
text = page.body_text
46+
expect(text).to be_present
47+
expect(text).to_not match(/<[^>]+>/)
48+
end
49+
50+
it "works for article that references `current_page` helper" do
51+
page = FactoryBot.build(:page, request_path: "/articles/what-you-need-to-know-about-sqlite")
52+
53+
text = page.body_text
54+
expect(text).to be_present
55+
expect(text).to_not match(/<[^>]+>/)
56+
end
57+
end
58+
59+
describe "#body_html" do
60+
it "renders processed text from body" do
61+
page = FactoryBot.build(:page, request_path: "/articles/introducing-joy-of-rails")
62+
63+
html = page.body_html
64+
expect(html).to be_present
65+
expect(html).to match(/<[^>]+>/)
66+
end
67+
68+
it "works for article that references `current_page` helper" do
69+
page = FactoryBot.build(:page, request_path: "/articles/what-you-need-to-know-about-sqlite")
70+
71+
html = page.body_html
72+
expect(html).to be_present
73+
expect(html).to match(/<[^>]+>/)
3574
end
3675
end
3776

0 commit comments

Comments
 (0)