Skip to content

Commit

Permalink
Add pagination for People index using Kaminari
Browse files Browse the repository at this point in the history
  • Loading branch information
esteres committed Jan 21, 2025
1 parent 3f6fff3 commit cdc7b79
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gem "jbuilder"
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
gem 'slim-rails'
gem "jsbundling-rails"
gem "kaminari"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ GEM
activesupport (>= 5.0.0)
jsbundling-rails (1.3.1)
railties (>= 6.0.0)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
logger (1.6.1)
loofah (2.23.1)
crass (~> 1.0.2)
Expand Down Expand Up @@ -294,6 +306,7 @@ DEPENDENCIES
faker
jbuilder
jsbundling-rails
kaminari
pry-rails
puma (~> 5.0)
rails (~> 7.0.1)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class PeopleController < ApplicationController

def index
@people = Person.includes(:company)
@people = Person.includes(:company).page(params[:page])
end

def new
Expand Down
2 changes: 2 additions & 0 deletions app/views/kaminari/_first_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
li.page-item
= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, remote: remote, class: 'page-link'
2 changes: 2 additions & 0 deletions app/views/kaminari/_gap.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
li.page-item.disabled
= link_to raw(t 'views.pagination.truncate'), '#', class: 'page-link'
2 changes: 2 additions & 0 deletions app/views/kaminari/_last_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
li.page-item
= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, remote: remote, class: 'page-link'
2 changes: 2 additions & 0 deletions app/views/kaminari/_next_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
li.page-item
= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, rel: 'next', remote: remote, class: 'page-link'
6 changes: 6 additions & 0 deletions app/views/kaminari/_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- if page.current?
li.page-item.active
= content_tag :a, page, data: { remote: remote }, rel: page.rel, class: 'page-link'
- else
li.page-item
= link_to page, url, remote: remote, rel: page.rel, class: 'page-link'
12 changes: 12 additions & 0 deletions app/views/kaminari/_paginator.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
= paginator.render do
nav
ul.pagination
== first_page_tag unless current_page.first?
== prev_page_tag unless current_page.first?
- each_page do |page|
- if page.left_outer? || page.right_outer? || page.inside_window?
== page_tag page
- elsif !page.was_truncated?
== gap_tag
== next_page_tag unless current_page.last?
== last_page_tag unless current_page.last?
2 changes: 2 additions & 0 deletions app/views/kaminari/_prev_page.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
li.page-item
= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, rel: 'prev', remote: remote, class: 'page-link'
11 changes: 5 additions & 6 deletions app/views/people/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ table.table
- @people.each do |person|
tr
th[scope="row"]= person&.id
td= person.try(:name)
td= person.try(:phone_number)
td= person.try(:email)
td= person.try(:company).try(:name)


td= person&.name
td= person&.phone_number
td= person&.email
td= person&.company&.name

= paginate @people
3 changes: 3 additions & 0 deletions config/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Kaminari.configure do |config|
config.default_per_page = 10
end

0 comments on commit cdc7b79

Please sign in to comment.