Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Rails 4.0 Whirlwind Tour

Rails 4.0 Whirlwind Tour

Andy Lindeman

October 10, 2012
Tweet

More Decks by Andy Lindeman

Other Decks in Programming

Transcript

  1. How to Install Rails 4? $ hub clone rails/rails $

    rails/railties/bin/rails new <path> --edge create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile ...
  2. Most Important Change # Rails 3 > "police".singularize => "polouse"

    # Rails 4 > "police".singularize => "police"
  3. Rails.queue # config/environments/production.rb # Jobs run in a background thread

    config.queue = ActiveSupport::Queue # Jobs run via Resque config.queue = ??? # Jobs run via delayed_job config.queue = ??? # Jobs run via Sidekiq (rails4 branch) config.queue = Sidekiq::Client::Queue
  4. Async ActionMailer # All mailers async config.action_mailer.async = true #

    Specific mailer async class MyMailer < ActionMailer::Base self.async = true end
  5. ActiveModel:: MassAssignmentSecurity (Rails 3) class Post < ActiveRecord::Base attr_accessible :title,

    :body end class PostsController <ApplicationController def create @post = Post.create(params[:post]) end end
  6. strong_parameters (Rails 4) class Post < ActiveRecord::Base end class PostsController

    <ApplicationController def create @post = Post.create(post_params) end private def post_params params.require(:post). permit(:title, :body) end end
  7. strong_parameters • Passing params directly into a mass-assigned model method

    from a controller raises ActiveModel::ForbiddenAttributesError • Mass-assigned model methods used elsewhere in the app accept all params • attr_accessible & attr_protected removed and extracted to the protected_attributes gem
  8. Caching: The Problem # app/views/projects/show.html.erb <% cache [ "v1", project

    ] do %> <h1>All documents</h1> <%= render project.documents %> <% end %> # app/views/documents/_document.html.erb <% cache [ "v1", document ] do %> My document: <%= document.name %> <% end %>
  9. cache_digest's solution # app/views/projects/show.html.erb <% cache project do %> <h1>All

    documents</h1> <%= render project.documents %> <% end %> # app/views/documents/_document.html.erb <% cache document do %> My document: <%= document.name %> <% end %>
  10. Caching Deprecations • Action caching removed and extracted to actionpack-action_caching

    gem • Page caching removed and extracted to actionpack-page_caching gem
  11. ActionController::Live class MyController < ApplicationController include ActionController::Live def index 100.times

    { response.stream.write "hello\n" # sleep 1 } response.stream.close end end Example from http://tenderlovemaking.com/2012/07/30/is-it-live.html
  12. ActionController::Live • Needs correctly configured web-server ◦ thin/rainbows/puma over unicorn

    • Resources ◦ Tenderlove Making: Is it live? ◦ Rails Guides: ActionController
  13. PATCH Verb class PostsController <ApplicationController # PATCH/PUT /posts/1 # PATCH/PUT

    /posts/1.json def update @post = Post.find(params[:id]) # ... end end • form_for @persisted_object uses PATCH • API requests can use PUT or PATCH
  14. Relation#all returns a relation (chainable) # Rails 3 > Post.all.class

    Array # Rails 4 > Post.all.class ActiveRecord::Relation > Post.all.to_a.class Array
  15. nil is no longer whiny # Rails 3 > nil.id

    RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id # Rails 4 > nil.id NoMethodError: undefined method `id' for nil:NilClass
  16. /updates?_(attribute|column)s?/ Validations run? Callbacks run? updated_at touched? Dirty attributes updated?

    update_attributes( :name => "value") YES YES YES YES update_attribute( :name, "value") NO YES YES YES update_columns( :name => "value") NO NO NO NO update_column( :name, "value") NO NO NO NO
  17. Relation#none # Chainable null object > Post.none #<ActiveRecord::Relation []> #

    Doesn't hit database > Post.none.to_sql "" > Post.none.to_a []
  18. #first and #last no longer have ordering gotcha # Rails

    3 > Post.first SELECT "posts".* FROM "posts" LIMIT 1 # Rails 4 > Post.first SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT 1
  19. Plugins are dead; long live plugins • Gems are A-OK!

    We're talking old school plugins • vendor/plugins is no longer a thing • Move plugins to lib/ and require manually in config/initializers • Or create a gemified version and manage via Bundler
  20. Rails 2 Finder Syntax: Gone! • User.find(:all, ...) • User.find(:first,

    ...) • User.find(:last, ...) • Use ARel chained syntax
  21. includes as an OUTER JOIN w/ conditions # Rails 3

    > Post.includes(:comments). where("comments.created_at > ?", ...) SELECT ... from posts LEFT OUTER JOIN ... WHERE ... # Rails 4 > Post.includes(:comments). where("comments.created_at > ?", ...) DEPRECATION WARNING
  22. includes as an OUTER JOIN w/ conditions # Rails 4

    > Post.includes(:comments). where("comments.created_at > ?", ...). references(:comments) > Post.includes(:comments). where(:comments => { :title => "..." }) > Post.includes(:comments). order("comments.created_at")
  23. Dynamic Finders find_by_... find_by_...! OK find_all_by_... scoped_by_... where(...) find_last_by_... where(...).last

    find_or_initialize_by_... where(...). first_or_initialize find_or_create_by_... where(...). first_or_create find_or_create_by_...! where(...). first_or_create!
  24. Eager-evaluated scopes # Deprecated scope :published, where(:published => true) #

    Pitfall scope :recent, where("created_at > ?", 1.week.ago) # Use a lambda (always, not just for time!) scope :published, -> { where(:published => true) }
  25. AJAX Forms & authenticity_token • form_for :remote => true •

    rails.js handles adding authenticity_token upon submit • JavaScript disabled? ◦ Rails 3 embeds token in form. Rails 4 does not. • :authenticity_token => true
  26. Deprecation Policy • "RailsVer" • 4.0 mostly just deprecates •

    4.1 removes those features ◦ Or provides them as gems you yourself must add to Gemfile • 5.0 removes support for some of those gems
  27. More Resources • Rails 4.0 Release Notes • Rails 4.0

    Mind Map • Boston.rb: What to Expect in Rails 4.0 • Edgerails.info Blog
  28. Routing Concerns # routes.rb concern :commentable do |options| resources :comments,

    options end concern :image_attachable do resources :images, only: :index end resources :posts, concerns: [:commentable, : image_attachable] do resource :video, concerns: :commentable end
  29. activeresource extracted from rails • gem 'rails' no longer gets

    you activeresource • activeresource will have its own release cycle
  30. Relation#___! posts = Post.all # Rails 3 posts = posts.where(title:

    "foo") # Rails 4 posts.where!(title: "foo")