Class: DataMapper::Pager
- Inherits:
-
Object
- Object
- DataMapper::Pager
- Defined in:
- lib/dm-pager/pager.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Current page number.
-
#next_page ⇒ Object
readonly
Next page or nil when no more pages are available.
-
#per_page ⇒ Object
readonly
Records per page.
-
#previous_page ⇒ Object
readonly
Previous page or nil when no previous page is available.
-
#total ⇒ Object
readonly
Total number of un-limited records.
-
#total_pages ⇒ Object
readonly
Total number of pages.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Pager
constructor
Initialize with options.
-
#to_html(uri, options = {}) ⇒ Object
Render the pager with the given uri and options.
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 = {} @page_param = .delete(:page_param) || :page @total = .delete :total @per_page = .delete :limit @current_page = .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_page ⇒ Object (readonly)
Current page number.
18 19 20 |
# File 'lib/dm-pager/pager.rb', line 18 def current_page @current_page end |
#next_page ⇒ Object (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_page ⇒ Object (readonly)
Records per page.
13 14 15 |
# File 'lib/dm-pager/pager.rb', line 13 def per_page @per_page end |
#previous_page ⇒ Object (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 |
#total ⇒ Object (readonly)
Total number of un-limited records.
8 9 10 |
# File 'lib/dm-pager/pager.rb', line 8 def total @total end |
#total_pages ⇒ Object (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
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, = {} return unless total_pages > 1 @uri, @options = uri, @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 |