Upgrading to Rails 3 - assets.en.oreilly.comassets.en.oreilly.com/1/event/45/Upgrading to Rails 3...

Post on 28-Jul-2020

7 views 0 download

Transcript of Upgrading to Rails 3 - assets.en.oreilly.comassets.en.oreilly.com/1/event/45/Upgrading to Rails 3...

OSCON 2010

Upgrading to

Rails 3Thursday, July 22, 2010

OSCON 2010

Michael Bleigh@mbleigh

Thursday, July 22, 2010

Thursday, July 22, 2010

OSCON 2010

Jeremy McAnally’sRails Upgrade Handbook

bit.ly/railsupgrade

Thursday, July 22, 2010

OSCON 2010

Rails 3 isDifferent

Thursday, July 22, 2010

OSCON 2010

Rails 3 is aBig #ange

Thursday, July 22, 2010

OSCON 2010

Why the hell should I bother?

Thursday, July 22, 2010

OSCON 2010

Modularity

Thursday, July 22, 2010

OSCON 2010

Rails 2.3 StackActiveRecord

ActiveSupport

ActiveResource

ActionPack

Test::Unit

Thursday, July 22, 2010

OSCON 2010

Rails 3 Ecosystem

ActiveSupport

ActiveResource

ActionPack

ActiveModel

ActiveRecordDataMapper

MongoMapper

RSpecTest::Unit

Bacon

Thursday, July 22, 2010

OSCON 2010

Rails 2.3 Controller

ActionController::Base

ApplicationController

YourController

Thursday, July 22, 2010

OSCON 2010

Rails 3 Controller

ActionController::Base

ApplicationController

YourController

AbstractController::Base

ActionController::Metal

Thursday, July 22, 2010

OSCON 2010

Less Monkeypatching

Thursday, July 22, 2010

OSCON 2010

Security

darwinbell via Flickr

Thursday, July 22, 2010

OSCON 2010

small change,

big impact

Thursday, July 22, 2010

OSCON 2010

HTML is escapedby default.

Thursday, July 22, 2010

<!-- Rails 2.3 --><div class='comment'> <%= comment.body %></div>

<!-- Rails 3 --><div class="comment"> <%= comment.body.html_safe %></div>

<!-- Rails 3 (alternate) --><div class="comment"> <%=raw comment.body %></div>

Thursday, July 22, 2010

OSCON 2010

It’s a good thing.TM

Thursday, July 22, 2010

OSCON 2010

New Apis

Thursday, July 22, 2010

OSCON 2010

$e Router

Thursday, July 22, 2010

OSCON 2010

Rack Everywhere!

Thursday, July 22, 2010

OSCON 2010

Fancy New DSL

Thursday, July 22, 2010

OSCON 2010

More Powerful

Thursday, July 22, 2010

# Rails 2.3map.connect '/help', :controller => 'pages', :action => 'help'

# Rails 3match '/help', :to => 'pages#help'

# Rails 2.3map.resources :users do |users| users.resources :commentsend

# Rails 3resources :users do resources :commentsend

Thursday, July 22, 2010

# Rails 2.3with_options :path_prefix => 'admin', :name_prefix => 'admin' do |admin| admin.resources :users admin.resources :postsend

# Rails 3namespace :admin resources :users resources :postsend

Thursday, July 22, 2010

# Rails 3

constraints(:subdomain => 'api') do resources :statuses resources :friendsend

match '/hello', :to => lambda{ |env| [200, {'Content-Type' => 'text/plain'}, 'Hello World']}

match '/other-site', :to => redirect('http://url.com')

Thursday, July 22, 2010

OSCON 2010

A%ionMailer

Thursday, July 22, 2010

OSCON 2010

It’s (mostly) justa controller.

Thursday, July 22, 2010

class Notifier < ActionMailer::Base default :from => "mikel@example.org"

