On our search for productivity and automation, we’ve tailored a template for Rails applications to make starting and configuring a new project a matter of minutes
Usually, the projects we tackle at CookiesHQ share a common set of features (shared by the vast majority of Rails projects anyway), and also we like to use some gems and ways to organize our files always, because that’s how we are used and like to work.
So, if we are going to use, let’s say, Devise, in 85% of your projects, and you have to go through installing and configuring Rspec and Capybara on each new project… wouldn’t it be a good idea to automate this?
When we run
rails new app_name we get a new empty application, Rails runs certain commands and launches some generators that result in our application skeleton. Rails provides a way of meddling in this generation process by allowing us to pass a template to the command, given that our template uses the methods provided by Thor, along with the Rails’ application template API.
Using those, we can inject actions in the Rails application generator, and introduce gems and files, modify existing files, ask for and give info to the user, run rake commands and rails generators… enough to replicate as much as possible the first configuration we used to do manually.
Without further ado, here’s the CookiesHQ Rails Application Template. Use it right away by cloning the repo and then run the rails generator passing the template:
rails new app_name -m cookieshq-rails-template/template.rb
Have in mind that you’ll need to have Ruby, and obviously Rails, installed!
What you get by using it
The read me file has all the info, but I’ll try to give you the highlights here:
- Rspec, Capybara and some other testing gems installed, configured and ready to go.
- If you choose to: Devise, a Devise User model and ActiveAdmin (with it’s AdminUser).
- PostgreSQL Database creation and migration.
- A gem to deploy on Heroku, or a capified project.
- Bootstrap gem installed, configured and working.
- Stylesheet folder structure that is SMACSS compliant.
- HAML views, and a layout that includes several meta tags to help you build a responsive app.
- Roadie gem with base layout and CSS based on HTMLEmailBoilerplate to apply styles to emails, as we described here.
- A first acceptance test and test run that will fail, to get your TDD/BDD on 😉
As common maintenance, we will keep the template usable by bug fixing when needed, Ruby version bumping, and adapting to new versions of Rails as they are released.
As improvements, it would be nice to revamp the files included in the repo and embed them in the template, so you can call it straight from Github instead of having to clone the repo. To do that comfortably, we would move all the long strings to auxiliary methods to keep code readable and easy to follow.
Also, we’ll try to create a tmuxinator config file so working on the project is even more automated from the beginning.
And of course, we’ll hear your suggestions and advice, wether they come as comments or pull requests.
So, checkout the CookiesHQ Rails Application Template on Github
and let us know what you think. We hope you find it as useful as we do, and we’ll be very happy if it inspires you to create your own! If that were the case, here is some reference material you might find of help:
- Rails guides on Application Templates
- Rails Application Templates on Stitchfix
- Thor actions list
- Rails guides on Generators
Picture Bring Me All The Blueprints by Big Dave Diode on Flickr