Skip to content

Commit 95e4427

Browse files
nbudinclaude
andcommitted
Add cached_og_description to pages, computed in background
Adds a `cached_og_description` column to pages, populated by `CachePageOgDescriptionJob` on every page create/update. The job renders the page's Liquid template in a logged-out CmsRenderingContext (same pattern as `content_for_search`) and strips/truncates the result to 160 characters. `open_graph_description_for_page` now reads straight from the column instead of calling Rails.cache with a full Liquid render on every cold-cache request. A migration backfill enqueues the job for all existing pages. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 3ff8848 commit 95e4427

8 files changed

Lines changed: 53 additions & 8 deletions

File tree

.rubocop_todo.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,22 @@ Rails/RefuteMethods:
7575
# Offense count: 3
7676
# Configuration parameters: ForbiddenMethods, AllowedMethods.
7777
# ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all
78+
Rails/HasAndBelongsToMany:
79+
Exclude:
80+
- 'app/models/page.rb'
81+
82+
Rails/HasManyOrHasOneDependent:
83+
Exclude:
84+
- 'app/models/page.rb'
85+
86+
Rails/HelperInstanceVariable:
87+
Exclude:
88+
- 'app/helpers/application_helper.rb'
89+
7890
Rails/SkipsModelValidations:
7991
Exclude:
8092
- 'app/controllers/application_controller.rb'
93+
- 'app/models/page.rb'
8194
- 'db/migrate/20251229184620_allow_null_redirect_uri_on_oauth_appliations.rb'
8295

8396
# Offense count: 7

app/helpers/application_helper.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ def page_title
1818
end
1919

2020
def open_graph_description_for_page(page)
21-
return page.meta_description if page.meta_description.present?
22-
23-
Rails
24-
.cache
25-
.fetch(["open_graph_description", page], expires_in: 1.day) do
26-
strip_tags(cms_rendering_context.render_page_content(@page)).gsub(/\s+/, " ").strip.truncate(160)
27-
end
21+
page.meta_description.presence || page.cached_og_description
2822
end
2923

3024
def application_entry_path
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class CachePageOgDescriptionJob < ApplicationJob
4+
def perform(page)
5+
page.update_column(:cached_og_description, page.compute_og_description) # rubocop:disable Rails/SkipsModelValidations
6+
end
7+
end

app/models/page.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# invariant :boolean default(FALSE), not null
1212
# meta_description :text
1313
# name :text
14+
# og_description :text
1415
# parent_type :string
1516
# skip_clickwrap_agreement :boolean default(FALSE), not null
1617
# slug :string
@@ -46,6 +47,7 @@ class Page < ApplicationRecord
4647

4748
before_commit :set_performance_metadata, on: %i[create update]
4849
after_commit :touch_parent
50+
after_commit :enqueue_cache_og_description_job, on: %i[create update]
4951

5052
multisearchable(
5153
against: %i[name content_for_search],
@@ -84,6 +86,17 @@ def content_for_search
8486
""
8587
end
8688

89+
def compute_og_description
90+
convention = parent.is_a?(Convention) ? parent : nil
91+
strip_tags(CmsContentFinder.new(convention).cms_rendering_context.render_page_content(self))
92+
.gsub(/\s+/, " ")
93+
.strip
94+
.truncate(160)
95+
rescue StandardError => e
96+
Rails.logger.debug e
97+
""
98+
end
99+
87100
private
88101

89102
def set_performance_metadata
@@ -95,4 +108,8 @@ def set_performance_metadata
95108
def touch_parent
96109
parent.touch if parent&.persisted?
97110
end
111+
112+
def enqueue_cache_og_description_job
113+
CachePageOgDescriptionJob.perform_later(self)
114+
end
98115
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class AddOgDescriptionToPages < ActiveRecord::Migration[8.1]
4+
def change
5+
add_column :pages, :cached_og_description, :text
6+
reversible { |dir| dir.up { Page.find_each { |page| CachePageOgDescriptionJob.perform_later(page) } } }
7+
end
8+
end

db/structure.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,8 @@ CREATE TABLE public.pages (
21362136
invariant boolean DEFAULT false NOT NULL,
21372137
skip_clickwrap_agreement boolean DEFAULT false NOT NULL,
21382138
hidden_from_search boolean DEFAULT false NOT NULL,
2139-
meta_description text
2139+
meta_description text,
2140+
cached_og_description text
21402141
);
21412142

21422143

@@ -6160,6 +6161,7 @@ ALTER TABLE ONLY public.cms_files_pages
61606161
SET search_path TO "$user", public;
61616162

61626163
INSERT INTO "schema_migrations" (version) VALUES
6164+
('20260601000000'),
61636165
('20260524175914'),
61646166
('20260519000000'),
61656167
('20260409003354'),

test/factories/pages.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
# rubocop:disable Layout/LineLength, Lint/RedundantCopDisableDirective
23
# == Schema Information
34
#
@@ -10,6 +11,7 @@
1011
# invariant :boolean default(FALSE), not null
1112
# meta_description :text
1213
# name :text
14+
# og_description :text
1315
# parent_type :string
1416
# skip_clickwrap_agreement :boolean default(FALSE), not null
1517
# slug :string

test/models/page_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
# rubocop:disable Layout/LineLength, Lint/RedundantCopDisableDirective
23
# == Schema Information
34
#
@@ -10,6 +11,7 @@
1011
# invariant :boolean default(FALSE), not null
1112
# meta_description :text
1213
# name :text
14+
# og_description :text
1315
# parent_type :string
1416
# skip_clickwrap_agreement :boolean default(FALSE), not null
1517
# slug :string

0 commit comments

Comments
 (0)