Skip to content

Commit deca0d6

Browse files
committed
Add pagination for People index using Kaminari
1 parent b0b6bdd commit deca0d6

12 files changed

+51
-7
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ gem "jbuilder"
1414
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
1515
gem 'slim-rails'
1616
gem "jsbundling-rails"
17+
gem "kaminari"
1718

1819
group :development, :test do
1920
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

Gemfile.lock

+13
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ GEM
115115
activesupport (>= 5.0.0)
116116
jsbundling-rails (1.3.1)
117117
railties (>= 6.0.0)
118+
kaminari (1.2.2)
119+
activesupport (>= 4.1.0)
120+
kaminari-actionview (= 1.2.2)
121+
kaminari-activerecord (= 1.2.2)
122+
kaminari-core (= 1.2.2)
123+
kaminari-actionview (1.2.2)
124+
actionview
125+
kaminari-core (= 1.2.2)
126+
kaminari-activerecord (1.2.2)
127+
activerecord
128+
kaminari-core (= 1.2.2)
129+
kaminari-core (1.2.2)
118130
logger (1.6.1)
119131
loofah (2.23.1)
120132
crass (~> 1.0.2)
@@ -290,6 +302,7 @@ DEPENDENCIES
290302
faker
291303
jbuilder
292304
jsbundling-rails
305+
kaminari
293306
pry-rails
294307
puma (~> 5.0)
295308
rails (~> 7.0.1)

app/controllers/people_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class PeopleController < ApplicationController
22

33
def index
4-
@people = Person.includes(:company)
4+
@people = Person.includes(:company).page(params[:page])
55
end
66

77
def new
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
li.page-item
2+
= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, remote: remote, class: 'page-link'

app/views/kaminari/_gap.html.slim

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
li.page-item.disabled
2+
= link_to raw(t 'views.pagination.truncate'), '#', class: 'page-link'
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
li.page-item
2+
= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, remote: remote, class: 'page-link'
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
li.page-item
2+
= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, rel: 'next', remote: remote, class: 'page-link'

app/views/kaminari/_page.html.slim

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- if page.current?
2+
li.page-item.active
3+
= content_tag :a, page, data: { remote: remote }, rel: page.rel, class: 'page-link'
4+
- else
5+
li.page-item
6+
= link_to page, url, remote: remote, rel: page.rel, class: 'page-link'
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
= paginator.render do
2+
nav
3+
ul.pagination
4+
== first_page_tag unless current_page.first?
5+
== prev_page_tag unless current_page.first?
6+
- each_page do |page|
7+
- if page.left_outer? || page.right_outer? || page.inside_window?
8+
== page_tag page
9+
- elsif !page.was_truncated?
10+
== gap_tag
11+
== next_page_tag unless current_page.last?
12+
== last_page_tag unless current_page.last?
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
li.page-item
2+
= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, rel: 'prev', remote: remote, class: 'page-link'

app/views/people/index.html.slim

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ table.table
1212
- @people.each do |person|
1313
tr
1414
th[scope="row"]= person&.id
15-
td= person.try(:name)
16-
td= person.try(:phone)
17-
td= person.try(:email)
18-
td= person.try(:company).try(:name)
19-
20-
15+
td= person&.name
16+
td= person&.phone_number
17+
td= person&.email
18+
td= person&.company&.name
2119

20+
= paginate @people
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Kaminari.configure do |config|
2+
config.default_per_page = 10
3+
end

0 commit comments

Comments
 (0)