Skip to content
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

Add Pagination for People #792

Merged
merged 5 commits into from
Oct 23, 2020
Merged
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 @@ -27,6 +27,7 @@ gem 'devise'
gem 'faker' # need this in prod for demo seeds to work
gem 'jbuilder', '~> 2.7'
gem 'mobility', '~> 0.8.9'
gem 'pagy'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 5.0'
gem 'rack-timeout'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ GEM
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
orm_adapter (0.5.0)
pagy (3.8.3)
pg (1.2.3)
pry (0.13.0)
coderay (~> 1.1)
Expand Down Expand Up @@ -276,6 +277,7 @@ DEPENDENCIES
jbuilder (~> 2.7)
listen (>= 3.0.5, < 3.2)
mobility (~> 0.8.9)
pagy
pg (>= 0.18, < 2.0)
pry
pry-byebug
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/concerns/pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Pagination
extend ActiveSupport::Concern

include Pagy::Frontend
include Pagy::Backend

included do
helper_method :pagy_bulma_nav
end
end
8 changes: 6 additions & 2 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]

include Pagination

def index
@people = Person.includes(:user, :preferred_contact_method, :location, :service_area).
references(:user, :preferred_contact_method, :location, :service_area)
@pagy, @people = pagy(
Person.includes(:user, :preferred_contact_method, :location, :service_area).
references(:user, :preferred_contact_method, :location, :service_area)
)
end

def show
Expand Down
9 changes: 9 additions & 0 deletions app/views/people/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@
<td><%= edit_button(person.user, "", "fa fa-user-edit") if person.user_id %></td>
</tr>
<% end %>
<% if @pagy.pages > 1 %>
<tr>
<td></td>
<td></td>
<td colspan=2>
<%= pagy_bulma_nav(@pagy).html_safe %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, looks more bulma-ish :)

</td>
</tr>
<% end %>
</tbody>
</table>
1 change: 1 addition & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'pagy/extras/bulma'
22 changes: 22 additions & 0 deletions spec/requests/people_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rails_helper'

describe '/people', type: :request do
describe 'GET /index' do
let!(:people) { create_list(:person, 40) }

before do
sign_in create(:user)
end

it 'renders a successful response' do
get people_url
expect(response).to be_successful
end

it 'paginates results' do
get people_url
people_links = response.body.scan %r{href="/people/\d+/.+"}
expect(people_links.size).to eq Pagy::VARS[:items]
end
end
end