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.

266 CHAPTER 10 Tracking state<br />

You can go one step further and move this code into a helper. Views are more for<br />

displaying information than for deciding how it should be output, which should be left<br />

to the helpers and controllers. Move this code into the app/helpers/tickets_helper.rb<br />

because this partial is displayed from the TicketsController’s show template. The<br />

entire TicketsHelper should now look like the following listing.<br />

Listing 10.20 app/helpers/tickets_helper.rb<br />

module TicketsHelper<br />

def state_for(comment)<br />

content_tag(:div, :class => "states") do<br />

if comment.state<br />

if comment.previous_state && comment.state != comment.previous_state<br />

"#{render comment.previous_state} &rarr; #{render comment.state}"<br />

else<br />

"&rarr;" + render(comment.state)<br />

end<br />

end<br />

end<br />

end<br />

end<br />

In this example, you check to see if the comment has an assigned state and then if it<br />

has a previous state. If it has a previous state that isn’t the assigned state, then you<br />

show the state transition; otherwise you render the assigned state.<br />

You can now replace the whole if statement in app/views/comments/_comment<br />

.html.erb with this single line:<br />

<br />

Now check to see if this is working by running your scenario using bin/cucumber<br />

features/creating_comments.feature:33:<br />

Then I should see "State: Open" within "#comments"<br />

expected there to be content "State: Open" in<br />

"\n \n user@ticketee.com (User)\n \n<br />

Open\n\n This is a real issue\n\n"<br />

It’s still failing? Are you sure you’ve set everything up correctly? Absolutely. Capybara<br />

(through Cucumber) is telling you that it still can’t find this text on the page. Lucky<br />

for you there’s a way to see the page that Cucumber is seeing: the “Then show me the<br />

page” step you saw a little earlier.<br />

10.3.4 Show me the page<br />

When you use this step, it saves the current page to a file and opens it in the browser<br />

so you’re able to see exactly what Cucumber is seeing. Let’s put this step as the secondto-last<br />

step in “Changing a ticket’s state.” The last three steps in this scenario should<br />

look like the following:

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

Saved successfully!

Ooh no, something went wrong!