Custom Starting Templates for Rails Apps

Dec 08, 2008

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




export const _frontmatter = {"read more":"Read more about Custom Starting Templates for Rails Apps","date":"2008-12-08","summary":"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) 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&hellip;","description":"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) 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&hellip;","categories":"thoughts","author":"Glenn","author_url":"http://glenngillen.com/","title":"Custom Starting Templates for Rails Apps","published":true,"category":"blog"}
Hi, I'm Glenn! 👋 I've spent most of my career working with or at startups. I'm currently the Director of Product @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.

Previously I led the Terraform product team @ HashiCorp, where we launched Terraform Cloud and set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.