class Ramaze::Helper::Paginate::Paginator

Provides easy pagination and navigation

Attributes

css[R]

Public Class Methods

new(data = [], page = 1, limit = 10, var = 'pager', opts = {}) click to toggle source
# File lib/ramaze/helper/paginate.rb, line 116
def initialize(data = [], page = 1, limit = 10, var = 'pager', opts = {})
  @data, @page, @limit, @var = data, page, limit, var
  @css = Paginate.trait[:paginate][:css].dup
  @css.merge!(opts[:css]) if opts[:css]
  @pager = pager_for(data)
  @page = @page > page_count ? page_count : @page
  @pager = pager_for(data)
end

Public Instance Methods

count() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 207
def count       ; @pager.count       ; end
current_page() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 202
def current_page; @pager.current_page; end
each(&block) click to toggle source
# File lib/ramaze/helper/paginate.rb, line 199
def each(&block); @pager.each(&block); end
empty?() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 206
def empty?      ; @pager.empty?      ; end
first_page?() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 200
def first_page? ; @pager.first_page? ; end
last_page() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 203
def last_page   ; @pager.last_page   ; end
last_page?() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 204
def last_page?  ; @pager.last_page?  ; end
navigation(limit = 8) click to toggle source

Returns String with navigation div.

This cannot be customized very nicely, but you can style it easily with CSS.

Output with 5 elements, page 1, limit 3:

<div class="pager">
  <span class="first grey">&lt;&lt;</span>
  <span class="previous grey">&lt;</span>
  <a class="current" href="/index?pager=1">1</a>
  <a href="/index?pager=2">2</a>
  <a class="next" href="/index?pager=2">&gt;</a>
  <a class="last" href="/index?pager=2">&gt;&gt;</a>
</div>

Output with 5 elements, page 2, limit 3:

<div class="pager" />
  <a class="first" href="/index?user_page=1">&lt;&lt;</a>
  <a class="previous" href="/index?user_page=1">&lt;</a>
  <a href="/index?user_page=1">1</a>
  <a class="current" href="/index?user_page=2">2</a>
  <span class="next grey">&gt;</span>
  <span class="last grey">&gt;&gt;</span>
</div>
needed?() click to toggle source

Useful to omit pager if it's of no use.

# File lib/ramaze/helper/paginate.rb, line 192
def needed?
  @pager.page_count > 1
end
next_page() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 205
def next_page   ; @pager.next_page   ; end
page_count() click to toggle source

these methods are actually on the pager, but we def them here for convenience (method_missing in helper is evil and even slower)

# File lib/ramaze/helper/paginate.rb, line 198
def page_count  ; @pager.page_count  ; end
prev_page() click to toggle source
# File lib/ramaze/helper/paginate.rb, line 201
def prev_page   ; @pager.prev_page   ; end

Private Instance Methods

pager_for(obj) click to toggle source
# File lib/ramaze/helper/paginate.rb, line 211
def pager_for(obj)
  @page = @page < 1 ? 1 : @page

  case obj
  when Array
    ArrayPager.new(obj, @page, @limit)
  when (defined?(DataMapper::Collection) and DataMapper::Collection)
    DataMapperPager.new(obj, @page, @limit)
  else
    obj.paginate(@page, @limit)
  end
end