What's new in Rails 5?

Tamás Michelberger

SSP

@tmichelberger

Lots of small improvements and catching up

Deprecations and breaking changes

Ruby 2.2.2 and above

  • Performance improvements
  • Incremental and Symbol GC
  • Ruby 2.0 syntax:
    • Key word arguments
    • Module#prepend

belongs_to required by default

  • In 4.x belongs_to gained a :required option

  • In 5.0 :required will be deprecated
  • Use :optional instead

alias_method_chain

Deprecated in favor of Module#prepend

What was Alias_method_chain anyway?

alias_method_chain :foo, :feature

# is the same as

alias_method :foo_without_feature, :foo
alias_method :foo, :foo_with_feature

Overriding a method but keeping the original version around.

Module#prepend

class Example
end

Example.ancestors
# => [Example, Object, Kernel, BasicObject]

module After
end

class Example
  include After
end

Example.ancestors
# => [Example, After, Object, Kernel, BasicObject]

module Before
end

class Example
  prepend Before
end

Example.ancestors
# => [Before, Example, Object, Kernel, BasicObject]

Halting callbacks with returning false

Use throw :abort instead

catch-trhow for Halting execution of a block

catch :done do
  loop do
    do_something_awesome
    throw :done if rand(100) < 10
  end
end

Keyword arguments

in your controller tests

# instead of this
get :show, { id: 12 }, nil, { user_id: 5 }

# now write this
get :show, params: { id: 12 }, session: { user_id: 5 }

Controller test deprecations

  • assings
  • assert_template
  • assert_select still works

  • Encourages to test the controller's result (HTTP status, headers and body) instead of internals.

New features

rails-api

  • rails-api project was merged into core
  • It is a modified version of base Rails geared toward JSON APIs

New minitest runner

  • RSpec like output
  • Running tests by specifying the line number 
  • Suggested commands to (re)execute failing tests

has_secure_token

Just like has_secure_password but for tokens.

class User < ActiveRecord::Base
 has_secure_token :auth_token, :password_reset_token, key_length: 30
end

user = User.new
user.save
user.auth_token                       # => "973acd04bc627d6a0e31200b74e2236"
user.password_reset_token             # => "e2426a93718d1817a43abbaa8508223"
user.regenerate_auth_token!           # => true
user.regenerate_password_reset_token! # => true

ActiveRecord#where.or

OR clauses has finally landed in Rails.

Post.where('id = 1').or(Post.where('id = 2'))
# => SELECT * FROM posts WHERE (id = 1) OR (id = 2)

Render from anywhere

Rendering views from anywhere in your application.

# render template
ApplicationController.render 'templates/name'
# render action
FooController.render :index
# render file
ApplicationController.render file: 'path'
# render inline
ApplicationController.render inline: '<%= 1 + 2 %>'

ActiveJob

  • It was introduced in Rails 4.2
  • In 5.0 it gets
    • priorities
    • locale awareness

Attributes API

New API for custom typecasting.

# db/schema.rb
create_table :store_listings, force: true do |t|
  t.decimal :price_in_cents
end

# app/models/store_listing.rb
class StoreListing < ActiveRecord::Base
end

store_listing = StoreListing.new(price_in_cents: '10.1')

# before
store_listing.price_in_cents # => BigDecimal.new(10.1)

class StoreListing < ActiveRecord::Base
  attribute :price_in_cents, :integer
end

# after
store_listing.price_in_cents # => 10

Turbolink 3 partial updates

Action CABLE

Action Cable

  • First class WebSocket integration in Rails
  • It introduces redis and EventMachine as new dependencies
  • Full stack: provides client and server side as well
  • API and implementation is still rapidly chaning

WebSockets

  • Two way communication between server and client.
  • It is basically an upgraded HTTP request to a bare TCP connection.
  • Good overall browser support.
  • Real-time web (buzzword alert!!)

Thanks!

Any Questions?

Links

What's new in Rails 5?

By Tamás Michelberger

What's new in Rails 5?

A short introduction to new features and breaking changes in Rails 5.

  • 634