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. 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
  2. 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
  3. 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
  4. Active Storage • Ϋϥ΢υʹϑΝΠϧΛΞοϓϩʔυ͕Մೳ (Amazon S3, Google Cloud, Microsoft Azure)

    • ϞσϧʹΫϥ΢υ্ͷϑΝΠϧΛؔ࿈͚ͮ • Railsαʔόʔ͔Β΋JavascriptΫϥΠΞϯτ͔Β ΋Ξοϓϩʔυ͕Մೳ • ը૾ͷม׵ॲཧ΋ՄೳʢMiniMagickʣ
  5. 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
  6. 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
  7. 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
  8. 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
  9. ը૾ϑΝΠϧͷૹ৴ͱදࣔ • View (ϑΥʔϜ) <%= form.file_field :avatar %> • Controller

    @user.attach(params[:user][:avatar]) • View (දࣔʣ <%= image_tag url_for(@user.avatar) %>
  10. Amazon S3Ͱ࢖͏ʹ͸ • gem 'aws-sdk-s3' • config.active_storage.service = :amazon •

    ֤؀ڥຖʹઃఆ • config/storage.ymlͰAWSʹΞΫηεͰ͖Δ ػີ৘ใʢΛಡΈࠐΉํ๏ʣΛࢦఆ
  11. 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
  12. μΠϨΫτΞοϓϩʔυΛ࢖͏ʹ͸ • activestorage.jsΛ૊ΈࠐΉ • Asset PipelineͰ࢖༻͢Δ৔߹ʢnpm΋͋Δʣ
 //= require activestorage •

    ϑΥʔϜϔϧύͰdirect_upload: trueΛࢦఆ͢Δ <%= form.file_field :avatar, direct_upload: true %>
  13. ·ͱΊ (2) • Active Storage • Rails 5.2ͰGemͷ௥Ճͳ͘࢖͑ΔϑΝΠϧ Ξοϓϩʔυػೳ •

    ݱ࣌఺Ͱ΋ϩʔΧϧͰͷ։ൃɺAmazon S3 ΁ͷΞοϓϩʔυ͸Մೳ