Everyone loves will_paginate, the super-simple, yet powerful library for pagination in Ruby on Rails. But, with the current 2.2.0 release, there are many reasons to love it even more.
Why, just look at what folks on Twitter had to say for older releases:
This has always been more than just code. It was beauty to write so that it can be beauty to use. Read on about what’s new in your favorite plugin.
Kyle Neath is a programmer with attitude; he knows that you can sit back in your chair while pagination just writes itself out like poetry underneath the inked feather of a great writer.
# in PoemsController
def index
@poems = Poem.paginate :page => params[:page], :order => 'created_at DESC'
end
# in views/poems/index.html.erb
<%= will_paginate %>
By the name of controller
will_paginate shall know
From where poems should flow.
Josh Susser discovered that the will_paginate gem is a real jewel to install. It is the same code like in the plugin, but it’s available anytime, anywhere!
gem sources -a http://gems.github.com/
gem install mislav-will_paginate
Hook it up in your app and you’ll be good to go:
# in config/environment.rb
gem 'mislav-will_paginate', '~> 2.2.0'
require 'will_paginate'
To find out more, hop to installation instructions section on our wiki.
What’s even better, there are news of even nicer gem dependencies for your application. Rails 2.1 is building up to be an awesome release.
Chris Saylor, if you loved “scope out” you would have adored has_finder. Then you would also dance with delight when it got merged to Rails 2.1 as named_scope
. But then you would become sad when you realize you have older apps and how they use Rails 1.2 or 2.0 where this is not available. :(
I’m here to cheer you up. I’ve actually done a sneaky thing: stolen named_scope
from edge Rails and made it available if you install plugin or gem version 2.2.0 even on older versions of Rails. I’ve once told Josh that William Paginate and has_finder will have a love child some day—now it has happened.
# in environment.rb
WillPaginate.enable_named_scope
# the Poem model
class Poem << ActiveRecord::Base
belongs_to :author
named_scope :about_love, :conditions => ['poems.body LIKE ?', '%love%']
end
Poem.about_love.find(:first)
# -> Oh, how quickly daft jumping zebras love!
# this code reads out like English ... and is also poetry by itself
Author.find_by_name('Nick Kallen').poems.about_love.paginate(:page => 1)
How amazing is it, Brian? But you don’t have to answer that—benchmarks show that will_paginate 2.2.0 is almost 2x more amazing than 2.1.0 when it comes to to speed of rendering in the views. I’ve optimized WillPaginate::LinkRenderer
to do our bidding much more efficient than before.
I really can’t tell, Geoffrey. What probably happened is that they tried to roll it in, but when they actually looked at it they were so overwhelmed by the beauty of its code that they burst into tears of joy, which filled up their keyboards and quickly rendered their MacBooks unusable for some time.
And by the way, God bless the person from whom I learned to write plugins. Who was that? Why, it was you!
Sandro obviously spent some quality nights with it. Did he dress accordingly? Unstyled, naked pagination links look as small twigs in the dirt. With a dash of CSS they can blossom like Japanese cherry in the spring.
You may not notice it, but page links are much more semantic than in previous versions … and a little bit easier to style. Designers rejoice.
You are not alone, Rick. It continues to rock everyone’s world. And, same as rocking out, it never ends.