Skip to content

Commit ed2eb15

Browse files
committed
feat: add pagination to tags #195
1 parent 3be3519 commit ed2eb15

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

app/controllers/tags_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
class TagsController < ApplicationController
3+
include Pagy::Backend
4+
35
before_action :set_tag, only: [ :show, :edit, :update, :destroy ]
46

57
def index
6-
@tags = Tag.includes(:cognates, :reverse_cognates).references(:tag)
8+
@pagy, @tags = pagy(Tag.includes(:cognates, :reverse_cognates).references(:tag))
79
end
810

911
def new

app/views/tags/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
</div>
2929
</div>
3030
</div>
31+
<div class="card-footer d-flex justify-content-end">
32+
<%== pagy_bootstrap_nav(@pagy) %>
33+
</div>
3134
</div>
3235
</div>
3336
</div>

spec/factories/tags.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#
1515
FactoryBot.define do
1616
factory :tag do
17-
name { Faker::ProgrammingLanguage.name }
17+
sequence(:name) do |n|
18+
Faker::ProgrammingLanguage.name + n.to_s
19+
end
1820

1921
trait :english do
2022
after(:build) do |tag|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "tags/index", type: :view do
4+
context "when there are no tags" do
5+
before(:each) do
6+
assign(:pagy, Pagy.new(count: 0))
7+
assign(:tags, [])
8+
end
9+
10+
it "renders no tags" do
11+
render
12+
assert_select "table>tbody>tr", count: 0
13+
end
14+
end
15+
16+
context "when there are tags but only one page" do
17+
before(:each) do
18+
assign(:pagy, Pagy.new(count: 1))
19+
assign(:tags, [ create(:tag, name: "Tag 1") ])
20+
end
21+
22+
it "renders a list of tags" do
23+
render
24+
assert_select "table>tbody>tr", count: 1
25+
end
26+
27+
it "renders pagination nav without page links" do
28+
render
29+
assert_dom "nav.pagy-bootstrap .page-link", text: "1", count: 1
30+
end
31+
end
32+
33+
context "when there are multiple pages of tags" do
34+
before(:each) do
35+
# Simulate being on page 2 with 10 items per page and 25 total items
36+
pagy = Pagy.new(count: 25, page: 2, items: 10)
37+
assign(:pagy, pagy)
38+
assign(:tags, create_list(:tag, 10))
39+
end
40+
41+
it "renders the current page of tags" do
42+
render
43+
assert_select "table>tbody>tr", count: 10
44+
end
45+
46+
it "renders pagination with multiple pages" do
47+
render
48+
assert_select "nav.pagy-bootstrap .page-item", minimum: 2
49+
assert_dom "nav.pagy-bootstrap .page-item.active", text: "2", count: 1
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)