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.

308 CHAPTER 11 Tagging<br />

This is because, like the tags parameter, you’re attempting to set a string value on a<br />

field that is actually an association. You must take the state key out of the parameters<br />

hash inside this step so that it is not parsed as a normal field of a ticket.<br />

To fix this little issue, open features/step_definitions/ticket_steps.rb and change the<br />

step definition to be this:<br />

Given /^"([^\"]*)" has created a ticket for this project:$/ do |email, table|<br />

table.hashes.each do |attributes|<br />

tags = attributes.delete("tags")<br />

state = attributes.delete("state")<br />

ticket = @project.tickets.create!(<br />

attributes.merge!(:user =><br />

User.find_by_email!(email)))<br />

ticket.state = State.find_or_create_by_name(state) if state<br />

ticket.tag!(tags) if tags<br />

ticket.save<br />

end<br />

end<br />

On the second line of this step definition, you remove the state key from the<br />

attributes hash, using the delete method again. On the second-to-last line you<br />

assign the ticket’s state, but only if there was a state defined from attributes. The<br />

ticket would be saved if you had specified a tag in the attributes, but if you didn’t then<br />

you need to call save again, as you do on the final line of this step definition.<br />

With all the Background fiddling done, you can add a scenario that searches for<br />

tickets with a given state. It goes like this:<br />

Scenario: Finding by state<br />

When I fill in "Search" with "state:Open"<br />

And I press "Search"<br />

Then I should see "Tag!"<br />

And I should not see "Tagged!"<br />

This should show any ticket with the open state, and hide all other tickets. When you<br />

run this feature with bin/cucumber features/searching.feature, you see that this is<br />

not the case. It can still see the Tagged! ticket:<br />

And I should not see "Tagged!"<br />

Failed assertion, no message given. (MiniTest::Assertion)<br />

When a user performs a search on only an undefined label (such as your state label),<br />

Searcher will return all the records for that table. This is the behavior you are seeing<br />

right now, so it means that you need to define your state label in your model. Let’s<br />

open app/models/ticket.rb and add this line to your searcher block:<br />

label :state, :from => :state, :field => "name"<br />

With this label defined, your newest scenario will now pass when you re-run bin/<br />

cucumber features/searching.feature:<br />

2 scenarios (2 passed)<br />

26 steps (26 passed)

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

Saved successfully!

Ooh no, something went wrong!