なるようになるブログ

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

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

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

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


new config to opt-out from adding app directories to $LOAD_PATH

railties/lib/rails/application/configuration.rbrailties/lib/rails/engine.rbの修正です。

autoload pathsを$LOAD_PATHに追加するかどうかを指定する為のconfig.add_autoload_paths_to_load_pathを追加しています。

デフォルトでは既存の挙動に合わせてtrueなのですが、Zeitwerkを使用している場合、内部的には絶対パスを使用しており、$LOAD_PATHにautoload pathsを追加する必要がありません。

そのため、Zeitwerkを使用している場合にはfalseを指定してもload処理は問題ない、かつ、不要なpathsを$LOAD_PATHに追加しない事でBootsnapの処理を減らす事ができる、というメリットがある為、configで挙動を指定出来るようにしています。

合わせて、このconfigについての説明をConfiguring Rails Applications guideに追加しています。


fixes typo in guide [ci skip]

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

先ほど追加されたconfig.add_autoload_paths_to_load_pathについての説明でタイポしている箇所があったのを修正しています。

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

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

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

activesupport/CHANGELOG.md


[ci skip] Add 6.0 Release Notes for Action Pack

rails guideのRuby on Rails 6.0 Release Notesの修正です。

Action Packの変更点についてRelease Notesに記載しています。


