Digging into New Features in Ruby on Rails 4.2

The first beta of Ruby on Rails 4.2 was announced in the mid of August and it looks amazing. Find out the changes in the latest release and help yourself to decide on upgrade plan quickly.

Ruby on Rails 4.2 seems to be obvious choice for the application as it is the most recent version as it has good application’s performance across different Rails versions.

Following are some of the major features introduced in Ruby on Rails 4.2 –

  1. Active Job, Action Mailer #deliver_later
  2. Adequate Record
  3. Web Console
  4. Foreign key support
  5. Easily load config files
  6. Bootstrapping apps
  7. Transform hash values

Active Job, Action Mailer #deliver_later

Active Job is an adapter layer on top of queuing systems like Resque, Delayed Job, Sidekiq, and more. You can write your jobs to Active Job, and they’ll run on all these queues with no changes.

Action Mailer now comes with a #deliver_later method, which adds your email to be sent as a job to a queue.

‘#deliver_later’ method enqueues the email to be delivered through Active Job. When the job runs it will send the email using deliver_now.

Notifier.welcome(User.first).deliver_later
Notifier.welcome(User.first).deliver_later(wait: 1.hour)
Notifier.welcome(User.first).deliver_later(wait_until: 10.hours.from_now)

GlobalID library makes it easy to pass Active Record objects to jobs by serializing them in a generic form. This means you no longer have to manually pack and unpack your Active Records by passing ids. Just give the job the straight AR object, and it’ll serialize it using GlobalID, and deserialize it at run time.

>> person_gid = Person.find(1).global_id
=> #<GlobalID ...

>> person_gid.uri
=> #<URI ...

>> person_gid.to_s
=> "gid://app/Person/1"

>> GlobalID::Locator.locate person_gid
=> #<Person:0x007fae94bf6298 @id="1">

Adequate Record

Rails 4.2 comes with a performance improvement feature called Adequate Record for Active Record.
AdequateRecord is a set of patches which makes ActiveRecord 2x faster.

How does Active Record work?

ActiveRecord constructs SQL string after doing so many transformations.
The transformations is as mentioned below –

ActiveRecord -> Arel -> SQL string

If query is written in following manner –

Post.where(...).where(...).order(..)

Here Active Record creates an instance of an ActiveRecord::Relation that contains the information passed to ‘where’ or ‘order’.

As soon as this method is called it turns Relation instance into an array.

Post.find(id), or Post.find_by_name(name).

Active Record does a transformation on the relation objects it turns the relation objects in to ARel objects which represent the SQL query AST. Finally, it converts the AST to an actually SQL string and passes that string to the database.

In order to gain speed the AdequateRecord allows static data separated from the dynamic data to cache the static data computations.

Web Console

Web Console is a set of debugging tools for your Rails application.

An interactive console is launched automatically in the default Rails error page. It makes it easy to inspect the stack trace and execute Ruby code in the stack trace’s bindings.

Running rails console is quite handy. Sometimes, though, you can’t easily access it, or maybe you want to easily share your session with a friend without configuring a remote desktop server.

Web Console can help you by running rails console (or any other commandline app), in full featured terminal right in the browser.

Foreign key support

Migration now supports adding foreign key and removing foreign key.

# add a foreign key to `articles.author_id` referencing `authors.id`
add_foreign_key :articles, :authors

# add a foreign key to `articles.author_id` referencing `users.lng_id`
add_foreign_key :articles, :users, column: :author_id, primary_key: "lng_id"

# remove the foreign key on `accounts.branch_id`
remove_foreign_key :accounts, :branches

# remove the foreign key on `accounts.owner_id`
remove_foreign_key :accounts, column: :owner_id

Foreign keys are dumped after the indexes to schema.rb.

add_foreign_key is reversible, remove_foreign_key is not.

There is a connection#supports_foreign_keys? method to determine wether the adapter does support the API.

Easily Load Config Files

In Rails 4.2, now it is very easy to load config files

e.g
config/redis.yml

development:
  host: localhost
  port: 6379
test:
  host: localhost
  port: 6379
production:
  host: redis-production
  port: 6379

And yaml variables can be called as follows –

irb(main):001:0> Rails.application.config_for(:redis)
=> {"host"=>"localhost", "port"=>6379}

If you call config_for(:redis), it’ll look for config/redis.yml in your Rails app, parse it, and returns the right configuration for your RAILS_ENV.

We can also put erb in yaml file –

development:
  host: localhost
  port: <%= ENV['REDIS_PORT'] %>
test:
  host: localhost
  port: <%= ENV['REDIS_PORT'] %>
production:
  host: redis-production
  port: <%= ENV['REDIS_PORT'] %>

Bootstrapping Apps

In order to enable automated setup code when bootstrapping an application Rails 4.2 has introduced bin/setup.

If bootsrap script is already written then it should be renamed to bin/setup

Transform Hash Values

‘transform_values’ accepts a block and apply the block operation to each value of hash

a = {a: 1, b: 2, c: 3}
a.transform_values{ |a|a*2 } 
#=> {:a=>2, :b=>4, :c=>6}

There is also other version transform_values! which change original hash

a = {a: 1, b: 2, c: 3}
a.transform_values!{ |a|a*2 } 
#=> {:a=>2, :b=>4, :c=>6}
a #=> {:a=>2, :b=>4, :c=>6}

Reference Links

Share the Love

Published in ruby-on-rails | Tagged with web-development, ruby, ruby-on-rails







Source link

مدونة تقنية تركز على نصائح التدوين ، وتحسين محركات البحث ، ووسائل التواصل الاجتماعي ، وأدوات الهاتف المحمول ، ونصائح الكمبيوتر ، وأدلة إرشادية ونصائح عامة ونصائح