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.

166 CHAPTER 8 More authorization<br />

Underneath the there is a project step in the Background for this feature is a<br />

new step B. It’s responsible for giving the specified user access to the specified project,<br />

but not just any permission: permission to view the project. This step is currently<br />

undefined, so when you run bin/cucumber features/viewing_projects.feature,<br />

you get the step definition for it:<br />

Given /^"([^"]*)" can view the "([^"]*)" project$/ do |arg1, arg2|<br />

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

end<br />

To implement this step, you use the not-yet-existent Permission model, which stores<br />

the permissions in the database. This model needs a related table called permissions,<br />

which contains three fields.<br />

The first field is the action field, which keeps track of the type of permission a<br />

user has on particular objects. The objects can be of different types, so you must create<br />

two fields to track the association to the object: thing_type and thing_id. This<br />

kind of association is called a polymorphic association, poly meaning “many” and morphic<br />

meaning “forms,” which is fitting. You’ll see more on these in a little while.<br />

One more field you add to this permissions table is a user_id column linking that<br />

Permission to a User.<br />

With all of that in mind, you can define this step in a new file at features/<br />

step_definitions/permission_steps.rb, as shown in the following listing.<br />

Listing 8.2 features/step_definitions/permission_steps.rb<br />

Given /^"([^"]*)" can view the "([^"]*)" project$/ do |user, project|<br />

Permission.create!(:user => User.find_by_email!(user),<br />

:thing => Project.find_by_name!(project),<br />

:action => "view")<br />

end<br />

In listing 8.2, you create a new Permission record with the action defined as view<br />

linking the project and user passed in. This record defines the users who can access<br />

the project. When you run this feature, you get an error because the Permission class<br />

is not yet defined:<br />

And "user@ticketee.com" can view the "TextMate 2" project<br />

uninitialized constant Permission (NameError)<br />

Define it now by generating the model using the following command, typed all on<br />

one line:<br />

rails generate model permission user_id:integer thing_id:integer<br />

thing_type:string action:string<br />

With this model and its related migration, you can run rake db:migrate and rake<br />

db:test:prepare to set up the development and test databases. When you run your<br />

feature again, you get this error message:<br />

And "user@ticketee.com" can view the "TextMate 2" project<br />

unknown attribute: user (ActiveRecord::UnknownAttributeError)

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

Saved successfully!

Ooh no, something went wrong!