Share
Embed
Transcript
44 slides
-
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 ... -
Most Important Change # Rails 3 > "police".singularize => "polouse"
# Rails 4 > "police".singularize => "police" -
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 -
Async ActionMailer # All mailers async config.action_mailer.async = true #
Specific mailer async class MyMailer < ActionMailer::Base self.async = true end -
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 -
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 -
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 -
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 %> -
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 %> -
Caching Deprecations • Action caching removed and extracted to actionpack-action_caching
gem • Page caching removed and extracted to actionpack-page_caching gem -
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 -
ActionController::Live • Needs correctly configured web-server ◦ thin/rainbows/puma over unicorn
• Resources ◦ Tenderlove Making: Is it live? ◦ Rails Guides: ActionController -
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 -
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 -
Relation#all replaces #scoped # Rails 4 > Post.scoped DEPRECATION WARNING:
Use Post.all instead -
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 -
/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 -
Relation#none # Chainable null object > Post.none #<ActiveRecord::Relation []> #
Doesn't hit database > Post.none.to_sql "" > Post.none.to_a [] -
#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 -
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 -
Rails 2 Finder Syntax: Gone! • User.find(:all, ...) • User.find(:first,
...) • User.find(:last, ...) • Use ARel chained syntax -
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 -
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") -
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! -
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) } -
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 -
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 -
More Resources • Rails 4.0 Release Notes • Rails 4.0
Mind Map • Boston.rb: What to Expect in Rails 4.0 • Edgerails.info Blog -
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 -
activeresource extracted from rails • gem 'rails' no longer gets
you activeresource • activeresource will have its own release cycle -
Relation#___! posts = Post.all # Rails 3 posts = posts.where(title:
"foo") # Rails 4 posts.where!(title: "foo")