27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Assigning permissions<br />

And I fill in "Title" with "Shiny!"<br />

And I fill in "Description" with "Make it so!"<br />

And I press "Create"<br />

Then I should see "Ticket has been created."<br />

Just as in your first scenario, you check the View check box for the project. Otherwise,<br />

the user wouldn’t be able to see the project where the new ticket link was. Then you<br />

check the Create Tickets check box, update the user’s permissions, and sign out. Next,<br />

you sign in as that user and make sure you can do what you just gave that user permission<br />

to do. When you run this feature with bin/cucumber features/assigning<br />

_permissions.feature, the first step fails because it can’t find the check box:<br />

cannot check field, no checkbox with id, name,<br />

or label 'permissions_1_create_tickets' found (Capybara::ElementNotFound)<br />

Let’s add this check box then! Open app/helpers/admin/permissions_helper.rb, and<br />

add the permission to your hash, changing this method from<br />

def permissions<br />

{<br />

"view" => "View"<br />

}<br />

end<br />

to<br />

def permissions<br />

{<br />

"view" => "View",<br />

"create tickets" => "Create Tickets"<br />

}<br />

end<br />

Keep in mind that the key must match the intended action for the Permission object;<br />

the value is just a label for this permission. The action is what you use in your<br />

authorized? helpers around the application.<br />

When you another key-value pair in this hash, the code inside app/views/admin/<br />

permissions/index.html.erb automatically shows a check box for this new permission.<br />

When you run this feature again, it passes because this new check box is visible and<br />

the permission is applied correctly:<br />

2 scenarios (2 passed)<br />

35 steps (35 passed)<br />

Wasn’t that a piece of cake? Let’s move on to the next permission now: updating tickets.<br />

Actually, let’s do both updating tickets and deleting tickets at the same time.<br />

THE DOUBLE WHAMMY<br />

Just to show the world how great you are at developing this application, you’ll now<br />

write two scenarios and get them both to pass at the same time. Add both of these<br />

scenarios to the end of features/assigning_permissions.feature, as shown in the following<br />

listing.<br />

207

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

Saved successfully!

Ooh no, something went wrong!