How to use Pagy with Sinatra and Sequel

Pagy is a modern pagination gem for Ruby. It works out of box with Rails and ActiveRecord (check out Quick Start guide here).

It’s a good idea to use Pagy with Sinatra and Sequel ORM. We need to adjust some settings though, let’s see how to do it.


First add to the Gemfile pagy gem:

gem 'pagy'

app.rb:

require 'sinatra/base'
require 'pagy'
require 'pagy/extras/bootstrap'

class App < Sinatra::Base
  include Pagy::Backend

  helpers do
    include Pagy::Frontend
  end

  get "/posts" do
    # where Post is a Sequel model
    @pagy, @posts = pagy(Post)
    erb :'posts/index'
  end

  private

  def pagy_get_vars(collection, vars)
    {
      count: collection.count,
      page: params["page"],
      items: vars[:items] || 25
    }
  end
end

views/posts/index.erb (Also don’t forget to include views/layout.erb)

<div class="row">
  <div class="col-lg-12">
    <h1>All Posts</h1>

    <% @posts.each do |post| %>
      <h2><%= post.title %></h2>
      <p><%= post.description %></p>
    <% end %>

    <%= pagy_nav_bootstrap(@pagy) if @pagy.pages > 1 %>
  </div>
</div>

Where:

  • include Pagy::Backend backend Pagy methods included to the controller
  • Inside helpers do block we are including frontend pagy methods for views with extra bootstrap pagination helper.
  • def pagy_get_vars it’s a special pagy method to override default logic and tell pagy how to work with custom environment than Rails. Count: pagy uses collection.count(:all) to get total size of records of model (ActiveRecord). It didn’t work for Sequel and correct solution will be collection.count. Page: provide current page param.


That’s all, now run Sinatra app and go to http://localhost:4567/posts?page=2 and you’ll see nice pagination made by Pagy:

Comments at Medium