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.

278 CHAPTER 10 Tracking state<br />

10.5 Locking down states<br />

This feature is going to take a little more than hiding the State select box on the form;<br />

you also need to tell the application to ignore the state parameter if the user doesn’t<br />

have permission to change the state. You implement this one piece at a time, beginning<br />

with ensuring the State select box is hidden from those who should be unable to<br />

change the state.<br />

10.5.1 Hiding a select box<br />

In previous chapters you’ve seen how you can hide links from certain users by using<br />

the CanCan-provided can? view helper. You can also use this helper to hide the state<br />

field in your comment form from users without the permission to change the state.<br />

First, you write a Cucumber scenario to ensure that the State box is always hidden<br />

from these users.<br />

You add this particular scenario to the bottom of the features/creating<br />

_comments.feature because its operation is based around creating a comment. The scenario<br />

to ensure that you don’t see this state field is a short and simple one:<br />

Scenario: A user without permission cannot change the state<br />

When I follow "Change a ticket's state"<br />

Then I should not see the "#comment_state_id" element<br />

This scenario contains two simple steps: one to go to the ticket page and another to<br />

assert that you don’t see the State select box. When you run this scenario by running<br />

bundle exec features/creating_comments.feature:44, you see that the second step<br />

isn’t yet defined:<br />

Undefined step: "I should not see the "#comment_state_id" element"<br />

As usual, the definition for this step appears at the bottom of Cucumber’s output:<br />

Then /^I should not see the "([^"]*)" element$/ do |arg1|<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

To define this step, you put its definition inside of features/step_definitions/<br />

application_steps.rb. To make it do the thing you want it to do, you can use one of<br />

Capybara’s helper methods called find.<br />

find can take many different forms, but in this case you’ll be using the following<br />

form:<br />

find(:css, css)<br />

By passing these two options to find, Capybara will look for any element on the page<br />

that matches the CSS selector. In this case, that would be css. If find does find an element<br />

or even a collection of elements, it will only return the first element from this list.<br />

If it can’t find anything, it will return nil. Using this knowledge, you can implement<br />

this step now as shown, putting it inside features/step_definitions/application_steps.rb:

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

Saved successfully!

Ooh no, something went wrong!