なるようになるブログ

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

rails commit log流し読み(2019/03/08)

2019/03/08分のコミットです。

CHANGELOGへの追加はありませんでした。


Build assets automatically if necessary

railties/test/isolation/abstract_unit.rbの修正です。

railtiesのテストで必要なassetsのbuildを、必要ならテスト内で行うよう修正しています。CIではCIの前処理で行うようになっているので不要なのですが、ローカルで一部テストを実施するのに必要なので、テスト内でも行うようにしています。


Add version awareness to rails db:system:change

railties/lib/rails/generators/rails/db/system/change/change_generator.rbの修正です。

db:system:changeでGemfile内のgemを置き換える際に、バージョン指定も置き換えるよう修正しています。

元々はgem名のみ置き換えるようになっていたのですが、それだと不正なバージョン指定になってしまう(元々gem 'sqlite3', '~> 1.3', '>= 1.3.6'となっていた場合、gem 'pg', '~> 1.3', '>= 1.3.6'になってしまい、リリースされていないバージョンの指定になる)為。

rails commit log流し読み(2019/03/07)

2019/03/07分のコミットです。

CHANGELOGへの追加はありませんでした。


Handle blank inserts like update_all; raise upfront.

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

insert_allに空の配列を指定した場合に、値を無視していたのをArgumentErrorをraiseするよう修正しています。update_allと振る舞いを合わせる為。


Fix test case name after file extraction

activerecord/test/cases/insert_all_test.rbの修正です。

insert_allに関するテストのテスト名がPersistenceTestになっていたのをInsertAllTestに修正しています。


Add some whitespace for readability.

activerecord/lib/active_record/insert_all.rbactiverecord/test/cases/insert_all_test.rbの修正です。

各箇所に適切に改行を追加しています。


Merge pull request #35497 from eileencodes/parse-yaml-without-erb-for-rake-tasks

activerecord/lib/active_record/tasks/database_tasks.rbrailties/lib/rails/application/configuration.rbの修正です。

複数DB用のtaskをロードする際に、database.ymlをERBのパース無しで読み込むよう修正しています。

元々はdatabase.ymlをERBのパースありで読み込んでいたのですが、それだと、database.ymlでenvに依存した値を定義している場合(e.g. some_config: <%= Rails.configuration.some_config %>)に、パース処理でエラーになってしまう(rake task構築時にenvはまだ読み込まれていない為)為、taskロード時にはERBのパース処理は行わないよう修正しています。


Bump Zeitwerk

activesupport/activesupport.gemspecの修正です。

zeitwerkのバージョンを>= 1.3.2に修正しています。


Update links and code examples in the guides to use HTTPS where the host supports it.

rails guideの修正です。

各箇所のリンクを可能な限りHTTPSに修正しています。


Merge pull request #35498 from sobrinho/fix-including-excluding-flatten

activesupport/lib/active_support/core_ext/array/access.rbactivesupport/lib/active_support/core_ext/enumerable.rbの修正です。

Array#including, Array#excluding, Enumerable#excludingで引数に対してflattenを呼び出す際に、再帰の深さに1を指定するようにしています。


[ci skip]Fix typo: constaint -> constraint

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

constraintconstaintにタイポしていたのを修正しています。


Move all Arel constructions from uniqueness validator into connection adapter

Active Recordの修正です。

uniqueness validatorで行っていたArelの生成処理をconnection adapter内に移動しています。


Just inherit ForeignKeyChangeColumnTest for with prefix/suffix tests

activerecord/test/cases/migration/foreign_key_test.rbの修正です。

foreign key columnの変更処理に関するテストをmoduleにして、prefix/suffixを指定したテストで共有していたのを、classに変更してそれを継承する形に修正しています。


Correct the limitation about order with distinct in the guide

rails guideのActive Record Query Interfaceの修正です。

Orderingの項にある、orderdistinctを一緒に使用した場合orderにはdistinctで指定されているfieldしか指定出来ないという説明が、MySQLでだけ起こるという説明になっていたのを ほとんどのDBで起こる旨説明を修正しています。


[ci skip] Fix typos

actionpack/lib/action_controller/renderer.rbactionview/test/ujs/public/vendor/jquery-2.2.0.jsのdocの修正です。

各箇所のタイポを修正しています。


Merge pull request #35508 from cjlarose/add-explicit-require-in-actiontext-initializer

actiontext/app/helpers/action_text/content_helper.rbの修正です。

不足していたrails-html-sanitizerのrequireを追加しています。


