Class: Ramaze::Helper::Paginate::Paginator

Inherits:
Object
  • Object
show all
Includes:
Ramaze::Helper
Defined in:
lib/ramaze/helper/paginate.rb

Overview

Provides easy pagination and navigation

Defined Under Namespace

Classes: ArrayPager, DataMapperPager

Constant Summary

Constant Summary

Constants included from Ramaze::Helper

Tagz

Instance Method Summary (collapse)

Constructor Details

- (Paginator) initialize(data = [], page = 1, limit = 10, var = 'pager')

A new instance of Paginator



69
70
71
72
73
74
# File 'lib/ramaze/helper/paginate.rb', line 69

def initialize(data = [], page = 1, limit = 10, var = 'pager')
  @data, @page, @limit, @var = data, page, limit, var
  @pager = pager_for(data)
  @page = @page > page_count ? page_count : @page
  @pager = pager_for(data)
end

Instance Method Details

- (Object) count



155
# File 'lib/ramaze/helper/paginate.rb', line 155

def count       ; @pager.count       ; end

- (Object) current_page



150
# File 'lib/ramaze/helper/paginate.rb', line 150

def current_page; @pager.current_page; end

- (Object) each(&block)



147
# File 'lib/ramaze/helper/paginate.rb', line 147

def each(&block); @pager.each(&block); end

- (Boolean) empty?

Returns:

  • (Boolean)


154
# File 'lib/ramaze/helper/paginate.rb', line 154

def empty?      ; @pager.empty?      ; end

- (Boolean) first_page?

Returns:

  • (Boolean)


148
# File 'lib/ramaze/helper/paginate.rb', line 148

def first_page? ; @pager.first_page? ; end

- (Object) last_page



151
# File 'lib/ramaze/helper/paginate.rb', line 151

def last_page   ; @pager.last_page   ; end

- (Boolean) last_page?

Returns:

  • (Boolean)


152
# File 'lib/ramaze/helper/paginate.rb', line 152

def last_page?  ; @pager.last_page?  ; end

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>


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ramaze/helper/paginate.rb', line 102

def navigation(limit = 8)
  g = Ramaze::Gestalt.new
  g.div :class => :pager do
    if first_page?
      g.span(:class => 'first grey'){ h('<<') }
      g.span(:class => 'previous grey'){ h('<') }
    else
      link(g, 1, '<<', :class => :first)
      link(g, prev_page, '<', :class => :previous)
    end

    lower = limit ? (current_page - limit) : 1
    lower = lower < 1 ? 1 : lower

    (lower...current_page).each do |n|
      link(g, n)
    end

    link(g, current_page, current_page, :class => :current)

    if last_page?
      g.span(:class => 'next grey'){ h('>') }
      g.span(:class => 'last grey'){ h('>>') }
    elsif next_page
      higher = limit ? (next_page + limit) : page_count
      higher = [higher, page_count].min
      (next_page..higher).each do |n|
        link(g, n)
      end

      link(g, next_page, '>', :class => :next)
      link(g, page_count, '>>', :class => :last)
    end
  end
  g.to_s
end

- (Boolean) needed?

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

Returns:

  • (Boolean)


140
141
142
# File 'lib/ramaze/helper/paginate.rb', line 140

def needed?
  @pager.page_count > 1
end

- (Object) next_page



153
# File 'lib/ramaze/helper/paginate.rb', line 153

def next_page   ; @pager.next_page   ; end

- (Object) page_count

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



146
# File 'lib/ramaze/helper/paginate.rb', line 146

def page_count  ; @pager.page_count  ; end

- (Object) prev_page



149
# File 'lib/ramaze/helper/paginate.rb', line 149

def prev_page   ; @pager.prev_page   ; end