Skip to content

Commit deca0d6

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

File tree

12 files changed

+51
-7
lines changed

12 files changed

+51
-7
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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
Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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'
Lines changed: 2 additions & 0 deletions
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'
Lines changed: 2 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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'
Lines changed: 12 additions & 0 deletions
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?
Lines changed: 2 additions & 0 deletions
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'

0 commit comments

Comments
 (0)