Frozen truncate (#36109)

activesupport/lib/active_support/core_ext/string/filters.rbの修正です。

truncateメソッドの戻り値のStringが、truncateする文字の長さによってfrozenされているかどうかが異なっていたのを、必ずunfrozenなStringを返すよう修正しています。他のメソッドと処理を合わせる為。

# before
'foobar'.truncate(5).frozen? # => true
'foobar'.truncate(6).frozen? # => false

# after
'foobar'.truncate(5).frozen? # => false
'foobar'.truncate(6).frozen? # => false

Fallback to spawn instead of fork in jruby (#36111)

railties/Rakefileの修正です。

railtiesのテストで必ずforkを使っていたのを、forkが使えない環境ではspawnを使うよう修正しています。

JRubyでテストを動かせるようにする為、との事です。


Fix merging left_joins to maintain its own join_type context

Active Recordの修正です。

異なるclassのleft_joinsをmergeした場合に、生成されるSQLがLEFT OUTER JOINにならないバグがあったのを修正しています。


Merge pull request #36120 from kamipo/should_maintain_join_type


[ci skip] Update the guide's model generator output.

rails guideのThe Rails Command Lineの修正です。

rails generateの項にあるmodel generatorの出力結果が実際の内容と異なっていたのを修正しています。

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

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

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


[ci skip] Documentation pass of upgrading guide.

rails guideのUpgrading Ruby on Railsの修正です。

Upgrading from Rails 5.2 to Rails 6.0の項のグラマー、言い回しの修正を行っています。


[ci skip] Remove à la just like that.

rails guideのUpgrading Ruby on Railsの修正です。

Notable changesの項のグラマーの修正を行っています。


[ci skip] Get npm just the way they like it

rails guideのUpgrading Ruby on Railsの修正です。

NPM -> npmに修正しています。


Merge pull request #36098 from michaelrbock/patch-1

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

tag_nametag_idメソッド内のdocのグラマーの修正を行っています。


Bump bootsnap to a version with disables iseq_cache for Ruby 2.5

Gemfilerailties/lib/rails/generators/rails/app/templates/Gemfile.ttの修正です。

bootsnapのバージョン指定を>= 1.4.2から>= 1.4.4に修正しています。

Ruby 2.5にはiseq cache関係のバグが色々とあり、そのバグの影響でiseq cacheとTracePointを一緒に使用すると正しく動作しない、という問題がありました。

そのため、Ruby 2.5ではiseq cacheを無効化するようbootsnap/setupで行うようにしており、その設定が行われているバージョンが必ず使用されるようにする為に修正しています。具体的にはzeitwerkがTracePointを使用しており、Ruby 2.5だとzeitwerk + bootsnapが正しく動作しないという問題があった為。

参考:Disable iseq cache in Ruby 2.5


Halve string allocations in S3 streaming and partial downloads

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

S3 streamingとpartial downloadsでbodyを取得するのにreadメソッドを使用していたのを、stringメソッド使用するよう修正しています。

Aws::S3::Object#getStringIOオブジェクトを返す、かつ、StringIO#readは値をコピーした新しいオブジェクトを返します。そのため、不要なオブジェクトの生成を避けるよう、値をそのまま取得出来るstringメソッドを使用するようにしています。

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

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

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

activerecord/CHANGELOG.md


Fix markup in CHANGELOGs [ci skip]

actionview/CHANGELOG.mdactivestorage/CHANGELOG.mdの修正です。

各エントリーのフォーマットの修正を行っています。


Adds touch option to has_one association

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

has_one associationでtouchオプションを使用出来るよう対応しています。


Start Rails 6.1 development

各ライブラリののバージョンを6.1.0.alphaに更新しています。 6.0のrc1がリリースされたため、masterは6.1になります。


Merge pull request #32313 from lulalala/model_error_as_object

Active Modelの修正です。

Hashベースで管理していたActive Modelのエラーを、エラーを管理する為のActiveModel::Errorクラスを追加し、そちらを使用する修正しています。

これにより、エラーを取得する為のより柔軟な処理(e.g. whereメソッド)やI18nのメッセージの遅延評価等が出来るようになっています。

合わせて、Hashベース前提だった幾つかのメソッドがdeprecateになっています。例えば、ActiveModel::Errors#<<ActiveModel::Errors#[]=はdeprecateになっており、代わりにActiveModel::Errors#add`を使う必要があります。


Adds documentation for has_one touch option after #35869 [ci skip]

rails guideのActive Record Associationsの修正です。

has_one associationがサポートしているオプションの一覧に:touchオプションを追加しています。


Change the deprecation for Enumerating ActiveModel::Errors to Rails 6.1 instead of 6.0 (#36087)

activemodel/lib/active_model/errors.rbの修正です。

ActiveModel::Errorクラスの追加に伴いdeprecateになったメソッドで出力されるdeprecateメッセージで、処理が変更になるRailsのバージョンに誤りがあったのを修正しています。


Fix outdated yarn.lock

yarn.lockの修正です。

yarn.lockを最新の内容に更新しています。


Update bug report templates

bug report templatesの修正です。

各ファイルのRailsのバージョンを5.2 -> 6.0に更新しています。

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

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

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

activestorage/CHANGELOG.md


Merge pull request #36063 from albertoalmagro/finish-documentation-for-after-save-commit

rails guideのActive Record Callbacksの修正です。

Transaction Callbacksの項にあるafter_commitを使用した場合のexampleコードを削除しています。ガイドの他の部分に同様のexampleコードがある為。


Merge pull request #36061 from shioyama/update_comment

activemodel/lib/active_model/attribute_methods.rbのdocの修正です。

attribute_method_matchers_matchingメソッド内の動作についてのコメントが、現在の挙動と一致しない内容になっていたのを修正しています。


Make docs on form_with_generates_ids config option match the value

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

config.action_view.form_with_generates_idsのデフォルト値の説明が実際の値と異なっていたのを修正しています。


Update document about perform_caching's default value

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

config.action_controller.perform_cachingconfig.action_mailer.perform_cachingのデフォルト値の説明が実際の値と異なっていたのを修正しています。


Make association builder methods private

Active Recordの修正です。

内部でだけ使用する想定のassociation builder methodsの可視性をprivateに変更しています。


Remove redundant test setups in log_subscriber_test

actionpack/test/controller/log_subscriber_test.rbの修正です。テストの各箇所でconfig.perform_cachingにtrueを指定していたのを、テストの前処理で値を指定するよう修正しています。


Fix rdoc rendering for push alias symbol

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

CollectionProxy#<<メソッドのdoc内の<<が正しく表示されてなかったのを修正しています。


Remove action_controller.perform_caching from api app's configs

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

API-only Applicationsの場合、view cacheに関するconfigをデフォルトで生成しないよう修正しています。API-only Applicationsだとview cacheに関するmoduleがデフォルトでロードされない為。


Rename local variable n to name

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

nameを保持する為の変数名が、nになっている箇所があったのをnameに修正しています。


Merge pull request #35852 from larouxn/resurrection

railties/lib/rails/generators.rbrailties/lib/rails/generators/rails/assets/assets_generator.rbの修正です。

Webpacker対応の際に削除されたjavascriptsjavascript_engineオプションを戻しています。

coffee-railsを引き続き使用し続ける場合に、これらのオプションが必要(coffee-railsで使用している)な為。


Lazy allocate @_start_transaction_state

activerecord/lib/active_record/core.rbactiverecord/lib/active_record/transactions.rbの修正です。

インスタンスの初期化時に@_start_transaction_stateの初期化(空Hashを指定)を行っていたのを、初期値はnilにして実際に使用する際にHashを指定するよう修正しています。


Simplify restore state condition

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

restore_transaction_record_stateメソッド内、state conditionのrestore処理をリファクタリングしています。


Re-organize init_internals

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

init_internalsリファクタリングとして、重複してあった@new_recordの初期化処理を修正、及び、define_attribute_methodsメソッドの呼び出しをinit_internalsで行うよ修正しています。


Fix sliced IN clauses to be grouped

Active Recordの修正です。

スライスされたIN句をグループ化するよう修正しています。合わせて、in_clause_lengthに関する処理をArel visitorのベースクラスで行うようリファクタリングしています。


Avoid redundant attribute_alias? before attribute_alias

Active Recordの修正です。

attribute aliasを取得する際に、attribute_aliasメソッドを呼び出す前にattribute_alias?かどうかをチェックしてからメソッドを呼び出していたのを、 attribute_alias?によるチェックは行わないよう修正しています。alias attributeの処理を高速化する為。


Don't fail ImageAnalyzer on unsupported types

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

ImageMagickでファイルの解析を行う前に、ImageMagickでサポートしているフォーマットかどうかをチェックし、サポートしていないフォーマットの場合は解析処理を行わないよう修正しています。

サポートしていないフォーマットの解析処理を行った場合に処理全体が終了してしまう(解析エラー時のexceptionをハンドリングしていない為)のを避ける為。


Update changelog for a bug-fix to read better [ci skip]

activestorage/CHANGELOG.mdの修正です。

先のImageMagickの対応のエントリーのグラマーの修正を行っています。

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

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

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

railties/CHANGELOG.md


Allow sass-rails greater than 5.x in new apps

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

新規に作成したアプリケーションで、sas-railsのバージョン指定を緩めています(~> 5.0 -> ~> 5)


[ci skip] Active Storage: updating associations replaces

rails guideのRuby on Rails 6.0 Release Notesの修正です。

Active StorageのNotable changesの項に、attached modelに対するupdate / update!の挙動が、associationの追加から更新(既存のファイルの削除)に変更になった事について説明を追加しています。


Merge pull request #36038 from st0012/fix-35602

actionpack/lib/action_controller/api.rbの修正です。

ActionController::APIでCaching moduleをincludeするよう修正しています。

が、API modeでview cacheがデフォルトで必要かというと、それは不要では無いか(使わないケースが多いのでは)という話になり直後にrevertされています。


Revert "Include Caching module for ActionController::API"

という訳で直前のCaching moduleに関する対応をrevertしています。


upgrades Zeitwerk to 2.1.4

Active Supportの修正です。

Zeitwerkのバージョンを2.1.4に修正しています。

合わせて、depend on Zeitwerk 2.1.0で削除したautoloaded_constants、及び、autoloaded?メソッドを戻しています。どちらもRailsガイドに説明が記載されているメソッドな為。


Implements the task zeitwerk:check

railties/lib/rails/tasks/zeitwerk.rakeの修正です。

アプリの構成が新しいautoloader(Zeitwerkを使用したloader)で使用出来るかどうかをチェックする為のzeitwerk:check taskを追加しています。

元のautoloaderと新しいautoloaderはちょいちょい挙動が違う箇所があり、このtaskを使うと新しいautoloaderが使用出来るか、出来ない場合は何が問題なのかを表示してくれるようになっています。


Merge pull request #36029 from kamipo/deprecate_where_not

Active Recordの修正です。

where.notがNORとして動作していたのがdeprecateになりました。Rails 6.1ではNANDとして動作するようになります。

User.where.not(name: "bob", role: "admin")
# => DEPRECATION WARNING: NOT conditions will no longer behave as NOR in Rails 6.1. To continue using NOR conditions, NOT each conditions manually (`.where.not(:name => ...).where.not(:role => ...)`).

メッセージに出ている通り、引き続きNORとして使用したい場合、条件を別に指定する必要があります。 NORだとpolymorphic associationに対するが期待通りに動作しない(where.not(polymorphic_type: object.class.polymorphic_name, polymorphic_id: object.id)だと期待する結果にならない)為。


Add PR link for Active Storage updating release note entry [ci skip]

rails guideのRuby on Rails 6.0 Release Notesの修正です。

Active StorageのNotable changesの項にある説明に、PRへのリンクを追加しています。


Merge pull request #36057 from jhawthorn/activejob_retry_logic

activejob/lib/active_job/exceptions.rbの修正です。

retry_onメソッドでexception_executions(exception毎のretry回数を保持する変数)が定義されていない場合に、Hash.new(0)exception_executionsを初期化していたのを、値を初期化するのではなくグローバルの実行数を使用するよう修正しています。

Rails 6.0から、exception毎のretry回数を保持するようになりました(Keep executions for each specific exception)。ただ、Rails 6より前に格納されたjobでは、retry回数を保持していない為、Support in-flight jobs stored before individual execution counters for retry_on (#34731)exception_executionsが定義されていない場合値を初期化するようにしました。

しかし、これだとRails 6とRails 5.2のアプリが混在にしている時に正しくリトライが行えないという問題が発生してしまっていました(Rails 6側のアプリでexception_executionsを初期化しても、Rails 5.2のアプリではその変数を扱えない為リトライ数が正しく管理出来てなかった)。

そのため、exception_executionsが定義されていない場合は、Rails 5.2の時と同様にグローバルの実行数を使用するようにしています。


Merge pull request #36060 from st0012/fix-35602-doc

rails guideのUsing Rails for API-only Applicationsの修正です。

Adding Other Modulesの項に、view Cachingを使用したい場合の設定方法についての説明を追加しています。


Merge pull request #36059 from composerinteralia/model-attribute-names

activemodel/lib/active_model/attributes.rbの修正です。

ActiveModel::Attributes#attribute_names.attribute_namesメソッドを追加しています。名前の通り、attributeの名前の配列を返すメソッドです。

class Person
  include ActiveModel::Attributes

  attribute :name, :string
  attribute :age, :integer
end

Person.attribute_names
# => ["name", "age"]

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

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

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

activestorage/CHANGELOG.md

actionpack/CHANGELOG.md


Allow ActiveStorage to generate variants of BMP images

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

Active StorageがデフォルトでサポートするフォーマットにBMP(image/bmp)を追加しています。


Merge pull request #36045 from yfxie/fix-normalize-the-hash-of-transformations

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

Active Storageのvariant処理で処理を行う前にdeep_symbolize_keysでkeyをSymbolに変換するよう修正しています。keyにStringが指定された場合にも動作するようにする為。


Remove never used database_selector class accessor

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

使用していないdatabase_selector accessorを削除しています。


PERF: 20% faster pk attribute access

Active Recordの修正です。

primary keyにアクセスする際にメソッド(self.class.primary_key)を使用していたのを、初期化時に値を変数に格納し、そちらを参照するようにしてメソッド呼び出しを減らすよう修正しています。高速化の為。


Remove useless set_value / get_value helper methods

activerecord/lib/active_record/relation.rbactiverecord/lib/active_record/relation/query_methods.rbの修正です。

relationで値を設定 / 取得する際にメソッド(set_valueget_value)を使用していたのを、メソッドを経由せず直接インスタンス変数を参照するよう修正しています。高速化の為。


Merge pull request #36047 from rmacklin/take-screenshot-sooner

actionpack/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rbの修正です。

system testで、テスト失敗時のスクリーンショットの取得処理をafter_teardownで行っていたのを、before_teardownで行うよう修正しています。テスト失敗からスクリーンショット取得までの時間を最小にする為(テスト失敗からスクリーンショット取得までにページが動的に変更される可能性を減らす為)。


Merge pull request #36040 from st0012/update-changelog-for-35145

actionview/CHANGELOG.mdの修正です。

collectionをpartial + cacheで繰り返しrenderした際にcollectionが正しくrenderされないバグがあったのを修正した、Fix partial caching ignore repeated items issueの対応についてCHANGELOGにエントリーを追加しています。


Don't table name qualify aggrigate column for virtual attribute

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

virtual attributeの為のaggrigate columnをテーブル名で修飾しないよう修正しています。


ActionCable: don't allowlist keys passed to the Redis initializer

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

Redis.newに指定するconfigの値について、明示的にkeyを指定して渡していたのを、Action Cableで使用するconfig(adapterchannel_prefix)をそのまま渡すよう修正しています。

redis-gem側の変更を意識しないで済むようにする為と、Redis Sentinelを使用出来るようにする為、との事です。


Avoid new string instance creation in InsertAll#execute

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

InsertAll#executeメソッドでString#+@を使用して不要なStringのインスタンスが生成されないよう修正しています。


Make Action Text's rendering helpers more configurable

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

ActionText::ContentHelper moduleでsanitizerや許可するtagを定数で管理していたのを、attributeに変更、及び、contentのsanitize処理をメソッドに切り出しています。アプリ側で設定を変更出来るようにする為。