# Rails 5: What’s new

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

- Date: 2015-04-07T00:00:00.000Z
- Authors: Kir Shatrov
- Categories: 
- URL: https://evilmartians.com/chronicles/rails-5-whats-new

---

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

I’ve found a great post regarding the [upcoming changes in Rails 5](https://www.jvanbaarsen.com/blog/2015/03/23/upcoming-changes-in-rails-5-0.html) by [Jeroen van Baarsen](https://twitter.com/jvanbaarsen), and you should check it out if you’re interested in Rails Edge developments.

This post was written before the RailsConf ActionCable/Turbolinks announces. To see our overview of RailsConf 2015 and our thoughts about the new features, check this post:
*Read also Evil Martians' article*: https://evilmartians.com/chronicles/railsconf-2015-recap

However, I felt like this post is just not enough since there are so many interesting things in the upcoming Rails 5 release. So here goes my very own “yet another review of Rails 5 features”.

## Rails Test runner

The new runner is, in fact, my favourite piece of Rails 5. I love minitest, and I always was a bit envy of RSpec users, as they have a great runner that allows you to specify the file and line number of a failing spec.

Now, [thanks to Yves Senn](https://github.com/rails/rails/pull/19216), we have the same feature when using Rails’ minitest:

```
$ 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 skips
Failed test:
rails test test/models/post_test.rb:32
rails test test/models/post_test.rb:23
```

At the bottom of this output, you can see the runner suggesting commands to execute only the failing samples line by line.

## ActiveRecord::Base#where.or

Previously, to use `OR` conditions with the ActiveRecord query builder, we had to construct the query manually by using Arel:

```ruby
User.where(users[:name].eq('bob').or(users[:age].lt(25)))
```

In 2013, Rails 4.0 [introduced](https://robots.thoughtbot.com/activerecords-wherenot) `where.not` chain method in ActiveRecord, and [now in 5.0 we finally have](https://github.com/rails/rails/pull/16052) `where.or`:

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

## belongs_to is required by default

DHH [wrote](https://github.com/rails/rails/issues/18233) that almost every `belongs_to` declaration seemed to be a required association. Later, [@simi implemented it](https://github.com/rails/rails/pull/18937).

However, if your `belongs_to` association is not required, you could use the `optional: true` option:

```ruby
class User
  belongs_to :org, optional: true
end
```

## ActiveRecord::Base#has_secure_token

Do you use the [`has_secure_password` option](http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html) introduced back in Rails 3.1?

From now on, for fields like subscription token, we have `has_secure_token`:

```ruby
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
```

## No more alias_method_chain

`alias_method_chain` method was there since the very early Rails versions and it was used all over the Rails code base and by various gems.

It was widely used to monkey patch or tweak any previously declared method:

```ruby
module ActionController
  class 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
end
```

But now, with `Module#prepend` available since Ruby 2.0 we can use native Ruby features to achieve the same result:

```ruby
module PatchedParams
  def params
    result = super
    # we can access and modify result here
    result
  end
end
ActionController::Base.prepend(PatchedParams)
```

In Rails 5, `alias_method_chain` [will be deprecated](https://github.com/rails/rails/pull/19434) in favor of the native `Module#prepend`.

Read more about `alias_method` chain here:

* [https://www.justinweiss.com/articles/rails-5-module-number-prepend-and-the-end-of-alias-method-chain/](https://www.justinweiss.com/articles/rails-5-module-number-prepend-and-the-end-of-alias-method-chain/)
* [http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/](http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/)

## Keyword arguments in controller & integration tests

Here is an example snippet you could find in a controller test:

```ruby
get :index, { id: 1 }, nil, user_id: 1
```

Does it make a lot of sense what every argument means exactly? Not much, so I proposed the [new syntax with keyword arguments support](https://github.com/rails/rails/pull/18323) for request methods:

```ruby
post :create, params: { y: x }, session: { key: 'value' }
get :view, params: { id: 1 }
get :view, params: { id: 1 }, format: :json
```
These changes apply when using any test framework — it doesn’t matter if you use RSpec or Minitest. The old syntax works as well, but it’s considered deprecated and will be removed later in Rails 5.1.

## Render From Anywhere

Earlier we had to use gems like [render_anywhere](https://github.com/yappbox/render_anywhere) to render any views outside of controller — for example in Rake tasks or background jobs.

Read more on the [new feature of Rails 5: Rendering views outside of actions](https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions).

In Rails 5 you’ll have the ability to render your views from anywhere:

```
ApplicationController.render _render_options_
```

Besides the usual rendering options, in Rails 5 there is a new `assigns` option for passing instance variables to templates.

---

Hope you like these new features. We will try to review more of them as we come closer to Rails 5 release candidates.

A great way to stay in touch with Edge Rails development is to subscribe to [This week in Rails](https://rails-weekly.ongoodbits.com/) to receive weekly email with scoop of interesting commits, pull requests and other stuff that is happening with Rails Core.

---

This article was written by [Evil Martians](https://evilmartians.com), a product development consultancy trusted by teams from early-stage startups to public companies. If you're working through a hard problem, [we can help](https://evilmartians.com/contact-us)
