なるようになるブログ

読書感想文かrailsについてかrubyについてか

rails commit log流し読み(2017/08/05)

2017/08/05分のコミットです。

CHANGELOGにのったコミットは以下の通りです。

activestorage/CHANGELOG.md


Merge pull request #30058 from y-yagi/use_assert_nil_if_value_is_nil_in_assert_field_default_value_

railties/lib/rails/generators/testing/assertions.rbの修正です。

assert_field_default_valueメソッドで引数valuenilの場合、assertionにassert_nilメソッドを使用するよう修正しています。

元々は引数の値に関わらずassert_equalを使用していたのですが、assert_equalのexpect引数にnilを指定するのはMinitest 5.10からdepreateになっている為。


Merge pull request #29520 from kirs/serialize-vs-postgres-native-column

activerecord/lib/active_record/attribute_methods/serialization.rbの修正です。

DB nativeのJSON / array型のカラムをserializeしようとした場合に、エラー(ColumnNotSerializableError)をraiseするよう修正しています。

元々サポートしてない挙動だったのですが、使おうとして上手く動かない、というissueがあった為、明示的にエラーにするようにしたようです。


Merge pull request #30020 from rails/active-storage-import

ファイルアップロード処理用ライブラリのActive Storageがrails本体にマージされました。

Active Storageは、carrierwaveshrine らと同じ類のファイルアップロード処理用ライブラリです。クラウドサービスに簡単にファイルをアップロード、及び、Active Recordから参照ができるようになっています。 現時点で対応しているクラウドサービスは、Amazon S3, Google Cloud Storage, Microsoft Azure Storageです。

使い方を簡単に。

まず、Active Storageに必要なファイルを生成する為のtask(rails activestorage:install)が提供されいるのでそれを実行します。実行するとActive Storageで使用するようのテーブルを生成する為のmigrationファイルが生成されます。中身は下記の通り。

class ActiveStorageCreateTables < ActiveRecord::Migration[5.1]
  def change
    create_table :active_storage_blobs do |t|
      t.string   :key
      t.string   :filename
      t.string   :content_type
      t.text     :metadata
      t.integer  :byte_size
      t.string   :checksum
      t.datetime :created_at

      t.index [ :key ], unique: true
    end

    create_table :active_storage_attachments do |t|
      t.string  :name
      t.string  :record_type
      t.integer :record_id
      t.integer :blob_id

      t.datetime :created_at

      t.index :blob_id
      t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
    end
  end
end

active_storage_blobsactive_storage_attachmentsの2テーブル作られるようになっています。

後は各種クラウドサービス用のgem(aws-sdkgoogle-cloud-storage等)をGemfileに定義すれば準備は完了です。

実際に使用するには、画像を使用する事を宣言する為のメソッド(has_one_attachedhas_many_attached)が提供されているので、それをmodelの中で使用すればアップロードが出来るようになります。

class User < ApplicationRecord
  has_one_attached :avatar
end

user.avatar.attach io: File.open("~/face.jpg"), filename: "avatar.jpg", content_type: "image/jpg"
user.avatar.attached? # => true

url_for(user.avatar) # まだこの辺りの挙動は怪しい印象

画像の情報は先に作成したテーブル(active_storage_blobsactive_storage_attachments)で管理するようになっており、model毎のテーブルにはデータを作らないようになっています。

ファイルのアップロード先(どのクラウドサービスを使うか)はconfig/storage.ymlで定義するようになっています。デフォルトでdevelopmenttest環境ではローカルにファイルを作成するようになっています。

また、JSからのファイルのダイレクトアップロード機能も提供されています。詳細はActive StorageのREADMEを参照して下さい。


Fix repository URL [ci skip]

activestorage/README.mdactivestorage/package.jsonの修正です。

GitHubへのリンクがhttps://github.com/rails/activestorageになっていたのをhttps://github.com/rails/railsに修正しています。


module ActiveStorage, not ActiveStorage::Class

Active Storageの修正です。

Active Storageの各クラスをActiveStorage module配下に移動しています。

+module ActiveStorage
 # Abstract baseclass for the concrete `ActiveStorage::Attached::One` and `ActiveStorage::Attached::Many`
 # classes that both provide proxy access to the blob association for a record.
-class ActiveStorage::Attached
-  attr_reader :name, :record
+  class Attached
+    attr_reader :name, :record

既存のコンポーネントの構成と合わせる、及び、API docに正しくクラス名が表示されるようにする為に、との事です。


Fix ruby warnings

Active Storageの修正です。

各コードでRubyのwarningが出ていたのをまとめて修正しています。


Add missing blank line between config.active_storage and config.action_cable

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.ttの修正です。

config.active_storageconfig.action_cableの間にスペースをいれるよう修正しています。


Change gem version of Active Storage to 5.2.0.alpha

activestorage/lib/active_storage/gem_version.rbの修正です。

Active Storageのバージョンを5.2.0.alphaに修正しています。


Revert “Merge pull request #15446 from akshay-vishnoi/doc_changes”

大分古いコミットの、[ci skip] Correct documentation of HashWithIndifferentAccess#dupをrevertしています。

HashWithIndifferentAccess#dupのdocのexampleコードを修正したコミットだったのですが、元のdocの方が正しかった為revertしています。

     #   dup  = hash.dup
     #   dup[:a][:c] = 'c'
     #
-    #   hash[:a][:c] # => nil
+    #   hash[:a][:c] # => "c"
     #   dup[:a][:c]  # => "c"

Remove unecesarry exception variable

activestorage/lib/active_storage/service/azure_storage_service.rbの修正です。

AzureStorageService#uploadメソッドから使用していない変数を削除しています。


Check for app.secrets.secret_key_base, not app.config.secret_key_base

activestorage/lib/active_storage/engine.rbの修正です。

secret_key_baseを参照するのに誤ってapp.configを使用していたのを、app.secretsを使用するよう修正しています。


Merge pull request #30068 from kamipo/user_start_with_than_regexp

activestorage/app/models/active_storage/blob.rbの修正です。

content typeをチェックするのにRegexp#=~を使用していたのをString#start_with?を使用するよう修正しています。

-    content_type =~ /^image/
+    content_type.start_with?("image")

start_with?の方が高速な為。