While Rails 5 is still in the pipeline I went through its changelog and the source code to see what’s coming up. I got very excited about a bunch of new features I believe will make our lives easier!
So, let’s start by looking at the changes. First of, the new version will require Ruby 2.2. So if you are thinking of upgrading your app to Rails 5, you should start with upgrading Ruby.
Rake Tasks
On previous versions of Rails, you have commands executed with rails and others with rake. In this new release, one of the features includes executing all Rake tasks using just Rails. The new format is similar to the older one:
rails db:migrate.
This move toward simplification will make both the framework and task automatization easier to learn.
Action Mailer
Regarding Action Mailer, the main change is that deliver and deliver! methods were removed and replaced by deliver_later and it’s bang version. This change was introduced to make Action Mailer compatible with Action Job (see below).
Also, all *_path methods have been removed to prevent introducing programming bugs when writing senders. Instead, Rails 5 will force us to use use the *_url methods.
Another change is that templates now respects I18n fallbacks. In previous versions of Rails if you had a template called welcome.en-US.erb and another called welcome.erb even if you had set the en-US as the default language, the template welcome.erb would be rendered. Rails 5 will use the local option rather than the fallback one.
So far, so good… and we’re just getting started.
Action Pack
Apart from Action Mailer, Action Pack also gets some interesting changes. Under development environment, when you browse to a non-existing route, Action Pack displays route information, letting the user filter the results. In previous versions this filtering was a pure Javascript regex trying to guess the route and was a little bit unreliable. Rails 5 improves this feature by getting accurate routing information directly from the Rails backend instead.
Action Controller
Now it is the turn for Action Controller. The novelty is a parameter of the protect_from_forgery method that allows prepending (or not) the forgery protection. As in previous versions, it also lets you add a conditional –e.g. like avoiding forgery protection in JSON requests. This is how:
protect_from_forgery prepend: false, unless: -> { request.format.json? }
When this option is set to false, Rails will not prepend the protection. In the example, it will also turn the protection off for JSON requests. If true, the callback will be inserted in the first place of the callback chain.
Action View
There are some cool new stuff in the Action View, too. One of them is the possibility of naming partials using any character, not only alphanumeric ones. In previous versions, partials’ names must start with an underscore and then followed only by alphanumeric characters, numbers, or underscores. In Rails 5 we can use any character after the underscore.
Another change is that helper methods like content_tag_for , div_for and so on were removed from the core and moved out to a separate gem, called record_tag_helper.
Active Job
Active Job, a great addition to Rails 4.2.0, also got some tweaks in the new version. Formerly a separate gem, Active Job was merged into Rails to serve as an adapter layer for the most popular queuing libraries around.
Active Job allows us to change between inline jobs or delay jobs only by changing one line of code:
From:
ActiveJob::Base.queue_adapter = :inline
To:
ActiveJob::Base.queue_adapter = :sidekiq # Sidekiq for instance # The following are the allowed adapters :backburner, :delayed_job, :qu, :que, :queue_classic, # :resque, :sidekiq, :sneakers, :sucker_punch
In the new version, all the jobs inherit from a base class app/jobs/application_job.rb. This is to be consistent with the already existing structure of others components in Rails, like controllers, models and the like.
The following is an example of how to use Active Job:
class BackgroundProcessJob < ActiveJob::Base queue_as :background_process def perform(id) # Perform the background process end end
Active Job is integrated with Action Mailer allowing us to easily send emails asynchronously. The methods deliver_now and deliver_later, and their bang versions, will use the preferred queue adapter.
Active Record
In my opinion, the most existing changes are in Active Record.
First, let’s refer to belongs_to. Before Rails 5, given an Employee that belongs_to a Company, nothing prevented from creating a new Employee without specifying the Company it belongs to. In the new version, a validation error will be thrown if the associated record is not specified.
If you migrate your app to Rails 5, this feature will remain disabled unless you specify the opposite. It will be enabled when creating new applications, though, or if you add the following line to the Rails configuration:
config.active_record.belongs_to_required_by_default = true
Second, another useful addition is ActiveRecord::Base.suppress. This method prevents its class from being saved while executing the enclosing block.
class Post < ActiveRecord::Base has_many :followers after_create -> { Notification.create!(post: self, followers: followers) } def copy # During the copy we want to avoid notifications from being saved Notification.suppress do # Perform a copy of the post. end end end
Third, a much expected feature: now the or method is available for relations:
Post.where('id = 1').or(Post.where('id = 2'))
And last, for migrations now it’s possible to specify a parameter :if_exists for dropping a table only if it exists.
And there are a lot of bug fixes, and other features… I can’t wait to start using this new version of Rails! Looking forward to it!