Module: MerbPaginate::ViewHelpers

Defined in:
lib/merb_paginate/view_helpers.rb

Overview

MerbPaginate view helpers

Currently there is only one view helper: merb_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 MerbPaginate::ViewHelpers.pagination_options hash. You can write to this hash to override default options on the global level:

MerbPaginate::ViewHelpers.pagination_options[:prev_label] = 'Previous page'

By putting this into your init.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     => 'MerbPaginate::LinkRenderer',
  :page_links   => true,
  :container    => true,
  :namespace    => nil
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pagination_optionsObject

there is not mattr_accessor here and it’s not a big deal to just make these two getter/setter methods for now



42
43
44
# File 'lib/merb_paginate/view_helpers.rb', line 42

def self.pagination_options
  @@pagination_options
end

.pagination_options=(options) ⇒ Object



46
47
48
# File 'lib/merb_paginate/view_helpers.rb', line 46

def self.pagination_options=(options)
  @@pagination_options = options
end

Instance Method Details

#merb_paginate(collection, options = {}) ⇒ Object

Renders Digg/Flickr-style pagination for a MerbPaginate::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)

  • :named_route – optional named route used to generate the pagination url

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

  • :renderer – class name of the 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:

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

… will result in:

<div class="pagination" id="wp_posts"> ... </div>

There is not magic controller inference anynmore. Pass in the variable lazy.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/merb_paginate/view_helpers.rb', line 84

def merb_paginate(collection, options = {}) # collection is required now! Booya!
  # early exit if there is nothing to render
  return nil unless collection.total_pages > 1
  
  options = options.to_mash.reverse_merge MerbPaginate::ViewHelpers.pagination_options.to_mash
  # create the renderer instance
  renderer_class = options[:renderer].to_s.constantize
  renderer = renderer_class.new collection, options, self
  # render HTML for pagination
  renderer.to_html
end

#page_entries_info(collection) ⇒ 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 entries 6 - 10 of 26 in total


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/merb_paginate/view_helpers.rb', line 130

def page_entries_info(collection)
  if collection.total_pages < 2
    case collection.size
    when 0; 'No entries found'
    when 1; 'Displaying <b>1</b> entry'
    else;   "Displaying <b>all #{collection.size}</b> entries"
    end
  else
    %{Displaying entries <b>%d&nbsp;-&nbsp;%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).



118
119
120
121
122
123
# File 'lib/merb_paginate/view_helpers.rb', line 118

def paginated_section(*args, &block)
  pagination = merb_paginate(*args).to_s
  content = pagination + capture(&block) + pagination
  #concat content, block.binding
  content
end