Class: Ramaze::Helper::Paginate::Paginator
- Inherits:
-
Object
- Object
- Ramaze::Helper::Paginate::Paginator
- 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
Instance Attribute Summary collapse
-
#css ⇒ Object
readonly
Returns the value of attribute css.
Instance Method Summary collapse
- #count ⇒ Object
- #current_page ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first_page? ⇒ Boolean
-
#initialize(data = [], page = 1, limit = 10, var = 'pager', opts = {}) ⇒ Paginator
constructor
A new instance of Paginator.
- #last_page ⇒ Object
- #last_page? ⇒ Boolean
-
#navigation(limit = 8) ⇒ Object
Returns String with navigation div.
-
#needed? ⇒ Boolean
Useful to omit pager if it’s of no use.
- #next_page ⇒ Object
-
#page_count ⇒ Object
these methods are actually on the pager, but we def them here for convenience (method_missing in helper is evil and even slower).
- #prev_page ⇒ Object
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
#css ⇒ Object (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
#count ⇒ Object
207 |
# File 'lib/ramaze/helper/paginate.rb', line 207 def count ; @pager.count ; end |
#current_page ⇒ Object
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
206 |
# File 'lib/ramaze/helper/paginate.rb', line 206 def empty? ; @pager.empty? ; end |
#first_page? ⇒ Boolean
200 |
# File 'lib/ramaze/helper/paginate.rb', line 200 def first_page? ; @pager.first_page? ; end |
#last_page ⇒ Object
203 |
# File 'lib/ramaze/helper/paginate.rb', line 203 def last_page ; @pager.last_page ; end |
#last_page? ⇒ Boolean
204 |
# File 'lib/ramaze/helper/paginate.rb', line 204 def last_page? ; @pager.last_page? ; end |
#navigation(limit = 8) ⇒ Object
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"><<</span>
<span class="previous grey"><</span>
<a class="current" href="/index?pager=1">1</a>
<a href="/index?pager=2">2</a>
<a class="next" href="/index?pager=2">></a>
<a class="last" href="/index?pager=2">>></a>
</div>
Output with 5 elements, page 2, limit 3:
<div class="pager" />
<a class="first" href="/index?user_page=1"><<</a>
<a class="previous" href="/index?user_page=1"><</a>
<a href="/index?user_page=1">1</a>
<a class="current" href="/index?user_page=2">2</a>
<span class="next grey">></span>
<span class="last grey">>></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 (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.
192 193 194 |
# File 'lib/ramaze/helper/paginate.rb', line 192 def needed? @pager.page_count > 1 end |
#next_page ⇒ Object
205 |
# File 'lib/ramaze/helper/paginate.rb', line 205 def next_page ; @pager.next_page ; end |
#page_count ⇒ Object
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_page ⇒ Object
201 |
# File 'lib/ramaze/helper/paginate.rb', line 201 def prev_page ; @pager.prev_page ; end |