Neat Pages Build Status Code Climate Coverage Status

A simple pagination API to paginate Mongoid models.

Install

gem install neat-pages

or add the following line to Gemfile:

gem 'neat-pages'

Setup

First you need to link the assets.

In your css manifest put : require neat_pages

In your javascript manifest put : require neat_pages

You only need to require the javascript if you use the AJAX functionality.

That's it.

Usage

Minimal integration

  # app/controllers/products_controller.rb

  def index
    paginate

    @products = Product.all.paginate(pagination)
  end
  <%# app/views/products/index.html.erb %>

  <%= render 'products', products: @products %>
  <%= neat_pages_navigation %>

Summon the power of AJAX pagination

  # app/controllers/products_controller.rb

  def index
    paginate per_page: 50 # Default is 20

    @products = Product.all.paginate(pagination)
  end
  <%# app/views/products/index.html.erb %>

  <%= neat_pages_ajax_items 'products', products: @products %>
  <%= neat_pages_navigation %>

Create the file app/views/products/index.neatpage.erb and place the following code in it.

  <%# app/views/products/index.neatpage.erb %>

  <% self.formats = ["html"] %>
  <%= render 'products', products: @products %>

Automatically paginate your Web Services

For every request that called the method paginate, the header's response will contain the following data :

  X-Total-Items=200;
  X-Total-Pages=20;
  X-Per-Page=10;
  X-Current-Page=3;

Out of Bound

If the page requested is out of bound, the controller method render_out_of_bound will be called. This method will render the text 'out_of_bound' with a status 404. If you want, you can override it like this :

  # app/controllers/application_controller.rb

  def render_out_of_bound
    render text: 'My custom code', status: 404
  end

Helper Methods

neat_pages_ajax_items(partial_path, options={})

Activate the ajax pagination. See the previous example.

neat_pages_navigation

Generate a page navigation. (ex: < 1 2 3 4 >)

neat_pages_status

Generate the current state of the pagination (ex: 30 to 40 / 200)

Javascript Events

neat_pages:update

Triggered on 'body' whenever a page has changed, from cache or from server. The event data is an object literal which holds those fields:

noPage The current page number

Locales

If you want to translate the text in the pagination helpers, just add the following keys in i18n.

  fr:
    neat_pages:
      next_page: Page suivante
      previous_page: Page précédente

Copyright (c) 2013 De Marque inc. See LICENSE for further details.