Skip to content

Commit 321749c

Browse files
committed
Add basic Topic index and show page for articles
1 parent 0a85d1f commit 321749c

File tree

8 files changed

+107
-3
lines changed

8 files changed

+107
-3
lines changed

app/content/models/sitepress_article.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ class SitepressArticle < Sitepress::Model
55
collection glob: "articles/*.html*"
66
data :title, :published, :updated, :summary, :description, :tags, :image
77

8-
delegate :mime_type, :handler, to: :page
8+
delegate :resource_path, :mime_type, :handler, to: :page
99

1010
def self.published(params = {})
11-
all
11+
take_published(all)
12+
end
13+
14+
def self.take_published(articles, params = {})
15+
articles
1216
.filter { |article| article.published?(preview: params[:preview]) }
13-
.sort { |a, b| b.published_on <=> a.published_on } # DESC order
17+
.sort { |a, b| b.published_on <=> a.published_on }
1418
end
1519

1620
def self.draft

app/controllers/topics_controller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class TopicsController < ApplicationController
2+
def index
3+
@topics = Topic.approved.with_pages.order(name: :asc)
4+
@topics = @topics.where("lower(slug) LIKE ?", "#{params[:letter].downcase}%") if params[:letter].present?
5+
end
6+
7+
def show
8+
@topic = Topic.find_by!(slug: params[:slug])
9+
end
10+
end

app/javascript/css/utilities/custom.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,11 @@
119119
.step-2 {
120120
font-size: var(--step-2);
121121
}
122+
123+
.text-subtle {
124+
color: var(--joy-text-subtle);
125+
}
126+
127+
.text-faint {
128+
color: var(--joy-text-faint);
129+
}

app/models/page.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class Page < ApplicationRecord
2121
has_many :topics, through: :page_topics
2222
has_many :approved_topics, -> { approved }, through: :page_topics, source: :topic, inverse_of: :pages
2323

24+
def self.as_published_articles
25+
SitepressArticle.take_published(all.map { |page| SitepressArticle.new(page.resource) })
26+
end
27+
28+
def sitepress_article
29+
SitepressArticle.new(resource)
30+
end
31+
2432
def resource = Sitepress.site.get(request_path)
2533

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

app/views/topics/index.html.erb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
<%= render Pages::Header.new(title: "Topics") %>
3+
<div class="section-content container py-gap mb-3xl">
4+
<% if @topics.blank? %>
5+
<div class="only:inherit grid grid-row-mid">
6+
<h2>No topics have been created yet!</h2>
7+
<div>
8+
<p>Coming soon ;)</p>
9+
</div>
10+
</div>
11+
<% else %>
12+
<% @topics.each do |topic| %>
13+
<%= tag.div id: dom_id(topic) do %>
14+
<h3>
15+
<%= link_to topic.name, topic_path(topic) %>
16+
<span class="text-faint">(<%= topic.pages_count %>)</span>
17+
</h3>
18+
<% end %>
19+
<% end %>
20+
<% end %>
21+
</div>

app/views/topics/show.html.erb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
<%= render Pages::Header.new(title: "Topic: #{@topic.name}") %>
3+
<div class="section-content container py-gap mb-3xl">
4+
<% if @topic.pages.blank? %>
5+
<div class="only:inherit grid grid-row-mid">
6+
<h2>No articles have been added for this topic yet!</h2>
7+
<div>
8+
<p>Coming soon. Maybe ;)</p>
9+
</div>
10+
</div>
11+
<% else %>
12+
<%= render Pages::List.new(@topic.pages.as_published_articles) %>
13+
<% end %>
14+
15+
<div><%= link_to "Back to Topics list", topics_path %></div>
16+
</div>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@
183183

184184
match "search", to: "searches#show", via: [:get, :post]
185185

186+
resources :topics, param: :slug, only: [:index, :show]
187+
186188
namespace :share do
187189
resources :snippets do
188190
resource :screenshot, only: [:new, :create, :show], controller: "snippet_screenshots"

spec/requests/topics_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Topics", type: :request do
4+
describe "GET /index" do
5+
it "returns http success" do
6+
article = Page.find_or_create_by!(request_path: "/articles/introducing-joy-of-rails")
7+
8+
topic_1 = FactoryBot.create(:topic, :approved)
9+
topic_2 = FactoryBot.create(:topic, :approved)
10+
11+
article.topics = [topic_1, topic_2]
12+
13+
get topics_path
14+
15+
expect(response).to have_http_status(:success)
16+
17+
expect(page).to have_content(topic_1.name)
18+
expect(page).to have_content(topic_2.name)
19+
end
20+
end
21+
22+
describe "GET /show" do
23+
it "returns http success" do
24+
article = Page.find_or_create_by!(request_path: "/articles/introducing-joy-of-rails")
25+
topic = FactoryBot.create(:topic, :approved)
26+
article.topics << topic
27+
28+
get topic_path(topic)
29+
30+
expect(response).to have_http_status(:success)
31+
expect(response.body).to include(topic.name)
32+
expect(response.body).to include("Introducing Joy of Rails")
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)