How to use the new templates in Rails

One of the new features in the upcoming Rails 2.3 is templates. I’ve been playing around with them and have documented some ways of using them below.

To start with, there’s a great write-up on most of the template methods on Pratik Naik’s blog

I want to play with some ideas around how you can further customise an initial rails project.

Initial template

#Install gems
gem 'mocha'
gem 'thoughtbot-shoulda'
gem 'thoughtbot-factory_girl'
gem 'thoughtbot-quietbacktrace'
gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'

rake 'gems:install'
rake 'gems:unpack'

#install plugins
plugin 'hoptoad_notifier', :git => 'git://github.com/thoughtbot/hoptoad_notifier.git'

#Delete all unecessary files
run "rm README"
run "rm public/index.html"
run "rm public/favicon.ico"
run "rm public/robots.txt"

#Setup git ignore file
file '.gitignore', <<-END
config/database.yml
db/schema.rb
log/*.log
public/stylesheets/*.css
*.swo
*.swp
tmp/
END
run 'touch tmp/.gitignore log/.gitignore'

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

Writing files

I like to use HAML which uses a less than conventional way of generating the plugin. First, the gem needs to be installed via

gem install haml

and then the plugin is installed via

haml --rails 

The plugin file is verry simple so we can include it directly in our script.

gem 'haml'

file 'vendor/plugins/haml/init.rb', <<-END
require 'rubygems'
begin
require File.join(File.dirname(__FILE__), 'lib', 'haml') # From here
rescue LoadError
require 'haml' # From gem
end

# Load Haml and Sass
Haml.init_rails(binding)
END

Modifying existing files

Rails generates some existing files which you might want to modify using your template such as un-commenting a line or inserting a line. I’ll demonstrate both by making adjustments to the application controller.

gsub_file 'app/controllers/application_controller.rb', /(class ApplicationController.*)/, "\\1\n  include HoptoadNotifier"
gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'

In the above lines, I am adding the Hoptoad notifier include line and un-commenting the

filter_parameter_logging

line.

Asking questions

For a totally custom template, you can ask questions and use the results in your template output.

If you’re installing the HoptoadNotifier plugin, the initialization script requires the API code, we can retrieve that as follows:

hoptoad_api_key = ask('What is your Hoptoad API key?')

initializer 'hoptoad.rb', <<-END
HoptoadNotifier.configure do |config|
config.api_key = '#{hoptoad_api_key}'
end
END

The complete template

#Install gems
gem 'mocha'
gem 'thoughtbot-shoulda'
gem 'thoughtbot-factory_girl'
gem 'thoughtbot-quietbacktrace'
gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'
gem 'haml'

rake 'gems:install'
rake 'gems:unpack'

#install plugins
plugin 'hoptoad_notifier', :git => 'git://github.com/thoughtbot/hoptoad_notifier.git'
file 'vendor/plugins/haml/init.rb', <<-END
require 'rubygems'
begin
require File.join(File.dirname(__FILE__), 'lib', 'haml') # From here
rescue LoadError
require 'haml' # From gem
end

# Load Haml and Sass
Haml.init_rails(binding)
END

#Configure Application Controller
gsub_file 'app/controllers/application_controller.rb', /(class ApplicationController.*)/, "\\1\n  include HoptoadNotifier"
gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'

#Setup Hoptoad
hoptoad_api_key = ask('What is your Hoptoad API key?')

initializer 'hoptoad.rb', <<-END
HoptoadNotifier.configure do |config|
config.api_key = '#{hoptoad_api_key}'
end
END

#Delete all unecessary files
run "rm README"
run "rm public/index.html"
run "rm public/favicon.ico"
run "rm public/robots.txt"

#Setup git ignore file
file '.gitignore', <<-END
config/database.yml
db/schema.rb
log/*.log
public/stylesheets/*.css
*.swo
*.swp
tmp/
END
run 'touch tmp/.gitignore log/.gitignore'

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