Migration from Rails2 to Rails3

download Migration from Rails2 to Rails3

If you can't read please download the document

Transcript of Migration from Rails2 to Rails3

Migration from Rails 2 to Rails 3Abdullah MuhammadHaseeb AhmadNadeem YasinUmair Amjad

Table of ContentsIntroduction to the New features in Rails 3Differences between Rails 2 and Rails 3Migration of Rails 2 app to Rails 3

Core New FeaturesRails 3.0Brand new routerExplicit dependency management with Bundler

Rails 3.1Reversible MigrationsAssets PipelinejQuery as the default JavaScript library

Rails 3.2Faster Development ModeNew Routing EngineAutomatic Query ExplainsTagged Logging

Rails 3.0Brand new router

Routes defined by each application are now name spaced within your Application moduleInstead ofWe useActionController::Routing::Routes.draw do |map| AppName::Application.routes do map.resources :postsresources :postsendend

1. Added match method to the routermatch "/main/:id", :to => "main#home"2. Added scope method to the router, allowing you to namespace routes for different languages or different actionsscope 'es' do resources :projects, :path_names => { :edit => 'cambiar' }, :path => 'proyecto'end# Gives you the edit action with /es/proyecto/1/cambiar

3. Added constraints method to the router, allowing you to guard routers with defined constraints. match "/foo/bar", :to => "foo#bar", :constraints => {:subdomain => "support"}

4. Added root method to the router as a short cut for match /, :to => path.root :to => "foo#bar"

The old style map commands still work as before with a backwards compatibility layer, however this will be removed in the 3.1 release.

Rails 3.0 Cont'd

Explicit dependency management with Bundler

Rails now uses a Gemfile in the application root to determine the gems you require for your application to start. Gemfile is processed by the Bundler, which then installs all your dependencies

Config.gem is dead, long live bundlerEveryone and their brother complained about Rails handling of vendored/bundled gems since config.gem was added sometime ago

config.gem "RedCloth", :lib =>'redcloth', :version=>">= 4.0"gem "RedCloth", ">= 4.0", :require =>'redcloth'Once you create a Gemfile, you simply have to run "bundle" pack and you're done!

Rails 3.1Assets Pipeline

The major change in Rails 3.1 is the Assets PipelineProvides a framework to concatenate and minify or compress JavaScript and CSS assets Main Features1. Concatenate assets Rails defaults to concatenating all JavaScript files into one master.js file and all CSS files into one master.css file.2. Asset minification or compression For CSS files, this is done by removing whitespace and comments. For JavaScript, more complex processes can be applied. You can choose from a set of built in options or specify your own.3. It allows coding assets via a higher-level language Supported languages include Sass for CSS, CoffeeScript for JavaScript, and ERB for both by default. All the assets are now pre-processed, compressed and minified by one central library, Sprockets. It is enabled by default. It can be disabled in config/application.rb config.assets.enabled = false

Skipping the remaining points of Assets Pipeline like fingerprinting at production environmenthttp://guides.rubyonrails.org/asset_pipeline.html

Rails 3.1 Cont'djQuery as the default JavaScript library

jQuery is the default JavaScript library that ships with Rails 3.1. But if you use Prototype, its simple to switch.$ rails new myapp -j prototypePrototype are no longer vendored and is provided from now on by the prototype-rails gems.

Reversible Migrations

Migrations are now reversible, meaning that Rails will figure out how to reverse your migrationsTo use reversible migrations, just define the change method.class MyMigration < ActiveRecord::Migration def change create_table(:horses) do |t| t.column :content, :text t.column :remind_at, :datetime end endendSome commands like remove_column cannot be reversed. If you care to define how to move up and down in these cases, you should define the up and down methods as before.

Rails 3.2Automatic Query Explains

Rails 3.2 comes with a nice feature that explains queries generated by ARel by defining an explain method in ActiveRecord::Relation.For example,

Product.order(:name).explainProduct Load (0.6ms) SELECT "products".* FROM "products" ORDER BY nameEXPLAIN (0.1ms) EXPLAIN QUERY PLAN SELECT "products".* FROM "products" ORDER BY name=> "EXPLAIN for: SELECT \"products\".* FROM \"products\" ORDER BY name\n0|0|0|SCAN TABLE products (~1000000 rows)\n0|0|0|USE TEMP B-TREE FOR ORDER BY\n"

Tagged LoggingWhen running a multi-user, multi-account application, its a great help to be able to filter the log by who did what TaggedLogging in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications

Differencesold scripts new hotnessscript/generate rails gscript/console rails cscript/server rails sscript/dbconsole rails db

New Router API map.resources :posts do |post|post.resources :comments end resources :posts doresources :comments end

NAMED ROUTESmap.connect 'login', :controller => 'session', :action => 'new'match 'login' => 'session#new'

LEGACY ROUTESmap.connect ':controller/:action/:id'map.connect ':controller/:action/:id.:format'

match ':controller(/:action(/:id(.:format)))'

map.root :controller => "users"root :to => "users#index"

ActionController - respond_withclass UsersController < ApplicationControllerdef index@users = User.allrespond_to do |format| format.html format.xml { render :xml => @users.to_xml }endend

.....

class UsersController < ApplicationControllerrespond_to :html, :xml, :jsondef index@users = User.allrespond_with(@users)end

Model@posts = Post.find(:all, :conditions => {:published => true} , :limit => 10)@posts = Post.where(:published => true).limit(10)

Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments, :order => "title", :limit => 10)Post.where(:author => "Joe").include(:comments).order(:title).limit(10)

REFACTOR@posts = Post.where(:published => true)@posts = @posts.order(params[:order])

@posts = Post.where(:published => true).order(params[:order])

Model Cont'd@published = Post.published@unpublished = Post.unpublished

class Post < ActiveRecord::Base default_scope :order => 'title' named_scope :published, :conditions => {:published => true} named_scope :unpublished, :conditions => {:published => false}end

class Post < ActiveRecord::Base default_scope order('title') scope :published, where(:published => true) scope :unpublished, where(:published => false)end

Action ViewCross-Site Scripting (XSS)

post %>Show

true %>Show

true) do |f| %>

Deprecated Methodslink_to_remote # link_to "Somewhere", '/posts/1', :remote => true# Somewhereremote_form_for observe_fieldobserve_formform_remote_tagbutton_to_remotesubmit_to_remotelink_to_functionperiodically_call_remote

prototype_legacy_helperhttps://github.com/rails/prototype_legacy_helper

Migration from Rails2 to Rails3rvm install 1.9.2rvm use 1.9.2@Rails3 --creatervm gemset listgem install rails -v=3.1.3script/plugin install git://github.com/rails/rails_upgrade.gitrake rails:upgrade:checkrake rails:upgrade:backuprvm 1.9.2rails new . -d mysqlbundle installrails s

Click to edit the title text format