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 />

Here you use Capybara’s all method, which will find all the elements matching the<br />

CSS selector specified and return them as an Array object. Then it’s a simple matter of<br />

calling count on that Array and making sure it contains as many as you say it should.<br />

You final undefined step asserts that you’re on a specific page of the pagination.<br />

Write this step like this:<br />

Then /^I see page (\d+) of tickets for this project$/ do |number|<br />

current_page = find(".pagination .current").text.strip<br />

current_page.should eql(number)<br />

end<br />

You use the find method rather than the all method because there is only going to<br />

be one current page element. This method will only return a single element, which<br />

matches the specified CSS selector. Calling text on this element will return the number<br />

inside it, and you can strip it (as it contains spaces) and then compare that to the<br />

number passed in for your step. If the two numbers are equal, then this step will pass.<br />

With the last of your undefined steps now actually defined, let’s see what happens<br />

when you run bin/cucumber features/pagination.feature:<br />

Then I should see 2 pages of pagination<br />

expected 2<br />

got 0<br />

IMPLEMENTING PAGINATION HELPERS<br />

Your step that checks for two pages of pagination wasn’t able to see any at all, most<br />

likely because you aren’t showing any right now! To fix this, you’ll have to display the<br />

pagination link in app/views/projects/show.html.erb by putting this line above the ul<br />

that displays tickets:<br />

<br />

This line will display the pagination links that your failing step currently requires.<br />

You’re going to need to set up the @tickets variable for pagination in your controller<br />

so that these pagination links know what page you’re on and that there are only 50<br />

tickets displayed. You’ll replace this line in the show action of app/controllers/<br />

projects_controller.rb<br />

@tickets = @project.tickets<br />

with this line:<br />

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

This page method will set @tickets to display only the tickets for the current page<br />

number, available in the params[:page] variable. The per method after it will retrieve<br />

50 ticket objects rather than Kaminari’s default 25. Your tickets don’t take up much<br />

room on your page so you can bump this up.<br />

When you run your feature again with bin/cucumber features/paginating<br />

_tickets.feature, it will pass because you’ve now got your pagination links showing:<br />

439

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

Saved successfully!

Ooh no, something went wrong!