Rails 5
Posted on Dec 14, 2015 by Fernando T
While there is no specific date for the release of Rails 5, the core team is working hard, and they are expected to launch version 5 soon. So I anticipate some of the new features and major changes regarding Rails 4.
##Rails 5 will only work on Ruby 2.2 and above Rails Framework runs over Ruby language (https://www.ruby-lang.org/en/). Keep in mind that for Rails 5 compatibility, you will need to have Ruby 2.2.2 or greater.
With Ruby 2.2.2, Rails 5 should get an improvement in performance, less memory usage, and less time spent in GC. This should help to improve the performance Besides the Ruby 2.2 improvements, the core team is doing a good job reducing object allocations, freezing immutable strings, removing unnecessary dependencies, and optimizing common operations. All of this will help to make Rails 5 faster.
So, if you are thinking of upgrading Rails for an existing application, it would also be a good time to consider an upgrade of Ruby.
##Turbolinks 3
Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body (or parts of it) and the title in the header.
Rails 4 includes Turbolinks. In Rails 5, we will be get a new version that, with the help of HTML5 custom data attributes, will have better speed and rendering in our Rails applications. The most important change to note in the new version is the Partial Replacement feature. Now, we will be able to tell Turbolinks what content we need to replace and what we do not. Turbolinks will look for HTML5 custom attributes data-turbolinks-permanent and data-turbolinks-temporary to decide the replacement strategy in our DOM.
##Action Cable
Action Cable seamlessly integrates WebSockets with the rest of your Rails application.
WebSocket(https://en.wikipedia.org/wiki/WebSocket) is a protocol providing full-duplex communication channels over a single TCP connection.
Action Cable allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable. It’s a full-stack offering that provides both a client-side JavaScript framework and a server-side Ruby framework. You have access to your full domain model written with ActiveRecord or your ORM of choice.
More details and examples on https://github.com/rails/actioncable
##ActiveRecord
###ActiveRecord::Base#where.or In Rails 5 we can use OR conditions with ActiveRecord in a native way. So, we can build this ActiveRecord query:
And the builder will translate to:
###belongs_to is required by default
When you create a Employee belongs_to Class relation, it was possible to create an employee without an associated Class relation. This leads to a lot of data inconsistencies. With Rails 5, the parent has become mandatory. If you try to insert an empty record here, ActiveRecord will raise an exception.
However, if your belongs_to association is not required, you could use the optional: true option:
###ActiveRecord’s attribute API The new API (http://edgeapi.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html) adds functionality on top of ActiveRecord models, and made it possible to override an attribute type to be a different type.
Consider the case where we have a field in the database defined as decimal but in our app we only care for the integer part of the number.
So, With attribute API we can do something like this:
Here we are overriding the automatically generated attribute from the database schema to be cast in our model as an integer instead of the original decimal.
###MySQL ActiveRecord adapter gets JSON support
If you happen to run your Rails application on top of MySQL 5.7.8 then your database has a new native JSON data type. From Rails 5 you should be able to use this new data type (https://github.com/rails/rails/pull/21110) in your ActiveRecord models.
##Other Features
###Render views outside of actions Rails 5 implements this feature with a render method, which is now available as a controller class method.
eg:
It supports the same arguments as an instance method but returns a rendered text of a template.
More details of this new feature here (https://medium.com/evil-martians/new-feature-in-rails-5-render-views-outside-of-actions-2fc1181e86a8#.gb2inzic6)
###Rake Inside Rails Now you don’t need to switch context between the Rake and Rails commands. You can run all Rake tasks with the Rails keyword.
For instance:
will now become:
##Conclusion
These are only a subset of the new features that we expect in the new Rails 5 version. In the Rails community, we are excited for all of these features…and MORE!
I leave to you the task of exploring and trying the rest of the cool features that Rails 5 provides!
##References & more information:
www.sitepoint.com/whats-new-rails-5
evilmartians.com/chronicles/rails-5-whats-new
rubyinrails.com/2015/09/04/what-is-new-in-rails-5-feature-changes
blog.michelada.io/whats-new-in-rails-5
medium.com/evil-martians/new-feature-in-rails-5-render-views-outside-of-actions