
WAIT IS OVER !!!! Lets Get into Action
Rails was praised for being the easiest way to get Ruby on the web. It was the easiest option, and the best. Though plenty of other frameworks arrived, Rails is still the industry leading framework for Ruby developers.
Rails 5 was announced on RailsConf past April. This was a major version of Ruby On Rails and there were number of new features and performance improvements that were included to this release.
Rails 5.0 beta1 has been released and it is 790 scruffy-looking nerf herders contributed to this release with over 7000 commits. Rails 5 works only with ruby 2.2.2 or better and reason is ruby 2.2 introduces a number of new features and performance enhancements that the Rails team wants to capitalize on and mainly to take advantage of new Incremental GC which will help to reduce memory consumption by our Rails applications.
New stuffs added to Rails 5
1) Action Cable
2) Rails API
3) New Command Router
4) Attributes API
5) ApplicationRecord
6) ActiveRecord::Relation#or
ActionCable:
It’s a completely integrated solution that includes an EventMachine-powered connection loop, a thread-backed channels layer for server-side processing, and a JavaScript layer for client-side interaction. It’s incredibly easy to use, and makes designing live features like chat, notifications, and presence so much easier.
The really lovely thing about Action Cable is that you get access to your entire Active Record and PORO domain model in your WebSockets work. We even added a brand-new ActionController::Renderer system that makes it trivial to render your templates outside of controllers, when you want to reuse server-side templates for WebSocket responses.
In development, Action Cable runs in-process with the rest of your app. To do this, we’ve switched the default development server from Webrick to Puma. In production, you may well want to run Action Cable servers in their own processes.
For more info: github-rails-actioncable
API Mode:
Rails is not only a great choice when you want to build a full-stack application that uses server-side rendering of HTML templates, but also a great companion for the new crop of client-side JavaScript or native applications that just needs the backend to speak JSON. We’ve made this even clearer now with the new –api mode. If you create a new Rails application using rails new backend –api, you’ll get a slimmed down skeleton and configuration that assumes you’ll be working with JSON, not HTML.
There’s still more work to be done on this feature, but we’re off to a great start. By default, API mode just relies on #to_json calls on model classes. But you can either use Jbuilder, Active Model Serializers, or look at the new JSONAPI::Resources project for a more advanced solution.
For more info: github-rails-pull
Command Router:
Though we have used both rails and rake commands, it is often difficult to remember which one to make use of and it definitely confusing for the beginners to understand when this needs to be used. For example, we start a console with rails console, but run migrations with rake db:migrate? That doesn’t make any sense. Starting in Rails 5, many of these old rake commands can be run with rails instead.
For more info: github-rails-issues
Attributes API:
This new API adds functionality on top of ActiveRecord models, it made 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. We can in our app just ignore the decimal part and format our number everywhere we need to use it to only display the integer part.
With attribute API we can do this in an easy way:
class Book < ActiveRecord::Base
end
book.quantity # => 12.0
class Book < ActiveRecord::Base
attribute :quantity, :integer
end
book.quantity # => 12
Here we are overriding the automatically generated attribute from the database schema to be cast in our model as an integer instead the original decimal. For every interaction of our model with the database, the attribute will be treated as a decimal as it should be.
We can even define our own custom types just by creating a class derived from ActiveRecord::Type::Value and implementing its contract to #cast, #serialize, and #deserialize values.
Custom attributes will honor ActiveModel::Dirty to track changes in our models. Also, these new attributes can be virtual, so there is no need to be backed by a table column.
ApplicationRecord:
Just like ApplicationController, we’re getting an ApplicationRecord model superclass in Rails 5. Now you don’t have to monkeypatch ActiveRecord::Base to add functionality!
For more info: github-rails-pull
ActiveRecord::Relation#or:
ActiveRecord::Relation is getting #or method, this will allow us to write queries with ActiveRecord DSL as follows:
# => SELECT * FROM books WHERE (status = 1) OR (status = 3)
#or method accepts a second relation as a parameter that is combined with an or. #or can also accept a relation in a form of model scope.
class Book < ActiveRecord::Base
scope :new_coming, -> { where(status: 3) }
end
Book.where(‘status = 1’).or(Book.new_coming)
# => SELECT * FROM books WHERE (status = 1) OR (status = 3)
You should really checkout the CHANGELOGs, though. There’s just so much new and good stuff available in all the frameworks:
- Action Mailer CHANGELOG
- Action Pack CHANGELOG
- Action View CHANGELOG
- Active Model CHANGELOG
- Active Record CHANGELOG
- Active Support CHANGELOG
- Active Job CHANGELOG
- Railties CHANGELOG
Hope the above informations were useful. Until next time…..