paginate-simple

The idea for building this gem came up when i was looking for a way to paginate my posts in a simple sinatra blog that i was building. I needed some kind of pagination that was not coupled with any of the existing ORM’s. The abstraction of a paginator should be simple.

It should be able to paginate some collection of data by providing a

  1. total number of items

  2. the page

  3. and number of items per page

It should be able to answer on the following

  1. How many items should i skip (offset)

  2. How many items should i get (per_page)

  3. Is there a next page and if there is give me the next page.

  4. Is there a previous page and if there is give me the previous page.

  5. It should be able to give you the first page and the last page.

  6. It should be able to provide you a way to iterate over the existing pages.

In the previous version of the gem i have created a PaginateSimple module to work as a singleton but in this version since it should be a pagination logic i have changed that behavior in order to provide a way to extend and change the functionality of the module and its usage.

Usage: In order to create a singleton pagination object we need to create a class Paginator that will extend this module and change some of its built in logic like this:

class Paginator
  extend PaginateSimple

  def self.current_page=(page)
    super page.to_i # allow strings to be passed like "1" as well
  end
end

If we need to have more than one Paginator we will include the module instead and then instantiate the object as needed.

class Paginator
  include PaginateSimple

  def current_page=(page)
    super page.to_i # allow strings to be passed like "1" as well
  end
end

@paginator = Paginator.new
@paginator.config :total => 100 , :page => 1, :per_page => 10

@another_paginator = Paginator.new 
@another_paginator.config :total => 100 , :page => 1, :per_page => 3

By default no helper for the paginator exists. But to write one is quite simple. Here is the example of a paginator helper that i use in my blog.

require 'sinatra/base'

module Sinatra
  module PaginationHelper
    def paginate(resource)
      data = []
      data << '<div class="pagination">'
        data << "<a href =\"/#{resource}/?page=#{Paginator.previous_page}\">Prev</a>" if Paginator.has_previous_page?
        data << ' | ' if Paginator.has_previous_page? and Paginator.has_next_page?
        data << "<a href =\"/#{resource}/?page=#{Paginator.next_page}\">Next</a>" if Paginator.has_next_page?
      data << '</div>'
      data.join(' ')
    end
  end

  helpers PaginationHelper
end

and i use it like this in my haml file

= paginate 'posts'

Contributing to paginate-simple

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 filip. See LICENSE.txt for further details.