なるようになるブログ

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

rails commit log流し読み(2022/11/24)

2022/11/24分のコミットです。

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

activerecord/CHANGELOG.md

actionview/CHANGELOG.md

actioncable/CHANGELOG.md


Merge PR #45696

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

persistanceにbuildメソッドを追加しています。newメソッドのラッパーなのですが、attributeをArrayで指定しての複数オブジェクトのまとめての作成などが出来るようになっています。

User.build([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])

# blockを指定した場合、blockは全てのオブジェクトで実行される
User.build([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
  u.is_admin = false
end

Validate ActiveStorage::Blob io is rewindable

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

ActiveStorage::Blob.create_and_upload!のioパラメータにrewindableじゃないオブジェクトが指定された場合にArgumentErrorをraiseするよう修正しています。


Merge PR #45660

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

serializeメソッドで、permitted classes 及び unsafe loadの設定を、attribute毎に指定出来るよう修正しています。

class User < ActiveRecord::Base
  serialize :preferences, yaml: { permitted_classes: [Symbol, Time] }
end

Merge PR #43019

actionview/lib/action_view/helpers/tags/select.rbの修正です。

grouped/nested collectionsをselectに指定した場合に、collectionの末尾に指定した要素がselect tagのattributeとして使われるよう修正しています。

<%= form.select :foo, [["North America", [["United States","US"],["Canada","CA"]], { disabled: "disabled" }]] %>
# => <select><optgroup label="North America" disabled="disabled"><option value="US">United States</option><option value="CA">Canada</option></optgroup></select>

Allow resetting singular associations

activerecord/lib/active_record/associations.rbactiverecord/lib/active_record/associations/builder/singular_association.rbの修正です。

has_one、及び、belongs_toassociationを使用している場合に、そのassociationのcacheをアンロードする為のreset_association(association`部分はassociation名)メソッドが使用出来るよう修正しています。

class Account < ApplicationRecord
  has_one :beneficiary
end

account.reset_beneficiary

Merge pull request #45119 from p8/guides/add-cors-to-security-guide

rails guideのSecuring Rails Applicationsの修正です。

CORSの対応方法について説明した、Cross-Origin Resource Sharingの項を追加しています。


Merge pull request #46440 from okuramasafumi/add-active-support-note-to-contribution-guide

rails guideのHow to contribute to Ruby on Railsの修正です。

Do you intend to add a new feature or change an existing one?の項に、Active Supportのcore extensionsの変更は基本的にはrejectされる、そのような変更を行いたい場合、Ruby本体へ提案を行う方が良い、という説明を追加しています。


Inline QueryConstraints module into Persistence

Active Recordの修正です。

query_constraintsに関する処理を専用のmoduleで行っていたのを、Persistence内で処理を定義するよう修正しています。専用のmoduleで行う程の処理の量では無いため。


Avoid warning when loading YAMLColumn generated by old versions of Rails on Ruby 2.7

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

Rails + Ruby 2.7 で生成したYAMLColumnをロードした場合に未初期化に関するwarningが出ていたのを修正しています。


feat: restore Action Cable Redis pub/sub listener on connection failure

actioncable/lib/action_cable/subscription_adapter/redis.rbの修正です。

Action CableのRediのpub/sub adapterで、Redisのconnectionが切れた場合、自動で再接続するよう修正しています。


Avoid validating belongs_to association if it has not changed

Active Recordの修正です。

required: truebelongs_to associationが定義されている場合、レコード更新時に必ずそのassociationの存在チェックをするqueryが実行されていたのを、associationの値が変更されていない場合は、そのチェックを実行しないよう修正しています。この挙動は、config.active_record.belongs_to_required_validates_foreign_keyにtrueを指定、または、load_defaults 7.1を指定した場合に使用されるようになっています。


option to disable all methods that ActiveRecord.enum generates

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

オプションで、ActiveRecord.enumのメソッドを全て生成しないように出来るよう修正しています。生成したく無い場合は、_instance_methodsにfalseを指定すれば良いようになっています。

class Book < ApplicationRecord
  enum status: [:proposed, :written], _instance_methods: false
end

LoggerThreadSafeLevel only impact the receiving logger

activesupport/lib/active_support/logger_thread_safe_level.rbの修正です。

LoggerSilenceがreceiverのloggerだけでなく、全てのloggerに影響を与えてしまう(全てのloggerの出力を抑止してしまう)バグがあったのを修正しています。