Got a favorite movie filmed in San Francisco? There are lots of great choices.
This for fun project uses a cool data set from Data SF as well as movie posters from The Movie DB (TMDB) all in service of providing a sample implementation for the Filterameter gem.
Filterameter provides declarative filters for query classes or Rails controllers to reduce boilerplate code and increase readability. How many times have you seen (or written) this controller action?
def index
@films = Films.all
@films = @films.where(name: params[:name]) if params[:name]
@films = @films.joins(:film_locations).merge(FilmLocations.where(location_id: params[:location_id])) if params[:location_id]
@films = @films.directed_by(params[:director_id]) if params[:director_id]
@films = @films.written_by(params[:writer_id]) if params[:writer_id]
@films = @films.acted_by(params[:actor_id]) if params[:actor_id]
end
It's redundant code and a bit of a pain to write and maintain. Not to mention what RuboCop is going to say about it. (If your head just went there, I checked: the 1987 film RoboCop was set in Detroit but filmed primarily in Dallas.)
Wouldn't it be nice if you could just declare the filters that the controller accepts?
filter :name, partial: true
filter :location_id, association: :film_locations
filter :director_id, name: :directed_by
filter :writer_id, name: :written_by
filter :actor_id, name: :acted_by
That's the value proposition of Filterameter. I hope you'll check it out.
- GitHub: https://github.com/RockSolt/filterameter
- Ruby Gems: https://rubygems.org/gems/filterameter
The film data is checked into the repo as a CSV file and can be loaded with a rake task:
bundle exec rake film_data:load
Once the data has been loaded, run the server with bin/dev
.
This product uses the TMDB API but is not endorsed or certified by TMDB.
In order to be able to look up the movie posters, you need to create an API key with TMDB. Then provide the key via environment variable TMDB_API_READ_ACCESS_TOKEN
.
Film data from DATA SF.
Movie posters from The Movie DB.