Module: Leaf::ViewHelpers::Base

Defined in:
lib/leaf/view_helpers/base.rb

Overview

The main view helpers module

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

Instance Method Summary collapse

Instance Method Details

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

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

<%= leaf @posts, :id => 'leaf_posts' %>

... will result in:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/leaf/view_helpers/base.rb', line 41

def leaf(collection, options = {})
  # early exit if there is nothing to render
  return nil unless collection.total_pages > 1
  
  options = Leaf::ViewHelpers.pagination_options.merge(options)
  
  # get the renderer instance
  renderer = case options[:renderer]
    when String then options[:renderer].constantize.new
    when Class then options[:renderer].new
    else options[:renderer]
  end
  
  # render HTML for pagination
  renderer.prepare collection, options, self
  renderer.to_html
end