These are my slides for my presentation to the Ruby Users of Minnesota on March 30, 2009.
It's intended to be "10 features in 10 minutes" -- mostly cool new things in Rails 2.3, but also some smaller things that fix annoyances I've had with Rails.
See the blog post that goes with it here: http://railspikes.com/2009/3/30/10-cool-things-in-rails-23
6. Routing Improvements
RESTful routes use 50% less memory
because formatted_* routes are no longer
generated.
Before:
formatted_users_path('xml')
formatted_users_path(:format => 'xml')
Now:
users_path(:format => 'xml')
7. Routing Improvements
Routes no longer inherit :only and :except.
Before:
map.resources :users, :only => :show do |user|
user.resources :hobbies, :except => :none
end
Now:
map.resources :users, :only => :show do |user|
user.resources :hobbies
end
11. Default Scopes
default_scope :order => 'name asc'
# will use default scope
User.all
# will use passed in order option.
User.all(:order => 'name desc')
puts User.all.map { |u| quot;#{u.id} - #{u.name}quot; }
3 - Alice
2 - Bob
1 - George
12. Nested Transactions
User.transaction do
user = User.create(:name => 'Luke')
User.transaction(:requires_new => true) do
user.hobbies.create(:name => 'Knitting')
raise ActiveRecord::Rollback(quot;Oh noes!!!quot;)
end
end
13. Asset Host Objects
class SslAssetHost
def call(source, request)
if request.ssl?
quot;#{request.protocol}#{request.host_with_port}quot;
else
quot;#{request.protocol}assets.example.comquot;
end
end
end
15. Nested Attributes...
class User < ActiveRecord::Base
has_many :hobbies, :dependent => :destroy
accepts_nested_attributes_for :hobbies
end
user = User.create(:name => 'Stan',
:hobbies_attributes =>
[{:name => 'Waterskiing'},
{:name => 'Hiking'}])
16. ...and Forms
<% form_for(@user) do |f| %>
<%= f.label :name, quot;User name:quot; %>
<%= f.text_field :name %>
<h2>Hobbies</h2>
<% f.fields_for(:hobbies) do |hf| %>
<%= hf.label :name, quot;Hobby name:quot; %>
<%= hf.text_field :name %>
<% end %>
<%= f.submit 'Create' %>
<% end %>
17. Rails Metal
class UsersApi
def self.call(env)
if env['PATH_INFO'] =~ /^/users.js/
[200,
{'Content-Type' => 'application/json'},
User.all.to_json]
else
# Return 404 to let Rails handle the request
[404, {'Content-Type' => 'text/html'},
'not found']
end
end
end
18. Rails Metal m/
class UsersApi
def self.call(env)
if env['PATH_INFO'] =~ /^/users.js/
[200,
{'Content-Type' => 'application/json'},
User.all.to_json]
else
# Return 404 to let Rails handle the request
[404, {'Content-Type' => 'text/html'},
'not found']
end
end
end