Skip to content

Commit 0fb0c7e

Browse files
committed
feat: add pagination to users #195
1 parent 7e0a3eb commit 0fb0c7e

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

app/controllers/users_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class UsersController < ApplicationController
2+
include Pagy::Backend
3+
24
before_action :redirect_contributors
35
before_action :set_user, only: %i[ edit update destroy ]
46

57
def index
6-
@users = User.all.search_with_params(user_search_params)
8+
@pagy, @users = pagy(User.all.search_with_params(user_search_params))
79
end
810

911
def new

app/views/users/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
<tbody>
7272
</table>
7373
</div>
74+
<div class="card-footer d-flex justify-content-end">
75+
<%== pagy_bootstrap_nav(@pagy) %>
76+
</div>
7477
</div>
7578
</section>
7679
</div>
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 "users/index", type: :view do
4+
context "when there are no users" do
5+
before(:each) do
6+
assign(:pagy, Pagy.new(count: 0))
7+
assign(:users, [])
8+
end
9+
10+
it "renders no users" do
11+
render
12+
assert_select "table>tbody>tr", count: 0
13+
end
14+
end
15+
16+
context "when there are users but only one page" do
17+
before(:each) do
18+
assign(:pagy, Pagy.new(count: 1))
19+
assign(:users, [ create(:user, email: "[email protected]") ])
20+
end
21+
22+
it "renders a list of users" 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 users" 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(:users, create_list(:user, 10))
39+
end
40+
41+
it "renders the current page of users" 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)