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.

10.2.4 Seeding states<br />

Changing a ticket’s state<br />

If you add some states to the db/seeds.rb file, users will be able to select them from the<br />

State drop-down box on the tickets page rather than leaving it blank and useless,<br />

much like it is now. With these states in the db/seeds.rb file, as mentioned before, you<br />

will have a repeatable way of creating this data if you ever need to run your application<br />

on another server, such as would be the case when you put the application on another<br />

computer.<br />

You’re adding these files to the db/seeds.rb so you have some to play around with<br />

in the development environment of your application. You’re attempting to figure out<br />

why, when a user picks Open from the State select box and clicks Create Comment,<br />

the state doesn’t display on the ticket that should be updated.<br />

When you go to the Ticketee Beta project to create a ticket and then attempt to create<br />

a comment on that ticket with the state of Open, you’ll see that there are no states<br />

(as shown in figure 10.7).<br />

You should add a couple of states to your seeds file now; they’ll<br />

be New, Open, and Closed. Ideally, New will be the default state of<br />

tickets, and you’ll set this up a little later on. Before adding these<br />

states, let’s add a couple of steps to features/seed.feature to always<br />

ensure that your states are defined.<br />

You extend this feature to go inside the Ticketee Beta project,<br />

create a ticket, and then begin to create a comment on that ticket. When it’s on the<br />

comment-creation screen, you check to see that all your states are in the state box. To<br />

do this, modify the scenario in this file to what’s shown in the following listing.<br />

Listing 10.14 features/seed.feature, the basics scenario<br />

Scenario: The basics<br />

Given I have run the seed task<br />

And I am signed in as "admin@ticketee.com"<br />

When I follow "Ticketee Beta"<br />

And I follow "New Ticket"<br />

And I fill in "Title" with "Comments with state"<br />

And I fill in "Description" with "Comments always have a state."<br />

And I press "Create Ticket"<br />

Then I should see "New" within "#comment_state_id"<br />

And I should see "Open" within "#comment_state_id"<br />

And I should see "Closed" within "#comment_state_id"<br />

The #comment_state_id element referenced here is the State select box for your comments,<br />

and you’re confirming that it’s got the three states you’re going to be seeding<br />

your database with. When you run this feature by running bin/cucumber features/<br />

seed.feature, it will fail because you don’t have your states yet:<br />

Then I should see "New" within "#comment_state_id"<br />

is not true. (Test::Unit::AssertionFailedError)<br />

Let’s add these states to your db/seeds.rb file by using the lines shown in the following<br />

listing.<br />

259<br />

Figure 10.7<br />

Oops! No states!

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

Saved successfully!

Ooh no, something went wrong!