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

Belighted WTF : What's new in Rails 4 ?

Belighted WTF : What's new in Rails 4 ?

Overview of changes, new features, deprecations in the upcoming new version of Ruby on Rails.

Avatar for Kevin Vanzandberghe

Kevin Vanzandberghe

October 03, 2012
Tweet

More Decks by Kevin Vanzandberghe

Other Decks in Programming

Transcript

  1. Table of Contents 1. Turbolinks 2. Queue API 3. Strong

    parameters 4. Cache digests 5. Routing concerns 6. ActiveRecord::Relation 7. Deprecations 8. Further reading
  2. Turbolinks Intended as a replacement for PJAX, focused on simplicity.

    • Uses pushState to reload the whole body. • Like PJAX, also replaces the <title> tag. • Default in Rails 4, enabled for every link. • Already available in 3.2.8 : https://github.com/rails/turbolinks
  3. Turbolinks : benchmark Steve Klabnik wrote a sample app to

    demonstrate speed gains with heavy assets : https://github.com/steveklabnik/turbolinks_test This uses both the JS/CSS files from Basecamp Next.
  4. Turbolinks : gotchas As it is enabled by default, you

    can opt-out on a per-link basis : It does not trigger the document.ready event in JS, so we'd have to also bind relevant code to page.change, like so : $(function() { initPage(); }); $(window).bind('page.change', function() { initPage(); }); link_to "Awesome home page", root_path, data: { no-turbolinks: true }
  5. Intends to unify all background job providers (delayed_job, resque, ...)

    under a common, easily-accessible API. Queue API
  6. Queue API : code example It just requires an object

    that responds to #run. Class UserMailJob < Struct.new(:user_id) def run user = User.find(user_id) UserMailer.welcome(user).deliver end end # In users_controller.rb Rails.queue.push UserMailJob.new(@user.id)
  7. Strong parameters Moves the concern of attribute access entirely to

    the controller. Also works for nested resources. class UsersController def create User.create(user_params) end private def user_params params.require(:user). permit(:email, :name) end end
  8. Cache digests In this situation, you'd normally have to manually

    expire all dependencies if you changed anything in _comment.html.erb Well, no more !
  9. Cache digests • Appends a MD5 digest to a cached

    template and all its dependencies. • Works implicitly for most rendering cases. • Can also be called explicitly with a comment. <%# Template Dependency: users/comment %> • Available already in 3.2.8 : https://github.com/rails/cache_digests
  10. ActiveRecord::Relation • Relation.first / Relation.last Always ordered by id. •

    Relation.all Always returns a ActiveRecord::Relation object. • Relation.none Returns a ActiveRecord::NullRelation, for those cases you would return an empty array.
  11. Deprecations • ActiveRecord::Base.scoped • Dynamic finder methods (find_all_by_...) • Hash-based

    finders (User.find(:all)) • Scopes are no longer eager-evaluated, need a lambda. • AR::SessionStore, ActiveRessource extracted as gems. • Rails 2-style plugins are completely removed (finally!)
  12. Rails Edge guides (generated from master) http://edgeguides.rubyonrails.org/4_0_release_notes.html BostonRB slides on

    Rails 4. https://speakerdeck.com/u/bostonrb/p/what-to-expect-in-rails-4-dot-0 Video on Rails Queues by Caike Souza http://www.codeschool.com/code_tv/rails-4-queues A Fresh cup : weekly follow-up of the changes in Rails master. http://afreshcup.com/ Further reading