Rails Application Templates

We have a very basic Rails stack called Prologue that we start with for a lot of projects. The Rails 2.3.8 version of Prologue was simply kept in a repo and whenever we needed to start a new project we would clone it and go from there. Pretty basic stuff. Recently I have been making a new version of Prologue on Rails 3 and I decided to take the approach of writing it as an application template instead.

The main advantages we've seen so far with this are:

  • Ability to mix and match templates
  • Dynamically setup basic variables on the fly
  • Dependencies stay up-to-date
  • Huge time savings in getting a project started

Writing templates for Rails 3 is extremely easy. Here's some examples you will find useful.

yes?

if yes?('Do you need sphinx? (yes/no)')

do stuff to setup sphinx

end

remove_file

removefile 'thisfileisgonna_die.rb'

create_file

createfile 'newfile.txt' do <<-'FILE' really sweet stuff all up in my text file FILE end

append_file

append_file 'db/seeds.rb' do <<-'FILE' User.create! :name => 'Quick Left' FILE end

gsub_file

gsubfile 'config/application.rb', /:password/, ':password, :passwordconfirmation'

gsubfile 'config/environments/production.rb', /config.i18n.fallbacks = true/ do <<-'RUBY' config.i18n.fallbacks = true config.actionmailer.defaulturloptions = { :host => 'yourhost.com' } ### ActionMailer Config # Setup for production - deliveries, no errors raised config.actionmailer.deliverymethod = :smtp config.actionmailer.performdeliveries = true config.actionmailer.raisedeliveryerrors = false config.actionmailer.default :charset => "utf-8" RUBY end

injectintofile

injectintofile 'config/application.rb', :after => "# Configure the default encoding used in templates for Ruby 1.9.n" do <<-'RUBY' config.generators do |g| g.template_engine :haml end RUBY end

apply

apply File.expandpath("../templates/sphinx.rb", _FILE__)

get

get "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "public/javascripts/jquery.js" get "http://github.com/rails/jquery-ujs/raw/master/src/rails.js", "public/javascripts/rails.js" get "http://html5shiv.googlecode.com/svn/trunk/html5.js", "public/javascripts/shiv.js"

run

run 'bundle install' run 'any command here'

generate

generate(:migration, "AddNameToUsers name:string")

inside

inside "public/javascripts" do removefile 'controls.js' removefile 'dragdrop.js' removefile 'effects.js' removefile 'prototype.js' end

say

say "templates are pretty dope"

gem

gem "rspec" gem "rspec-rails", ">= 2.0.0.beta.19", :group => [:test, :development] gem "cucumber", :git => "git://github.com/aslakhellesoy/cucumber.git", :group => [:test, :development]

git

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

Those examples should really get you going. Once you're ready to try out the application template simply run:

rails new my_app -m path/to/template.rb

Beyond that just keep in mind you can use any methods provided by Thor::Actions and Rails::Generators::Actions. How sweet is that!


QuickLeft closeicon

Let's Build Your Project

844.QL.NERDS

Quick Left HQ
902 Pearl St.
Boulder, CO 80302
303.242.5536
Quick Left San Francisco
44 Tehama St.
San Francisco, CA 94105
Quick Left Portland
529 SW 3rd Ave.
2nd Floor
Portland, OR 97204
503.504.4963
Quick Left Denver
Galvanize
1644 Platte St.
Denver, CO 80202