Rails 2.0.2 released, so what’s new?

In: Open Source|Ruby on Rails

17 Dec 2007

Rails 2.0.2 was tagged not too long ago and will hit the gem servers soon I expect. Being a minor point release there aren’t many changes, but here’re the ones I feel are worth mentioning:

ActionPack changes

Configurable asset hosts: You can now pass a proc to ActionController::Base.asset_host. This allows you to get by the hard-coded assumption that you have 4 asset hosts numbered 0-3 when using the ‘%d’ wildcard (introduced in Rails 2.0.1).

I contributed this patch so I’m gonna be lazy and copy and paste most of the documentation I wrote for patch.

The example proc below generates http://assets1.example.com and http://assets2.example.com randomly.

ActionController::Base.asset_host = Proc.new { |source| "http://assets#{rand(2) + 1}.example.com" }
image_tag("rails.png")
  => <img src="http://assets2.example.com/images/rails.png" alt="Rails" />
stylesheet_include_tag("application")
  => <link href="http://assets1.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />

The proc also takes a single source parameter which is the path of the source asset. This can be used to generate a particular asset host depending on the asset path. This is useful if you have, say, images hosted elsewhere, or you want an SEO-friendly URL for your images:

ActionController::Base.asset_host = Proc.new { |source|
  if source.starts_with?('/images')
    "http://images.example.com"
  else
    "http://assets.example.com"
  end
}
image_tag("rails.png")
  => <img src="http://images.example.com/images/rails.png" alt="Rails" />
stylesheet_include_tag("application")
  => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />

Asset cache directories are automatically created: In Rails 2.0.1, if you tried to cache your assets (javascripts and stylesheets) in a subdirectory (example below), you either need to check it into your source control or have your deployment tool (Capistrano, etc.) create it for you. Josh Peek contributed a patch that created the subdirectory instead so you can do something like:

javascript_include_tag(:all, :cache => "cache/money")

and not have to bother about creating the ‘cache’ subdirectory.

Railties changes

Default database is now SQLite3: The default database is now SQLite3 instead of MySQL. Running ‘rails takeover_the_world_app’ will now come with a database.yml that’s setup for SQLite3. To get the old behavior (where it’s preconfigured for MySQL), run rails -d mysql takeover_the_world_app instead. This is mostly to make this “just work” on Mac OS X Leopard, where SQLite3 is already installed with the OS.

Template loading is faster: DHH has turned on ActionView::Base.cache_template_loading by default in the production.rb environment config file that basically means that Rails no longer does file system stat calls when loading templates. The downside is that you have to restart your Rails application to see templates changes in production mode, but it’s not really that big an issue since no one should be editing templates anyway ‘live’ in a production application.

New rake tasks for migrations: rake db:migrate:redo to undo your last migration and re-run it – very useful when developing migrations (when you screw up, that is). rake db:migrate:reset drops the database, re-creates it, and then runs all migrations.

rake task for generating secret keys: rake secret to generate a secure key that you can use for cookie sessions. This is useful for updating Rails applications from 1.x to 2.x, which uses cookie-based sessions by default and requires a secret key.

config.action_controller.session = {
  :session_key => '_your_app_session',
  :secret      => 'some super long string that you can generate with the rake secret task'
}

Personally I just use super long quotes from Family Guy.

That’s it! For the curious, expect Ruby 1.9 compatibility, improved caching, and big ActionPack and rendering refactoring changes in Rails 2.1.

6 Responses to Rails 2.0.2 released, so what’s new?

Avatar

luis buenaventura

December 19th, 2007 at 10am

I was trying some of this out on our project and ended up with some really weird looking URLs :

In my development.rb:

ActionController::Base.asset_host = Proc.new { |source| “http://asset#{rand(3)}.moomai.com” }

So I tried using the traditional method, thusly:

ActionController::Base.asset_host = “http://asset%d.moomai.com”

And instead got this in my views:

Nice. Something’s not right, I gather?

Avatar

Chu Yeow

December 19th, 2007 at 10am

I think part of your comment got cut off. If you could re-post I’ll be able to help you figure out your problem.

Avatar

A Fresh Cup - Notes from a recovering Microsoft addict

December 23rd, 2007 at 10pm

[…] Rails 2.0.2 released, so what’s new? – Another take on the new features. […]

Avatar

Living on the Edge (of Rails) #3 - X-Sendfile and many other sexy enhancements - redemption in a blog

January 16th, 2008 at 11pm

[…] you to set ActionController::Base.asset_host to a proc that took a single source argument (as detailed in my earlier post). People were still running into problems with asset hosting though, particularly while trying to […]

Avatar

gertas

May 14th, 2008 at 12am

It’s quite while on this but I have a comment on usage of ActionController::Base.asset_host presented here. Using rand may cause more harm than benefit. The problem is that same asset may have to be downloaded twice in above scenario because for two separate page impressions different hosts may be generated.
Something needs to keep a same host name for a same asset. The solution I found which works well is using .hash method on source and leaving last two bits of resulting number (x AND 3). This gives a number 0-3 which may be used to prepare host name. It should look like this:

ActionController::Base.asset_host = Proc.new { |source| “http://assets#{source.hash & 3}.example.com” }

Havn’t tested above line – written from memory.

Avatar

Rails 2.0.2 Released, 2.1.0 is on the horizon

December 10th, 2008 at 6am

[…] The Rails core team has released Rails 2.0.2. It just keeps getting better and better, and faster and faster. Here’s a breakdown of the updates. For more details, check out the blog of Cheah Chu Yeow. […]