module WillPaginate::ViewHelpers::Base

The main view helpers module

This is the base module which provides the will_paginate view helper.

Public Instance Methods

page_entries_info(collection, options = {}) click to toggle source

Renders a helpful message with numbers of displayed vs. total entries. You can use this as a blueprint for your own, similar helpers.

<%= page_entries_info @posts %>
#-> Displaying posts 6 - 10 of 26 in total

By default, the message will use the humanized class name of objects in collection: for instance, “project types” for ProjectType models. Override this to your liking with the :entry_name parameter:

<%= page_entries_info @posts, :entry_name => 'item' %>
#-> Displaying items 6 - 10 of 26 in total

Entry name is entered in singular and pluralized with String#pluralize method from ActiveSupport. If it isn’t loaded, specify plural with :plural_name parameter:

<%= page_entries_info @posts, :entry_name => 'item', :plural_name => 'items' %>

By default, this method produces HTML output. You can trigger plain text output by passing :html => false in options.

# File lib/will_paginate/view_helpers/base.rb, line 87
def page_entries_info(collection, options = {})
  entry_name = options[:entry_name] || (collection.empty?? 'entry' :
               collection.first.class.name.underscore.gsub('_', ' '))
  
  plural_name = if options[:plural_name]
    options[:plural_name]
  elsif entry_name == 'entry'
    plural_name = 'entries'
  elsif entry_name.respond_to? :pluralize
    plural_name = entry_name.pluralize
  else
    entry_name + 's'
  end

  unless options[:html] == false
    b  = '<b>'
    eb = '</b>'
    sp = '&nbsp;'
  else
    b  = eb = ''
    sp = ' '
  end
  
  if collection.total_pages < 2
    case collection.size
    when 0; "No #{plural_name} found"
    when 1; "Displaying #{b}1#{eb} #{entry_name}"
    else;   "Displaying #{b}all #{collection.size}#{eb} #{plural_name}"
    end
  else
    %Q{Displaying #{plural_name} #{b}%d#{sp}-#{sp}%d#{eb} of #{b}%d#{eb} in total} % [
      collection.offset + 1,
      collection.offset + collection.length,
      collection.total_entries
    ]
  end
end
will_paginate(collection, options = {}) click to toggle source

Renders Digg/Flickr-style pagination for a WillPaginate::Collection object. Nil is returned if there is only one page in total; pagination links aren’t needed in that case.

Options

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

  • :previous_label – default: “« Previous”

  • :next_label – default: “Next »”

  • :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)

  • :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: WillPaginate::LinkRenderer)

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

  • :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”.

All options beside listed ones are passed as HTML attributes to the container element for pagination links (the DIV). For example:

<%= will_paginate @posts, :id => 'wp_posts' %>

… will result in:

<div class="pagination" id="wp_posts"> ... </div>
# File lib/will_paginate/view_helpers/base.rb, line 41
def will_paginate(collection, options = {})
  # early exit if there is nothing to render
  return nil unless collection.total_pages > 1
  
  options = WillPaginate::ViewHelpers.pagination_options.merge(options)
  
  if options[:prev_label]
    WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated.")
    options[:previous_label] = options.delete(:prev_label)
  end
  
  # get the renderer instance
  renderer = case options[:renderer]
  when String
    options[:renderer].constantize.new
  when Class
    options[:renderer].new
  else
    options[:renderer]
  end
  # render HTML for pagination
  renderer.prepare collection, options, self
  renderer.to_html
end