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

What's New in Rails 4.2

What's New in Rails 4.2

A brief overview of new features in Ruby on Rails 4.2

Presented at Austin on Rails on October 28, 2014

Anthony Lewis

October 28, 2014
Tweet

More Decks by Anthony Lewis

Other Decks in Programming

Transcript

  1. What's New In
    Rails 4.2
    Anthony Lewis

    View Slide

  2. @AnthonyLewis
    Best known for my hit single “Candy Rain”

    View Slide

  3. @AnthonyLewis
    Also, wrote a book…

    View Slide

  4. Rails 4.2
    • Active Job
    • Action Mailer
    • Global ID
    • Adequate Record
    • Web Console
    • Other Changes
    • Installation
    • Release Notes

    View Slide

  5. Active Job
    • Framework for background processing using
    Resque, Delayed Job, Sidekiq, etc.
    • Switch between background runners without
    having to rewrite jobs
    • If no adapter is specified, jobs run immediately

    View Slide

  6. Configuration
    • Add your preferred background processing library
    to your application’s Gemfile:
    • Update Rails configuration:
    gem 'sidekiq'
    config.active_job.queue_adapter = :sidekiq

    View Slide

  7. Creating Jobs
    • Use the Rails generator to create a new job:
    $ bin/rail g job process_video
    create app/jobs/process_video_job.rb

    View Slide

  8. Job Source
    • The newly created job:
    class ProcessVideoJob < ActiveJob::Base
    queue_as :default
    def perform(*args)
    # Do something later
    end
    end

    View Slide

  9. Enqueue Job
    • Enqueue with or without a time:
    # Run next
    ProcessVideo.perform_later(@video)
    # Run tomorrow
    ProcessVideo.set(wait: 1.day)
    .perform_later(@video)

    View Slide

  10. Error Handling
    • Catch exceptions with a block:
    class ProcessVideoJob < ActiveJob::Base
    ...
    rescue_from(Exception) do |e|
    # do something with exception
    end
    end

    View Slide

  11. Action Mailer
    • ActionMailer now uses ActiveJob to send email
    • Call deliver_now to send a message inline
    # Send inline
    UserMailer.success_email(@user)
    .deliver_now

    View Slide

  12. Action Mailer
    • Call deliver_later to enqueue delivery
    • Accepts :wait, :wait_until, and :queue options
    # Enqueue and send later
    UserMailer.success_email(@user)
    .deliver_later

    View Slide

  13. Global ID
    • A unique identifier for a model instance
    • ActiveJob uses Global ID to automatically serialize
    and deserialize objects
    irb> video.to_global_id.to_s
    => "gid://appname/Video/1"

    View Slide

  14. Adequate Record
    • Optimizations to Active Record that make the find,
    find_by_*, and find_by methods up to 2x faster
    • Caches the static parts of the SQL query
    User.find(1)
    # Caches the static part:
    # SELECT * FROM users WHERE id = ?
    User.find(2)
    # Uses the cache, executes faster

    View Slide

  15. Web Console
    • Debugging tool for your Rails app
    • Automatically opens a Rails console on exceptions
    • Can also be opened with the console view helper
    <%= console %>

    View Slide

  16. View Slide

  17. Other Changes
    • Previously deprecated functionality has been
    removed
    • Check for deprecation warnings before updating
    • The respond_with / class-level respond_to methods
    have been moved to the responders gem
    • The default host for rails server is now localhost
    instead of 0.0.0.0
    • If you dev in a virtual machine this will bite you

    View Slide

  18. Other Changes
    • The default log level for production is now :debug,
    just like development and test
    • The HTML sanitizer has been replaced with a new
    version.
    • Some sanitized output might change
    • Foreign key support (finally)
    • And more…

    View Slide

  19. Release Notes
    • The work-in-progress release notes are online:
    http://edgeguides.rubyonrails.org/4_2_release_notes.html

    View Slide

  20. Installation
    # In your terminal:
    $ gem install rails --pre
    # In your Gemfile:
    gem 'rails', '~> 4.2.0.beta2'

    View Slide

  21. Questions?

    View Slide

  22. Thank You!

    View Slide