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

What's new in Ruby on Rails 5.2

Ryo Hashimoto
November 01, 2017

What's new in Ruby on Rails 5.2

Active Storage / Current attributes / Encrypted configuration

Ryo Hashimoto

November 01, 2017
Tweet

More Decks by Ryo Hashimoto

Other Decks in Programming

Transcript

  1. What’s new in

    Ruby on Rails 5.2
    @ryohashimoto

    View Slide

  2. Rails 5.2ʹೖΓͦ͏ͳ৽ػೳ
    • Current attributes
    • Encrypted configuration
    • Active Storage

    View Slide

  3. Current attributes
    • Current͸ϦΫΤετͷલޙͰϦηοτ͞ΕΔ
    ม਺
    • ֤ϦΫΤετͰڞ௨ͳॲཧͷ݁ՌΛ୅ೖ͢Δͷ
    ʹ࢖༻
    • ྫʣϩάΠϯதͷϢʔβʔ৘ใΛೖΕΔ

    ʢCurrent.user / Current.user_agentʣ

    View Slide

  4. module Authentication
    extend ActiveSupport::Concern
    included do
    before_action :authenticate
    end
    private
    def authenticate
    if authenticated_user = User.find_by(id:
    cookies.encrypted[:user_id])
    Current.user = authenticated_user
    else
    redirect_to new_session_url
    end
    end
    end

    View Slide

  5. module SetCurrentRequestDetails
    extend ActiveSupport::Concern
    included do
    before_action do
    Current.request_id = request.uuid
    Current.user_agent = request.user_agent
    Current.ip_address = request.ip
    end
    end
    end
    class ApplicationController < ActionController::Base
    include Authentication
    include SetCurrentRequestDetails
    end

    View Slide

  6. Encrypted configuration

    (config/credentials.yml.enc)
    • ػີ৘ใΛ1ͭͷϑΝΠϧ಺ʹ҉߸Խͯ֨͠ೲ
    • config/secrets.yml, SECRET_BASE_KEYΛγϯ
    ϓϧʹஔ͖׵͑Δ΋ͷ
    • ؀ڥม਺ RAILS_MASTER_KEY Ͱ෮߸ԽͰ͖Δ
    • rails credentials:edit ͰฤूΛߦ͏

    View Slide

  7. config/credentials.yml.encͷத਎
    # aws:
    # access_key_id: 123
    # secret_access_key: 345
    # Used as the base secret for all MessageVerifiers in
    Rails, including the one protecting cookies.
    secret_key_base:
    b4e08f4b6314eaab0d52c5cafcf51f6c38f0160d82c05bae2007d
    d9fb08f3efec68fca32a0ec473a653a84259b461165443b17c15e
    5666b3924c3afc78730e9c

    View Slide

  8. Active Storage
    • Ϋϥ΢υʹϑΝΠϧΛΞοϓϩʔυ͕Մೳ
    (Amazon S3, Google Cloud, Microsoft Azure)
    • ϞσϧʹΫϥ΢υ্ͷϑΝΠϧΛؔ࿈͚ͮ
    • Railsαʔόʔ͔Β΋JavascriptΫϥΠΞϯτ͔Β
    ΋Ξοϓϩʔυ͕Մೳ
    • ը૾ͷม׵ॲཧ΋ՄೳʢMiniMagickʣ

    View Slide

  9. طଘͷΞοϓϩʔυGemͱͷҧ͍
    • ֤ϞσϧʹΞοϓϩʔυ͢ΔϑΝΠϧͷ৘ใΛ
    ࣋ͨͳ͍ʢϑΝΠϧͱͷඥ෇͚ʹதؒςʔϒϧ
    Λར༻ʣ
    • RailsͷPolymorphicؔ࿈ɺMountable Engineɺ
    Active JobͳͲΛ׆͔ͯ͠࡞ΒΕ͍ͯΔ
    • μΠϨΫτΞοϓϩʔυ͕Մೳ

    View Slide

  10. Active StorageΛ࢖༻͢Δʹ͸
    • Rails 5.2Ͱ͸Gemͷ௥Ճ͸ෆཁ
    rails active_storage:install
    rails db:migrate
    ্هͰBlob / AttachmentϞσϧͰ࢖༻͢Δςʔ
    ϒϧ͕DB্ʹ࡞੒͞ΕΔ

    View Slide

  11. Blob / AttachmentϞσϧ
    • Blob
    • ϑΝΠϧ৘ใʢContent-TypeɺαΠζɺࣝผ༻
    ͷkey΍ϝλσʔλͳͲʣΛ֨ೲ
    • Attachment
    • ϑΝΠϧͱଞͷϞσϧͱͷؔ࿈͚ͮ༻ͷ

    தؒςʔϒϧ

    View Slide

  12. Migrations (1) Blob
    class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
    def change
    create_table :active_storage_blobs do |t|
    t.string :key, null: false
    t.string :filename, null: false
    t.string :content_type
    t.text :metadata
    t.bigint :byte_size, null: false
    t.string :checksum, null: false
    t.datetime :created_at, null: false
    t.index [ :key ], unique: true
    end
    ...
    end
    end

    View Slide

  13. Migrations (2) Attachment
    class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
    def change
    ...
    create_table :active_storage_attachments do |t|
    t.string :name, null: false
    t.references :record, null: false,
    polymorphic: true, index: false
    t.references :blob, null: false
    t.datetime :created_at, null: false
    t.index [:record_type, :record_id, :name, :blob_id],
    name: "index_active_storage_attachments_uniqueness",
    unique: true
    end
    end
    end

    View Slide

  14. Blob / Attachmentͱͷؔ࿈
    Article
    (Record)
    Attachment Blob
    class Article < ActiveRecord::Base
    has_many_attached :images
    class ActiveRecord::Attachment < ActiveRecord::Base
    belongs_to :record, polymorphic: true
    belongs_to :blob
    class ActiveRecord::Blob < ActiveRecord::Base
    has_many :attachments

    View Slide

  15. has_many_attached
    class Article < ActiveRecord::Base
    has_many_attached :images
    # ಺෦తʹ͸ҎԼͷΑ͏ʹͳ͍ͬͯΔ
    has_many :images_attachments,
    -> { where(name: images) }, as: :record
    has_many :images_blobs,
    through: :images_attachments,
    class_name: "ActiveStorage::Blob",
    source: :blob

    View Slide

  16. ը૾ϑΝΠϧͷૹ৴ͱදࣔ
    • View (ϑΥʔϜ)
    <%= form.file_field :avatar %>
    • Controller
    @user.attach(params[:user][:avatar])
    • View (දࣔʣ
    <%= image_tag url_for(@user.avatar) %>

    View Slide

  17. ࣮ࡍʹ࢖ͬͯΈ·ͨ͠

    View Slide

  18. Amazon S3Ͱ࢖͏ʹ͸
    • gem 'aws-sdk-s3'
    • config.active_storage.service = :amazon
    • ֤؀ڥຖʹઃఆ
    • config/storage.ymlͰAWSʹΞΫηεͰ͖Δ
    ػີ৘ใʢΛಡΈࠐΉํ๏ʣΛࢦఆ

    View Slide

  19. config/storage.yml
    test:
    service: Disk
    root: <%= Rails.root.join("tmp/storage") %>
    local:
    service: Disk
    root: <%= Rails.root.join("storage") %>
    # Use rails credentials:edit to set the AWS secrets (as
    aws:access_key_id|secret_access_key)
    amazon:
    service: S3
    access_key_id: <%=
    Rails.application.credentials.dig(:aws, :access_key_id) %>
    secret_access_key: <%=
    Rails.application.credentials.dig(:aws, :secret_access_key) %>
    region: us-east-1
    bucket: your_own_bucket

    View Slide

  20. ࣮ࡍʹ࢖ͬͯΈ·ͨ͠

    View Slide

  21. μΠϨΫτΞοϓϩʔυ
    • ϒϥ΢βʢJavaScriptΫϥΠΞϯτʣ͔Β௚઀
    Ϋϥ΢υʹΞοϓϩʔυͰ͖Δ
    • Webαʔόʔ΍ΞϓϦέʔγϣϯαʔόʔʹհ
    ͞ͳ͍ʢෛ୲Λ͔͚ͳ͍ʣͷͰɺߴ଎
    • HerokuͳͲΞϓϦέʔγϣϯαʔόͷσΟεΫ
    ༰ྔ͕খ͍͞৔߹ʹ΋࢖༻Ͱ͖Δ

    View Slide

  22. μΠϨΫτΞοϓϩʔυΛ࢖͏ʹ͸
    • activestorage.jsΛ૊ΈࠐΉ
    • Asset PipelineͰ࢖༻͢Δ৔߹ʢnpm΋͋Δʣ

    //= require activestorage
    • ϑΥʔϜϔϧύͰdirect_upload: trueΛࢦఆ͢Δ
    <%= form.file_field :avatar, direct_upload: true
    %>

    View Slide

  23. ΍ͬͯΈ͚ͨͲಈ͔ͳ͔ͬͨ
    • S3ʹΞοϓϩʔυ͸੒ޭ͍ͯ͠Δ

    View Slide

  24. ·ͱΊ (1)
    • Current Attributes
    • ֤ϦΫΤετͰڞ௨ͳॲཧͷ݁ՌΛೖΕΔ͜
    ͱ͕Ͱ͖Δ
    • Encrypted configuration
    • ҉߸Խͨ͠1ͭͷϑΝΠϧʹػີ৘ใΛอଘ͢
    Δ

    View Slide

  25. ·ͱΊ (2)
    • Active Storage
    • Rails 5.2ͰGemͷ௥Ճͳ͘࢖͑ΔϑΝΠϧ
    Ξοϓϩʔυػೳ
    • ݱ࣌఺Ͱ΋ϩʔΧϧͰͷ։ൃɺAmazon S3
    ΁ͷΞοϓϩʔυ͸Մೳ

    View Slide