I enjoy writing Rails applications. The Rails framework is mature and has become a very flexible development environment. Given that everybody has a slightly different way of dealing with day to day issues. Things tend to slows down and become more complicated as a framework matures and attempts to meet every bodies needs.
With Rails, I usually spend some time tweaking the initial application to get it to conform to the way that I want. This includes getting certain gems integrated into the application that I use in development. Wouldn't it be nice if I could customize this and save time?
Rails allows for the customization of applications through a Rails Application Template. An application template is a simple ruby file that uses a Domain Specific Language for adding gems/initializers. The documentation states that the template is evaluated in the context of a Rails::Generators::AppGenerator instance. This means that it is called using the apply action of Thor. So you can use the actions provided by the Thor gem to perform file operations, run command, and insert content into files.
By using my own application template I can create a default rails application configured to meet my requirements. This saves me valuable development time.
My Custom Rails Application Template
I like to use the following gems: Thin, Compass-Rails, Modernizr, Susy, RSpec, and Capybara. I also like to use BetterErrors and RailsPanel for debugging. Some require source code tweaks before they are fully functional in the application. This can be automated with the custom Rails Application Template shown below.
<script src="https://gist.github.com/rdhiggins/8357835.js"></script>
The script starts out by replacing the README.rdoc file with a skeleton README.md file. It also includes the list of gems that I typically use.
The RSpec bundle needs to be installed in order to initialize RSpec support under rails. This requires that the "bundle install" command is run. After this command has executed the generate "rspec:install" command can be called. Capybara requires a "spec/features" directory.
The remaining portion of the template performs the file modifications that fully integrate the gems into the application. This is accomplished using the Thor method inject_into_file.
Compass support is realized through changing the application.css file into an 'application.css.scss' file. This new file contains the basic imports for compass and well and suzy for grid development.
Capybara support is inserted into the 'spec/spec_helper.rb' file. BetterErrors only requires a custom initializer when you are not using the default editor. In my case I Use Sublime Text.
The next section modifies the 'application.html.erb' file to support HTML5 Boiler plate page structure. This also includes the addition of the Modernizr javascript library and common viewport meta tags.
The template asks the user if they want a root controller generated. The template then calls the generate command and then installs a root route.
The last section of the script performs the steps to initialize a Git repository.
Other Possibilities
There are other possibilities if a custom application template is not your cup of tea. Numerous sources of templates can be found by performing a google search. Rails Composer is also worth checking out. It provides a 'composer.rb' template that supports a large number of configuration options.
References
- Rails Application Template documentation
- Rails Composer
- Custom Rails Application Template Gist
- Thor
- Thor Actions
- Thin
- Compass-Rails
- Modernizr
- Susy
- RSpec
- Capybara
- BetterErrors
- RailsPanel