Fix incorrect identifier quoting [ci skip]

rails guideのActive Record Query Interfaceactiverecord/lib/active_record/relation/query_methods.rbのdocの修正です。

selectメソッドを使用した際に発行されるSQLの例で、識別子のクォートが実際に生成されるSQLと異なっていたのを修正しています。


Refactor AR::Querying to extract QUERYING_METHODS list

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

ActiveRecord::Querying moduleでrelationにdelegateするメソッドの一覧を定数に切り出しています。テストでの確認をしやすくする為。


Merge pull request #35503 from samjohn/cannot-grammar-correction

各箇所のcan notcannotに修正しています。

どちらも綴りとしては間違えてないのですが、cannotの方が一般的な為とのことです。

参考: Cannot or Can Not?


Delegate only query method to relation as with except

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

relationにdelegateするメソッドの一覧にonlyを追加しています。

exceptと似たような挙動をするメソッドなのですが、exceptは元々delegate対象に含まれており、振る舞いを合わせる為onlyもrelationにdelegateするようにしています。


Update racc to latest version

Gemfile.lockの修正です。

racc gemのバージョンを1.4.15に更新しています。Ruby 2.7でコンパイルする為の修正が含まれている為。


Learn more about inflections[ci skip]

rails guideのActive Support Core Extensionsの修正です。

Inflectionsの項にある、config/initializers/inflections.rbについての説明の言い回しを修正しています。


Corrected execution result of Enumerable#sum [ci skip]

rails guideのActive Support Core Extensionsの修正です。

Enumerable#sumメソッドのexampleの実行結果が実際の実行結果と異なっていたのを修正しています。


Update multiline? sample code [ci skip]

rails guideのActive Support Core Extensionsの修正です。

Regexp#multiline?メソッドの書かれているRails内での使用例が、既に存在していないメソッドだった為、現在まだあるメソッドを例に使用するよう修正しています。

rails commit log流し読み(2019/03/06)

2019/03/06分のコミットです。

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

activerecord/CHANGELOG.md

activesupport/CHANGELOG.md


