Title
Rails 3.1! What's new?
Body
In a nutshell:
-
Overview
-
Asset Pipeline
-
HTTP Streaming
-
Identity Maps
-
Reversible Migrations
-
Upgrading to 3.1
Overview:
-
To install rails 3.1, create a gemset using rvm.
-
Then install rails 3.1 gem as below:
-
rvm gemset create rails3.1 -> rvm gemset use rails3.1 --default -> gem install rails
-
Rails 3.1 includes 'jquery' as it's default javascript library.
-
It also auto runs the bundler while creating a new app.
-
So inorder to create a new app, use prototype as default library and to skip auto-bundle do:
-
rails new test_app -j prototype --skip-bundle
-
Now open the gemfile with a text editor where you see that certain gems are included by default.
-
An important addition to the gem file is the assets group which includes CoffeeScript, SASS and uglifier by default.
-
CoffeeScript is a scripting language which complies into javascript.
-
SASS is an extension of CSS which offers features like nesting and variables in styling.
-
Uglifier is a javascript minifier.
-
And also under test group 'turn' gem which makes the test output look pretty.
-
Apart from these new additions, we are going to include a gem 'therubyracer' which is a javascript runtime.
-
Now run 'bundle install' and take a look the directory structure.
-
The major change we see here is that a new directory 'assets' has been added under 'app' directory.
-
And the resources as such images, javascripts and stylesheets from 'public' directory has been relocated to this new directory 'assets'.
Asset Pipeline:
-
The asset pipeline is one of the biggest new features in Rails 3.1.
-
Take a look at the assets directory under which we have javascripts, stylesheets and images.
-
Both the javascripts and stylesheets directory have manifest file, 'application.js' and 'application.css' respectively.
-
Each of these manifest file will be compiled into including all the files listed here.
-
These manifest files are managed by sprockets, which is a ruby library for compiling and serving web assets.
-
When a request comes in, sprockets, looks at manifest and compiles every file mentioned in here.
-
By default entire directory structure of assets is included, hence any javascript or css file is compiled.
-
These assets are automatically cached and served by the Rack Cache middleware so they’re pretty fast.
-
If we want to have the webserver itself handle serving and hosting the assets instead we can precompile them by running, 'rake assets:precompile'
-
These manifest files eliminates the need of including the javascripts and css files explicitly.
-
In the layout file we just need to include the manifest files and the rest is pulled out from the manifest.
HTTP Streaming:
-
The ordinary http requests happens in 3 steps, http request -> dynamic content generation -> http response.
-
But in http streaming the conent is generated as chunks and each chunch is sent over while the next chunk is being generated.
-
This significantly reduces the response time.
-
On local machine under development environment we can use unicorn server to demonstrate this, but any improvement is too small to notice as the response is fast.
-
Where as on production, there will be significant improvement over http response.
-
Currently Phusion Passenger 2.2.5 on wards support http streaming.
-
In order to start streaming all we need to do is add a small piece of code to the controller.
Identity Map:
-
Indentity maps is one of the important addtions to rails 3.1.
-
It is by default disabled in config file application.rb
-
Inorder to enable it we add,
-
config.active_record.identity_map = true and ActiveRecord::IdentityMap.enabled = true
-
Now that the identity map is enabled, whenever a object is created, it is loaded into memory.
-
And during the same instance if the same object is requested, it is loaded from the memory instead of database.
-
This reduces the load time.
Reversible Migrations:
-
In rails 3.1 the 'up' and 'down' methods are eliminated and a single method 'change' is introduced which performs both the functions.
-
Find more info on reversible migrations here.
Upgrading to 3.1:
-
Upgrading to rails 3.0.X to 3.1.X is pretty simple.
-
First we need to update the app to latest 3.0.X version, which at this time is 3.0.10
-
Just edit the gemfile, change the version of rails and run 'bundle update'.
-
Then again edit the gemfile and include the following gems and run 'bundle update'

-
Now add the following code to config/application.rb

-
Go to config/environment/development.rb and comment out "config.action_view.debug_rjs = true"
-
And include "config.assets.compress = false" and "config.assets.debug = true".
-
Now go to config/environment/production.rb and include 'config.serve_static_assets = true' and 'config.static_cache_control = "public, max-age=3600" ' .
-
Then go to config/environment/test.rb and include "config.assets.compress = true", "config.assets.compile = false" and "config.assets.digest = true"
-
Add .sass-cache to to git ignore.
-
Create a directory 'assets"under 'app' directory and move the javascripts, stylesheets and image directories to this directory.
-
Edit the manifest file 'application.js' and 'application.css' as


-
Include the manifest files in the layout as,

-
Fire up the rails server and check whether the app is running.
Conclusion:
-
This post just gives you a brief idea of the new features available in rails 3.1.
-
There are a lot of resources available on the net, if you want to get into depth of rails 3.1.
-
You can find the Power Point Presentation of this post here.
Cheers
Shiv
Rails Detectives
Associated Tags
rails 3.1
Created At
2011-11-29 06:33:09 UTC
Last Updated At
2011-11-29 06:33:09 UTC