Discussing some of the features of Rails 4.2

Vinothini B

2 min read

I would like to discuss some of the new features of the new Rails 4.2 in this blog.
ActiveJob, ActionMailer #deliver_later, AdequateRecord, Web Console and Foreign key support are some noted features in Rails 4.2. Lets me go over them one by one.
Web Console:
First we will see about Web Console. When we create a new Rails 4.2 app you will see that there’s a new gem added to the Gemfile which is web-console. Web console gem will provide an interactive Ruby Console directly in your browser accessible in your development mode. If you’ve already used the better_errors gem you can get some idea about what this means. This gem will provide you with a console in the default Rails error pages. We can execute Ruby code and inspect the defects right from that page. We can also embed the console anywhere in a view to launch an interactive console session and execute code in it. You can also access the web console by navigating to http://localhost:3000/console! In Windows though, Rails 4.2 with the web console gem gives you the error message – ‘Not able to load such file –pty’. But the Rails committers are working on this and have said this will be fixed in the next release.
Foreign key support:
The add_foreign_key and remove_foreign_key methods in your migrations will allow you to easily create foreign keys in Rails 4.2. At this time, only the mysql, mysql2 and postgresql adapters support this feature.
# add a foreign key to `articles.author_id` referencing `authors.id`
add_foreign_key :articles, :authors
# remove the foreign key on `accounts.branch_id`
remove_foreign_key :accounts, :branches

ActiveJob, ActionMailer #deliver_later:
ActiveJob and its integration with Rails is one of the most significant features in Rails 4.2. From now Rails will have its own queuing interface and you can swap whatever queuing library you are using (resque, delayed job, sidekiq, etc) without changing your application code. Thanks to ActiveJob, ActionMailer now natively supports sending email jobs to a queue using the deliver_later method. But if we want to for some reason deliver email immediately then we need to use the deliver_now method and the old deliver method is now deprecated. If we don’t have any queuing system configured then everything will be sent immediately (no async functionality).
Creating a Job:
ActiveJob provides a Rails generator to create jobs. The following will create a job.
$ rails generate job send_invitation
Create a job that will run on a specific queue:
$ rails generate job send_invitation --queue welcome_email
Here’s what a job looks like:
[source]
class SendInvitationJob < ActiveJob::Base queue_as :default def perform(*args) # Do something later end end [/source] Enqueue the Job:
[source]MyJob.perform_later record #Enqueue a job to be performed as soon the queuing system is free.
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record) #Enqueue a job to be performed tomorrow at noon.
MyJob.set(wait: 1.week).perform_later(record) #Enqueue a job to be performed 1 week from now.
[/source]
Callbacks:
Callbacks allow you to trigger some logic during the life cycle of a job. The various available callbacks are before_enqueue, around_enqueue, after_enqueue, before_perform, around_perform and after_perform
GlobalID:
ActiveJob supports GlobalID as parameters. This makes it possible to pass live ActiveRecord objects to your job instead of class/id pairs, which you then have to manually deserialize. Let’s take a look at how the objects are serialized in the table when, say, DelayedJob is used without GlobalIDs:
--- !ruby/object:Delayed::PerformableMailer object: !ruby/class 'Subscribe' method_name: :apply_yourself args:- 82
Now with GlobalIDs we will have:
--- !ruby/object:Delayed::PerformableMethodobject: !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper {} method_name: :perform args: - !ruby/class 'NameCapitalizerJob' - adcb78c1-f707-481a-856c-66026e525976 - gid://activejob-example/Friend/1
The format of the GlobalID string is gid://AppName/ModelClassName/record_id.
These are some of the significant new features introduced in Rails 4.2. I personally like the new ActiveJob feature which integrates queuing into Rails and makes it much like how ActiveRecord abstracts us from the details of the database being used. Web Console is also a cool feature but since I do most of my development work on Windows I’m waiting for the guys to fix the issue with it so I can enjoy using it.
Thanks!

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *