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

What's new in Rails 4 ?

What's new in Rails 4 ?

This presentation present Rails 4.0.1, outdated now.

Sylvain Peigney

December 03, 2013
Tweet

More Decks by Sylvain Peigney

Other Decks in Programming

Transcript

  1. Ruby Ruby 1.9.3 (minimum) Ruby 2.0.0 (prefered) – backward compatible

    1.9 and faster ! Rails 4.0.1 Bug fixes + Active Record method order restored Let's go ! BEFORE WE START $ gem install rails Successfully installed rails-4.0.1 1 gem installed
  2. MATCH ROUTE # in rails 4, you'll get an error

    match '/mosquitos/:id/kill', to: 'mosquitos#kill' post '/mosquitos/:id/kill', to: 'mosquitos#kill ' match '/mosquitos/:id/kill', to: 'mosquitos#kill', via: :post match '/mosquitos/:id/kill', to: 'mosquitos#kill', via: :all Rails 3 Rails 4
  3. PATCH VERB $ rake routes mosquitos GET /mosquitos(.:format) mosquitos#index POST

    /mosquitos(.:format) mosquitos#create new_mosquito GET /mosquitos/new(.:format) mosquitos#new edit_mosquito GET /mosquitos/:id/edit(.:format) mosquitos#edit mosquito GET /mosquitos/:id(.:format) mosquitos#show PUT /mosquitos/:id(.:format) mosquitos#update PATCH /mosquitos/:id(.:format) mosquitos#update DELETE /mosquitos/:id(.:format) mosquitos#destroy PATCH, http 1.1(modify an existing HTTP resource) Rails 4 Added in forms helpers and tests.
  4. ROUTING CONCERNS AnnoyingApp::Application.routes.draw do resources :projects do resources :comments end

    resources :tasks do resources :comments end resources :milestone do resources :comments end end Without concerns... With concerns, much more DRY AnnoyingApp::Application.routes.draw do # options can be added to concerns concern :commentable do resources :comments end resources :projects, concerns: :commentable resources :tasks, concerns: :commentable resources :milestone, concerns: :commentable end
  5. FINDERS # find is deprecated Insect.find(:all, conditions: { name: 'Bee'

    }) # dynamic finders are deprecated Insect.find_all_by_kind('Flying') Insect.find_last_by_sound('bzzzz') # dynamic finders with one arg, not deprecated Insect.find_by_sound('bzzzz') # deprecated: dynamic finder with create or init Insect.find_or_initialize_by_name('Bee') Insect.find_or_create_by_name('Bee') RAILS 3 (gem activerecord-deprecated_finders) Insect.where(name: 'Bee') Insect.where(kind: 'Flying') Insect.where(sound: 'bzzzz').last Insect.find_by_sound('bzzzz') Insect.find_by(sound: 'bzzzz') params = { sound: 'bzzzz', name: 'Bee'} Insect.find_by(params) Insect.find_or_initialize_by(name: 'Bee') Insect.find_or_create_by(name: 'Bee') RAILS 4
  6. UPDATE @insect.update_attributes(post_params) # skip validation @insect.update_attribute(:kind, 'Crawling') @insect.update_column(:kind, 'Crawling') RAILS

    3 (not deprecated in Rails 4) @insect.update(post_params) # skip validation @insect.update_columns(:kind, 'Crawling') RAILS 4 (prefered way)
  7. ACTIVERECORD::RELATION Insect.none # better than [], e.g. if you chain

    a scope #<ActiveRecord::Relation []> Insect.all #<ActiveRecord::Relation [#<Insect id: 1, name : 'Mosquito'...>, ...]> none all Insect.where.not(kind: 'Crawling') # generate better SQL syntax not These methods now return an ActiveRecord::Relation
  8. SCOPES @insect.update_attributes(post_params) # skip validation @insect.update_attribute(:kind, 'Crawling') @insect.update_column(:kind, 'Crawling') @insect.update(post_params)

    # skip validation @insect.update_columns(:kind, 'Crawling') # loaded on class load scope :dead, where(state: 'dead') default_scope where(state: 'alive') RAILS 3 (deprecation warning in Rails 4) # loaded everytime :) scope :dead, ->{ where(state: 'dead') } # give it a proc object or a block default_scope { where(state: 'alive') } default_scope ->{ where(state: 'alive') } RAILS 4 , proc object or block
  9. STRONG PARAMETERS Rails 3 (model was responsible for whitelisting parameters)

    class Insect < ActiveRecord::Base end Rails 4 (controller is now responsible for whitelisting parameters) def create @insect = Insect.new(insect_params) # ... end def update @insect.update(insect_params) # ... end private def insect_params params.require(:insect).permit(:name,:kind,:sound) end class Insect < ActiveRecord::Base attr_accessible :name,:kind,:sound end # You can still use this in Rails 4 with gem 'protected_attributes'
  10. BEFORE ACTION before_filter is now known as before_action Same for

    other filters (after_filter, skip_before_filter...) Filters methods will work with no warnings
  11. SESSIONS Rails 3 cookies were readable easily with Marshal SecretApp::Application.config.secret_key_base

    = '637fe3fc6ee1fc23e...' # I should get that in an environment variable Rails 4 now they're not (in config/initializers/secret_token.rb) cookie = "BAh7CUkiD3Nlc3Npb25faWQGOgZFRkkiJ..." Rack::Session::Cookie::Base64::Marshal.new.decode(cookie) => { "session_id" => "0ba43a95f93e91538...", "user_id" => "42", "_csrf_token" => "xdpFHDeEeuVlRD9mIBW/tFcKI..." }
  12. CUSTOM FLASH TYPES <%= flash[:notice] %> == <%= notice %>

    <%= flash[:alert] %> == <%= alert %> Rails 3 & 4 shorter flash helper class ApplicationController < ActionController::Base add_flash_types :buzz, :crackle flash[:buzz] = 'BzzzZZzzzzZZzzzz' flash[:crackle] = 'Crcrcrccrcrcrcrrcrcrrccr' end <%= buzz %> <%= crackle %> Rails 4 now you can register them
  13. COLLECTIONS FORM HELPER collection_select(object, method, collection, value_method, text_method) Rails 3

    & 4 collection_select helper collection_radio_buttons(object, method, collection, value_method, text_method) collection_check_boxes(object, method, collection, value_method, text_method) Rails 4 collection_radio_buttons & collection_check_boxes
  14. DATE FIELD <%= f.date_select :kill_date %> Rails 3 & 4

    date_select Rails 4 date_field, future proof <%= f.date_field :kill_date %>
  15. JBUILDER TEMPLATE # in app/views/insects/index.json.jbuilder json.array!(@insects) do |insect| json.extract! insect,

    :name, :kind, :sound json.url insect_url(insect, format: :json) end Now you can customize the json response easily # app/views/insects/index.json.ruby insects_hashes = @insects.map do |insect| { name: insect.name, kind: insect.kind, url: insect_url(insect) } end insects_hashes.to_json Or in plain ruby # in app/views/insects/index.json.jbuilder json.array!(@insects) do |insect| json.extract! insect, :name, :kind, :sound json.url insect_url(insect, format: :json) end Now you can customize the json response easily # app/views/insects/index.json.jbuilder json.array!(@insects) do |insect| json.extract! insect, :name, :kind, :sound json.url insect_url(insect, format: :json) end Now you can customize the json response easily
  16. TURBOLINKS Reload just the title and the body of your

    page At each click, the page is entirely repainted With Turbolinks Without Turbolinks
  17. TURBOLINKS # Gemfile gem 'turbolinks' # app/assets/javascript/application.js //= require turbolinks

    Present in Rails 4 $(document).ready(init); $(document).on('page:fetch'); # or better $(document).on('click', '#myElement', myAction); # or even gem 'jquery.turbolinks' //= require jquery.turbolinks Beware of your code, it could be not ran
  18. PUBLIC/INDEX.HTML The default index is no more in public folder

    but dynamically generated, so no need to delete this poor page it anymore.
  19. WHAT'S NEXT ? Thread safety Tests (MiniTest) Cache ActionController::Live Etags

    ActiveSupport::Queue (4.1) async ActionMailer Postgres hstore & array
  20. RESSOURCES As usual, the Rails Guides (documentation updated): http://guides.rubyonrails.org/ http://guides.rubyonrails.org/upgrading_ruby_on_rails.html

    (WIP) Interactive course: http://rails4.codeschool.com/ Screencasts: http://railscasts.com/episodes/400-what-s-new-in-rails-4 http://railscasts.com/episodes/415-upgrading-to-rails-4
  21. THANK YOU Use Rails 4 now ! I'm Sylvain Peigney,

    a ruby freelance developer My next project is @volpeo