Open SourceRails 2: The Top Five Features List

Rails 2: The Top Five Features List

It’s the start of the new year, and as usual the media is trotting out their annual “lists” for 2007 ad nauseum. Best sports moments, worst political gaffes, sexiest celebrities… you name the topic and somebody’s come up with a list itemizing its relevance in 2007. I thought I’d pen my first Developer.com column of the year in similar fashion, highlighting my personal take on top five features of the new Rails 2 release. Keep in mind this isn’t a comprehensive breakdown, nor is it a summary of the most complicated new features, but rather it’s an overview of a few key new features which are already making me a more productive developer. And that’s ultimately the point of Rails, isn’t it?

Feature #1: Even Easier View Management

In an effort to stay DRY (Don’t Repeat Yourself), Rails developers often use partials to maintain a single data format that can be invoked from within numerous controller methods. For instance, consider the following controller, which might be used to retrieve URLs from a database:

class LinksController < ApplicationController
   def index
      @links = Link.find(:all)
   end

   def show
      @link = Link.find(params[:id])
   end
   ...
end

A single partial would be used for both the index and show methods. It might look like this:

<p><%= link_to h(link.name), link.url %></p>

Finally, the respective views would look like this:

# index view

<%= render :partial => 'link', :collection => @links %>

# show view

<h3><%= @link.name %></h3>

<%= render :partial => 'link', :object => @link %>

Rails 2 allows you to create even more succinct view code when referencing partials. Running Rails 2, the following views can be rewritten like so:

# index view

<%= render :partial => @links %>

# show view

<h3><%= @link.name %></h3>

<%= render :partial => @link %>

Feature #2: Very Easy Authentication

I’ve used the Restful Authentication plugin on several Rails projects, and find it to be an incredibly intuitive and easy way to manage all aspects of user authentication, including registering, logging in to, and out of the application. But perhaps it’s overkill if you’re simply looking for an easy way to password protect a page. Rails 2 resolves this dilemma by introducing a basic HTTP authentication feature. To use it, you create a before_filter within the controller you’d like to protect, and within the filter method use authenticate_or_request_with_http_basic, like so:

class AdminController < ApplicationController
   before_filter :authenticate

   protected
   def authenticate
      authenticate_or_request_with_http_basic do |username,
      password| username == 'jason' && password == 'supersecret'
   end
   ...
end

When the user attempts to access any resource found under the Admin controller, he’ll be greeted with the familiar authentication prompt. Entering a username of jason and a password of supersecret will grant him access; otherwise, he’ll be prompted anew.

Of course, hardcoding a username and password into the method isn’t exactly practical. Instead, you can invoke a method that will check against the database for a matching login pair.

Feature #3: Condensed Migrations Syntax

Migrations is far-and-away one of my favorite features of the Rails framework. In fact, I’ve written extensively about the topic in recent articles published here on Developer.com (See “Raking MySQL Over Rails” and “Manage Your MySQL Database with Rails’ Migrations“). Indeed, the ability to manage your database schemas using a distinctly programmatic approach is truly compelling to developers like myself who prefer to keep database-related tasks at arms’ length.

Evolving a schema programmatically is indeed appealing, but even Migrations syntax can become a bit tedious over the course of an application’s lifetime. For instance, I recently created a migration for a new table; it looked like this:

class CreateUsers < ActiveRecord::Migration
   def self.up
      create_table :users do |t|
         t.column :name, :string
         t.column :email, :string
         t.column :bio, :text
         t.column :created_at, :datetime
         t.column :updated_at, :datetime
      end
   end

   def self.down
      drop_table :chapter_comments
   end
end

In Rails 2, you can significantly cut down on the amount of typing involved for these sorts of declarations by using a condensed syntax that allows you to combine columns of like-types together. Revisiting the previous migration, you can rewrite it like so:

class CreateUsers < ActiveRecord::Migration
   def self.up
      create_table :users do |t|
         t.string :name, :email
         t.text :bio
         t.datetime :created_at, :updated_at
      end
  end

   def self.down
      drop_table :chapter_comments
   end
end

Feature #4: Active Record Data and XML/JSON

A recent large Rails-driven project I was involved in called for the publishing of Active Record data in JSON format (JavaScript Object Notation). JSON is a lightweight data format that makes it very easy to pass data among a variety of languages, in my case between Ruby and JavaScript (namely, the Ext JS JavaScript framework). Rails has long supported JSON to varying degrees, but only with Rails 2 can you really kick your JSON support up a notch.

For instance, you can use the new version 2 features to retrieve your del.icio.us bookmarks in JSON format (as an example, see my del.icio.us Rails links in JSON format here) and store them in a local database table using the from_json method. You then can retrieve the data from your database and expose it in JSON format using the to_json method, like so:

@links = Link.find(:all)

render :json => @links.to_json()

If you wanted just to expose the link URLs in JSON format, you can use the :only attribute:

@links = Link.find(:all)

render :json => @links.to_json(:only => url)

Feature #5: Performance and Maintainability Upgrades

This past year has seen a tremendous amount of debate regarding Rails’ ability to scale. Regardless of which side of the fence you sit regarding this discussion, it probably doesn’t come as a surprise that the Rails community took special interest in addressing various performance-related matters in the 2.0 release. Likewise, in response to the increased use of Rails within particularly complex, high-traffic environments, several maintainability enhancements made their way into the new version. I’ll close out my homage to “the list” with a quick summary of my favorite new features in regards to both performance and maintainability:

Improved Configuration Management

The config/environment.rb file contains many of your Rails project’s key configuration directives, such as the current working environment, which version of Rails your application is using, and the organization of various load paths. However, because the information in this file is globally available to your application, it tends to become the catch-all location for required configuration data used by things such as external libraries. To help you more effectively organize configuration data, you’ll find a new directory in config called initializers. You can use this directory to store configuration data in well-organized files, thereby leaving environment.rb to manage only those configuration directives that are originally found in the file.

Improved JavaScript Loading Performance

If you’ve ever taken advantage of Rails’ built-in JavaScript capabilities by including <%= javascript_include_tag :defaults %> and then viewed the page source, you’ll see there were actually five JavaScript files that the browser requested:

<script src="/javascripts/prototype.js?1198788702"
        type="text/javascript"></script>
<script src="/javascripts/effects.js?1198788702"
        type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1198788703"
        type="text/javascript"></script>
<script src="/javascripts/controls.js?1198788703"
        type="text/javascript"></script>
<script src="/javascripts/application.js?1198788703"
        type="text/javascript"></script>

Although the javascript_include_tag makes it convenient for the programmer to add these default libraries, the result is five additional requests to retrieve these files. You can reduce these requests to one by adding the :cache attribute to the tag, like so:

<%= javascript_include_tag :defaults, :cache => true %>

Keep in mind this will work only when your application is running in production mode. Once the change is made, restart your server (in production mode), reload the page, and check the source again. This time, you’ll see just a single request:

<script src="/javascripts/all.js?1198796365"
        type="text/javascript"></script>

Conclusion

Although no future release of Rails will be as groundbreaking as the original, rest assured that Rails 2.0 is well worth the upgrade. I hope this “list” gave you some insight into why!

About the Author

W. Jason Gilmore is co-founder of IT Enlightenment. He’s the author of several books, including the best-selling Beginning PHP and MySQL 5: Novice to Professional, Second Edition (Apress, 2006. 913pp.). Jason loves receiving email, so don’t hesitate to write him at wjATwjgilmore.com.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories