🤯 New: Coding Assessments Practice your skills on real-world programming challenges

Easy Internationalization for Your Rails App with BDD, Part IV

John Ivanoff

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:

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.

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.

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.

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

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.

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.

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

Save the file and run Cucumber.

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

Save the file and run Cucumber.

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

Save the file and run Cucumber.

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

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

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.

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

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?



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

All green please.

Ok, remove the wip tag and roll the dice…

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

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

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

Now rerun rake routes

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.




JavaScript: Novice to Ninja, 2nd Edition