Friday, January 03, 2014

New features Rails 4

New features Rails 4

  • Deprecated dynamic finders: Rails 4 deprecates all dynamic finder methods (with the exception of find_by and find_by_…). Instead, you’ll use where:

find_all_by_... => where(...)
scoped_by_... => where(...)
find_last_by_... => where(...).last
find_or_initialize_by... => where(...).first_or_initialize
find_or_create_by... =>  can be rewritten using find_or_create_by(...) or where(...).first_or_create
find_or_create_by_...! => can be rewritten using find_or_create_by!(...) or where(...).first_or_create!

  • Renamed Callbacks: Action callbacks in controllers are now renamed from *_filter to *_action

before_filter >> before_action
after_filter  >> after_action

  • Routing Concerns: In Rails 4, routing concerns has been added to the router. The basic idea is to define common sub-resources (like comments) as concerns and include them in other resources/routes.

Rails3 code
               resources :posts do
                               resources :comments
               resources :articles do
                               resources :comments
                               resources :remarks
Rails4 code
               concern :commentable do
                               resources :comments

               concern :remarkable do
                               resources :remarks

               resources :posts, :concerns => :commentable
               resources :articles, :concerns => [:commentable, :remarkable]

  •  Datetypes:  Here are all the Rails 4 (ActiveRecord migration) datatypes:

If you use PostgreSQL, you can also take advantage of these:
They are stored as strings if you run your app with a not-PostgreSQL database.

  •  Queuing system: Rails4 added the support for a queue to run background jobs. The queuing API is very simple. The ActiveSupport::Queue class comes with a push method that accepts any object, as long as that object defines a run method.


class TestJob
def run
                puts "I am running!"

You can queue a job to print “I am running!” in the background by pushing an instance of that class to Rails.queue:
=> "I am running!"

  •  Strong Parameters: In Rails 4, a new pattern has been introduced to secure your models from mass assignment. You can filter the parameters passed to your model in the controller instead of ‘whitelisting’ the attributes in your model using “attr_accessible”.

class PostController < ApplicationController
def create
@post = Post.create(params[:user])

You can protect against unexpected input with declarations in the model using “attr_accessible”.

attr_accessible :title, :description

In Rails 4, you don’t need to worry about unexpected input attributes in your model anymore.
Strong Parameters gem moves user input into the controller.

class PostController < ApplicationController

def create
@post = Post.create(post_params)

def post_params
params.require(:post).permit(:title, :description)

The “require” method ensures that the specified key is available in the “params” hash, and raises an ActionController::ParameterMissing exception if the key doesn’t exist.
The “permit” method protects you from unexpected mass assignment.

  • Custom Flash Types: You can register your own flash types to use in redirect_to calls and in templates. For example:

      # app/controllers/application_controller.rb
      class ApplicationController
            add_flash_types :error, :catastrophe

      # app/controllers/things_controller.rb
      class ThingsController < ApplicationController
            def create
                  # ... create a thing
            rescue Error => e
                  redirect_to some_path, :error => e.message
            rescue Catastrophe => e
                  redirect_to another_path, :catastrophe => e.message

      # app/views/layouts/application.html.erb
      <div class="error"><%= error %></div>
      <div class="catastrophe"><%= catastrophe %></div>

No comments:

Post a Comment