Glenn Gillen

Custom Starting Templates for Rails Apps

Okay, so I've been a little slow on the uptake on this one. But for anybody else that missed it, you can now define your own standard setup for a rails app. (And kudos to Peter Szinek, who I work with on the leading ruby web spider/data scraping library, for the heads up on this last week)

Reducing the time taken to start coding

That's essentially what this change does. Most of us probably have out preferred setup, especially if we've deployed a few sites. Typically I create the rails directory structure, then pull in HAML, rspec, rspec on rails, my form helper, restful auth, etc. It's not terribly difficult, but it is laborious. Well someone else I once worked with, Jeremy McAnally (woo!, look at me name drop ;), created a great little tool called rg which would allow you to define your own templates for when you create a new application. Now just like engines has been pulled into the latest edge core to give rails the same ability as merb-slices, so too has rg now been pulled in to match the functionality offered by creating your own custom merb-stack.

How to create a custom rails template

So if like me you have your usual grab bag you want to setup when you start a new app, you can define your own template.rb file like this:

run "rm public/index.html"
git :init

plugin "rspec", :git => "git://github.com/dchelimsky/rspec.git", :submodule => true
plugin "rspec-rails", :git => "git://github.com/dchelimsky/rspec-rails.git", :submodule => true
plugin "exception-notifier", :git => "git://github.com/rails/exception_notification.git", :submodule => true
plugin "restful-authentication", :git => "git://github.com/technoweenie/restful-authentication.git", :submodule => true
plugin "semantic-form-builder", :git => "git://github.com/rubypond/semantic_form_builder.git", :submodule => true
plugin "paperclipped", :git => "git://github.com/kbingman/paperclipped.git", :submodule => tru

gem "will-paginate", :git => "git://github.com/mislav/will_paginage.git"
gem "haml", :git => "git://github.com/nex3/haml.git"
rake "gems:install", :sudo => true
run "haml --rails ."

route "map.login '/login', :controller => 'sessions', :action => 'new'"

rakefile("cruise_controle.rake") do
  <<-TASK
    desc "Run all the tests, including API and acceptance tests"
    task :cruise do
      Rake::Task['db:migrate'].invoke
      Rake::Task['spec'].invoke
      Rake::Task['spec:stories'].invoke
      Rake::Task['metrics:all'].invoke
      Rake::Task['flogger:record'].invoke
    end
  TASK
end

initializer 'form_builder.rb', <<-CODE
  ActionView::Base.default_form_builder = SemanticFormBuilder
CODE

generate("authenticated", "user session")
generate("rspec")

rake("db:migrate")

git :add => "."
git :commit => "-a -m 'Initial commit'"

There is quite a lot going on there, I'll quickly run through what I've done. Firstly, I've deleted index.html as I never use it. Next we initialise a local git repository and pull in all the plug-ins I want (as git submodules). Next we have a couple of gems I need, so we define them and install (using sudo) if they're not already on the system. I setup a route to the login page, create a rake task to run cruise, an initializer to change the default form builder to be my own. And then finally I create the user/session models for restful-authentication, migrate the database, and commit all the changes.

Running your custom template

Now to create a new rails app using this template do the following:

rails my_new_app -m ~/template.rb

Or alternatively if you work across various machines you can post the template up to a server (git, your own box, etc) and specify a full HTTP to the remote file:

rails my_new_app -m http://gist.github.com/33443.txt

Glenn Gillen

I'm an advisor to, and investor in, early-stage tech startups. Beyond that I'm an incredibly fortunate husband and father. Working on a developer-facing tool or service? Thinking about starting one? Email me and let me know or come to one of our days to help make it a reality.