Skip to content

Commit fc6f1d6

Browse files
authored
Merge pull request #313 from joyofrails/feat/current-page-fix
Refactor away from Current.page
2 parents fe10b8d + 1587d3c commit fc6f1d6

File tree

17 files changed

+202
-99
lines changed

17 files changed

+202
-99
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/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/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"]) %>

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/javascript/test/fixtures/views/searches/combobox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
dialog:open@window->search-combobox#openInTarget
66
"><form data-controller="autosubmit-form" data-autosubmit-delay-value="300" data-autosubmit-minimum-length-value="3" data-turbo-frame="search" action="/search" accept-charset="UTF-8" method="post"><div class="flex items-center flex-row pl-2 col-gap-xs"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 1200 1200" class="w-[32px] fill-current text-theme">
77
<path d="m1135.2 986.4-254.4-226.8c124.8-181.2 105.6-430.8-55.199-591.6-181.2-182.4-476.4-182.4-657.6 0-182.4 181.2-182.4 476.4 0 658.8 160.8 160.8 410.4 178.8 591.6 55.199l226.8 253.2c38.398 43.199 105.6 45.602 146.4 3.6016l6-6c40.801-40.801 39.598-108-3.6016-146.4zm-422.4-273.6c-120 120-313.2 120-432 0-120-120-120-313.2 0-432 120-120 313.2-120 432 0 120 120 120 313.2 0 432z"></path>
8-
</svg><label for="query" class="sr-only">Query</label> <input value="Rails" autofocus="autofocus" role="combobox" aria-expanded="false" aria-autocomplete="none" aria-controls="search-listbox" aria-owns="search-listbox" aria-haspopup="listbox" aria-activedescendant="" id="search-combobox" data-action="
8+
</svg><label for="search-combobox" class="sr-only">Query</label> <input value="Rails" autofocus="autofocus" role="combobox" aria-expanded="false" aria-autocomplete="none" aria-controls="search-listbox" aria-owns="search-listbox" aria-haspopup="listbox" aria-activedescendant="" id="search-combobox" data-action="
99
focus-&gt;combobox#tryOpen
1010
input-&gt;autosubmit-form#submit
1111
" placeholder="Search Joy of Rails" class="w-full step-1" type="search" name="query" /></div><input value="search" autocomplete="off" type="hidden" name="name" id="name" /></form><ul id="search-listbox" role="listbox" class="grid" data-controller="search-listbox"><li aria-label="Here’the thing" role="option" id="search-option_search_result_articles-heres-the-thing" class="rounded"><a href="/articles/heres-the-thing"><div>Here’the thing</div><div>Ruby is a programming language</div></a></li><li aria-label="Introducing Joy of Rails" role="option" id="search-option_search_result_articles-joy-of-rails" class="rounded"><a href="/articles/joy-of-rails"><div>Introducing Joy of Rails</div><div>Rails is a web application framework</div></a></li></ul></div>

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

0 commit comments

Comments
 (0)