Tamás Michelberger
Building software even when it's an overkill
In 4.x belongs_to gained a :required option
Deprecated in favor of Module#prepend
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.
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]
Use throw :abort instead
catch :done do
loop do
do_something_awesome
throw :done if rand(100) < 10
end
end
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 }
assings
assert_template
assert_select still works
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
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)
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 %>'
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
https://medium.com/evil-martians/the-rails-5-post-9c76dbac8fc#.itnql8l5m
By Tamás Michelberger
A short introduction to new features and breaking changes in Rails 5.