Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save radar/224739 to your computer and use it in GitHub Desktop.
Save radar/224739 to your computer and use it in GitHub Desktop.

My first look at rails 3.0.pre and the steps to take to create and live with rails 3.0.pre that I came across.

Why was I looking at the secret Rails 3.0? Their generators are being migrated over to Thor and I wanted to see them in action. I might migrate newgem to use Thor too.

Here's a quick poke around of getting started and interesting things I found. Any hiccups and workarounds are meant as a guide to pre-pre-3.0 users/developers and not as a criticism of rails-core. Rails 3.0 is looking shiny and awesome.

Getting Started

As of today, you cannot install 3.0.pre from rubygems [1]. So, let's install them from source. Which is handy, you might like to patch something.

$ cd ~/gems
$ git clone git://github.com/rails/rails.git
use_ruby_187 or use_ruby_191

Since you don't have the 3.0.pre gems installed, you're about to hit bump #1. Ruby 1.8.6 doesn't have Symbol#to_proc but it's required to create a rails app. This means you'll need to be able to switch to another version of ruby temporarily if you're on ruby 1.8.6 [2].

cd ~/Sites
ruby ~/gems/rails/railties/bin/rails

Oooh, look at all the new options! Some new ones are:

-F, [--freeze]             # Freeze Rails in vendor/rails from the gems
-O, [--skip-activerecord]  # Skip ActiveRecord files
-T, [--skip-testunit]      # Skip TestUnit files
-J, [--skip-prototype]     # Skip Prototype files

The -D, --with-dispatchers flags have been removed. --freeze isn't new, but -F is.

So, to create an app, I dutifully used:

ruby ~/gems/rails/railties/bin/rails edgerailsapp -F

BAM! Fail. The -F option to freeze/vendor rails fails without the gems installed. So don't use it.

ruby ~/gems/rails/railties/bin/rails edgerailsapp
ln -s ~/gems/rails vendor/rails

If you're on Windows without the symlink command ln, then copy the downloaded rails source into vendor/rails.

Fetch Rails' dependencies

Rails 3.0 source uses the new bundler project to describe its own dependencies. F

cd ~/gems
git clone git://github.com/wycats/bundler
cd bundler
sudo rake install

Now, back in your app, you need to install some rails dependencies here too. It's a good chance to see how you'll bundle gem dependencies in the future.

$ cd ~/Sites/edgerailsapp

Add the following lines to Gemfile in your project:

gem "arel",          :git => "git://github.com/rails/arel.git"
gem "rack-mount",    :git => "git://github.com/rails/rack-mount.git"

Welcome to the future of gem dependencies for rails apps. Ultimately you won't need to manually add these lines yourself. When rails is distributed as gems it will automatically install these for you, I assume/hope/guess. But for today, you seem to need them.

Now locally (within your app) install these gems:

$ gem bundle

Phew. Ooh, my god. Phew. Only now, will script/generate work.

$ script/generate

For me, this outputs:

Please select a generator.
Builtin: app, controller, generator, helper, integration_test, mailer, metal, migration, model, model_subclass, observer, performance_test, plugin, resource, scaffold, scaffold_controller, session_migration, stylesheets.
Others: app_layout:app_layout, check_migration_version:check_migration_version, database_yml_mysql:database_yml_mysql, deploy:deploy, home_route:home_route.

The Builtin generators are the latest and greatest in Thor technology. Rails 3.0 no longer uses its own generator but is built upon Thor.

The Others generators are my own local generators from ~/.rails/generators. Amusingly, instead of app_layout it is called app_layout:app_layout. Not surprisingly at all, if I try to run the rails 2 generator it fails:

$ script/generate app_layout:app_layout
[WARNING] Could not load generator at "/Users/drnic/.rails/generators/app_layout/app_layout_generator.rb". Error: uninitialized constant Rails::Generator
Could not find generator app_layout:app_layout.

Poop. Every rails 2 generator is broken. When I start to migrate some I'll post about it. In the meantime, José Valim has written some introduction thoughts on using Thor as a generator.

You can also learn about how to write rails 3.0 generators by looking at the source code for the new generators like rails, model, scaffold.

Footnotes

[1] Two portions of rails 3.0.pre are available as pre-release gems: activesupport (which is now very modularised and only loads up the parts that you require) and activemodel (which is shiny and new and hence completely safe for rails-core to release).

[2] There are two popular ways to have easy, non-intrusive access to alternate versions of ruby: rvm and ruby_switcher.sh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment