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.

270 CHAPTER 10 Tracking state<br />

You’ll start out by writing a feature to create new states, which will involve creating<br />

a new controller called Admin::StatesController. This controller will provide the<br />

admins of your application with the basic CRUD functionality for states, as well as the<br />

ability to mark a state as the default, which all tickets will then be associated with.<br />

We’re not going to look at adding an edit, update, or destroy action to this controller<br />

because it’s been covered previously and should be left as an exercise for you.<br />

10.4.1 Adding additional states<br />

You have three default states from the db/seeds.rb file currently: New, Open, and<br />

Closed. If the admin users of your application wish to add more, they can’t—not until<br />

you’ve created the Admin::StatesController and the new and create actions inside<br />

it. This will allow those users to create additional states which then can be assigned to<br />

a ticket.<br />

You have this lovely State model, but no way for admins of the application to add<br />

any new records to it. What if they want to add more states? You’ll create a new controller<br />

called Admin::StatesController and put a new and create action in it so that<br />

admins can create more states.<br />

But before you write any real code, you’ll write a feature that describes the process<br />

of creating a state. Put it in a new file called features/creating_states.feature, which is<br />

shown in the following listing.<br />

Listing 10.21 features/creating_states.feature<br />

Feature: Creating states<br />

In order to be able to specify other states for tickets<br />

As an admin<br />

I want to add them to the application<br />

Background:<br />

Given there are the following users:<br />

| email | password | admin |<br />

| admin@ticketee.com | password | true |<br />

And I am signed in as them<br />

Scenario: Creating a state<br />

When I follow "Admin"<br />

And I follow "States"<br />

And I follow "New State"<br />

And I fill in "Name" with "Duplicate"<br />

And I press "Create State"<br />

Then I should see "State has been created."<br />

Here you sign in as an admin user and go through the motions of creating a new state.<br />

When you run this feature through using the command bin/cucumber features/<br />

creating_states.feature, it will fail because it can’t find the States link:<br />

And I follow "States"<br />

no link with title, id or text 'States' found (Capybara::ElementNotFound)

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

Saved successfully!

Ooh no, something went wrong!