Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails 4.0 Whirlwind Tour

Rails 4.0 Whirlwind Tour

04ec66ce302551a608ec3a0451cfa1f2?s=128

Andy Lindeman

October 10, 2012
Tweet

Transcript

  1. Rails 4.0 Whirlwind Tour Andy Lindeman @alindeman

  2. At the end! Priority given to tweets: #ATLRUG Questions?

  3. 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 ...
  4. Most Important Change # Rails 3 > "police".singularize => "polouse"

    # Rails 4 > "police".singularize => "police"
  5. ActiveSupport::Queue Rails.queue

  6. Rails.queue class ExpensiveOperation def run # ... end end Rails.queue.push(ExpensiveOperation.new)

  7. 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
  8. Async ActionMailer # All mailers async config.action_mailer.async = true #

    Specific mailer async class MyMailer < ActionMailer::Base self.async = true end
  9. Mass assignment protection on controllers https://github.com/rails/strong_parameters strong_parameters

  10. 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
  11. 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
  12. 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
  13. PJAX-esque https://github.com/rails/turbolinks Turbolinks

  14. Cache Digests https://github.com/rails/cache_digests Russian Doll Caching

  15. 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 %>
  16. 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 %>
  17. Caching Deprecations • Action caching removed and extracted to actionpack-action_caching

    gem • Page caching removed and extracted to actionpack-page_caching gem
  18. "We'll do it ActionController::Live" ActionController::Live

  19. 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
  20. ActionController::Live • Needs correctly configured web-server ◦ thin/rainbows/puma over unicorn

    • Resources ◦ Tenderlove Making: Is it live? ◦ Rails Guides: ActionController
  21. 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
  22. ActiveRecord

  23. 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
  24. Relation#all replaces #scoped # Rails 4 > Post.scoped DEPRECATION WARNING:

    Use Post.all instead
  25. 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
  26. /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
  27. Relation#none # Chainable null object > Post.none #<ActiveRecord::Relation []> #

    Doesn't hit database > Post.none.to_sql "" > Post.none.to_a []
  28. #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
  29. (There are more, these are highlights) Possible Upgrade Gotchas

  30. Enough said Ruby >= 1.9.3

  31. 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
  32. Rails 2 Finder Syntax: Gone! • User.find(:all, ...) • User.find(:first,

    ...) • User.find(:last, ...) • Use ARel chained syntax
  33. 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
  34. 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")
  35. 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!
  36. 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) }
  37. 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
  38. 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
  39. More Resources • Rails 4.0 Release Notes • Rails 4.0

    Mind Map • Boston.rb: What to Expect in Rails 4.0 • Edgerails.info Blog
  40. When? • No idea! • "Probably before Christmas [2012]" -

    @sikachu
  41. Extra Content

  42. 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
  43. activeresource extracted from rails • gem 'rails' no longer gets

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

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