Paginated Squirrel

Posted by Jon Yurek

Mar 30

Latest and greatest…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

001 >> posts = Post.find(:all) do
         updated_on > 1.year.ago
         paginate :page => 4, :per_page => 10
       end
    => [...]
005 >> posts.total_results
    => 46
006 >> posts.pages.current
    => 4
007 >> posts.pages.current_range
    => 31..40
008 >> posts.pages.next
    => 5
009 >> posts.pages.previous
    => 3

The new paginate squirrel method takes a hash with :page (1-indexed) and :per_page (which default to 1 and 20, respectively). When you use it, your results array is automatically extended with a helpful pages method that gives you all you need to know to paginate your views. It also puts a total_results method on there, so you know how big the whole search is.

And before you ask, yes, I know some of you asked for this to work with paginating_find, and it does. But we found that paginating_find was conflicting with other plugins, leading to some really weird errors. Plus, it’d always been my intention to add pagination (among other things) anyway.

Please Note: The repo has changed. It’s now at https://svn.thoughtbot.com/plugins/squirrel/trunk for the trunk and https://svn.thoughtbot.com/plugins/squirrel/tags/rel-1.0.0 for this release.


Sorry, comments are closed for this article.

© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots