Rails5.2の新機能
ざっくりとですが、Rails 5.2の新機能のポイントをまとめてみました。リリースノートはこちら
Active Storage
Active Storageはファイルアップロードの新機能で、Amazon S3、Google Cloud Storage、Microsoft Azure Storage等のクラウドストレージへのアップロードが標準で利用でき、ダイレクトアップロード、ミラーリング機能によって複数のクラウドストレージのあいだでファイルを同期することもできます。
Redis Cache Store
config.cache_store = :redis_cache_storeとすることで、Redisをキャッシュとして使えるようになります。
HTTP/2 Early Hints
Early Hintsは、Webサーバーにリクエストが完了する前にヘッダで必要なJavaScriptやスタイルシート(assets)のURLを伝えることができ、より高速なページの表示を可能にします。
Bootsnap
Railsアプリケーションの起動時間を短縮するライブラリです。ドキュメントによると起動時間が半分程度になるとのことです。
Content Security Policy
Webブラウザに対してどのページからならリソースをダウンロードして良いかを設定することで、セキュリティを管理するHTTPの仕様です。これによってクロスサイトスクリプティング(XSS)といった脆弱性からWebサイトを守ることができます。
Credentials
これまで、複数存在していたRailsアプリケーション内の機密情報(config/secrets.ymlや、SECRET_BASE_KEY)を1つのファイル(config/credentials.yml.enc)内に暗号化して格納できるようになりました。
Railsアップデート
事前にRubyのバージョンを2.5以上にする必要があります。
Gemfile
1 |
gem 'rails', '~> 5.2.0' |
1 |
$ bundle install --path vendor/bundle |
アップデートコマンドを実行。
1 |
$ rails app:update |
上書き確認が表示されます。
以下を参考に、Y で上書き、 n でスキップ、 d で事前の差分確認などで進めてください。
1 2 3 4 5 6 |
Y - yes, overwrite n - no, do not overwrite a - all, overwrite this and all others q - quit, abort d - diff, show the differences between the old and the new h - help, show this help |
私の場合の、上書き確認されたファイルと新規作成されたファイルなど載せておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Overwrite /config/boot.rb Overwrite /config/routes.rb Overwrite /config/application.rb Overwrite /config/cable.yml Overwrite /config/puma.rb Overwrite /config/spring.rb Overwrite /config/environments/development.rb Overwrite /config/environments/production.rb Overwrite /config/environments/test.rb identical config/initializers/application_controller_renderer.rb identical config/initializers/assets.rb identical config/initializers/backtrace_silencers.rb create config/initializers/content_security_policy.rb identical config/initializers/cookies_serializer.rb create config/initializers/cors.rb identical config/initializers/filter_parameter_logging.rb identical config/initializers/inflections.rb identical config/initializers/mime_types.rb create config/initializers/new_framework_defaults_5_2.rb identical config/initializers/wrap_parameters.rb identical config/locales/en.yml identical config/storage.yml Overwrite /bin/bundle Overwrite /bin/rails Overwrite /bin/rake Overwrite /bin/setup Overwrite /bin/updaten Overwrite /bin/yarn |
gem ‘bootsnap’の追加
config/boot.rbにrequire ‘bootsnap/setup’と追加されたように、Rails 5.2から bootsnap を利用して起動するようになりました。
Gemfile
1 |
gem 'bootsnap', require: false |
Active Storageの設定
ここからはActive Storageを導入する場合です。アップデートコマンドでconfig/environmentsにはconfig.active_storage.service = :localと追加されています。
Gemfile
1 |
gem 'activestorage', '~> 5.2.0' |
config/application.rb に以下を追加して rails active_storage:install を実行します。
config/application.rb
1 |
require 'active_storage/engine' |
1 |
$ rails active_storage:install |
すると、次のマイグレーションファイルが作られます。見てみると、active_storage_blobsとactive_storage_attachmentsという2つのテーブルが生成されています。Blobはファイル名、ファイルの種類、バイト数、誤り検出符号などのメタデータを保持し、Attachmentは、BlobオブジェクトとActive Recordオブジェクトを紐付けるための中間テーブルです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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 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 |
1 |
$ rails db:migrate |
これでアップデート完了しました!