Bootstrap Pager

Gem Version Build Status Coverage Status

Bootstrap Pager is an updated version of the Kaminari pagination gem, which is a Scope & Engine based, clean, powerful, customizable and sophisticated paginator for modern web app frameworks and ORMs

New Features added in Bootstrap Pager

Features

Clean

Does not globally pollute Array, Hash, Object or AR::Base.

Easy to use

Just bundle the gem, then your models are ready to be paginated. No configuration required. Don't have to define anything in your models or helpers.

Simple scope-based API

Everything is method chainable with less “Hasheritis”. You know, that's the Rails 3 way. No special collection class or anything for the paginated values, instead using a general AR::Relation instance. So, of course you can chain any other conditions before or after the paginator scope.

Customizable engine-based I18n-aware helper

As the whole pagination helper is basically just a collection of links and non-links, Bootstrap Pager renders each of them through its own partial template inside the Engine. So, you can easily modify their behaviour, style or whatever by overriding partial templates.

ORM & template engine agnostic

Bootstrap Pager supports multiple ORMs (ActiveRecord, Mongoid, MongoMapper) multiple web frameworks (Rails, Sinatra), and multiple template engines (ERB, Haml).

Modern

The pagination helper outputs the HTML5 <nav> tag by default. Plus, the helper supports Rails 3 unobtrusive Ajax.

Engine Namespace

By passing in the `:engine_namespace` option, you can get the pager to use a different helper for your paths.

Supported versions

Install

Put this line in your Gemfile:

gem 'bootstrap_pager'

Then bundle install:

% bundle install

Usage

Query Basics

General configuration options

You can configure the following default values by overriding these values using BootstrapPager.configure method.

default_per_page  # 25 by default
max_per_page      # nil by default
window            # 4 by default
outer_window      # 0 by default
left              # 0 by default
right             # 0 by default
page_method_name  # :page by default
param_name        # :page by default

There's a handy generator that generates the default configuration file into config/initializers directory. Run the following generator command, then edit the generated file.

% rails g bootstrap_pager:config

Configuring default per_page value for each model

Configuring max per_page value for each model

Controllers

Views

Helpers

I18n and labels

The default labels for 'first', 'last', 'previous', '…' and 'next' are stored in the I18n yaml inside the engine, and rendered through I18n API. You can switch the label value per I18n.locale for your internationalized application. Keys and the default values are the following. You can override them by adding to a YAML file in your Rails.root/config/locales directory.

en:
  views:
    pagination:
      first: "&laquo; First"
      last: "Last &raquo;"
      previous: "&lsaquo; Prev"
      next: "Next &rsaquo;"
      truncate: "..."

Customizing the pagination helper

BootstrapPager includes a handy template generator.

Paginating a generic Array object

BootstrapPager provides an Array wrapper class that adapts a generic Array object to the paginate view helper. However, the paginate helper doesn't automatically handle your Array object (this is intentional and by design). BootstrapPager::paginate_array method converts your Array object into a paginatable Array that accepts page method.

BootstrapPager.paginate_array(my_array_object).page(params[:page]).per(10)

You can specify the total_count value through options Hash. This would be helpful when handling an Array-ish object that has a different count value from actual count such as RSolr search result or when you need to generate a custom pagination. For example:

BootstrapPager.paginate_array([], total_count: 145).page(params[:page]).per(10)

Creating friendly URLs and caching

Because of the page parameter and Rails 3 routing, you can easily generate SEO and user-friendly URLs. For any resource you'd like to paginate, just add the following to your routes.rb:

resources :my_resources do
  get 'page/:page', :action => :index, :on => :collection
end

This will create URLs like /my_resources/page/33 instead of /my_resources?page=33. This is now a friendly URL, but it also has other added benefits…

Because the page parameter is now a URL segment, we can leverage on Rails page caching!

NOTE: In this example, I've pointed the route to my :index action. You may have defined a custom pagination action in your controller - you should point :action => :your_custom_action instead.

Sinatra/Padrino support

Since version 0.13.0, bootstrap_pager started to support Sinatra or Sinatra-based frameworks experimentally.

To use bootstrap_pager and its helpers with these frameworks,

require 'bootstrap_pager/sinatra'

or edit gemfile:

gem 'bootstrap_pager', :require => 'bootstrap_pager/sinatra'

More features are coming, and again, this is still experimental. Please let us know if you found anything wrong with the Sinatra support.

Infinite Scroll with Jquery

For infinite scroll technology, add jquery.infinitescroll to your application.js file.

//= require jquery.infinitescroll
//= require infinitescroll

Then, add class=“infinitescroll” to the div containing your pagination call, and add the class “infinitescroll-item” to the items within that div that represent individual objects.

<div id="posts" class="infinitescroll">
    <div id="post_5" class="infinitescroll-item">
        ... Post content here ...
    </div>
    ... Many more posts here ...
    <%= pagination @posts %>
</div>

Contributors

Copyright © 2013 Sour Cherry Web. See MIT-LICENSE for further details.