Skip to content

Commit

Permalink
Merge pull request #792 from yez/main
Browse files Browse the repository at this point in the history
Add Pagination for People
  • Loading branch information
solebared authored Oct 23, 2020
2 parents 4ebd849 + 90a4624 commit 7be6eb6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
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 %>
</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

0 comments on commit 7be6eb6

Please sign in to comment.