Module: WillPaginate::ViewHelpers::ActionView

Includes:
Base
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>

Instance Method Summary collapse

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
73
74
75
76
# File 'lib/will_paginate/view_helpers/action_view.rb', line 65

def paginated_section(*args, &block)
  pagination = will_paginate(*args).to_s
  
  unless ::ActionView::Base.respond_to? :erb_variable
    concat pagination
    yield
    concat pagination
  else
    content = pagination + capture(&block) + pagination
    concat(content, block.binding)
  end
end

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

:nodoc:



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

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

  super(collection, options.symbolize_keys)
end