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.

Pagination<br />

Alright, so that one’s passing. Let’s see what happens when you run rake cucumber:ok<br />

spec again:<br />

66 scenarios (66 passed)<br />

756 steps (756 passed)<br />

# and<br />

72 examples, 0 failures, 19 pending<br />

All areas where you need pagination are working. You’ve called the per method twice,<br />

once in the show action of the ProjectsController and again in the search method<br />

of TicketsController. If you wish to change the number of elements returned for a<br />

list of tickets, you’d need to change both of these locations. Instead, you’ll move the<br />

setting for the number of ticket objects returned on each page into the model.<br />

CLEANING UP AFTER YOURSELF<br />

Let’s take the per(50) call out of this line in the show action of ProjectsController<br />

@tickets = @project.tickets.page(params[:page]).per(50)<br />

turning it into this:<br />

@tickets = @project.tickets.page(params[:page])<br />

Next, you’ll do the same thing for the line that uses per in the search action of the<br />

TicketsController<br />

@tickets = @tickets.page(params[:page]).per(50)<br />

changing it into this:<br />

@tickets = @tickets.page(params[:page])<br />

To make 50 objects the default for this model, you can put this line directly under the<br />

searcher block in your Ticket model:<br />

paginates_per 50<br />

Now when your application is asked for a list of paginated tickets, you’ll be given 50<br />

tickets per page. You can make sure this is the case by rerunning your “Paginating tickets”<br />

feature by running bin/cucumber features/paginating_tickets.feature:<br />

1 scenario (1 passed)<br />

10 steps (10 passed)<br />

Alright, that’s still working, so that’s good! Are your features and specs still working<br />

too? Another quick run of rake cucumber:ok spec will tell you:<br />

66 scenarios (66 passed)<br />

752 steps (752 passed)<br />

# and<br />

72 examples, 0 failures, 19 pending<br />

Great, time to make a commit with this new feature:<br />

git add .<br />

git commit -m "Added pagination for tickets"<br />

git push<br />

441

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

Saved successfully!

Ooh no, something went wrong!