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.

204 CHAPTER 8 More authorization<br />

<br />

<br />

<br />

<br />

<br />

Here you use just the key from the hash to define a uniquely identifiable name for this<br />

check box. The second argument is the value returned to the controller, which you<br />

use to determine whether or not this check box is checked. The third argument uses<br />

the @ability object to determine whether or not this check box is displayed as<br />

checked. By using this method, you get a tag like this:<br />

<br />

You’re given both the id and name attributes, which are generated from the first argument<br />

you passed to check_box_tag. The id attribute indicates not the permission’s ID<br />

but the ID of the project that you’re determining the permission is for. You use the id<br />

attribute shortly to check this box using Capybara and the parsed-into-params version<br />

of the name attribute just after that in your controller.<br />

When you run bin/cucumber features/assigning_permissions.feature again,<br />

you reach the following undefined step:<br />

When I check "View" for "TextMate 2"<br />

Undefined step: "I check "View" for "TextMate 2"" (Cucumber::Undefined)<br />

...<br />

When /^I check "([^"]*)" for "([^"]*)"$/ do |arg1, arg2|<br />

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

end<br />

Take the snippet at the end of the output and put it in features/step_definitions/<br />

permission_steps.rb underneath the other step definitions but above the methods. You<br />

should end up with a step definition like this:<br />

When /^I check "([^"]*)" for "([^"]*)"$/ do |permission, name|<br />

project = Project.find_by_name!(name)<br />

permission = permission.downcase.gsub(" ", "_")<br />

field_id = "permissions_#{project.id}_#{permission}"<br />

steps(%Q{When I check "#{field_id}"})<br />

end<br />

For this step definition, you must first find the project by its name so you can get its ID,<br />

because a later part of this step that checks the permission check box requires the ID<br />

of the project to compile the ID for the fields. 3 Then you use the handy steps method<br />

you used in chapter 7 to call another step where you pass in the downcase’d and<br />

3 The project name won’t change, but the ID might, so you use the identifier that’s unlikely to change.

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

Saved successfully!

Ooh no, something went wrong!