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

What's coming in Rails 5.2, and sneak peek into Rails 6

What's coming in Rails 5.2, and sneak peek into Rails 6

The next minor version of Ruby on Rails is right around the corner! Let's look at those new features that are coming in the next few weeks to Rails 5.2, and sneak peek into those new features that will come in Rails 6.0, the next major release of Rails.

Presented at Great Western Rails #3 in Bristol, UK on March 21, 2018 and Tokyo Rubyist Meetup on Apr 4, 2018.

Prem Sichanugrist

March 20, 2018
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

  1. What's coming in
    Rails 5.2
    and sneak peek into Rails 6
    #rails52

    View Slide

  2. Prem Sichanugrist
    @sikachu

    /sikachu
    #rails52

    View Slide

  3. View Slide

  4. Pull Request Link
    https://github.com/rails/rails/pull/25992
    #rails52

    View Slide

  5. #tag
    #rails52
    https://github.com/rails/rails/pull/25992

    View Slide

  6. 5.2
    #rails52

    View Slide

  7. New Features
    Custom Mailer Delivery Job
    H2 Early Hints
    Headless Chrome and Headless Firefox Support
    DSL for Configuring CSP Header
    Verbose Query Log
    One-Way Migration in change block
    Active Storage
    Built-in Redis Cache Store
    ActiveSupport::CurrentAttributes
    #rails52

    View Slide

  8. Improvements
    Recycle Cache Keys
    AEAD Encrypted Cookies
    Support pg 1.0
    Safer Raw SQL handling
    New Threshold for Cache Compression
    freeze_time
    Bootsnap in Default Gemfile
    #rails52

    View Slide

  9. Active Storage
    #rails52

    View Slide

  10. NEW
    Active Storage
    #rails52

    View Slide

  11. NEW
    Active Storage
    #rails52
    class User < ApplicationRecord
    # Associates an attachment and a blob. When the user
    # is destroyed they are purged by default (models
    # destroyed, and resource files deleted).
    has_one_attached :avatar
    end
    class AvatarsController < ApplicationController
    def update
    # params[:avatar] contains a
    # ActionDispatch::Http::UploadedFile object
    Current.user.avatar.attach(params.require(:avatar))
    redirect_to Current.user
    end
    end

    View Slide

  12. NEW
    Active Storage
    #rails52
    Direct Uploads

    View Slide

  13. NEW
    Active Storage
    #rails52
    # Sprockets (Asset Pipeline)
    //= require activestorage
    # Yarn/Webpacker
    import * as ActiveStorage from "activestorage"
    ActiveStorage.start()
    # View file
    <%= form.file_field :attachments, multiple: true,
    direct_upload: true %>

    View Slide

  14. NEW
    Active Storage
    Q: Should I switch to Active Storage from [___________]?
    #rails52

    View Slide

  15. NEW
    Active Storage
    Q: Should I switch to Active Storage from [___________]?
    A: Sure!*
    #rails52

    View Slide

  16. NEW
    Active Storage
    Q: Should I use Active Storage for my new project?
    #rails52

    View Slide

  17. NEW
    Active Storage
    Q: Should I use Active Storage for my new project?
    A: Yes.
    #rails52

    View Slide

  18. Recycle Cache Keys
    #rails52
    https://github.com/rails/rails/pull/29092

    View Slide

  19. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1-20180320190000"

    View Slide

  20. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1-20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190013"

    View Slide

  21. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1-20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190013"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190025"

    View Slide

  22. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1-20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190013"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190025"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190102"

    View Slide

  23. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1-20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190013"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190025"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190102"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190130"

    View Slide

  24. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1-20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190013"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190025"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190102"
    post.update!({ ... })
    post.cache_key #=> "post/1-20180320190130"

    View Slide

  25. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    "post/3-20180319190000"
    "post/5-20180317014200"
    Redis

    View Slide

  26. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    "post/1-20180320190013"
    "post/3-20180319190000"
    "post/5-20180317014200"
    Redis

    View Slide

  27. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    "post/1-20180320190025"
    "post/1-20180320190013"
    "post/3-20180319190000"
    "post/5-20180317014200"
    Redis

    View Slide

  28. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    "post/1-20180320190102"
    "post/1-20180320190025"
    "post/1-20180320190013"
    "post/3-20180319190000"
    "post/5-20180317014200"
    Redis

    View Slide

  29. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    "post/1-20180320190130"
    "post/1-20180320190102"
    "post/1-20180320190025"
    "post/1-20180320190013"
    "post/3-20180319190000"
    "post/5-20180317014200"
    Redis

    View Slide


  30. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys

    View Slide

  31. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1"
    post.cache_version #=> "20180320190000"

    View Slide

  32. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1"
    post.cache_version #=> "20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1"
    post.cache_version #=> "20180320190013"

    View Slide

  33. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    post.cache_key #=> "post/1"
    post.cache_version #=> "20180320190000"
    post.update!({ ... })
    post.cache_key #=> "post/1"
    post.cache_version #=> "20180320190013"
    post.update!({ ... })
    post.cache_key #=> "post/1"
    post.cache_version #=> "20180320190025"

    View Slide

  34. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    "post/1"
    "post/3"
    "post/5"
    Redis

    View Slide

  35. #rails52
    https://github.com/rails/rails/pull/29092
    Recycle Cache Keys
    config.active_record.cache_versioning = true

    View Slide

  36. AEAD Encrypted Cookies
    #rails52
    https://github.com/rails/rails/pull/28132

    View Slide

  37. AEAD
    Authenticated Encryption with Associated Data
    #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies

    View Slide

  38. Rails 4
    AES in CBC mode with HMAC using SHA1

    #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies

    View Slide

  39. Rails 4
    AES in CBC mode with HMAC using SHA1

    Rails 5.2
    AES-GCM
    #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies

    View Slide

  40. Benefits of AES-GCM
    #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies

    View Slide

  41. Benefits of AES-GCM
    Faster encryption/decryption

    #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies

    View Slide

  42. Benefits of AES-GCM
    Faster encryption/decryption

    Shorter ciphertext
    #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies

    View Slide

  43. #rails52
    https://github.com/rails/rails/pull/28132
    AEAD Encrypted Cookies
    config.action_dispatch.
    use_authenticated_cookie_encryption = true

    View Slide

  44. H2 Early Hints Support
    #rails52
    https://github.com/rails/rails/pull/30744

    View Slide

  45. Rails 5.2 + Puma + h2o
    #rails52
    https://github.com/rails/rails/pull/30744
    H2 Early Hints Support

    View Slide

  46. #rails52
    https://github.com/rails/rails/pull/30744
    H2 Early Hints Support
    HTTP/1.1 103 Early Hints
    Link: ; rel=preload; as=style
    Link: ; rel=preload; as=script
    HTTP/1.1 200 OK
    Date: Tue, 20 Mar 2018 15:12:00 GMT
    Content-Length: 4200
    Content-Type: text/html; charset=utf-8
    Link: ; rel=preload; as=style
    Link: ; rel=preload; as=script

    View Slide

  47. #rails52
    https://github.com/rails/rails/pull/30744
    H2 Early Hints Support
    <%= preload_link_tag "application.css" %>
    type="text/css" />

    View Slide

  48. #rails52
    https://github.com/rails/rails/pull/30744
    H2 Early Hints Support
    http://eileencodes.com/posts/http2-early-hints/

    View Slide

  49. DSL for Configuring CSP Header
    #rails52
    https://github.com/rails/rails/pull/31162

    View Slide

  50. Content-Security-Policy
    Control access to content from a user-agent
    #rails52
    https://github.com/rails/rails/pull/31162
    DSL for Configuring CSP Header

    View Slide

  51. #rails52
    https://github.com/rails/rails/pull/31162
    DSL for Configuring CSP Header
    #config/initializers/content_security_policy.rb
    Rails.application.config.content_security_policy do |p|
    p.default_src :self, :https
    p.font_src :self, :https, :data
    p.img_src :self, :https, :data
    p.object_src :none
    p.script_src :self, :https
    p.style_src :self, :https, :unsafe_inline
    # Specify URI for violation reports
    # p.report_uri "/csp-violation-report-endpoint"
    end

    View Slide

  52. #rails52
    https://github.com/rails/rails/pull/31162
    DSL for Configuring CSP Header
    # Override policy inline
    class PostsController < ApplicationController
    content_security_policy do |p|
    p.upgrade_insecure_requests true
    end
    end

    View Slide

  53. MDN web docs
    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
    #rails52
    https://github.com/rails/rails/pull/31162
    DSL for Configuring CSP Header

    View Slide

  54. Verbose Query Log
    #rails52
    https://github.com/rails/rails/pull/26815

    View Slide

  55. #rails52
    https://github.com/rails/rails/pull/26815
    Started GET "/news/popular" for ::1 at 2016-10-19 00:5
    Processing by NewsController#popular as HTML
    Version Load (57.3ms) SELECT "versions".* FROM "ve
    Verbose Query Log

    View Slide

  56. #rails52
    https://github.com/rails/rails/pull/26815
    Started GET "/news/popular" for ::1 at 2016-10-19 00:5
    Processing by NewsController#popular as HTML
    Version Load (57.3ms) SELECT "versions".* FROM "ve
    ↳ app/views/news/show.html.erb:9:in `_app_views_news
    Verbose Query Log

    View Slide

  57. #rails52
    https://github.com/rails/rails/pull/26815
    Started GET "/news/popular" for ::1 at 2016-10-19 00:5
    Processing by NewsController#popular as HTML
    Version Load (57.3ms) SELECT "versions".* FROM "ve
    ↳ app/views/news/show.html.erb:9:in `_app_views_news
    Verbose Query Log

    View Slide

  58. #rails52
    https://github.com/rails/rails/pull/26815
    config.active_record.verbose_query_logs = true
    Verbose Query Log

    View Slide

  59. ActiveSupport::CurrentAttributes
    #rails52
    https://github.com/rails/rails/pull/29180

    View Slide

  60. Subdomain.current
    Account.current
    #rails52
    https://github.com/rails/rails/pull/29180
    ActiveSupport::CurrentAttributes

    View Slide

  61. #rails52
    https://github.com/rails/rails/pull/29180
    ActiveSupport::CurrentAttributes
    class Subdomain < ApplicationRecord
    cattr_accessor :current #
    end
    class Account < ApplicationRecord
    def self.current
    $current_account #
    end
    end

    View Slide

  62. #rails52
    https://github.com/rails/rails/pull/29180
    ActiveSupport::CurrentAttributes
    # app/models/current.rb
    class Current < ActiveSupport::CurrentAttributes
    attribute :subdomain
    attribute :account
    end

    View Slide

  63. Current.subdomain
    Current.account
    #rails52
    https://github.com/rails/rails/pull/29180
    ActiveSupport::CurrentAttributes

    View Slide

  64. Thread-Isolated Storage
    Thread.current[_____]
    #rails52
    https://github.com/rails/rails/pull/29180
    ActiveSupport::CurrentAttributes

    View Slide

  65. #rails52

    https://github.com/rails/rails/pull/29180
    ActiveSupport::CurrentAttributes

    View Slide

  66. #rails52

    View Slide

  67. Built-in Redis Cache Store
    config.cache_store = :redis_cache_store
    #rails52
    https://github.com/rails/rails/pull/31134

    View Slide

  68. New Threshold for Cache Compression
    Automatically compress value > 1kB
    #rails52
    https://github.com/rails/rails/pull/31147

    View Slide

  69. freeze_time
    Shorthand for travel_to Time.current
    #rails52
    https://github.com/rails/rails/pull/29681

    View Slide

  70. Bootsnap in Default Gemfile
    Add gem "bootsnap" for current projects
    #rails52
    https://github.com/rails/rails/pull/29313

    View Slide

  71. Headless Chrome
    Headless Firefox
    #rails52
    https://github.com/rails/rails/pull/30876

    https://github.com/rails/rails/pull/31365

    View Slide

  72. #rails52
    https://github.com/rails/rails/pull/30876

    https://github.com/rails/rails/pull/31365
    driven_by :selenium, using: :headless_chrome
    driven_by :selenium, using: :headless_firefox
    Headless Chrome & Firefox

    View Slide

  73. Custom Mailer Delivery Job
    #rails52
    https://github.com/rails/rails/pull/29457

    View Slide

  74. #rails52
    https://github.com/rails/rails/pull/29457
    class MyMailer < ApplicationMailer
    self.delivery_job = MyCustomDeliveryJob
    end
    Custom Mailer Delivery Job

    View Slide

  75. 5.2
    #rails52

    View Slide

  76. 6.0
    #rails6

    View Slide

  77. #rails6
    >= 2.4.1

    View Slide

  78. .create_or_find_by
    Use Unique index to mitigate possible

    race condition in find_or_create_by
    https://github.com/rails/rails/pull/31989
    #rails6

    View Slide

  79. Person.pick(:name)
    Same as Person.pluck(:name).first
    https://github.com/rails/rails/pull/31941
    #rails6

    View Slide

  80. Goodbye ‚
    utf8 attribute no longer generated in form_tag by default
    https://github.com/rails/rails/pull/32125
    #rails6

    View Slide

  81. Goodbye–✓
    utf8 attribute no longer generated in form_tag by default
    https://github.com/rails/rails/pull/32125
    #rails6

    View Slide

  82. update_attributes → update
    update_attributes! → update!
    https://github.com/rails/rails/pull/31998
    #rails6

    View Slide

  83. Parallel Testing
    http://edgeguides.rubyonrails.org/testing.html#parallel-testing

    https://github.com/rails/rails/pull/31900
    #rails6

    View Slide

  84. Expanded rails routes
    https://github.com/rails/rails/pull/32130
    #rails6

    View Slide

  85. https://github.com/rails/rails/pull/32130
    #rails6
    Expanded rails routes
    $ rails routes --expanded
    --[ Route 1 ]------------------------------------------------------------
    Prefix | high_scores
    Verb | GET
    URI | /high_scores(.:format)
    Controller#Action | high_scores#index
    --[ Route 2 ]------------------------------------------------------------
    Prefix | new_high_score
    Verb | GET
    URI | /high_scores/new(.:format)
    Controller#Action | high_scores#new
    --[ Route 3 ]------------------------------------------------------------
    Prefix | blog
    Verb |
    URI | /blog
    Controller#Action | Blog::Engine
    [ Routes for Blog::Engine ]
    --[ Route 1 ]------------------------------------------------------------
    Prefix | cart
    Verb | GET
    URI | /cart(.:format)
    Controller#Action | cart#show

    View Slide

  86. 5.2
    6.0

    View Slide