Easy Internationalization for Your Rails App with BDD, Part IV

Share this article

Deleting a Location.

In the first part, we built an application that lists locations in English and Spanish.

In the second part, we added the ability to create locations in English and Spanish.

In the third part, we added the ability to edit locations in English and Spanish.

In this part we will delete a location, showing localization concerns along the way. Remember, we are using Cucumber and BDD to drive out our application.

Why don’t we set up four locations and then delete the third location?

How would you write the scenario to do that? We can write the scenario like:

[gist id=”1796647″]

That will give us the four locations, make sure we are on the English site, delete the third location, and have three locations left. Also, notice we’re using the @wip tag again so we can focus on this test.

Now let’s run Cucumber and see what happens.

[gist id=”1796385″]

At least one of the prevoius steps we wrote is being used. Let’s implement step definitions for the undefined ones. Open up the features/step_definitions/location_step.rb file and copy in the pending steps.

[gist id=”1796496″]

In the first step we need to get factory girl to create all of the locations. The step will loop through however many times we tell it, creating a location on each iteration. Why don’t we name them “Location 1”, “Location 2″, and so on.

What would be a way to do that? Here’s how I did it.

[gist id=”1796557″]

Probably not the swankiest but you can re-factor later after it passes. Let’s run Cucumber and see what happens.

[gist id=”1796706”]

We’re creating locations like nobody’s business.

Time to implement delete location step:

  • go to the locations index page
  • find the third location in the list
  • click some link to delete it

How do you find the third location in the list?

In Capybara you can use scoping. For example we can look within a list to find an nth child.

[gist id=”1796894″]

First, we go to the index page for locations. Since we are looking for the 3rd location, we look within the list for it. Once the link is found, clink_link deletes the location.

Let’s run Cucumber and see what happens.

[gist id=”1810746″]

The destroy link does not exist, yet. Open up the app/views/locations/index.html.erb file and add the destroy link.

[gist id=”1810760″]

Save the file and run Cucumber.

[gist id=”1810769″]

Cucumber complains about the lack of a delete route. We’ve been here before with show and update. Here’s the /config/route.rb file

[gist id=”1811014″]

Save the file and run Cucumber.

[gist id=”1811022″]

No action for ‘destroy’. Open up the /app/controllers/locations_controller.rb file and add that method.

[gist id=”1811041″]

Save the file and run Cucumber.

[gist id=”1811042″]

Now to implement the next step. If we had four locations and we delete one, then we should have three (Math is FUN). So, Location count should equal three? The answer is in the question

[gist id=”2078099″]

I feel that will work. Oh Cucumber, please let us know if we pass.

[gist id=”2078137″]

GREEN! Break time.

Borrar a Ubicación

It is time to do the same thing in Spanish. Remember what to do? Bingo. You add a new line to example’s table.

[gist id=”2081169″]

Do you think this will be green? Let’s see.

[gist id=”2081180″]

D’oh! We haven’t even looked at the locales yet. We need to add the translation for destroy – both in English and Spanish. Do you remember where those are and what needs to happen?

config/locales/en.yml

[gist id=”2081361″]

confif/locales/es.yml

[gist id=”2081368″]

What else do we need to do? You got it. Change the destroy link to show the translation.

[gist id=”2081385″]

All green please.

[gist id=”2081412″]

Ok, remove the wip tag and roll the dice…

[gist id=”2081467″]

Ship it! Ok, maybe not. I’m sure some more re-factoring and DRYing up could go on. Take the routes for example.

[gist id=”2081866″]

Run rake routes in the terminal and you should see something like this.

[gist id=”2081592″]

We can re-factor the up the location routes with one line.

[gist id=”2081991″]

Now rerun rake routes

[gist id=”2082053″]

I know. Why didn’t we do that in the first place? Two reasons:

1) We were just writing code to make the tests pass. It wasn’t until later that we knew we needed them all.
2) Look at how much you’ve learned about the magic of routes. Some of the mystery has been revealed.

Now, rerun cucumber and make sure all the tests still pass. Did yours?

I know during this series we did some stuff the long way. Did it take the mystery out of internationalization? Now it’s time to take your applications global.

 

Cheers,

John

John IvanoffJohn Ivanoff
View Author

John is a Dallas-based front-end/back-end web developer with 15+ years experience. His professional growth has come from big corporate day jobs and weekend freelance. His is enjoys working the Ruby the most these days and has even added pain to the process by developing Rails for Windows! He’s had many years of enjoyment with Cold Fusion and has strong background in web standards.

i18n
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week