def welcome_email(user) @name = user.name attachments['terms.pdf'] = File.read( Rails.root.join('docs/terms.pdf') ) mail(:to => user.email, :subject => "G’day Mate!") endend

Thursday, July 22, 2010

class UsersController < ApplicationController respond_to :html def create @user = User.new(params[:user])

Notifier.welcome_email(@user).deliver if @user.save respond_with @user endend

Thursday, July 22, 2010

OSCON 2010

Bundler

Thursday, July 22, 2010

OSCON 2010

Caution: Entering Controversy

Thursday, July 22, 2010

# Rails 2.3

# environment.rbconfig.gem 'acts-as-taggable-on'config.gem 'ruby-openid', :lib => false

# test.rbconfig.gem 'rspec'config.gem 'cucumber'

# Rails 3

# Gemfilegem 'acts-as-taggable-on'gem 'ruby-openid', :require => false

group :test do gem 'rspec' gem 'cucumber'end

Thursday, July 22, 2010

OSCON 2010

Dependency Resolver

Thursday, July 22, 2010

OSCON 2010

A%iveRelation

Thursday, July 22, 2010

OSCON 2010

Like named scopes, only more so.

Thursday, July 22, 2010

# Rails 2.3

Book.all( :conditions => {:author => "Chuck Palahniuk"}, :order => "published_at DESC", :limit => 10)

# Rails 3

Book.where(:author => "Chuck Palahniuk") .order("published_at DESC").limit(10)

Thursday, July 22, 2010

Inherently Chainable

Thursday, July 22, 2010

# Rails 3

def index @books = Book.where(:author => params[:author]) if params[:author] @books = @books.order(:title) if params[:sort] == 'title' respond_with @booksend

Thursday, July 22, 2010

# Rails 2.3

class Book named_scope :written_by {|a| {:conditions => {:author => a}}} named_scope :after {|d| {:conditions => ["published_on > ?", d]}}# Rails 3

class Book class << self def written_by(name) where(:author => name) end def after(date) where(["published_on > ?", date]) end endend

Thursday, July 22, 2010

OSCON 2010

Be&er H'ks

Thursday, July 22, 2010

OSCON 2010

Generators

Thursday, July 22, 2010

config.generators do |g| g.orm :mongomapper g.test_framework :rspec g.integration_tool :rspecend

rails g model my_model

Thursday, July 22, 2010

OSCON 2010

Engines

Thursday, July 22, 2010

#lib/your_plugin/engine.rbrequire "your_plugin"require "rails"

module YourPlugin class Engine < Rails::Engine engine_name :your_plugin endend

Thursday, July 22, 2010

OSCON 2010

Lots more...railsdispatch.com

edgeguides.rubyonrails.org

Thursday, July 22, 2010

OSCON 2010

But we’re already on Rails 2.3!

Thursday, July 22, 2010

OSCON 2010

How do we cope?

Thursday, July 22, 2010

OSCON 2010

Ignore it alland cheat.github.com/rails/

rails_upgrade

Thursday, July 22, 2010

OSCON 2010

Finds Key Blockers:

Routes, Bundler, application.rb

Thursday, July 22, 2010

OSCON 2010

3 Step Process

Thursday, July 22, 2010

OSCON 2010

Analyze Your Apprake rails:upgrade:check

Thursday, July 22, 2010

OSCON 2010

Backup 2.3 Filesrake rails:upgrade:backup

Thursday, July 22, 2010

OSCON 2010

Run Upgradesrake rails:upgrade:routesrake rails:upgrade:gemsrake rails:upgrade:configuration

Thursday, July 22, 2010

OSCON 2010

Takeaways

Thursday, July 22, 2010

Tests help. Unfortunately, they

may not run.

Thursday, July 22, 2010

Don’t be afraid to re-generate.

Thursday, July 22, 2010

Just take it one problem at a time.

Thursday, July 22, 2010

OSCON 2010

(estions?@mbleigh @intridea

github.com/mbleigh/upgrade-to-rails3

Thursday, July 22, 2010