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
メソッドで引数value
がnilの場合、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は、carrierwaveやshrine らと同じ類のファイルアップロード処理用ライブラリです。クラウドサービスに簡単にファイルをアップロード、及び、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_blobs
とactive_storage_attachments
の2テーブル作られるようになっています。
後は各種クラウドサービス用のgem(aws-sdk
、google-cloud-storage
等)をGemfileに定義すれば準備は完了です。
実際に使用するには、画像を使用する事を宣言する為のメソッド(has_one_attached
、has_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?
url_for(user.avatar)
画像の情報は先に作成したテーブル(active_storage_blobs
、active_storage_attachments
)で管理するようになっており、model毎のテーブルにはデータを作らないようになっています。
ファイルのアップロード先(どのクラウドサービスを使うか)はconfig/storage.yml
で定義するようになっています。デフォルトでdevelopment
、test
環境ではローカルにファイルを作成するようになっています。
また、JSからのファイルのダイレクトアップロード機能も提供されています。詳細はActive StorageのREADMEを参照して下さい。
Fix repository URL [ci skip]
activestorage/README.md
、
activestorage/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_storage
とconfig.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?
の方が高速な為。