Module: Paginate::Helpers::Merb

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

Overview

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

Instance Method Summary collapse

Methods included from Shared

#page_set

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/merb.rb', line 39

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

#page_url(page, path = request.path, q = request.query_string) ⇒ Object

page_url generates a URL (for page links), given a page number and optionally a path and query string. By default, the path is the path of the current request, and the query_string is also that of the current request. This allows for order and condition related fields in the query string to be used in the page link.



76
77
78
79
80
81
82
83
# File 'lib/helpers/merb.rb', line 76

def page_url(page, path = request.path, q = request.query_string)
  # Remove any current reference to page in the query string
  q.to_s.gsub!(/page=(-?[\d]+)(&?)/, '') 
  # Assemble new link
  link = "#{path}?page=#{page}&#{q}"
  link = link[0..-2] if link[-1..-1] == '&' # Strip trailing ampersand
  link
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/merb.rb', line 16

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