Skip to content

alchimikweb/neat-pages

 
 

Repository files navigation

Neat Pages Build Status Code Climate Coverage Status

A simple pagination API to paginate ActiveRecord and 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.

This will wrap the content of your partial in a div by default. If your items are in a table you can pass the parameter wrapper: 'tbody'. So the call would look like that :

  <%# app/views/products/index.html.erb %>
  <table>
    <%= neat_pages_ajax_items 'products', products: @products, wrapper: 'tbody' %>
  </table>
  <%= neat_pages_navigation %>

neat_pages_more_button

Generate a more button that will lazy load the records instead of showing a navigation like (< 1 2 3 4 >).

neat_pages_navigation

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

neat_pages_link_relation_tags

Put this line in the head tag to generate the link relation tags

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

neat_pages:over

Triggered on 'body' when there's no more record to load. Use with the more button.

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
      more: Plus de produits
      over: Aucun autre produit de disponible

Copyright

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

About

A simple pagination API to paginate Mongoid Models or any other stuff...

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 85.5%
  • CoffeeScript 11.4%
  • CSS 2.5%
  • Other 0.6%