27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

436 CHAPTER 16 Basic performance enhancements<br />

16.1.1 Introducing Kaminari<br />

The Kaminari gem 1 is a new breed of pagination gem written by Akira Matsuda, and is<br />

considered the Rails 3 successor to the will_paginate gem, 2 which was the favorite<br />

for a long time. 3<br />

After you install this gem, you’re given an interface on the models of your application,<br />

which allows you to make calls like this:<br />

@project.tickets.page(2).per(50)<br />

This call would ask for the second page of tickets, with each page containing 50 tickets.<br />

It’s a very clean API. Those familiar with will_paginate will be used to a syntax like this:<br />

@project.tickets.paginate(:per_page => 50, :page => 2)<br />

The syntax is a little longer, but it’s a little clearer what it’s doing to those who are<br />

familiar with it. You’ll use Kaminari here just for something different. In your views,<br />

you can use the same paginate method, which is made available by both gems:<br />

<br />

This little helper generates the output shown in figure 16.2.<br />

To install this gem, add this line to your Gemfile underneath the searcher gem:<br />

gem 'kaminari'<br />

You’ll then run the bundle install command to install the gem. With the gem<br />

installed, you can now begin to write a Cucumber feature to test that when you’re on<br />

Figure 16.2 Pagination helper<br />

1 http://github.com/amatsuda/kaminari.<br />

2 http://github.com/mislav/will_paginate.<br />

3 Since this original writing, will_paginate has been updated to be Rails 3 compatible.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!