Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails4.pdf

mnasr
December 12, 2012

 Rails4.pdf

mnasr

December 12, 2012
Tweet

Other Decks in Programming

Transcript

  1. Outline • Upgrade to Rails 4.0 • Rails Queue •

    Few Features in Rails 4.0 • Simpler Methods • Things to keep in mind
  2. Upgrade to Rails 4.0 • Rails 4.0 requires Ruby 1.9.3

    or higher. • Update your Gemfile to depend on : rails = 4.0.0 sass-rails ~> 3.2.3 coffee-rails ~> 3.2.1 uglifier >= 1.0.3
  3. Rails.queue The queuing API is very simple. You push an

    object onto the queue and that object is expected to respond to a run method. Let's take a look: class TestJob Rails.queue.push(TestJob.new ) def run puts "I am running!“ => "I am running!" end end
  4. Rails.queue Short response time is critical for every web application.

    Time consuming operations and long-running tasks which require intensive computation often can not be processed immediately during the normal HTTP request/response cycle. Otherwise the application gets If you want to keep your app fast and unresponsive soon. The solution responsive,then it is recommendable to is background job processing. move those long-running tasks into background processes. After the job is placed in a background queue, the application can return a response immediately
  5. Strong parameters class User <ActiveRecord::Base attr_accessible :name end def create

    @user = User.new(params[:user]) @user.admin = params[:user][:admin] end class UsersController < ActionController::Base def create @user = User.new(user_params) end private def user_params params.require(:user).permit(:name) end end Strong parameters is the latest tool in the fight against mass assignment vulnerabilities.
  6. Turbolinks • It is a brand new piece of JavaScript

    that is going to be integrated as part of Rails 4. • When enabled on a site, it will fetch a part of a page and replace the contents of the current page with a new page. • It works by fetching the clicked link asynchronously and then replacing the entire contents of the document body with the new page. • The benefit of this is that you save client side time by not having to re- check for existing css, javascript, and possibly even images.
  7. PATCH verb A new http verb will be introduced. The

    verb PATCH will be used instead of PUT on update form by default. You will still be <%= form_for @user do |f| %> <%= form_for @user, method: :put do |f| %> <%= ... %> <%= ... %> <% end %> <% end %>
  8. Simpler methods Scopes scope :recent, -> { where(created_at: 1.week.ago) }

    Join table create_join_table :products, :categories Store uses special code flag store :settings, accessors: [ :color, :homepage ], coder: JSON Pluck multiple columns Client.pluck(:id, :name) # SELECT clients.id, clients.name FROM clients # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']] Index in migration create_table :messages do |t| t.references :person, :index => true end
  9. Things to keep in mind Rails 4 provides : •

    Simpler Methods. • Faster Queuing. • Stronger Parameters. • Saving Client's Time.