Blog
September 2009
First of all, if you're not using will_paginate for all of your pagination then you should be. It's well known that the built in pagination in Rails is slow and clunky. will_paginate will easily replace your ActiveRecord pagination, but it can paginate just about anything else.
John Nunemaker's Twitter Plugin makes short work of hooking your app up to the Twitter API. With a few strokes of code you can be pulling statuses down like mad. Now if you want to list a bunch of statuses you'll likely want to have some kind of pagination.
First there's the standard Auth
httpauth = Twitter::HTTPAuth.new(username, password)
client = Twitter::Base.new(httpauth)
Then setup some pagination defaults, and grab some tweets.
current_page = params[:page] || 1
per_page = 100
tweets = client.user_timeline(:count => per_page, :page => current_page)
Then just wrap the paginator around your collection and the deed is done.
if !tweets.empty?
@tweets = WillPaginate::Collection.create(current_page, per_page, tweets.first.user.statuses_count) do |pager|
pager.replace(tweets)
end
end
Finally, insert the pagination links in the view.
<%= will_paginate @tweets %>
That's it. A marriage of two great plugins indeed.
When people find out I've been renovating my house for 7 years they always ask, "where did you learn how to do all that stuff?" The truth is that I learned it all as I was doing it. I've replaced copper and PVC plumbing, run gas lines, rebuilt door and window frames, installed kitchen cabinets, sinks and countertops, dry walled, floored, painted, electrified as well as many other things I had no idea how to do before we moved in. It's not because I'm a super awesome dude, it's because I shed my fear of learning new things.
When you think about it, companies make products that are easy to use. Just because you're a professional plumber doesn't mean you want to spend hours putting together an unnecessarily complex system. Sure there are things best left to the professionals. I wasn't about to rent a backhoe and dig up my own sewer line, or get up on 30 foot scaffolding to hang siding.
I constantly think about this in my career. Sometimes I avoid adding a new feature, or trying a new tool because I'm afraid that it will be too complicated or take too much time. This reminds me of when XML was just a buzzword. For a long time I just heard descriptions of it and I was so afraid that it was some crazy complex system that I would never understand. Then when I actually saw what they were talking about I was like "That's it?!".
Most things in this world are much more simple than they look from the outside. If you just jump in, it's really not too bad.

