Module: WillPaginate::ViewHelpers
- Defined in:
- lib/will_paginate/view_helpers.rb
Overview
Will Paginate view helpers
Currently there is only one view helper: will_paginate
. It renders the pagination links for the given collection. The helper itself is lightweight and serves only as a wrapper around link renderer 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 WillPaginate::ViewHelpers.pagination_options hash. You can write to this hash to override default options on the global level:
WillPaginate::ViewHelpers.[:prev_label] = 'Previous page'
By putting this into your environment.rb 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', :prev_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 :param_name => :page, :params => nil, :renderer => 'WillPaginate::LinkRenderer', :page_links => true, :container => true }
Class Method Summary collapse
Instance Method Summary collapse
-
#page_entries_info(collection, options = {}) ⇒ Object
Renders a helpful message with numbers of displayed vs.
-
#paginated_section(*args, &block) ⇒ Object
Wrapper for rendering pagination links at both top and bottom of a block of content.
-
#will_paginate(collection = nil, options = {}) ⇒ Object
Renders Digg/Flickr-style pagination for a WillPaginate::Collection object.
Class Method Details
.total_pages_for_collection(collection) ⇒ Object
:nodoc:
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/will_paginate/view_helpers.rb', line 169 def self.total_pages_for_collection(collection) #:nodoc: if collection.respond_to?('page_count') and !collection.respond_to?('total_pages') WillPaginate::Deprecation.warn <<-MSG You are using a paginated collection of class #{collection.class.name} which conforms to the old API of WillPaginate::Collection by using `page_count`, while the current method name is `total_pages`. Please upgrade yours or 3rd-party code that provides the paginated collection. MSG class << collection def total_pages; page_count; end end end collection.total_pages end |
Instance Method Details
#page_entries_info(collection, options = {}) ⇒ Object
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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/will_paginate/view_helpers.rb', line 150 def page_entries_info(collection, = {}) entry_name = [:entry_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' ')) if collection.total_pages < 2 case collection.size when 0; "No #{entry_name.pluralize} found" when 1; "Displaying <b>1</b> #{entry_name}" else; "Displaying <b>all #{collection.size}</b> #{entry_name.pluralize}" end else %{Displaying #{entry_name.pluralize} <b>%d - %d</b> of <b>%d</b> in total} % [ collection.offset + 1, collection.offset + collection.length, collection.total_entries ] end end |
#paginated_section(*args, &block) ⇒ Object
Wrapper for rendering pagination links at both top and bottom of a block of content.
<% paginated_section @posts do %>
<ol id="posts">
<% for post in @posts %>
<li> ... </li>
<% end %>
</ol>
<% end %>
will result in:
<div class="pagination"> ... </div>
<ol id="posts">
...
</ol>
<div class="pagination"> ... </div>
Arguments are passed to a will_paginate
call, so the same options apply. Don’t use the :id
option; otherwise you’ll finish with two blocks of pagination links sharing the same ID (which is invalid HTML).
132 133 134 135 136 |
# File 'lib/will_paginate/view_helpers.rb', line 132 def paginated_section(*args, &block) pagination = will_paginate(*args).to_s content = pagination + capture(&block) + pagination concat content, block.binding end |
#will_paginate(collection = nil, options = {}) ⇒ Object
Renders Digg/Flickr-style pagination for a WillPaginate::Collection object. Nil is returned if there is only one page in total; no point in rendering the pagination in that case…
Options
-
:class
– CSS class name for the generated DIV (default: “pagination”) -
:prev_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). Passtrue
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>
Using the helper without arguments
If the helper is called without passing in the collection object, it will try to read from the instance variable inferred by the controller name. For example, calling will_paginate
while the current controller is PostsController will result in trying to read from the @posts
variable. Example:
<%= will_paginate :id => true %>
… will result in @post
collection getting paginated:
<div class="pagination" id="posts_pagination"> ... </div>
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 |
# File 'lib/will_paginate/view_helpers.rb', line 83 def will_paginate(collection = nil, = {}) , collection = collection, nil if collection.is_a? Hash unless collection or !controller collection_name = "@#{controller.controller_name}" collection = instance_variable_get(collection_name) raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " + "forget to pass the collection object for will_paginate?" unless collection end # early exit if there is nothing to render return nil unless WillPaginate::ViewHelpers.total_pages_for_collection(collection) > 1 = .symbolize_keys.reverse_merge WillPaginate::ViewHelpers. # get the renderer instance renderer = case [:renderer] when String [:renderer].to_s.constantize.new when Class [:renderer].new else [:renderer] end # render HTML for pagination renderer.prepare collection, , self renderer.to_html end |