/usr/lib/ruby/1.8/ramaze/helper/paginate.rb is in libramaze-ruby1.8 2010.06.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | require 'ramaze/gestalt'
module Ramaze
module Helper
# Helper for pagination and pagination-navigation.
#
# See detailed API docs for Paginator below.
# Also have a look at the examples/helpers/paginate.rb
module Paginate
include Traited
# Define default options in your Controller, they are being retrieved by
# ancestral_trait, so you can also put it into a common superclass
trait :paginate => {
:limit => 10,
:var => 'pager',
}
# Returns a new Paginator instance.
#
# Note that the pagination relies on being inside a Ramaze request to
# gain necessary metadata about the page it resides on, you cannot use it
# outside of Ramaze yet.
#
# The examples below are meant to be used within your controller or view.
#
# Usage with Array:
# data = (1..100).to_a
# @pager = paginate(data, :limit => 30, :page => 2)
# @pager.navigation
# @pager.each{|e| puts(e) }
#
# Usage with Sequel:
# data = Article.filter(:public => true)
# @pager = paginate(data, :limit => 5)
# @pager.navigation
# @pager.each{|e| puts(e)
#
# Note that you must first extend Sequel with the pagination extension.
# Sequel.extension :pagination
#
# +dataset+ may be a Sequel dataset or an Array
# +options+ Takes precedence to trait[:paginate] and may contain
# following pairs:
# :limit The number of elements used when you call #each on the
# paginator
# :var The variable name being used in the request, this is helpful
# if you want to use two or more independent paginations on the
# same page.
# :page The page you are currently on, if not given it will be
# retrieved from current request variables. Defaults to 1 if
# neither exists.
def paginate(dataset, options = {})
options = ancestral_trait[:paginate].merge(options)
limit = options[:limit]
var = options[:var]
page = options[:page] || (request[var] || 1).to_i
Paginator.new(dataset, page, limit, var)
end
# Provides easy pagination and navigation
class Paginator
include Ramaze::Helper
helper :link, :cgi
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
# 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>
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
# Useful to omit pager if it's of no use.
def needed?
@pager.page_count > 1
end
# these methods are actually on the pager, but we def them here for
# convenience (method_missing in helper is evil and even slower)
def page_count; @pager.page_count end
def each(&block) @pager.each(&block) end
def first_page?; @pager.first_page?; end
def prev_page; @pager.prev_page; end
def current_page; @pager.current_page; end
def last_page; @pager.last_page; end
def last_page?; @pager.last_page?; end
def next_page; @pager.next_page; end
def empty?; @pager.empty?; end
def count; @pager.count; end
private
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
def link(g, n, text = n, hash = {})
text = h(text.to_s)
action = Current.action
params = request.params.merge(@var.to_s => n)
hash[:href] = action.node.r(action.path, params)
g.a(hash){ text }
end
# Wrapper for Array to behave like the Sequel pagination
class ArrayPager
def initialize(array, page, limit)
@array, @page, @limit = array, page, limit
@page = page_count if @page > page_count
end
def size
@array.size
end
def empty?
@array.empty?
end
def page_count
pages, rest = size.divmod(@limit)
rest == 0 ? pages : pages + 1
end
def current_page
@page
end
def next_page
page_count == @page ? nil : @page + 1
end
def prev_page
@page <= 1 ? nil : @page - 1
end
def first_page?
@page <= 1
end
def last_page?
page_count == @page
end
def each(&block)
from = ((@page - 1) * @limit)
to = from + @limit
a = @array[from...to] || []
a.each(&block)
end
include Enumerable
end
# Wrapper for DataMapper::Collection to behave like the Sequel
# pagination.
# needs 'datamapper' (or 'dm-core' and 'dm-aggregates')
class DataMapperPager < ArrayPager
def initialize(*args)
unless defined?(DataMapper::Aggregates)
Ramaze::Log.warn "paginate.rb: it is strongly " +
"recommended to require 'dm-aggregates'"
end
super
end
def size
@cached_size ||= @array.count
end
def empty?
size == 0
end
end
end
end
end
end
|