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
remove_file 'this_file_is_gonna_die.rb'
create_file
create_file 'new_file.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
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do <<-'RUBY'
config.i18n.fallbacks = true
config.action_mailer.default_url_options = { :host => 'yourhost.com' }
### ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
RUBY
end
inject_into_file
inject_into_file '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.expand_path("../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
remove_file 'controls.js'
remove_file 'dragdrop.js'
remove_file 'effects.js'
remove_file '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!