03 Dec, 2008

Published at 07:03PM

Tagged with innovation, programming, and rails

This post has 0 comments

Generate Rails app's from a template

Let’s daydream for a minute. Wouldn’t it be nice if, instead of maintaining a full-blown Rails application, you could just maintain some small file (or template) that specified how the Rails generator should behave. Maybe it would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
template(:rph) do
  gem 'RedCloth'
  gem 'mislav-will_paginate'
  gem 'ruby-openid'

  plugin 'rspec', :git => 'git://github.com/dchelimsky/rspec.git'
  plugin 'rspec-rails', :git => 'git://github.com/dchelimsky/rspec-rails.git'
  plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git'
  plugin 'open_id_authentication', :git => 'git://github.com/rails/open_id_authentication.git'
  plugin 'hoptoad_notifier', :git => 'git://github.com/thoughtbot/hoptoad_notifier.git'
  plugin 'navigation_helper', :git => 'git://github.com/rpheath/navigation_helper.git'
  plugin 'acts_as_lookup', :git => 'git://github.com/rpheath/acts_as_lookup.git'
  plugin 'input_css', :git => 'git://github.com/rpheath/input_css.git'
  plugin 'form_assistant', :git => 'git://github.com/rpheath/form_assistant.git'

  initializer 'hoptoad.rb' do
    HoptoadNotifier.configure do |config|
      config.api_key = 'HOPTOAD-KEY'
    end
  end

  initializer 'time_formats.rb' do
    { :short_date => "%x", :long_date => "%a, %b %d, %Y" }.each do |k, v|
      ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(k => v)
    end
  end

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

  rake('db:migrate')
end

Then, maybe you could do something like this to use it:

1
$ rails new_application -m /path/to/rph.rb

That would be awesome.

Hmmm… but then I’d have to make sure that I always had access to this template, or I’d be up you know what without a paddle. Ideally, I’d like to keep it in a gist or something, and be able to use it the same way:

1
$ rails new_application -m http://gist.github.com/31759.txt

Now, I know what you’re thinking, and you’re right. We’re no longer daydreaming! This has officially become a reality.

How. Awesome. Is. That.

I’ve just scratched the surface, too. Checkout template_runner.rb to see what else you can do.

Man, I love Rails.

Comments

Do you have something to say about this post?
Retype the image to the right Spam Hint: Are You Human? Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch