Module: WillPaginate::ActionView

Includes:
ViewHelpers
Defined in:
lib/will_paginate/view_helpers/action_view.rb

Overview

ActionView helpers

This module serves for availability in ActionView templates. It also adds a new view helper: paginated_section.

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>

Defined Under Namespace

Classes: LinkRenderer

Instance Method Summary collapse

Methods included from I18n

load_path, locale_dir

Instance Method Details

#page_entries_info(collection = nil, options = {}) ⇒ Object

:nodoc:



36
37
38
39
40
41
# File 'lib/will_paginate/view_helpers/action_view.rb', line 36

def page_entries_info(collection = nil, options = {}) #:nodoc:
  options, collection = collection, nil if collection.is_a? Hash
  collection ||= infer_collection_from_controller

  super(collection, options.symbolize_keys)
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).



65
66
67
68
69
70
71
72
# File 'lib/will_paginate/view_helpers/action_view.rb', line 65

def paginated_section(*args, &block)
  pagination = will_paginate(*args)
  if pagination
    pagination + capture(&block) + pagination
  else
    capture(&block)
  end
end

#will_paginate(collection = nil, options = {}) ⇒ Object

:nodoc:



26
27
28
29
30
31
32
33
34
# File 'lib/will_paginate/view_helpers/action_view.rb', line 26

def will_paginate(collection = nil, options = {}) #:nodoc:
  options, collection = collection, nil if collection.is_a? Hash
  collection ||= infer_collection_from_controller

  options = options.symbolize_keys
  options[:renderer] ||= LinkRenderer

  super(collection, options).try(:html_safe)
end

#will_paginate_translate(keys, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/will_paginate/view_helpers/action_view.rb', line 74

def will_paginate_translate(keys, options = {})
  if respond_to? :translate
    if Array === keys
      defaults = keys.dup
      key = defaults.shift
    else
      defaults = nil
      key = keys
    end
    translate(key, options.merge(:default => defaults, :scope => :will_paginate))
  else
    super
  end
end