Project

paginate

0.01
No commit activity in last 3 years
No release in over 3 years
Paginate collections using SIZE+1 to determine if there is a next page. Includes ActiveRecord and ActionView support.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Paginate

Build Status CodeClimate Test Coverage Gem Gem

Paginate collections using SIZE+1 to determine if there is a next page. Includes ActiveRecord and ActionView support.

Install

gem install paginate

Usage

You can use Paginate with or without ActiveRecord. Let's try a simple array pagination. Imagine that you have something like this in your controller.

@things = Array.new(11) {|i| "Item #{i}"}

Then on your view:

<%= paginate @things %>

That's it! This is all you have to do! In this case we're using the default page size (which is 10). The url used on page links is taken from the current requested uri.

You can set default values globally:

Paginate.configure do |config|
  config.size = 20
  config.param_name = :p
end

More examples:

Post.paginate(1)                                # page 1 from Post model
Post.paginate(page: 1)                          # page 1 from Post model
Post.paginate(page: 1, size: 5)                 # page 1 from Post model with custom size
@user.things.paginate(:page => params[:page])   # paginate association
<%= paginate @things, size: 5 %>
<%= paginate @things, url: -> page { things_path(:page => page) } %>
<%= paginate @things, "/some/path" %>
<%= paginate @things, param_name: :p %>

To render the collection, you must use the render helper, providing the :paginate => true option. This is required cause we're always considering SIZE + 1, so if you use the regular +each+ or don't pass this option, you end up rendering one additional item.

<%= render @things, paginate: true %>
<%= render @things, paginate: true, size: 5 %>
<%= render "thing", collection: @things, paginate: true, size: 5 %>

I18n support

If you want to translate links, you have implement the following scopes:

en:
  paginate:
    next: "Older"
    previous: "Newer"
    page: "Page %{page}"
    more: "Load more"

Styling

If you want something like Twitter Search use this CSS:

.paginate { overflow: hidden; }
.paginate li { float: left; }
.paginate li.previous-page:after { content: "«"; padding: 0 5px; }
.paginate li.next-page:before { content: "»"; padding: 0 5px; }
.paginate .disabled { display: none; }

You can create new renderers. Paginate comes with two renderers.

  • Paginate::Renderer::List: is the default renderer. Uses a <ul> with previous and next page. Also displays the current page.
  • Paginate::Renderer::More: define a "Load more" page. This is useful for renderer pagination through AJAX.

To create a new renderer, just inherit from Paginate::Renderer::Base and define the render method. Here's the Paginate::Renderer::More code:

module Paginate
  module Renderer
    class More < Base
      def more_label
        I18n.t("paginate.more")
      end

      def render
        return unless processor.next_page?

        <<-HTML.html_safe
          <p class="paginate">
            <a class="more" href="#{next_url}" title="#{more_label}">#{more_label}</a>
          </p>
        HTML
      end
    end
  end
end

You can specify the default renderer by setting the Pagination.configuration.renderer option.

Paginate.configure do |config|
  config.renderer = Paginate::Renderer::More
end

You can also specify while calling the paginate helper.

<%= paginate @items, renderer: Paginate::Renderer::More %>

License

(The MIT License)

Copyright - Nando Vieira • http://nandovieira.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.