Rails CHANGELOG "2019-11-18".."2019-11-24"

この期間の CHANGELOG.md へのコミットは6件。

ActiveRecordに関して1件、ActiveSupportに関して1件、ActionPackに関して4件。

そのほか Fixup CHANGELOGs [ci skip] · rails/rails@214f439 でマークダウンの整形(インデントの調整や文末のピリオド付与など)もされている。

ActiveRecord

nil を含むArrayをwhere句に指定した場合に、そのwhere句に対する unscope が正しく動作しないバグを修正した

コミット: Fix unscoped grouped where · rails/rails@12afdba

unscope ってなんだっけ

Removes an unwanted relation that is already defined on a chain of relations. This is useful when passing around chains of relations and would like to modify the relations without reconstructing the entire chain.

引用: ActiveRecord::QueryMethods

「すでに定義されたリレーションから不要なリレーションを消すよ。これはリレーション全体を作り直すことなくリレーションを変更できるから便利だよ。」的な感じだ。

手元(Rails 6.0.1)でバグを確認してみる

[1] > users = User.where(id: [1, 2])
=>   User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (1, 2)
[2] > users.unscope(where: :id)
=>   User Load (1.0ms)  SELECT "users".* FROM "users"
[3] > users = User.where(id: [nil, 1, 2])
=>   User Load (1.0ms)  SELECT "users".* FROM "users" WHERE ("users"."id" IN (1, 2) OR "users"."id" IS NULL)
[4] > users.unscope(where: :id)
=>   User Load (1.1ms)  SELECT "users".* FROM "users" WHERE ("users"."id" IN (1, 2) OR "users"."id" IS NULL)

なるほど確かに [4] で unscope が効いていない。これが効くように修正したとのこと。

Arel.fetch_attribute を修正している。

Allow attributes to be fetched from Arel node groupings.

引用: Fix unscoped grouped where by gmcgibbon · Pull Request #37733 · rails/rails

「ノードグループから属性を取得できるようにしたよ」ということなので、

多分ノードグループというのが手元で動かしたときの ("users"."id" IN (1, 2) OR "users"."id" IS NULL) のことで、ここから "users"."id" と特定できるようにしたよ、的なことじゃないかなぁと思う。

修正前まではこれが特定できなかったから、 unscope しようとしてもその対象が見つからないため、 unscope できなかったんじゃないかな。

ActiveSupport

定数名の修正

コミット: Follow up 997770f5955a36f0c800388c4592c961e184aec4 · rails/rails@9714d5d

Check the actual constant used · rails/rails@997770f の修正の follow up

元々の修正を辿ると、issue は これ `cpu_time` in `ActiveSupport::Notifications::Instrumenter` using incorrect clock? · Issue #37441 · rails/rails で、CLOCK_PROCESS_CPUTIME_ID じゃなくて CLOCK_THREAD_CPUTIME_ID を使おうぜ〜、というやつ。

CHANGELOG の定数名が CLOCK_PROCESS_CPUTIME_ID のままだったので CLOCK_THREAD_CPUTIME_ID に書き換えている。

ActionPack

ActionController::Parameters にkeyの存在確認をする member? メソッドを追加した

コミット: Add params.member? to mimic Hash behavior · rails/rails@34b6e8c

もともと has_key? はあったから機能的には十分だったはず。

だけど、 Hashが has_key?member? を両方使えるのに ActionController::Parameters では has_key? しか使えないのはややこしい?から統一した、という感じかなぁ。

システムテストでスクショを保存するとき、pathの / \- に書き換えるようにした

コミット: Remove slashes and backslashes from image paths · rails/rails@3c3b80e

ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper の修正。

スクショのディレクトリが階層構造になっていると tmp:clear でファイルを消そうとしたときにエラーになってしまうとのこと。

respond_to#any に関して、リクエストに基づくContent-Typeではなく、blockで指定されたContent-Typeを返すようにした

コミット: Modify respond_to behaviour always setting the request's content type: · rails/rails@42c5157

def my_action
  respond_to do |format|
    format.any { render(json: { foo: 'bar' }) }
  end
end

get('my_action.csv')

引用: Modify respond_to behaviour always setting the request's content type: · rails/rails@42c5157

修正前はCSVが返るようになっていたけど、修正後はJSONが返るようになる。

もし今 respond_to { |format| format.any } を使っているコードがある場合、この修正前後でアプリケーションの挙動が変わる可能性があるので注意が必要。

↑と同じ内容

コミット: Merge pull request #37617 from Edouard-chin/ec-respond-to-contenttype · rails/rails@4fe7675

さっきのと全く同じ内容っぽいけど、コミットは別なのか 😮