Module: Paginate::Helpers::Rails

Includes:
Shared
Defined in:
lib/helpers/rails.rb

Overview

Pagination helpers for Rails applications. See Paginate::Helpers::Shared for additional methods

Instance Method Summary collapse

Methods included from Shared

#page_set, #url_for_pagination

Instance Method Details

Returns links for pages, given a collection and optional padding (see Shared#page_set). Returned html will be along the lines of:

<div class="pageLinks">' +
  <span class="pagePrevious"><a href="/list?page=4">&laquo;</a></span>
  <span class="pageSpacer">...</span>
  <span class="pageNumber"><a href="/list?page=3">3</a></span>
  <span class="pageNumber"><a href="/list?page=4">4</a></span>
  <span class="pageCurrent">5</span>
  <span class="pageNumber"><a href="/list?page=6">6</a></span>
  <span class="pageNumber"><a href="/list?page=7">7</a></span>
  <span class="pageSpacer">...</span>
  <span class="pageNext"><a href="/list?page=6">&raquo;</a></span>
</div>

CSS classes are pageSpacer, pageNumber, pageDisabled, pageCurrent, pagePrevious, and pageNext. pageLinks is the class of the enclosing div.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/helpers/rails.rb', line 39

def page_links(collection, padding = 3)
  current = collection.current_page
  pages   = collection.pages
  
  (:div, {:class => 'pageLinks'}, false) do
    html = ''.html_safe
    if current == 1
      html << (:span, :class => 'pageDisabled pagePrevious') { '&laquo;'.html_safe }
    else
      html << (:a, :href => url_for_pagination(current - 1), :class => 'pagePrevious') { '&laquo;'.html_safe }
    end
    
    page_set(current, pages, padding).each do |page|
      case page
      when 0
        html << (:span, :class => 'pageSpacer') { '...' }
      when current
        html << (:span, :class => 'pageCurrent') { page.to_s }
      else
        html << (:a, :href => url_for_pagination(page), :class => 'pageNumber') { page.to_s }
      end
    end
  
    if current == pages
      html << (:span, :class => 'pageDisabled pageNext') { '&raquo;'.html_safe }
    else
      html << (:a, :href => url_for_pagination(current + 1), :class => 'pageNext') { '&raquo;'.html_safe }
    end
    html
  end
end

#pagination_partial(collection, partial_name = "layout/page_links", padding = 3) ⇒ Object

A quick method for creating pagination links, using a view partial, (layouts/_page_links, by default). Arguments:

  • collection: an enumerable collection with #current_page and #pages methods.

  • partial_name: location of the partial to use

  • padding: Maximum number of page links before and after current_page.



16
17
18
19
# File 'lib/helpers/rails.rb', line 16

def pagination_partial(collection, partial_name = "layout/page_links", padding = 3)
  padding ||= 3
  render(:partial => partial_name, :locals => {:current_page => collection.current_page, :pages => collection.pages, :padding => padding})
end