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
Constant Summary
Constants included from Ramaze::Helper
Instance Attribute Summary (collapse)
-
- (Object) css
readonly
Returns the value of attribute css.
Instance Method Summary (collapse)
-
- (Object) count
-
- (Object) current_page
-
- (Object) each(&block)
-
- (Boolean) empty?
-
- (Boolean) first_page?
-
- (Paginator) initialize(data = [], page = 1, limit = 10, var = 'pager', opts = {})
constructor
A new instance of Paginator.
-
- (Object) last_page
-
- (Boolean) last_page?
-
- (Object) link(g, n, text = n, hash = {})
private
-
- (Object) navigation(limit = 8)
Returns String with navigation div.
-
- (Boolean) needed?
Useful to omit pager if it's of no use.
-
- (Object) next_page
-
- (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).
-
- (Object) pager_for(obj)
private
-
- (Object) prev_page
Constructor Details
- (Paginator) initialize(data = [], page = 1, limit = 10, var = 'pager', opts = {})
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
- (Object) css (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
- (Object) count
207 |
# File 'lib/ramaze/helper/paginate.rb', line 207 def count ; @pager.count ; end |
- (Object) current_page
202 |
# File 'lib/ramaze/helper/paginate.rb', line 202 def current_page; @pager.current_page; end |
- (Object) each(&block)
199 |
# File 'lib/ramaze/helper/paginate.rb', line 199 def each(&block); @pager.each(&block); end |
- (Boolean) empty?
206 |
# File 'lib/ramaze/helper/paginate.rb', line 206 def empty? ; @pager.empty? ; end |
- (Boolean) first_page?
200 |
# File 'lib/ramaze/helper/paginate.rb', line 200 def first_page? ; @pager.first_page? ; end |
- (Object) last_page
203 |
# File 'lib/ramaze/helper/paginate.rb', line 203 def last_page ; @pager.last_page ; end |
- (Boolean) last_page?
204 |
# File 'lib/ramaze/helper/paginate.rb', line 204 def last_page? ; @pager.last_page? ; end |
- (Object) link(g, n, text = n, hash = {}) (private)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/ramaze/helper/paginate.rb', line 224 def link(g, n, text = n, hash = {}) text = h(text.to_s) action = Current.action params = request.params.merge(@var.to_s => n) params.keys.each do |p| if params[p].is_a?(Array) and not p.end_with?('[]') params["#{p}[]"] = params.delete(p) end end hash[:href] = action.node.r(action.path, params) g.a(hash){ text } end |
- (Object) navigation(limit = 8)
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 |
- (Boolean) needed?
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 |
- (Object) next_page
205 |
# File 'lib/ramaze/helper/paginate.rb', line 205 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)
198 |
# File 'lib/ramaze/helper/paginate.rb', line 198 def page_count ; @pager.page_count ; end |
- (Object) pager_for(obj) (private)
211 212 213 214 215 216 217 218 219 220 221 222 |
# 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 |
- (Object) prev_page
201 |
# File 'lib/ramaze/helper/paginate.rb', line 201 def prev_page ; @pager.prev_page ; end |