class DataMapper::Pager
Attributes
current_page[R]
Current page number.
next_page[R]
Next page or nil when no more pages are available.
per_page[R]
Records per page.
previous_page[R]
Previous page or nil when no previous page is available.
total[R]
Total number of un-limited records.
total_pages[R]
Total number of pages.
Public Class Methods
new(options = {})
click to toggle source
Initialize with options.
# File lib/dm-pager/pager.rb, line 38 def initialize options = {} @page_param = options.delete(:page_param) || :page @total = options.delete :total @per_page = options.delete :limit @current_page = options.delete @page_param @total_pages = total.quo(per_page).ceil @next_page = current_page + 1 unless current_page >= total_pages @previous_page = current_page - 1 unless current_page <= 1 end
Public Instance Methods
to_html(uri, options = {})
click to toggle source
Render the pager with the given uri and options.
Examples¶ ↑
User.page(2).pager.to_html('/users') User.page(2).pager.to_html('/users', :size => 3)
Options¶ ↑
:size Number of intermediate page number links to be shown; Defaults to 7
# File lib/dm-pager/pager.rb, line 61 def to_html uri, options = {} return unless total_pages > 1 @uri, @options = uri, options @size = option :size raise ArgumentError, 'invalid :size; must be an odd number' if @size % 2 == 0 @size /= 2 [%Q(<ul class="#{Pagination.defaults[:pager_class]}">), first_link, previous_link, more(:before), intermediate_links.join("\n"), more(:after), next_link, last_link, '</ul>'].join end
Private Instance Methods
first()
click to toggle source
Determine first intermediate page.
# File lib/dm-pager/pager.rb, line 146 def first @first ||= begin first = [current_page - @size, 1].max if (current_page - total_pages).abs < @size first = [first - (@size - (current_page - total_pages).abs), 1].max end first end end
first_link()
click to toggle source
First link.
# File lib/dm-pager/pager.rb, line 139 def first_link li 'first jump', link_to(1, option(:first_text)) if previous_page end
intermediate_links()
click to toggle source
Intermediate page links array.
# File lib/dm-pager/pager.rb, line 107 def intermediate_links (first..last).map do |page| classes = ["page-#{page}"] classes << 'active' if current_page == page li classes.join(' '), link_to(page) end end
last()
click to toggle source
Determine last intermediate page.
# File lib/dm-pager/pager.rb, line 159 def last @last ||= begin last = [current_page + @size, total_pages].min if @size >= current_page last = [last + (@size - current_page) + 1, total_pages].min end last end end
last_link()
click to toggle source
Last link.
# File lib/dm-pager/pager.rb, line 132 def last_link li 'last jump', link_to(total_pages, option(:last_text)) if next_page end
li(css_class = nil, contents = nil)
click to toggle source
Renders a <li> with the given css_class and contents.
# File lib/dm-pager/pager.rb, line 172 def li css_class = nil, contents = nil "<li#{%( class="#{css_class}") if css_class}>#{contents}</li>\n" end
link_to(page, contents = nil)
click to toggle source
Link to page with optional anchor tag contents.
# File lib/dm-pager/pager.rb, line 91 def link_to page, contents = nil %Q(<a href="#{uri_for(page)}">#{contents || page}</a>) end
more(position)
click to toggle source
More pages indicator for position.
# File lib/dm-pager/pager.rb, line 98 def more position return '' if position == :before && (current_page <= 1 || first <= 1) return '' if position == :after && (current_page >= total_pages || last >= total_pages) li 'more', option(:more_text) end
next_link()
click to toggle source
Next link.
# File lib/dm-pager/pager.rb, line 125 def next_link li 'next jump', link_to(next_page, option(:next_text)) if next_page end
option(key)
click to toggle source
Fetch key from the options passed to to_html, or its default value.
# File lib/dm-pager/pager.rb, line 84 def option key @options.fetch key, Pagination.defaults[key] end
previous_link()
click to toggle source
Previous link.
# File lib/dm-pager/pager.rb, line 118 def previous_link li 'previous jump', link_to(previous_page, option(:previous_text)) if previous_page end
uri_for(page)
click to toggle source
Uri for page. The following conversions are made to the uri previously passed to to_html:
/items # Appends query string => /items?page=2 /items?page=1 # Adjusts current page => /items?page=2 /items?foo=bar # Appends page pair => /items?foo=bar&page=1
# File lib/dm-pager/pager.rb, line 185 def uri_for page case @uri when /\b#{@page_param}=/ ; @uri.gsub /\b#{@page_param}=\d+/, "#{@page_param}=#{page}" when /\?/ ; @uri += "&#{@page_param}=#{page}" else ; @uri += "?#{@page_param}=#{page}" end end