Skip to content

Commit 1b1c436

Browse files
authored
Merge pull request #277 from joyofrails/feat/page-timestamps
Add page timestamps
2 parents 9e2c706 + f702081 commit 1b1c436

31 files changed

+256
-114
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/jobs/pages/batch_analyze_topics_job.rb

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

55
def perform
6-
scope = Page.where.missing(:topics)
7-
.and(Page.where(request_path: SitepressArticle.published.map(&:request_path)))
8-
9-
scope.find_each do |page|
6+
Page.published.where.missing(:topics).find_each do |page|
107
Pages::AnalyzeTopicsJob.perform_later(page)
118
end
129
end

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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Pages
2+
class RefreshSearchIndexJob < ApplicationJob
3+
def perform
4+
Page.published.find_each(&:update_in_search_index)
5+
end
6+
end
7+
end

app/jobs/pages/search_index_refresh_job.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/models/page.rb

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
# Table name: pages
77
#
88
# id :string not null, primary key
9+
# indexed_at :datetime
10+
# published_at :datetime
911
# request_path :string not null
1012
# created_at :datetime not null
1113
# updated_at :datetime not null
1214
#
1315
# Indexes
1416
#
17+
# index_pages_on_indexed_at (indexed_at)
18+
# index_pages_on_published_at (published_at)
1519
# index_pages_on_request_path (request_path) UNIQUE
1620
#
1721
class Page < ApplicationRecord
@@ -27,29 +31,29 @@ def data = NullData.new(title: nil, description: nil)
2731
has_many :topics, through: :page_topics
2832
has_many :approved_topics, -> { approved }, through: :page_topics, source: :topic, inverse_of: :pages
2933

34+
scope :published, -> { where(["published_at < ?", Time.zone.now]) }
35+
scope :indexed, -> { where(["indexed_at < ?", Time.zone.now]) }
36+
3037
# def resource_data
3138
delegate :data, to: :resource, allow_nil: true, prefix: true
3239

33-
# We currently have a split system of Sitepress and Page models for handling static pages
34-
# While not ideal, it currently allows us to live in both worlds depending on the context.
35-
# Ultimately, migrating away from Sitepress for indexed content may be what‘s needed, but
36-
# keeping the split personality for now.
37-
def self.as_published_articles
38-
SitepressArticle.take_published(all.map { |page| SitepressArticle.new(page.resource) })
39-
end
40+
# We currently have a dual system of content management between Sitepress and
41+
# Page models for handling static pages While not ideal, it currently allows
42+
# us to live in both worlds depending on the context. Ultimately, migrating
43+
# away from Sitepress for indexed content may be what‘s needed, but keeping
44+
# the split personality for now.
45+
#
46+
def self.upsert_collection_from_sitepress!(limit: nil)
47+
enum = SitepressPage.all.resources.lazy
48+
49+
if limit
50+
enum = enum.filter do |sitepress_resource|
51+
Page.find_by(request_path: sitepress_resource.request_path).nil?
52+
end
53+
end
4054

41-
def self.upsert_from_sitepress!(limit: nil)
42-
# Targeting specific Sitepress models until we have a better way to make
43-
# Page model aware of published state
44-
enum = [
45-
SitepressArticle,
46-
SitepressSlashPage
47-
].lazy.flat_map { |model| model.all.resources }
48-
49-
enum = enum.filter do |sitepress_resource|
50-
Page.find_by(request_path: sitepress_resource.request_path).nil?
51-
end.map do |sitepress_resource|
52-
Page.create!(request_path: sitepress_resource.request_path)
55+
enum = enum.map do |sitepress_resource|
56+
upsert_page_from_sitepress!(sitepress_resource)
5357
end
5458

5559
if limit
@@ -59,20 +63,44 @@ def self.upsert_from_sitepress!(limit: nil)
5963
enum.to_a
6064
end
6165

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

74+
def published? = !!published_at
75+
76+
def published_on = published_at&.to_date
77+
78+
def updated_on = updated_at&.to_date
79+
80+
def indexed? = !!indexed_at
81+
82+
def sitepress_article = SitepressArticle.new(resource)
83+
6684
def resource = Sitepress.site.get(request_path) ||
6785
NullResource.new(request_path: request_path)
6886

6987
def body_text = Nokogiri::HTML(SitepressPage.render_html(resource)).text.squish
7088

71-
def url = request_path
72-
7389
def title = resource.data.title
7490

7591
def body = resource.body
7692

7793
def description = resource.data.description
94+
95+
def image = resource.data.image
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
102+
103+
def upsert_page_from_sitepress!
104+
self.class.upsert_page_from_sitepress!(resource)
105+
end
78106
end

app/models/page/searchable.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module Searchable
66
ConfigurationError = Class.new(Error)
77

88
included do
9-
after_create_commit :create_in_search_index
10-
after_update_commit :update_in_search_index
119
after_destroy_commit :remove_from_search_index
1210

1311
scope :search, ->(query) do
@@ -57,6 +55,7 @@ def update_in_search_index
5755
transaction do
5856
remove_from_search_index
5957
create_in_search_index
58+
touch(:indexed_at)
6059
end
6160
end
6261

@@ -65,10 +64,6 @@ def remove_from_search_index
6564
end
6665

6766
class_methods do
68-
def refresh_search_index
69-
find_each(&:update_in_search_index)
70-
end
71-
7267
def create_search_index(page)
7368
id, title, body = [:id, :title, :body_text].map { |attr| page.send(attr) }
7469
Rails.logger.info "[#{self}] Creating search index: #{id} #{title}"

app/models/page_topic.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# == Schema Information
2+
#
3+
# Table name: page_topics
4+
#
5+
# id :integer not null, primary key
6+
# created_at :datetime not null
7+
# updated_at :datetime not null
8+
# page_id :string not null
9+
# topic_id :integer not null
10+
#
11+
# Indexes
12+
#
13+
# index_page_topics_on_page_id (page_id)
14+
# index_page_topics_on_page_id_and_topic_id (page_id,topic_id) UNIQUE
15+
# index_page_topics_on_topic_id (topic_id)
16+
#
17+
# Foreign Keys
18+
#
19+
# page_id (page_id => pages.id)
20+
# topic_id (topic_id => topics.id)
21+
#
122
class PageTopic < ApplicationRecord
223
belongs_to :page
324
belongs_to :topic, counter_cache: :pages_count

app/models/topic.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
# == Schema Information
2+
#
3+
# Table name: topics
4+
#
5+
# id :integer not null, primary key
6+
# description :text
7+
# name :string
8+
# pages_count :integer default(0), not null
9+
# slug :string not null
10+
# status :string default("pending"), not null
11+
# created_at :datetime not null
12+
# updated_at :datetime not null
13+
#
14+
# Indexes
15+
#
16+
# index_topics_on_pages_count (pages_count)
17+
# index_topics_on_slug (slug) UNIQUE
18+
# index_topics_on_status (status)
19+
#
120
class Topic < ApplicationRecord
221
include Sluggable
322

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)