Add insert_all to ActiveRecord models (#35077)

Active Recordの修正です。

bulk insert用のinsert_all / insert_all! / upsert_allメソッドを追加しています。

User.insert_all([
  { name: 'a', created_at: Time.current, updated_at: Time.current },
  { name: 'b', created_at: Time.current, updated_at: Time.current },
  { name: 'c', created_at: Time.current, updated_at: Time.current }
])
# => #<ActiveRecord::Result:0x0000562614ba2d78 @columns=["id"], @rows=[[5], [6], [7]], @hash_rows=nil, @column_types={"id"=>#<ActiveModel::Type::Integer:0x0000562614a48db0 @precision=nil, @scale=nil, @limit=8, @range=-9223372036854775808...9223372036854775808>}>

callback、validationは実行されないので、callbackで指定される値は自分で明示的に指定する必要があります。

insert_all!はunique constraintに違反した場合、ActiveRecord::RecordNotUniqueをraiseするようになっています(insert_allは無視)。

User.insert_all([
  { id: 1, name: 'a', created_at: Time.current, updated_at: Time.current },
  { id: 1, name: 'b', created_at: Time.current, updated_at: Time.current },
])
# => INSERT INTO "users"("id","name","created_at","updated_at") VALUES (1, 'a', '2019-03-05 23:10:02.382965', '2019-03-05 23:10:02.382977'), (1, 'b', '2019-03-05 23:10:02.382980', '2019-03-05 23:10:02.382983') ON CONFLICT  DO NOTHING RETURNING "id"

User.insert_all!([
  { id: 1, name: 'a', created_at: Time.current, updated_at: Time.current },
  { id: 1, name: 'b', created_at: Time.current, updated_at: Time.current },
])
# => ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  重複キーが一意性制約"users_pkey"に違反しています)

upsert_allは名前の通りでUPSERT(PostgreSQL / SQLiteではON CONFLICTMySQLではON DUPLICATE KEY UPDATE)が使用されるようになっています。PostgreSQL及びSQLiteはUPSERTがサポートされているバージョンである必要があります。

また、PostgreSQLの場合はreturningオプションで返すattributesを指定、PostgreSQL / SQLiteunique_byオプションでレコードを照会する時に使用するカラムを指定出来るようになっています。


Ensure clear_cache! clears the prepared statements cache

Active Recordの修正です

adapter毎に行っていたprepared statements cacheのクリア処理を、abstract adapterのclear_cache!で行うよう修正しています。

元々は特定のadapterでのみprepared statementsをサポートしていた為個別に行っていたのですが、現在は全てのadapterでサポートしている為、処理漏れを防ぐためにabstract adapterで行うようにしています。


Added Array#including, Array#excluding, Enumerable#including, Enumerable#excluding

activesupport/lib/active_support/core_ext/array/access.rbactivesupport/lib/active_support/core_ext/enumerable.rbの修正です。

まずcollectionに値を追加する為のArray#includingEnumerable#includingメソッドを追加しています。

[ 1, 2, 3 ].including(4, 5) #=> [ 1, 2, 3, 4, 5 ]

値を追加する際にflattenを呼び出している為、Array#+と少しだけ挙動が違います。

[ 1, 2, 3 ].including([[4, 5]])
# => [1, 2, 3, 4, 5]

[ 1, 2, 3 ] + [[4, 5]]
# => [1, 2, 3, [4, 5]]

次に、Array#without及びEnumerable#withoutArray#excludingEnumerable#excludingにリネームしています。これはincludingと名前を合わせる為との事です。なお、withoutもaliasとして残っており、引き続き使用出来るようになっています。

最後に、Array#excluding及びEnumerable#excludingで値をflattenで展開してから処理するよう修正しています。withoutも同様。

# before
[ 1, 2, 3, 4, 5 ].without([4, 5]) #=> [ 1, 2, 3, 4, 5 ]

# after
[ 1, 2, 3, 4, 5 ].without([4, 5]) #=> [ 1, 2, 3 ]

Enable Performance/ReverseEach cop to avoid newly adding reverse.each

.rubocop.ymlの修正です。

Performance/ReverseEach copを追加しています。


Enable Lint/AmbiguousOperator and Lint/AmbiguousRegexpLiteral cops

.rubocop.ymlの修正です。

Lint/AmbiguousOperatorLint/AmbiguousRegexpLiteral copを追加しています。


[ci skip] Fix typo beacuse -> because

activerecord/lib/active_record/persistence.rbのdocの修正です。

beacusebecauseに修正しています。


Allow remove_foreign_key with both to_table and options

ACtive Recordの修正です。

remove_foreign_keyメソッドでto_tableoptionsを一緒に指定出来るよう修正しています。

同じテーブルに複数のforeign keyがある場合に削除対象を明示的に指定出来るようにする(e.g. remove_foreign_key :astronauts, :rockets, column: "myrocket_id")為。

rails commit log流し読み(2019/03/05)

2019/03/05分のコミットです。

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

activerecord/CHANGELOG.md


Allow truncate for SQLite3 adapter and add rails db:seed:replant (#34779)

Active Recordの修正です。

全tablesのtruncate + seed taskの実行をセットで行うdb:seed:replant taskを追加しています。合わせて、truncateが無いsqlite3 adapterにtruncateメソッドを追加しています。中身はDELETEを実行しているだけ。


Merge pull request #35434 from matthewd/faster-isolated-ar

activerecord/Rakefileactiverecord/test/cases/unconnected_test.rbの修正です。

Active Recordのisolated testで、railtiesのテスト同様に、毎回コマンドを実行するのではなくforkしたプロセスを使い回すよう修正しています。テスト高速化の為。


Fix guide to use correct number of files created by controller generator

rails guideのGetting Started with Railsの修正です。

Generating a Controllerの項にあるcontroller generateが生成するファイルの数が実際の生成数と異なっていたのを修正しています。Rails 6からCoffeeScriptのスタブを生成しないようになり、生成されるファイルの数が1つ減った為。


Fix typo in test name

activerecord/test/cases/tasks/database_tasks_test.rbの修正です。

テストメソッド名でタイポしていたのを修正しています。


Decrypt the ASt test config if available

activestorage/Rakefileの修正です。

Active Storageのテストで使用する各種ストレージサービス用のsecretの情報をrake task内でdecryptするよう修正しています。Travisの場合、.travis.ymlでdecryptを行うようになっているのですが、Travis以外のCIでも動作するようにする為だと思います。多分。


Reset connection_handlers to default when any test dirties that

activerecord/test/cases/tasks/database_tasks_test.rbの修正です。

database taskのテストの後処理でconnection handlerをdefaultに戻すよう修正しています。テスト内でconnection handlerの状態を変えてしまっている為。

rails commit log流し読み(2019/03/04)

2019/03/04分のコミットです。

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

activerecord/CHANGELOG.md


Fixed reselect throwing NoMethodError on ActiveRecord.

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

Relationにdelegateするメソッドにreselectを追加しています。


Add an example about how to load images under app/javascript/images

railties/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.ttの修正です。

app/javascript/imagesにあるファイルをロードする場合のexampleを追加しています。


Correctly escape ERB tag

railties/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.ttの修正です。

先のapp/javascript/imagesにあるファイルをロードする場合のexampleでERBタグの記載方法に誤りがあったのを修正しています。


Move changelog entry about reselect method to the section of the next release

activerecord/CHANGELOG.mdの修正です。

reselectを追加した対応(Add reselect method. This is a short-hand for unscope(:select).select(fields).)のエントリーをファイルの先頭に移動しています。


Deprecate mismatched collation comparison for uniquness validator

Active Recordの修正です。

uniqueness validatorで、case insensitiveなcolumnに対しても強制的にcase sensitiveな比較を行っていたのがdeprecateになりました。Rails 6.1からは、case insensitiveなcolumnについては、case sensitiveな比較は行われなくなるようになります。引き続きcase sensitiveな比較を使用したい場合は、case_sensitive: trueオプションを指定すればOKです。

なお、影響があるのはMySQLのみです(sqlite3、PostgreSQLはデフォルトでcase sensitiveな為)。


Fix typo in CHANGELOG.md [ci skip]

activerecord/CHANGELOG.mdの修正です。

uniquenessuniqunessにタイポしていたのを修正しています。

rails commit log流し読み(2019/03/03)

2019/03/03分のコミットです。

CHANGELOGへの追加はありませんでした。


Oracle 12.2+ supports 128 byte identifier length

activerecord/test/cases/migration/columns_test.rbの修正です。

indexに関するテストで、OracleAdapterの場合省略されたindex名でチェックしていたのを、他のadapter同様省略していないindex名でチェックするよう修正しています。

元々Oracleではidentifier lengthが30byteだった為index名を略すようになっていたのですが、Oracle 12.2+からidentifier lengthが128byteになり略さないようになった為。


activesupport: Simplify class_attribute implementation (#35454)

activesupport/lib/active_support/core_ext/class/attribute.rbメソッドについて、メソッド引数にキーワード引数を使用、singleton classのメソッド再定義にredefine_singleton_methodを使用、不要なデフォルトの再定義を削除、等のリファクタリングを行っています。


Rails DateTime type is mapped to Oracle TIMESTAMP since Rails 5.0

activerecord/test/cases/date_test.rbの修正です。

OracleのTIMESTAMP typeをRailsDateTime typeにマッピングするようにした事により不要になったOracle enhaced adapterの場合の分岐処理を削除しています。


Oracle database can run delete statement with order by andfetch first n rows only

activerecord/test/cases/relation/delete_all_test.rbの修正です。

delete_all + order + limitのテストがOracle enhaced adapterで実行されないようになっていたのを、実行するよう修正しています。

Support Oracle 12c top-N queryの対応により、上記テストは通るようになっている為。


Remove unnecessary current_adapter?(:OracleAdapter) for index length

activerecord/test/cases/associations/eager_test.rbactiverecord/test/cases/migration/index_test.rbの修正です。

identifier lengthが128byteになった事により不要になったOracle enhaced adapterの場合の分岐処理を削除しています。

rails commit log流し読み(2019/03/02)

2019/03/02分のコミットです。

CHANGELOGへの追加はありませんでした。


Use the correct model in the test

activerecord/test/models/post.rbの修正です。

Post modelのauthor_favorites_with_scope associationで使用するクラスに誤りがあった(scopeの指定が無いクラスを使用していた)のを修正しています。


Merge pull request #35429 from jhawthorn/template_format_nil

Action View、Acton Mailerの修正です。

No nil format on templatesActionView::Templateformatの指定が必須になったのですが、それはやめにして、ActionView::Templateformatnil(無指定)を許容するよう修正しています。

formatが指定されていない場合、元の挙動と同様にdetailsからformatを取得するようになっています。ただ、元の実装と異なり、その取得したformatをtemplateには設定しないようにしています。そのため、templateがimmutableなのは変わらず。


Add test and change how format set in ActionMailer

Action Mailerの修正です。

Action Mailerでmultipart formatsが自動で設定される事を確認するテストを追加、及び、formatsを指定するのにself.formatsに依存していたのを、renderメソッドの引数に明示的にformatsを指定するよう修正しています。