Skip to content

Commit ba9763a

Browse files
committed
Refactor away from Current.page
I used Current.page as a quick-and-dirty workaround to be able to render inline Polls in Sitepress articles from different contexts. Local variables can be used instead and that’s what I’ve done here.
1 parent fe10b8d commit ba9763a

File tree

10 files changed

+35
-20
lines changed

10 files changed

+35
-20
lines changed

app/content/layouts/article.html.erb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
<% page = @page %>
21
<% set_meta_tags(
3-
title: page.title,
4-
description: page.description,
2+
title: current_page.title,
3+
description: current_page.description,
54
keywords: %w[rails hotwire],
65
og: {
76
type: "article",
8-
url: page.request_path,
9-
image: page.meta_image && asset_url(page.meta_image)
7+
url: current_page.request_path,
8+
image: current_page.meta_image && asset_url(current_page.meta_image)
109
},
1110
twitter: {
1211
card: "summary"
@@ -16,11 +15,11 @@
1615
<%= render_layout "application" do %>
1716
<article itemscope itemtype="http://schema.org/Article" class="mb-3xl">
1817
<%= render Pages::Header::Container.new do |c| %>
19-
<%= c.title { page.title } %>
20-
<%= c.description { page.description } if page.description %>
21-
<%= render Pages::Timestamp.new published_on: page.published_on, updated_on: page.revised_on, class: "text-small block" %>
18+
<%= c.title { current_page.title } %>
19+
<%= c.description { current_page.description } if current_page.description %>
20+
<%= render Pages::Timestamp.new published_on: current_page.published_on, updated_on: current_page.revised_on, class: "text-small block" %>
2221
<div class="text-small">
23-
<% topics = @page.topics.approved.pluck(:slug) %>
22+
<% topics = current_page.topics.approved.pluck(:slug) %>
2423
<% if topics.present? %>
2524
<%= render Pages::Topics.new(topics: topics) %>
2625
<% end %>
@@ -29,8 +28,8 @@
2928
<div class="article-content container" itemprop="articleBody">
3029
<%= yield %>
3130

32-
<% cache [@page, :related_pages] do %>
33-
<% related_pages = @page.related_pages.published.limit(3) %>
31+
<% cache [@current_page, :related_pages] do %>
32+
<% related_pages = @current_page.related_pages.published.limit(3) %>
3433
<% if related_pages.present? %>
3534
<h3>More articles to enjoy</h3>
3635
<ul class="list-none">
@@ -54,6 +53,6 @@
5453
</section>
5554
<% end %>
5655

57-
<% if page.enable_twitter_widgets %>
56+
<% if current_page.enable_twitter_widgets %>
5857
<%= javascript_include_tag "https://platform.twitter.com/widgets.js", charset: "utf-8", async: true %>
5958
<% end %>

app/content/pages/articles/what-you-need-to-know-about-sqlite.html.mdrb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It’s basically this: computers have gotten faster, and Rails has finally figur
1717

1818
With the recent release of Rails 8, it’s easier than ever to choose SQLite as your primary database for production. The question is, _should you?_
1919

20-
<%= render Share::Polls::LazyPagePoll.new(Current.page, "SQLite on Rails Live Poll", "How likely are you to use SQLite in your next Rails app?" => ["Definitely! 🎸", "Strongly considering 💪", "On the fence ⚖️", "Maybe, but not likely 👻", "No way 🚫"]) %>
20+
<%= render Share::Polls::LazyPagePoll.new(@current_page, "SQLite on Rails Live Poll", "How likely are you to use SQLite in your next Rails app?" => ["Definitely! 🎸", "Strongly considering 💪", "On the fence ⚖️", "Maybe, but not likely 👻", "No way 🚫"]) %>
2121

2222
## SQLite3::Database.open
2323

app/content/pages/articles/zee-kitchen-sink-permanent-draft.html.mdrb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ tags:
1313

1414
## Inline Poll Demo
1515

16-
<%= render Share::Polls::LazyPagePoll.new(Current.page, "SQLite on Rails Poll", "How likely are you to use SQLite in your Rails app?" => ["100%", "Strongly considering", "On the fence", "Not very, but open minded", "No way"]) if Current.page %>
16+
<%= render Share::Polls::LazyPagePoll.new(current_page, "SQLite on Rails Poll", "How likely are you to use SQLite in your Rails app?" => ["100%", "Strongly considering", "On the fence", "Not very, but open minded", "No way"]) if current_page %>

app/content/pages/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ title: Joy of Rails
99
Experience the<br><span class="text-gradient">Joy of Ruby on Rails</span>
1010
<% end %>
1111
<p>
12-
Interactive lessons to learn the framework that will make you fall in love with building web apps all over again 💜
12+
Interactive lessons in the framework that will make you fall in love with building web apps all over again 💜
1313
</p>
1414
</div>
1515
<% end %>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module CurrentPage
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
helper_method :current_page
6+
end
7+
8+
def current_page
9+
@current_page
10+
end
11+
end

app/controllers/feed_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class FeedController < ApplicationController
2+
include CurrentPage
3+
24
def index
35
@articles = Page.published.order(published_at: :desc).limit(50)
46

app/controllers/site_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
class SiteController < Sitepress::SiteController
2+
include CurrentPage
3+
4+
before_action do
5+
@current_page = Page.find_or_initialize_by(request_path: request.path)
6+
end
7+
28
# This is the default controller for Sitepress. It is used to render the pages of the site.
39
# We prefer not to override this method but instead use the hooks provided by Sitepress below.
410
#
511
# render_resource is a helper method provided by Sitepress to render the current resource.
612
# requested_resource is the Sitepress::Resource object that represents the current page.
713
#
8-
def show
9-
Current.page = @page = Page.find_or_initialize_by(request_path: request.path)
1014

15+
def show
1116
super
1217
end
1318

app/models/current.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ class Current < ActiveSupport::CurrentAttributes
22
attribute :admin_user
33
attribute :user
44
attribute :color_scheme
5-
attribute :page
65
end

app/views/components/atom/entry_content.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def render_template_html
5454
layout: false,
5555
content_type: "application/atom+xml",
5656
assigns: {
57+
current_page: article,
5758
format: :atom
5859
}
5960
)

app/views/feed/index.atom.builder

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ atom_feed do |feed|
44

55
@articles.take(50).each do |article|
66
next if article.resource_missing?
7-
Current.page = article
8-
97
feed.entry(
108
article,
119
id: "tag:#{request.host},2005:article/#{article.atom_feed_id}",

0 commit comments

Comments
 (0)