Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

Paginated Squirrel

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.


About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.