Neat Pages [![Build Status](https://secure.travis-ci.org/alchimikweb/neat-pages.png?branch=master)](http://travis-ci.org/alchimikweb/neat-pages) [![Code Climate](https://codeclimate.com/github/alchimikweb/neat-pages.png)](https://codeclimate.com/github/alchimikweb/neat-pages) [![Coverage Status](https://coveralls.io/repos/alchimikweb/neat-pages/badge.png)](https://coveralls.io/r/alchimikweb/neat-pages) =============== A simple pagination API to paginate Mongoid models. Install ------- ``` gem install neat-pages ``` or add the following line to Gemfile: ```ruby 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 ```ruby # app/controllers/products_controller.rb def index paginate @products = Product.all.paginate(pagination) end ``` ```erb <%# app/views/products/index.html.erb %> <%= render 'products', products: @products %> <%= neat_pages_navigation %> ``` ### Summon the power of AJAX pagination ```ruby # app/controllers/products_controller.rb def index paginate per_page: 50 # Default is 20 @products = Product.all.paginate(pagination) end ``` ```erb <%# 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. ```erb <%# 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 : ```ruby # 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. ```yml fr: neat_pages: next_page: Page suivante previous_page: Page précédente ``` Copyright --------- Copyright (c) 2013 De Marque inc. See LICENSE for further details.