Permalink
Browse files

Add Helper::Paginate and example

  * Paginates both Array and Sequel datasets
  • Loading branch information...
1 parent f7e8c33 commit 5663881fe34db56aa21463f66e3262641f6b99b1 @manveru manveru committed Aug 12, 2008
Showing with 216 additions and 0 deletions.
  1. +71 −0 examples/helpers/paginate.rb
  2. +145 −0 lib/ramaze/helper/paginate.rb
View
71 examples/helpers/paginate.rb
@@ -0,0 +1,71 @@
+require 'rubygems'
+require 'ramaze'
+
+USERS = DATA.read.split("\n")
+
+class MainController < Ramaze::Controller
+ helper :paginate
+
+ trait :paginate => {
+ :limit => 6,
+ :var => 'user_page',
+ }
+
+ def index
+ @pager = paginate(USERS)
+%q~
+<html>
+
+ <head><title>Users</title></head>
+ <body>
+ #{@pager.navigation if @pager.needed?}
+ <pre>
+ <?r @pager.each do |user| ?>
+ #{user}
+ <?r end ?>
+ </pre>
+ #{@pager.navigation if @pager.needed?}
+ </body>
+</html>
+~
+ end
+end
+
+Ramaze.start
+
+__END__
+Michael Fellinger 1868 [ 77.13% ]
+Aman Gupta 211 [ 8.71% ]
+Jonathan Buch 89 [ 3.67% ]
+Gabriele Renzi 49 [ 2.02% ]
+Ara T. Howard 44 [ 1.82% ]
+Clive Crous 37 [ 1.53% ]
+Keita Yamaguchi 27 [ 1.11% ]
+Ryan Grove 17 [ 0.70% ]
+Pistos 16 [ 0.66% ]
+Wang, Jinjing 11 [ 0.45% ]
+Colin Shea 9 [ 0.37% ]
+Sam Carr 6 [ 0.25% ]
+Stephan Maka 5 [ 0.21% ]
+samcarr 4 [ 0.17% ]
+raggi 3 [ 0.12% ]
+Clinton R. Nixon 3 [ 0.12% ]
+Chris Duncan 3 [ 0.12% ]
+Richard Outten 2 [ 0.08% ]
+Andy Smith 2 [ 0.08% ]
+skaar 1 [ 0.04% ]
+Yasushi Abe 1 [ 0.04% ]
+Thomas Leitner 1 [ 0.04% ]
+Rob Lievaart 1 [ 0.04% ]
+Riku Raisaenen 1 [ 0.04% ]
+Matt Rubens 1 [ 0.04% ]
+Martin Hilbig 1 [ 0.04% ]
+Leo Borisenko 1 [ 0.04% ]
+Lars Olsson 1 [ 0.04% ]
+Jeremy Evans 1 [ 0.04% ]
+Jean-Francois Chevrette 1 [ 0.04% ]
+James Tucker 1 [ 0.04% ]
+Fabian Buch 1 [ 0.04% ]
+Christian Neukirchen 1 [ 0.04% ]
+Carlo Zottmann 1 [ 0.04% ]
+Andrew Farmer 1 [ 0.04% ]
View
145 lib/ramaze/helper/paginate.rb
@@ -0,0 +1,145 @@
+require 'ramaze/gestalt'
+
+module Ramaze
+ module Helper
+ module Paginate
+ trait :paginate => {
+ :limit => 10,
+ :var => 'pager',
+ }
+
+ def paginate(dataset, hash = {})
+ options = ancestral_trait[:paginate].merge(hash)
+ limit = options[:limit]
+ var = options[:var]
+ page = options[:page] || (request[var] || 1).to_i
+
+ Paginator.new(dataset, page, limit, var)
+ end
+
+ class Paginator
+ include Ramaze::Helper::Link
+ include Ramaze::Helper::CGI
+
+ def initialize(data = [], page = 1, limit = 10, var = 'pager')
+ @data, @page, @limit, @var = data, page, limit, var
+ @pager = pager_for(data)
+ end
+
+ def pager_for(obj)
+ case obj
+ when Array
+ ArrayPager.new(obj, @page, @limit)
+ else
+ obj.paginate(@page, @limit)
+ end
+ end
+
+ def navigation
+ out = [ g.div(:class => :pager) ]
+
+ if first_page?
+ out << g.span(:class => 'first grey'){ '<<' }
+ out << g.span(:class => 'previous grey'){ '<' }
+ else
+ out << link(1, '<<', :class => :first)
+ out << link(prev_page, '<', :class => :previous)
+ end
+
+ (1...current_page).each do |n|
+ out << link(n)
+ end
+
+ out << link(current_page, current_page, :class => :current)
+
+ if last_page?
+ out << g.span(:class => 'next grey'){ '>' }
+ out << g.span(:class => 'last grey'){ '>>' }
+ else
+ (next_page..page_count).each do |n|
+ out << link(n)
+ end
+
+ out << link(next_page, '>', :class => :next)
+ out << link(page_count, '>>', :class => :last)
+ end
+
+ out << '</div>'
+ out.map{|e| e.to_s}.join("\n")
+ end
+
+ def link(n, text = n, hash = {})
+ text = h(text.to_s)
+
+ params = Ramaze::Request.current.params.merge(@var => n)
+ hash[:href] = Rs(Ramaze::Action.current.name, params)
+
+ g.a(hash){ text }
+ end
+
+ def g
+ Ramaze::Gestalt.new
+ end
+
+ def needed?
+ @pager.page_count > 1
+ end
+
+ def method_missing(meth, *args, &block)
+ @pager.send(meth, *args, &block)
+ end
+
+ 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 = @array.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
+
+ end
+ end
+ end
+end

0 comments on commit 5663881

Please sign in to comment.