Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Upgrading to Rails 4

Upgrading to Rails 4

Presentation given at the December 2013 edition of Coimbra's own Ruby meetup, Coimbra.rb

http://www.meetup.com/Coimbra-rb

Avatar for Jorge Oliveira Santos

Jorge Oliveira Santos

December 13, 2013
Tweet

Other Decks in Programming

Transcript

  1. Rails 4 First major release since 2011 Released in June

    4.01 in November 4.02 in December 2
  2. First things first Make sure you have a solid test

    suite This will make the process straightforward Otherwise… It’s a good time to start writing one! If you are not willing to write it, expect tons of debugging work… 3
  3. Gem compatibility rails4_upgrade gem $ rake rails4:check! ! ** GEM

    COMPATIBILITY CHECK **! +--------------------+----------------------+! | Dependency Path | Rails Requirement |! +--------------------+----------------------+! | draper 0.18.0 | actionpack ~> 3.2 |! | draper 0.18.0 | activesupport ~> 3.2 |! | simple_form 2.0.4 | actionpack ~> 3.0 |! | simple_form 2.0.4 | activemodel ~> 3.0 |! +--------------------+----------------------+! https://github.com/alindeman/rails4_upgrade 4
  4. Gem compatibility (II) Ready 4 Rails 4 - http://ready4rails4.net/ Collaborative

    database Your gems don’t appear here? Contribute! ! ! ! ! 5
  5. Major Gemfile changes source 'https://rubygems.org'! ! # Bundle edge Rails

    instead: gem 'rails', github: 'rails/rails'! gem 'rails', '4.0.2'! ! # Use sqlite3 as the database for Active Record! gem 'sqlite3'! ! # Use SCSS for stylesheets! gem 'sass-rails', '~> 4.0.0'! ! # Use Uglifier as compressor for JavaScript assets! gem 'uglifier', '>= 1.3.0'! ! # Use CoffeeScript for .js.coffee assets and views! gem 'coffee-rails', '~> 4.0.0'! ! # Use jquery as the JavaScript library! gem 'jquery-rails'! ! # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks! gem 'turbolinks'! ! # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder! gem 'jbuilder', '~> 1.2'! ! group :doc do! # bundle exec rake doc:rails generates the API under doc/api.! gem 'sdoc', require: false! end! 6
  6. Directory changes Concerns folders for both controllers and models Better

    organization for default /test folder /plugins and /script folders removed /bin folder added - App binaries / rails, rake, gem-specific 7
  7. Configuration Changes # /config/initializers/secret_token.rb! ! # Rails 3! MyApp::Application.config.secret_token =

    '762a2f23950e3...'! ! # Rails 4! MyApp::Application.config.secret_key_base = 'xx762a2f2395...'! secret_token is now secret_key_base CSS and JS compressors must be specified (Asset Pipeline) # /config/environments/production.rb! ! # Compress JavaScripts and CSS.! config.assets.js_compressor = :uglifier! config.assets.css_compressor = :sass! 8
  8. ActiveRecord Deprecations Old finder syntax (isn’t this soooo Rails 2.x?)

    # Rails 2/3??! Post.find(:all, conditions: { published_on: 2.weeks.ago }, limit: 5)! ! # Rails 4! Post.where(published_on: 2.weeks.ago).limit(5)! Relationship-based ordering # Rails 3! has_many :readings, :order => :timestamp! ! # Rails 4! has_many :readings, -> { order(:timestamp) }! 9
  9. AR Deprecations (II) Scopes now require a callable object #

    Rails 3! scope :recent, where(published_at: Time.now - 2.weeks)! ! # Rails 4! scope :recent, -> { where(published_at: Time.now - 2.weeks) }! Changes on Eager Loading # Rails 3! Post.includes(:comments).where('comments.text = ?', '(Y)')! ! # Rails 4! Post.includes(:comments).where("comments.text = ?", '(Y)').references(:comments)! 10
  10. Other deprecated finders # find_all_by_! Rails 3: Team.find_all_by_name('Justice League')! Rails

    4: Team.where(name: 'Justice League')! ! # find_last_by_! Rails 3: Team.find_last_by_name('Justice League')! Rails 4: Team.where(name: 'Justice League').last! ! # find_or_create_by_! Rails 3: Team.find_or_create_by_name('Justice League')! Rails 4: Team.where(name: 'Justice League').first_or_create! ! # find_or_create_by_...!! Rails 3: Team.find_or_create_by_name!('Justice League')! Rails 4: Team.where(name: 'Justice League').first_or_create!! ! # find_or_initialize_by_! Rails 3: Team.find_or_initialize_by_name('Justice League')! Rails 4: Team.where(name: 'Justice League').first_or_initialize! ! # scoped_by_! Rails 3: Team.scoped_by_name('Justice League')! Rails 4: Team.where(name: 'Justice League')! 11
  11. Strong Parameters Despite of using attr_accessible on the model to

    control which params can be directly assigned This logic now lies on the controller # Rails 3! # app/models/comment.rb! attr_accessible :text! ! # Rails 4! # app/controllers/comments_controller.rb! def comment_params! params.require(:comment).permit(:text)! end! 12
  12. Strong Parameters (II) If you are not willing to update

    all your app to use strong_params: Just include the protected_attributes gem on your Gemfile github.com/rails/protected_attributes 13
  13. Routing Deprecations # Rails 3! match "/auth/:service/callback" => "services#create"! #

    Rails 4! get "/auth/:service/callback", to: "services#create"! ! # or! ! match "/auth/:service/callback", to: 'services#create', via: [:get, :post]! Match without specifying method PUT now defaults to PATCH 14
  14. Routing Concerns # Rails 3! resources :activities do! resources :comments!

    end! ! resources :tasks do! resources :comments! end! # Rails 4! concern :commentable do! ! resources :comments! end! ! resources :activities, concerns: [:commentable]! ! resources :tasks, concerns: [:commentable]! 15
  15. Deployment concerns Capistrano Assets Manifest file format has changed: Check

    if current release has assets_manifest.yml file Remove /shared/assets/manifest.yml Otherwise move manifest.yml to the current release path (rename it to assets_manifest.yml) and remove original file https://github.com/capistrano/capistrano/wiki/Upgrading-to-Rails-4 16