On February 18th, 2010 was O'Reilly's "Exploring Rails 3" online conference and these are Gregg Pollack's slides. They are by no means a complete record of improvements in Rails 3, but they should serve to get your appetite wet.
1 of 45
Downloaded 2,662 times
More Related Content
Rails 3 Beautiful Code
1. Beautiful Code in Rails 3
by
Gregg Pollack
Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
2. Starting a New App
$ rails
Usage:
rails APP_PATH [options]
Options:
‐O, [‐‐skip‐activerecord] # Skip ActiveRecord files
‐r, [‐‐ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/greggpollack/.rvm/rubies/ruby‐
‐T, [‐‐skip‐testunit] # Skip TestUnit files
[‐‐dev] # Setup the application with Gemfile pointing
to your rails checkout
‐J, [‐‐skip‐prototype] # Skip Prototype files
‐G, [‐‐skip‐git] # Skip Git ignores and keeps
‐m, [‐‐template=TEMPLATE] # Path to an application template
‐d, [‐‐database=DATABASE] # Preconfigure for selected database
[‐‐edge] # Setup the application with Gemfile
# pointing to Rails repository
3. $ rails test_app
create $ ls script/
create README
create .gitignore
...
$ cd test_app/
rails
$ rails
Usage: rails COMMAND [ARGS]
The most common rails commands are:
generate Generate new code (short‐cut alias: "g")
c
console Start the Rails console (short‐cut alias: " ")
s
server Start the Rails server (short‐cut alias: " ")
dbconsole Start a console for the database specified in config/database.yml
(short‐cut alias: "db")
In addition to those, there are:
application Generate the Rails application code
destroy Undo code generated with "generate"
benchmarker See how fast a piece of code runs
profiler Get profile information from a piece of code
plugin Install a plugin
runner Run a piece of code in the application environment
All commands can be run with ‐h for more information.
4. old scripts new hotness
script/generate rails g
script/console rails c
script/server rails s
script/dbconsole rails db
5. old scripts new hotness
script/generate r g
script/console r c
script/server r s
script/dbconsole r db
alias r='rails'
9. New Routing API
Rails 2
map.resources :posts do |post|
post.resources :comments
end
Rails 3
resources :posts do
resources :comments
end
10. New Routing API
Rails 2
map.resources :posts, :member => { :confirm => :post, :notify => :post } do |post|
post.resources :comments, :member => { :preview => :post }, :collection => { :archived => :get }
end
Rails 3
resources :posts do
member do Rails 3
post :confirm
resources :posts do
get :notify
member do
end
post :confirm
get :notify
resources :comments do
end
member do
post :preview
resources :comments do
end
post :preview, :on => :member
get :archived, :on => :collection
collection do
end
get :archived
end
end
end
end
14. Beautiful Code in Rails 3
Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
#Rails3OMGPonies!
15. New ActionController Syntax
Regular Syntax
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html
format.xml { render :xml => @users.to_xml }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
...
16. New ActionController Syntax
Improved Syntax
class UsersController < ApplicationController
respond_to :html, :xml, :json
def index
@users = User.all
respond_with(@users)
end
def show
@user = User.find(params[:id])
respond_with(@user)
end
...
19. New ActionMailer Syntax
Rails 2
def welcome(user, subdomain)
subject 'Welcome to TestApp'
recipients user.email
from '[email protected]'
body :user => user, :subdomain => subdomain
end
UserMailer.deliver_welcome(user, subdomain)
Rails 3
def welcome(user, subdomain)
@user = user
@subdomain = subdomain
mail(:from => "[email protected]",
:to => user.email,
:subject => "Welcome to TestApp")
end
UserMailer.welcome(user, subdomain).deliver
20. New ActionMailer Syntax
Rails 3
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def welcome(user, subdomain)
@user = user
@subdomain = subdomain
attachments['test.pdf'] = File.read("#{Rails.root}/public/test.pdf")
mail(:to => @user.email, :subject => "Welcome to TestApp") do |format|
format.html { render 'other_html_welcome' }
format.text { render 'other_text_welcome' }
end
end
end
welcome.text.erb
Defaults welcome.html.erb
21. Nic k Kallen
ActiveRelation
replaces the internal ad-hoc query generation with
query generation based on relational algebra.
22. ActiveRelation
Rails 2
@posts = Post.find(:all, :conditions => {:published => true})
immediately queries the db
returns an Array of Posts
Rails 3
@posts = Post.where(:published => true)
doesn’t query the db
returns an ActiveRecord::Relation
24. We can refactor!
@posts = Post.where(:published => true)
if params[:order]
@posts = @posts.order(params[:order])
end
@posts = Post.where(:published => true)
@posts = @posts.order(params[:order])
@posts = Post.where(:published => true).order(params[:order])
25. ActiveRelations can be Shared
@posts = Post.where(:published => true).order(params[:order])
posts = Post.order(params[:order])
@published = posts.where(:published => true)
@unpublished = posts.where(:published => false)
This is obviously a bad example (should be using named routes)
@published = Post.published
@unpublished = Post.unpublished
26. ActiveRelation
@published = Post.published
@unpublished = Post.unpublished
Rails 2
class Post < ActiveRecord::Base
default_scope :order => 'title'
named_scope :published, :conditions => {:published => true}
named_scope :unpublished, :conditions => {:published => false}
end
Rails 3
class Post < ActiveRecord::Base
default_scope order('title')
scope :published, where(:published => true)
scope :unpublished, where(:published => false)
end
27. ActiveRelation
New Finder Methods
where(:conditions)
having(:conditions)
select
group
order
limit
offset
joins
includes(:include)
lock
readonly
from
28. ActiveRelation
Rails 2
Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments,
:order => "title", :limit => 10)
Rails 3
Post.where(:author => "Joe").include(:comments).order(:title).limit(10)
Remember, this version doesn’t do the query immediately
29. Beautiful Code in Rails 3
Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
#Rails3OMGPonies!
30. Use of external libraries
ActiveRecord ActiveRelation
ActionView Erubis
Erubis is a fast, secure, and very extensible implementation of ERB
32. Adopting Unobtrusive Javascript
HTML 5 custom data attributes data-*
Custom data attributes are intended to store custom data private to the page or
application, for which there are no more appropriate attributes or elements
data-remote
data-method
data-confirm
data-disable-with
39. /public/stylesheets/rails.js
document.observe("dom:loaded", function() {
$(document.body).observe("click", function(event) {
var message = event.element().readAttribute('data-confirm');
if (message) {
// ... Do a confirm box
}
var element = event.findElement("a[data-remote=true]");
if (element) {
// ... Do the AJAX call
}
var element = event.findElement("a[data-method]");
if (element) {
// ... Create a form
}
});
40. jQuery in Rails?
http://github.com/rails/jquery-ujs
$('a[data-confirm],input[data-confirm]').live('click', function () {
// ... Do a confirm box
});
$('form[data-remote="true"]').live('submit', function (e) {
// ... Do an AJAX call
});
42. Beautiful Code in Rails 3
Starting a new app
New Router API
ActionController - respond_with
ActionMailer Syntax
ActiveRelation (arel)
ERB Strings Escaped
Unobtrusive Javascript
#Rails3OMGPonies!
43. Missing (that I didn’t have time for)
APIs
Bundler
http://railscasts.com/episodes/201-bundler
Making Generators with Thor
http://bit.ly/rails3generators
44. Creative Commons
name author URL
rainbow of 80s toys merwing✿little dear http://www.flickr.com/photos/merwing/2152164258/
Old Loc Merlijn Hoek http://www.flickr.com/photos/merlijnhoek/1040997599/
Notting Hill Gate Eole http://www.flickr.com/photos/eole/942309733/
Britomart Train Station EssjayNZ http://www.flickr.com/photos/essjay/260511465/
Das Licht Small http://www.flickr.com/photos/small/62713023/
Metro Genova opti mystic http://www.flickr.com/photos/miiilio/2503634282/
Immobility Dilemna gilderic http://www.flickr.com/photos/gilderic/3528157964/
train station nolifebeforecoffee http://www.flickr.com/photos/nolifebeforecoffee/1803584805/
Mystical station Jsome1 http://www.flickr.com/photos/jsome1/2226394415/
Railswaystation Pieter Musterd http://www.flickr.com/photos/piet_musterd/2233025691/
The Handover MarkyBon http://www.flickr.com/photos/markybon/152769885/
EN57 magro_kr http://www.flickr.com/photos/iks_berto/1328682171/
45. Presentation by: If you need help with a Rails 3
project, feel free to give us a call
http://envylabs.com
Come to our Rails 3 training at
Railsconf 2010
Gregg Pollack
http://bit.ly/rails3ropes
407-754-5517
[email protected]
We’re also available to run Rails 3
Training at your company, email:
Ruby5 Podcast [email protected]
http://ruby5.envylabs.com