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.

Fixing what you broke<br />

else<br />

Project.readable_by(current_user)<br />

end<br />

This statement is useful when you have short conditional statements like this one, but<br />

it shouldn’t be (ab)used for longer conditional statements. As a general rule of<br />

thumb, if the line for a ternary statement is longer than 80 characters, it’s probably<br />

best to split it out over multiple lines for better readability.<br />

In the find_project method, you can call find on what this new for method<br />

returns, and now in the index method, you can use it in an identical fashion, but just<br />

replace the call to find with all, like this:<br />

def index<br />

@projects = Project.for(current_user).all<br />

end<br />

Because you are referencing current_user in this action, you must modify the<br />

before_filter line that references authenticate_user! to ensure that users are<br />

signed in before they visit this page. Let’s change it to this:<br />

before_filter :authenticate_user!, :only => [:index, :show]<br />

When you run the feature again with bin/cucumber features/viewing_projects<br />

.feature, it passes:<br />

1 scenario (1 passed)<br />

9 steps (9 passed)<br />

Ensure that everything is working as it should by running rake cucumber:ok spec.<br />

Oops! You broke some of the scenarios inside the Hidden Links feature, as shown by<br />

this output from the cucumber:ok part of the command you just ran:<br />

cucumber features/hidden_links.feature:23<br />

cucumber features/hidden_links.feature:27<br />

cucumber features/hidden_links.feature:36<br />

cucumber features/hidden_links.feature:40<br />

If you run the first one of these features with bin/cucumber features/hidden_links<br />

.feature:23, you’ll see that it can’t find the TextMate 2 link.<br />

When I follow "TextMate 2"<br />

no link with title, id or text 'TextMate 2' found (Capybara::ElementNotFound)<br />

This error occurs because the user in this feature doesn’t have permission to view the<br />

TextMate 2 project. But if you look at this scenario, it’s for users who are not signed<br />

in—users who can no longer visit this page because, when they do, they are redirected<br />

to the sign-in page. This means that the following scenarios no longer apply and can<br />

be deleted:<br />

Scenario: Edit project link is hidden for non-signed-in users<br />

Scenario: Delete project link is hidden for non-signed-in users<br />

177

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

Saved successfully!

Ooh no, something went wrong!