Skip to content

Commit 8074654

Browse files
committed
Add support for previewing publishable articles on home page
1 parent 67b6184 commit 8074654

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

app/content/models/article_page.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ class ArticlePage < Sitepress::Model
77

88
delegate :mime_type, :handler, to: :page
99

10-
def self.published
11-
all.filter(&:published?)
10+
def self.published(params = {})
11+
all
12+
.filter { |article| article.published?(preview: params[:preview]) }
1213
.sort { |a, b| b.published_on <=> a.published_on } # DESC order
1314
end
1415

1516
def self.draft
1617
all.filter(&:draft?)
1718
end
1819

19-
def published?
20-
published_on.presence && published_on <= Date.today
20+
# Consider an article published if it has a published date prior to today.
21+
# If preview is true, consider the article published regardless of the date.
22+
def published?(preview: false)
23+
published_on.presence && (preview || published_on <= Date.today)
2124
end
2225

2326
def draft? = !published?

app/content/pages/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Joy of Rails
44

55
<section style="min-height: 51vh">
66
<div class="main-content container py-gap lg:py-3xl">
7-
<% ArticlePage.published.take(4).zip(%w[left right].cycle).each do |(page, side)| %>
7+
<% ArticlePage.published(params).take(4).zip(%w[left right].cycle).each do |(page, side)| %>
88
<%= render Pages::Summary.from_page(page, side: side) %>
99
<% end %>
1010
</div>

spec/requests/site/articles_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
# Filter out index.html.erb from article pages before selecting first draft
77
let(:first_draft) { ArticlePage.draft.lazy.filter { |article| article.page.request_path != "/articles" }.first }
88

9+
describe "GET /" do
10+
it "lists recent published articles" do
11+
get "/"
12+
13+
expect(response).to have_http_status(:ok)
14+
end
15+
16+
it "supports preview flag" do
17+
get "/", params: {preview: true}
18+
19+
expect(response).to have_http_status(:ok)
20+
end
21+
end
22+
923
describe "GET /articles" do
1024
it "lists published articles" do
1125
get "/articles"

0 commit comments

Comments
 (0)