Using filters in contollers
If you don’t know what is contoller filters and how use them, checkout oficial documentation. In our case we have to know that there are posibilities to apply filters for one action and this one form of using:
This code looks too bad and I don’t know how you fill when you see it, but my brain explodes.
Useing Gon in a DRY way
So go down to business and make something with this peace of ugly code. Let’s create LocationGonFilter class and move it to app/filters folder. You should create this folder if you don’t have it.
After this we can refactor our controller:
Test for this filter will be look like this:
I’m using FactoryGirl here to create locations
PS. I believe that every class should be responsible for only one thing (this is a Single responsibility principle (SRP)). In my post I’ve explained how to achieve it for controller layer. Filters are separated layer and there we should filter parameters, controllers should inly listen requests, pass action to bottom layer (model) and then response on request, they should not contain complidated logic