Image for post
Image for post

Rails 5: what’s new

Test runner, where.or, new test request API, Render From Anywhere and no more alias_method_chain

Kir Shatrov
Apr 7, 2015 · 5 min read
Image for post
Image for post

Rails Test runner

$ bin/rails test test/models/post_test.rb
Run options: — seed 48587
# Running:..FF.Finished in 0.024376s, 205.1198 runs/s, 287.1677 assertions/s.1) Failure:
PostTest#test_failling_one [test/models/post_test.rb:32]:
Failed refutation, no message given
2) Failure:
PostTest#test_has_likes [test/models/post_test.rb:23]:
Failed assertion, no message given.
5 runs, 7 assertions, 2 failures, 0 errors, 0 skipsFailed test:rails test test/models/post_test.rb:32
rails test test/models/post_test.rb:23
Image for post
Image for post

ActiveRecord::Base#where.or

User.where(users[:name].eq(‘bob’).or(users[:age].lt(25)))
Post.where("id = 1").or(Post.where("id = 2")) # => SELECT * FROM posts WHERE (id = 1) OR (id = 2)
Image for post
Image for post

belongs_to is required by default

class User
belongs_to :org, optional: true
end
Image for post
Image for post

ActiveRecord::Base#has_secure_token

class User < ActiveRecord::Base
has_secure_token :token1, :token2, key_length: 30
end
user = User.new
user.save
user.token1 # => “973acd04bc627d6a0e31200b74e2236”
user.token2 # => “e2426a93718d1817a43abbaa8508223”
user.regenerate_token1! # => true
user.regenerate_token2! # => true
Image for post
Image for post

No more alias_method_chain

class ActionController::Base
# overriding ActionController::Base load from gem
alias_method_chain :params, :patched
def params_with_patched
result = params_without_patched
# we can access and modify the result here
result
end
end
module PatchedParams
def params
result = super
# we can access and modify result here
result
end
end
ActionController::Base.prepend(PatchedParams)
Image for post
Image for post

Keyword arguments in controller & integration tests

get :index, { id: 1 }, nil, { user_id: 1 }
post :create, params: { y: x }, session: { a: ‘b’ }
get :view, params: { id: 1 }
get :view, params: { id: 1 }, format: :json
Image for post
Image for post

Render From Anywhere

ApplicationController.render _render_options_
Image for post
Image for post
Image for post
Image for post

Evil Martians

The Martian Chronicles

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store