Skip to content

Add pagination to People List #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -290,6 +302,7 @@ DEPENDENCIES
faker
jbuilder
jsbundling-rails
kaminari
pry-rails
puma (~> 5.0)
rails (~> 7.0.1)
Expand Down
14 changes: 7 additions & 7 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
class PeopleController < ApplicationController

def index
@people = Person.all
page = params[:page].to_i
page = 1 if page == 0
@people = People::List.new(page: page).execute
end

def new
@person = Person.new
end

def create
if Person.create(person_attributes)
@person = Person.new(person_attributes)
if @person.save
redirect_to people_path, notice: 'Successfully created entry'
else
render :create, alert: 'Unsuccessfully created entry'
render :new, alert: 'Unsuccessfully created entry', status: :unprocessable_entity
end
end

private

def person_attributes
params.require(:person).permit(:name, :email, :phone)
params.require(:person).permit(:name, :email, :phone_number)
end

end

10 changes: 10 additions & 0 deletions app/models/people/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class People::List
private attr_accessor :page
def initialize(page:)
self.page = page
end

def execute
Person.order(:id).page(page).per(10)
end
end
4 changes: 4 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
class Person < ApplicationRecord

belongs_to :company, optional: true

validates :name, presence: true
validates :phone_number, presence: true
validates :email, presence: true
end
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'
3 changes: 2 additions & 1 deletion app/views/people/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ table.table
tr
th[scope="row"]= person&.id
td= person.try(:name)
td= person.try(:phone)
td= person.try(:phone_number)
td= person.try(:email)
td= person.try(:company).try(:name)
= paginate @people



14 changes: 14 additions & 0 deletions config/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

Kaminari.configure do |config|
# config.default_per_page = 25
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
# config.max_pages = nil
# config.params_on_first_page = false
end
4 changes: 4 additions & 0 deletions spec/controllers/people_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
it 'has status found' do
expect(post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo' } }).to have_http_status(:found)
end

it 'doesn\'t create a user when is missing phone_number' do
expect(post :create, params: { person: { name: 'foo', email: 'foo' } }).to have_http_status(:unprocessable_entity)
end
end
end
5 changes: 5 additions & 0 deletions spec/factories/companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :company do
name { "Company default" }
end
end
7 changes: 7 additions & 0 deletions spec/factories/people.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :person do
name { "John Doe" }
email { "john@doe.com" }
phone_number { "9770607060" }
end
end
20 changes: 17 additions & 3 deletions spec/features/people/index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'rails_helper'

RSpec.describe 'Listing people', type: :feature do
before do
Person.create(
before do
FactoryBot.create(
:person,
name: 'Foo Bar',
phone_number: 'Biz',
email: 'Baz'
Expand All @@ -18,10 +19,23 @@
end
end

scenario 'with more users than allowed in view' do
people_list = FactoryBot.create_list(:person, 11)
last_record = people_list.last
last_record.update(name: 'Jane Doe', email: 'jane@doe.com')

visit people_path

expect(page).to_not have_content('Jane Doe')

click_on 'Next'

expect(page).to have_content('Jane Doe')
end

scenario 'New person', type: :feature do
visit new_person_path

expect(page).to have_field :person_name
end

end
14 changes: 13 additions & 1 deletion spec/views/people/index.html.slim_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
require "rails_helper"

describe "people/index.html.slim" do
it "Displays the users"
let(:company) { FactoryBot.create(:company, name: "Default Company") }
let!(:first_person) { FactoryBot.create(:person, phone_number: "Foo", company: company) }
let!(:second_person) { FactoryBot.create(:person, name: "Jane Doe", email: "jane@doe.com.br", phone_number: "Bar", company: company) }
it "Displays the users" do
assign(:people, People::List.new(page: 1).execute)
render

expect(rendered).to match(/John/) # name (John)
expect(rendered).to match(/Foo/) # phone number
expect(rendered).to match(/Jane/) # name (Jane)
expect(rendered).to match(/Bar/) # phone number
expect(rendered).to match(/Default Company/) # company name
end
end