Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (58 sloc) 2.33 KB
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:
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.
In DataMapper, they are :all and :count.
You can build your own by using:
NullPager = => 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.
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.
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|
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.