Module: Pagination::ViewHelpers

Defined in:
lib/pagination/view_helpers.rb

Overview

Pagination view helpers

The main view helper, #paginate, renders pagination links around (top and bottom) given collection. The helper itself is lightweight and serves only as a wrapper around LinkRenderer instantiation; the renderer then does all the hard work of generating the HTML.

Global options for helpers

Options for pagination helpers are optional and get their default values from the Pagination::ViewHelpers.pagination_options hash. You can write to this hash to override default options on the global level:

Pagination::ViewHelpers.pagination_options[:previous_label] = 'Previous page'

By putting this into “config/initializers/pagination.rb” (or simply environment.rb in older versions of Rails) you can easily translate link texts to previous and next pages, as well as override some other defaults to your liking.

Constant Summary collapse

@@pagination_options =

default options that can be overridden on the global level

{
  :class          => 'pagination',
  :previous_label => '« Previous',
  :next_label     => 'Next »',
  :inner_window   => 4, # links around the current page
  :outer_window   => 1, # links around beginning and end
  :separator      => ' ', # single space is friendly to spiders and non-graphic browsers
  :controls       => :both,
  :per_page       => 10,
  :param_name     => :page,
  :params         => nil,
  :renderer       => 'Pagination::LinkRenderer',
  :page_links     => true,
  :container      => true
}

Instance Method Summary collapse

Instance Method Details

#paginate(collection = [], options = {}, &block) ⇒ Object

Renders Digg/Flickr-style pagination for a Pagination::Collection object. Nil is returned if there is only one page in total; no point in rendering the pagination in that case…

Options

Display options:

  • :previous_label – default: “« Previous” (this parameter is called :prev_label in versions 2.3.2 and older!)

  • :next_label – default: “Next »”

  • :page_links – when false, only previous/next links are rendered (default: true)

  • :inner_window – how many links are shown around the current page (default: 4)

  • :outer_window – how many links are around the first and the last page (default: 1)

  • :separator – string separator for page HTML elements (default: single space)

  • :controls – display controls only at the :top or :bottom of the pagination block (default: :both)

  • :per_page – number of items displayed per page (default: 10)

HTML options:

  • :class – CSS class name for the generated DIV (default: “pagination”)

  • :container – toggles rendering of the DIV container for pagination links, set to false only when you are rendering your own pagination markup (default: true)

  • :id – HTML ID for the container (default: nil). Pass true to have the ID automatically generated from the class name of objects in collection: for example, paginating ArticleComment models would yield an ID of “article_comments_pagination”.

Advanced options:

  • :param_name – parameter name for page number in URLs (default: :page)

  • :params – additional parameters when generating pagination links (eg. :controller => "foo", :action => nil)

  • :renderer – class name, class or instance of a link renderer (default: Pagination::LinkRenderer)

All options not recognized by paginate will become HTML attributes on the container element for pagination links (the DIV). For example:

<% paginate @posts, :style => 'font-size: small' do |posts| %>
  ...
<% end %>

… will result in:

<div class="pagination" style="font-size: small"> ... </div>


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pagination/view_helpers.rb', line 80

def paginate(collection = [], options = {}, &block)
  options = options.symbolize_keys.reverse_merge Pagination::ViewHelpers.pagination_options

  collection = Pagination::Collection.new(options.merge(:collection => collection, :current_page => params[options[:param_name]]|| 1))

  # get the renderer instance
  renderer = case options[:renderer]
  when String
    options[:renderer].to_s.constantize.new
  when Class
    options[:renderer].new
  else
    options[:renderer]
  end

  # render HTML for pagination
  renderer.prepare collection, options, self
  pagination = renderer.to_html.to_s

  if block_given?
    yield collection and return nil unless collection.total_pages > 1

    top = [:top, :both].include?(options[:controls]) ? pagination : ""
    bottom = [:bottom, :both].include?(options[:controls]) ? pagination : ""
    unless ActionView::Base.respond_to? :erb_variable
      concat top
      yield collection
      unless bottom.empty?
        concat bottom
      end
    else
      content = top + capture(&block) + bottom
      concat(content, block.binding)
    end
  else
    collection
  end
end