Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 178fafc959
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (58 sloc) 2.386 kb
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
from: Lin Jen-Shin (aka godfat 真常)
date: Wed, Nov 18, 2009 at 1:41 AM
subject: pagination tool i wrote

Sorry that I can't express clearly in English speaking... :s
Here's the source for pagify I mentioned before:
http://github.com/godfat/pagify

I broke things into 4 parts:
1. pager
2. page
3. pagifier
4. helper

Pager is which uses `fetcher' and 'counter' to
calculate offset and limit. In ActiveRecord,
they are :find and :count.
http://github.com/godfat/pagify/blob/master/lib/pagify/pager/active_record.rb
In DataMapper, they are :all and :count.
You can build your own by using:
 NullPager =
   BasicPager.new(:fetcher => lambda{|*a|[]}, :counter => lambda{0})
Or just extending BasicPager.
NullPager is which always return nothing,
used to pretend that there's no data at all.

Page is which roughly equal to WillPaginate::Collection.
Currently there are only BasicPage and NullPage.
http://github.com/godfat/pagify/blob/master/lib/pagify/page/basic.rb

NullPage is something like empty WillPaginate::Collection.
Sometimes you would want:
 Message.pagify(:page => random_page).each{ |m|
   puts m
 }
always work even if random_page is really out of range.

Pagifier is which make Message.pagify work right.
http://github.com/godfat/pagify/blob/master/lib/pagify/pagifier/active_record.rb

Note that `User.pagify.some_filter.another_filter` would work
under DataMapper but not ActiveRecord, and you have to make
Array "pagified" too, or ActiveRecord would fail on some places.
DataMapper isn't suffering from this, so Array should be clean.
(never monkey patch too many things in build-in classes)

Helper is which deal with HTML generating.
There's HTML, Rails, and Innate helper currently.

Links are generated by `request.path` and appended with
a page query string in URL in Rails. You could provide a
block to override this default:
 pagify_links(@messages){ your_awesome_url }
Or a lower level operation:
 @messages.html.links{ |page|
    "/message/list.html?page=#{page}"
  }

Setting is per pager basis:
 @pager.html.setting[:prev_text] = '<<'
Or global default:
 Pagify::Helper::HTML.setting[:prev_text] = '<='

It's fun to invent something when I know that
it's not a very hard task and I could probably
do better. No matter whether do I really do better,
at least I know what I've written, and I know where
should I look into if there's something wrong.

cheers,
Something went wrong with that request. Please try again.