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

Constants included from Ramaze::Helper

Tagz

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = [], page = 1, limit = 10, var = 'pager', opts = {}) ⇒ Paginator

Returns a new instance of Paginator.



116
117
118
119
120
121
122
123
# 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

Instance Attribute Details

#cssObject (readonly)

Returns the value of attribute css.



114
115
116
# File 'lib/ramaze/helper/paginate.rb', line 114

def css
  @css
end

Instance Method Details

#countObject



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

def count       ; @pager.count       ; end

#current_pageObject



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

def current_page; @pager.current_page; end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?      ; @pager.empty?      ; end

#first_page?Boolean

Returns:

  • (Boolean)


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

def first_page? ; @pager.first_page? ; end

#last_pageObject



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

def last_page   ; @pager.last_page   ; end

#last_page?Boolean

Returns:

  • (Boolean)


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

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>


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ramaze/helper/paginate.rb', line 153

def navigation(limit = 8)
  g = Ramaze::Gestalt.new
  g.div :class => :pager do
    if first_page?
      g.span(:class => "#{@css[:first]} #{@css[:disabled]}"){ h('<<') }
      g.span(:class => "#{@css[:prev]} #{@css[:disabled]}"){ h('<') }
    else
      link(g, 1, '<<', :class => @css[:first])
      link(g, prev_page, '<', :class => @css[:prev])
    end

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

    (lower...current_page).each do |n|
      link(g, n, n, :class => @css[:number])
    end

    link(g, current_page, current_page,
      :class => "#{@css[:current]} #{@css[:number]}" )

    if last_page?
      g.span(:class => "#{@css[:next]} #{@css[:disabled]}"){ h('>') }
      g.span(:class => "#{@css[:last]} #{@css[:disabled]}"){ 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, n, :class => @css[:number])
      end

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

#needed?Boolean

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

Returns:

  • (Boolean)


192
193
194
# File 'lib/ramaze/helper/paginate.rb', line 192

def needed?
  @pager.page_count > 1
end

#next_pageObject



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

def next_page   ; @pager.next_page   ; end

#page_countObject

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



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

def page_count  ; @pager.page_count  ; end

#prev_pageObject



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

def prev_page   ; @pager.prev_page   ; end