Skip to content

Instantly share code, notes, and snippets.

@latortuga
Last active October 29, 2016 14:32
Show Gist options
  • Save latortuga/ac410296c83e8bf2f270 to your computer and use it in GitHub Desktop.
Save latortuga/ac410296c83e8bf2f270 to your computer and use it in GitHub Desktop.
rails-4-2-overview

What's new in Rails 4.2?

ActiveJob

Adapter layer on top of queueing systems like Resque and Delayed Job. Full adapter list: :backburner, :delayed_job, :qu, :que, :queue_classic, :resque, :sidekiq, :sneakers, :sucker_punch

ActiveJob::Base.queue_adapter = :inline # default queue adapter

# Declare a job:
class MyJob < ActiveJob::Base
  queue_as :my_jobs

  def perform(record)
    record.do_work
  end
end

# Enqueue a job:
MyJob.perform_later(@user) # Perform as soon the queueing system is free.
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(@user)  # Perform tomorrow at noon.
MyJob.set(wait: 1.week).perform_later(@user) # Perform 1 week from now.

ActionMailer

New deliver_later method that pushes an email to ActiveJob. ActionMailer method deliver has been deprecated and replaced by deliver_now.

GlobalID

Provides a new serialization for parameters into ActiveJob jobs.

Old:

class TrashableCleanupJob
  def perform(trashable_class, trashable_id, depth)
    trashable = trashable_class.constantize.find(trashable_id)
    trashable.cleanup(depth)
  end
end

New:

class TrashableCleanupJob
  def perform(trashable, depth)
    trashable.cleanup(depth)
  end
end

Mix GlobalID::Identification in to any model with a #find(id) class method. Support is automatically included in Active Record.

>> person_gid = Person.find(1).to_global_id
=> #<GlobalID ...

>> person_gid.uri
=> #<URI ...

>> person_gid.to_s
=> "gid://app/Person/1"

>> GlobalID::Locator.locate person_gid
=> #<Person:0x007fae94bf6298 @id="1">

Real foreign keys

add_foreign_key/remove_foreign_key are now available in migrations.

# add a foreign key to `articles.author_id` referencing `authors.id`
add_foreign_key :articles, :authors
   
# add a foreign key to `articles.author_id` referencing `users.lng_id`
add_foreign_key :articles, :users, column: :author_id, primary_key: "lng_id"
   
# remove the foreign key on `accounts.branch_id`
remove_foreign_key :accounts, :branches
   
# remove the foreign key on `accounts.owner_id`
remove_foreign_key :accounts, column: :owner_id

Only supported by mysql, mysql2, postgresql adapters at this time.

respond_with / class-level respond_to extracted

These two constructs have been extracted to the responders gem.

If you're still using them, add the following to your Gemfile:

gem 'responders', '~> 2.0'
# app/controllers/users_controller.rb
 
class UsersController < ApplicationController
  respond_to :html, :json
 
  def show
    @user = User.find(params[:id])

    respond_with @user
  end
end

Instance-level respond_to is unaffected:

# app/controllers/users_controller.rb
 
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html
      format.json { render json: @user }
    end
  end
end

Adequate Record

tenderlove has made some ActiveRecord queries up to twice as fast.

Web Console

For those still on Rails < 4.2

better_errors + binding_of_caller = "REPL, local/instance variable inspection, pretty stack frame names"

New version: Accessible in development mode localhost:3000/console

What about Rails 4.1? (Release: April 8, 2014)

Spring Preloader

It makes running tests, rake, and generators much faster on large applications.

Alternatives: zeus, spork

ActionPack variants

before_action { request.variant = :tablet if request.user_agent =~ /iPad/ }
# ...
respond_to do |format|
  format.html do |html|
    html.tablet # renders app/views/projects/show.html+tablet.erb
    html.phone { extra_setup; render ... }
  end
end

ActiveRecord enums

class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]
end
 
conversation.archived!
conversation.active? # => false
conversation.status  # => "archived"
 
Conversation.archived # => Relation for all archived Conversations
 
Conversation.statuses # => { "active" => 0, "archived" => 1 }

ActionMailer Previews

App secrets in secrets.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment