Skip to content

Commit 92f6209

Browse files
committed
Factor out Sitepress in favor of Page for article layout
1 parent 49c859d commit 92f6209

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

app/content/layouts/article.html.erb

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
<% page = @page %>
12
<% set_meta_tags(
2-
title: current_page.data.title!,
3-
description: current_page.data.description,
3+
title: page.title,
4+
description: page.description,
45
keywords: %w[rails hotwire],
56
og: {
67
type: "article",
7-
url: current_page.url,
8-
image: current_page.data.meta_image && asset_url(current_page.data.meta_image)
8+
url: page.request_path,
9+
image: page.meta_image && asset_url(page.meta_image)
910
},
1011
twitter: {
1112
card: "summary"
@@ -15,24 +16,11 @@
1516
<%= render_layout "application" do %>
1617
<article itemscope itemtype="http://schema.org/Article" class="mb-3xl">
1718
<%= render Pages::Header.new(
18-
title: current_page.data.title!,
19-
description: current_page.data.description,
20-
published_on: current_page.data.published&.to_date,
21-
updated_on: current_page.data.updated&.to_date
19+
title: page.title,
20+
description: page.description,
21+
published_on: page.published_on,
22+
updated_on: page.updated_on
2223
) %>
23-
<%- if current_page.data.toc -%>
24-
<aside>
25-
<h2 class="font-semibold mb-4 text-sm leading-6 uppercase tracking-widest">Table of Contents</h2>
26-
<!-- Disable Turbolinks for same-page Table of Contents navigation
27-
-->
28-
<nav
29-
data-controller="table-of-contents"
30-
data-turbo="false"
31-
class="toc">
32-
<%= render_toc(current_page) %>
33-
</nav>
34-
</aside>
35-
<%- end -%>
3624
<div class="article-content container" itemprop="articleBody">
3725
<%= yield %>
3826
</div>
@@ -47,6 +35,6 @@
4735
</section>
4836
<% end %>
4937

50-
<% if current_page.data.enable_twitter_widgets %>
38+
<% if page.enable_twitter_widgets %>
5139
<%= javascript_include_tag "https://platform.twitter.com/widgets.js", charset: "utf-8", async: true %>
5240
<% end %>

app/models/page.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ def data = NullData.new(title: nil, description: nil)
3434
# def resource_data
3535
delegate :data, to: :resource, allow_nil: true, prefix: true
3636

37-
# We currently have a split system of Sitepress and Page models for handling static pages
38-
# While not ideal, it currently allows us to live in both worlds depending on the context.
39-
# Ultimately, migrating away from Sitepress for indexed content may be what‘s needed, but
40-
# keeping the split personality for now.
37+
# We currently have a dual system of content management between Sitepress and
38+
# Page models for handling static pages While not ideal, it currently allows
39+
# us to live in both worlds depending on the context. Ultimately, migrating
40+
# away from Sitepress for indexed content may be what‘s needed, but keeping
41+
# the split personality for now.
42+
#
4143
def self.as_published_articles
4244
SitepressArticle.take_published(all.map { |page| SitepressArticle.new(page.resource) })
4345
end
@@ -63,20 +65,38 @@ def self.upsert_from_sitepress!(limit: nil)
6365
enum.to_a
6466
end
6567

66-
def sitepress_article
67-
SitepressArticle.new(resource)
68+
def self.upsert_page_from_sitepress!(sitepress_resource)
69+
page = Page.find_or_initialize_by(request_path: sitepress_resource.request_path)
70+
page.published_at = sitepress_resource.data.published.to_time.middle_of_day if sitepress_resource.data.published
71+
page.updated_at = sitepress_resource.data.updated.to_time.middle_of_day if sitepress_resource.data.updated
72+
page.save!
73+
page
6874
end
6975

76+
def published? = !!published_at
77+
78+
def published_on = published_at&.to_date
79+
80+
def updated_on = updated_at&.to_date
81+
82+
def indexed? = !!indexed_at
83+
84+
def sitepress_article = SitepressArticle.new(resource)
85+
7086
def resource = Sitepress.site.get(request_path) ||
7187
NullResource.new(request_path: request_path)
7288

7389
def body_text = Nokogiri::HTML(SitepressPage.render_html(resource)).text.squish
7490

75-
def url = request_path
76-
7791
def title = resource.data.title
7892

7993
def body = resource.body
8094

8195
def description = resource.data.description
96+
97+
def meta_image = resource.data.meta_image
98+
99+
def toc = resource.data.toc
100+
101+
def enable_twitter_widgets = resource.data.toc
82102
end

app/views/searches/results/page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(page)
99

1010
def view_template
1111
a(
12-
href: page.url,
12+
href: page.request_path,
1313
data: {
1414
turbo_frame: "_top"
1515
},

0 commit comments

Comments
 (0)