Class: DataMapper::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-pager/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pager

Initialize with options.



38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#current_pageObject (readonly)

Current page number.



18
19
20
# File 'lib/dm-pager/pager.rb', line 18

def current_page
  @current_page
end

#next_pageObject (readonly)

Next page or nil when no more pages are available.



28
29
30
# File 'lib/dm-pager/pager.rb', line 28

def next_page
  @next_page
end

#per_pageObject (readonly)

Records per page.



13
14
15
# File 'lib/dm-pager/pager.rb', line 13

def per_page
  @per_page
end

#previous_pageObject (readonly)

Previous page or nil when no previous page is available.



23
24
25
# File 'lib/dm-pager/pager.rb', line 23

def previous_page
  @previous_page
end

#totalObject (readonly)

Total number of un-limited records.



8
9
10
# File 'lib/dm-pager/pager.rb', line 8

def total
  @total
end

#total_pagesObject (readonly)

Total number of pages.



33
34
35
# File 'lib/dm-pager/pager.rb', line 33

def total_pages
  @total_pages
end

Instance Method Details

#to_html(uri, options = {}) ⇒ Object

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

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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
  [%(<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