Skip to content

Commit 7e0a3eb

Browse files
committed
feat: add pagination to topics #195
1 parent ed2eb15 commit 7e0a3eb

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

app/controllers/topics_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class TopicsController < ApplicationController
2+
include Pagy::Backend
3+
24
before_action :set_topic, only: [ :show, :edit, :tags, :update, :destroy, :archive ]
35

46
def index
5-
@topics = scope.search_with_params(search_params)
7+
@pagy, @topics = pagy(scope.search_with_params(search_params))
68
@available_providers = other_available_providers
79
@languages = scope.map(&:language).uniq.sort_by(&:name)
810
end

app/views/topics/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<% end %>
3939
</div>
4040
</div>
41+
<div class="card-footer d-flex justify-content-end">
42+
<%== pagy_bootstrap_nav(@pagy) %>
43+
</div>
4144
</div>
4245
<% end %>
4346
</div>

spec/factories/topics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
factory :topic do
2626
association :provider
2727
association :language
28-
title { "topic title" }
28+
sequence(:title) { |n| "topic title #{10}" }
2929
description { "many topic details" }
3030
published_at { DateTime.new(2023, 1, 1) }
3131
state { 0 }
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "topics/index", type: :view do
4+
before(:each) do
5+
def view.search_params = {}
6+
def view.topics_title = "Topics"
7+
8+
# Allow the original render method to work normally
9+
allow(view).to receive(:render).and_call_original
10+
# But stub specifically the search partial render
11+
allow(view).to receive(:render).with("search", any_args).and_return("")
12+
13+
assign(:available_providers, [])
14+
end
15+
16+
context "when there are no topics" do
17+
before(:each) do
18+
assign(:pagy, Pagy.new(count: 0))
19+
assign(:topics, [])
20+
end
21+
22+
it "renders no topics" do
23+
render
24+
assert_select "table>tbody>tr", count: 0
25+
end
26+
end
27+
28+
context "when there are topics but only one page" do
29+
before(:each) do
30+
assign(:pagy, Pagy.new(count: 1))
31+
assign(:topics, [ create(:topic, title: "Topic 1") ])
32+
end
33+
34+
it "renders a list of topics" do
35+
render
36+
assert_select "table>tbody>tr", count: 1
37+
end
38+
39+
it "renders pagination nav with unique item" do
40+
render
41+
assert_dom "nav.pagy-bootstrap .page-item", text: "1", count: 1
42+
end
43+
end
44+
45+
context "when there are multiple pages of topics" do
46+
before(:each) do
47+
# Simulate being on page 2 with 10 items per page and 25 total items
48+
pagy = Pagy.new(count: 25, page: 2, items: 10)
49+
assign(:pagy, pagy)
50+
assign(:topics, create_list(:topic, 10))
51+
end
52+
53+
it "renders the current page of topics" do
54+
render
55+
assert_select "table>tbody>tr", count: 10
56+
end
57+
58+
it "renders pagination with multiple pages" do
59+
render
60+
assert_select "nav.pagy-bootstrap .page-item", minimum: 2
61+
assert_dom "nav.pagy-bootstrap .page-item.active", text: "2", count: 1
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)