なるようになるブログ

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

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

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

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

activerecord/CHANGELOG.md


[ci skip] The find method coerces the given arguments to integer if the primary key is integer

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

[ci skip] Fix the documentation of ActiveRecord::FinderMethods#findで修正したto_iメソッドが呼ばれる条件の説明に誤りがあった(primary keyがintegerの場合にto_iが呼ばれるので元の説明が正しい)のを再度修正しています。


Removes unnecessary dot in regexp

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

TranslationHelper#html_safe_translation_key?メソッドの正規表現から、不要なドットを削除しています。


Reset dirty topics table for SerializedAttributeTest

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

SerializedAttributeTest実行時にtopics tableをリセットするよう修正しています。

topics tableの状態を変えてしまうテストがあり、その影響でSerializedAttributeTestがエラーになってしまうのを避ける為。


Avoid Topic.dup to prevent weird test failure

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

test_serialized_attribute_works_under_concurrent_initial_accessでテスト用にmodelをdupしていた(テスト内でattributeを変更しており他のテストに影響が出るのを避ける為)のを、Class.newでサブクラスを作成するよう修正しています。

dupしてクラスを作成すると何故かエラーになってしまう事がある為。


Remove unused requires

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

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


Merge pull request #33611 from willianveiga/feature/reselect-method

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

ActiveRecord::QueryMethodsselect statementを再指定する為のreselectメソッドを追加しています。 unscope(:select).select(fields)のショートハンド。

Post.select(:title, :body).reselect(:created_at)
# => SELECT `posts.created_at` FROM `posts`

Relax table name detection in from to allow any extension like INDEX hint

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

fromメソッドのテーブル名取得処理を修正し、from("comments USE INDEX (PRIMARY)")のようなINDEX hintを含むSQLも使用出来るよう修正しています。Rails 5.2まではこのような使い方が出来、元の挙動を維持する為。


Merge pull request #35431 from kamipo/enable_sql_cache_on_find

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

base classではfindメソッドでSQL statement cacheが有効になるよう修正しています。

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

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

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


Allow returning nil for default_scope

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

default_scopenilを返せるよう修正しています。


Refactor generated_relation_methods to remove duplicated code on ClassSpecificRelation

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

ClassSpecificRelation module内の重複コードを消せるようgenerated_relation_methodsメソッドをリファクタリングしています。


add note about idle connection reaper to 5.2 release notes

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

idle状態のコネクションの扱いが変更になった対応(Flush idle database connections)についてrelease noteにエントリーを追加しています。


Use dedicated Topic model for SerializedAttributeTest

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

serialize attributeのテストで他のテストと同じmodelを使用していたのを、専用のmodelを使用するよう修正しています。テスト内でattributeを変更しており、それが他のテストに影響しないようにする為。


[ci skip] Fix the documentation of ActiveRecord::FinderMethods#find

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

ActiveRecord::FinderMethods#findメソッド内のRecordNotFoundActiveRecord::RecordNotFoundに修正、及び、to_iメソッドが呼ばれる場合の説明に誤りがあったのを修正しています。が、説明については元の内容が正しかった為、後ほど再修正されています。

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

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

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

activerecord/CHANGELOG.md


fixes different count calculation when using size manual select with DISTINCT

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

selectDISTINCTを指定 + countを使用した場合に、relationがload済みかどうかで結果が変わる(relationがloadされていない場合、select部分が無視されたcountが実行される)バグがあったのを修正しています。


[ActiveStorage] Ensure that the _blob association is properly loaded when attaching ::One

activestorage/lib/active_storage/attached/changes/create_one.rbの修正です。

has_one_attachedメソッドを使用している場合に、attachment save時にxx_blob associationが正しくロードされないバグがあったのを修正しています。


Fix random CI failure due to non-deterministic sorting order

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

assertionで値を比較する際に、値をidでsortしてからチェックするよう修正しています。 sortを指定しないと結果が不定になる為。


Test that no datetime precision isn't truncated on assignment

activerecord/test/cases/date_time_precision_test.rbactiverecord/test/cases/time_precision_test.rbの修正です。

値指定時にdatetime precisionが切り捨てられない事を確認するテストを追加しています。


Don't cache find_by statements on STI subclasses

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

STIのサブクラスでfind_by文をキャッシュしないよう修正しています。

type IN (?,?,?,?)部分が動的に変わる(サブクラス毎に変わる)為。


Merge pull request #35411 from rails/pass-locals-to-template

Action Viewの修正です。

ActionView::Template#initializeの引数にlocalsを指定するのを必須にするよう修正しています(この時点では互換性の為に値を指定しないでも問題無いようになっていま)。

No nil format on templates や、Template has one variant と同様の対応で、template object生成時に必要な値を全て保持出来るようにする為。


Fix code examples' indentation in ActionDispatch::Routing [ci skip]

actionpack/lib/action_dispatch/routing.rbのdocの修正です。

ActionDispatch::Routing moduleのdoc内のexampleコードのインデントがおかしくなっていたのを修正しています。


Add negative scopes for all enum values (#35381)

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

enum定義時にnegative scope(not_xxx)も定義するよう修正しています。

class Post < ActiveRecord::Base
  enum status: %i[ drafted active trashed ]
end

Post.not_drafted # => where.not(status: :drafted)
Post.not_active  # => where.not(status: :active)
Post.not_trashed # => where.not(status: :trashed)

:scissors:

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

不要なスペースを削除するよう修正しています。


Fix preload with nested associations

activerecord/lib/active_record/associations/preloader/through_association.rbの修正です。

through associationのpreload処理で、middle recordがnilの場合にassociationのreset処理を行わないよう修正しています。

nestしたassociationをpreloadする際にmiddle recordがnilになってしまう事があり、その場合にエラーになってしまうのを避ける為。


Run activesupport's memcache store tests on Buildkite

Active Supportの修正です。

テストの際に使用するmemcache serverをMEMCACHE_SERVERS env経由で指定するよう修正しています。memcache storeに関するテストがBuildkite上で動作するようにする為。


Fix test that was broken by adding a default scope to an existing model

Active Recordのテストの修正です。

先の、Fix preload with nested associationsの修正の際に、既存のmodelのdefault_scopeを追加したのですが、その影響で既存のテストが壊れてしまったので、default_scopeを指定する為のmodelを既存のmodelとは別に定義するよう修正しています。


Fix random CI failure due to non-deterministic sorting order

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

assertionで値を比較する際に、値をidでsortしてからチェックするよう修正しています。 sortを指定しないと結果が不定になる為。


Fix indentation [ci skip]

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

インデントがずれてしまった箇所があったのを修正しています。

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

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

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

activerecord/CHANGELOG.md


Update READ_QUERY regex

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

read queryかどうかのチェック用の正規表現で、先頭のスペースや括弧(()に対する考慮が不足していたのを修正しています。(SELECT ...) UNION (SELECT ...)のようなqueryが正しくread queryと判定されるようにする為。


Fix prepared statements caching to be enabled even when query caching is enabled

Active Recordの修正です。

query cachingが有効になっている場合に、prepared statementのcacheが使用されないバグがあったのを修正しています。


Refactor type_to_sql to handle converting limit to size in itself

Active Recordの修正です。

type_to_sqlメソッドで変換後のsizeで処理を行うようリファクタリングしています。


Allow custom cache-control header in AC::Live

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

ActionController::Liveで強制的にCache-Control headerを指定していたのを、headerが設定済みの場合は指定せず元の値をそのまま使用するよう修正しています。


Depend on bootsnap 1.4.1

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

新規に作成したアプリケーションで使用するbootsnapのバージョンを1.4.1以上に修正しています。

Zeitwerk autoloadersを使用している、かつ、frozen_string_literalを有効にしている場合に、FrozenErrorがclass load時に発生してしまうという問題があり、bootsnap 1.4.1でその問題が対応されている為。


Fix class name in the documentation [ci skip]

actionmailer/lib/action_mailer/mail_delivery_job.rbのdocの修正です。

doc内でmail delivery jobのclass名が誤っていたのを修正しています。


Merge pull request #35404 from rails/no-nil-format-on-templates

Action Viewの修正です。

ActionView::Template#initializeの引数にformatを追加しています。この時点では互換性の為に値を指定しないでも問題無いようになっていますが、今後はActionView::Templateインスタンスを生成する際に、formatの指定が必須になります。

これにより、templateが必ずformatを保持する状態になる為、処理の途中でのformatの値の変化、及びformatnilかどうかのチェックが削除出来るようになっています。


Merge pull request #35406 from rails/template-has-one-format

Action Viewの修正です。

元々ActionView::TemplateformatをArrayで複数保持していたのですが、先のActionView::Template#initializeの引数にformatを追加した対応でformatは一つのみ保持するようになった為、その変更に合わせて各箇所を修正しています(formats.firstしていた箇所をformatを参照するよう修正等)。


Improve Template#inspect output (#35407)

actionview/lib/action_view/template.rbの修正です。

ActionView::Template#inspectメソッドの出力内容を改善し、class名、localsの値が表示されるよう修正しています。


Preparing for 6.0.0.beta2 release

各ライブラリのバージョンを6.0.0.beta2に更新しています。


Merge pull request #35408 from rails/template-has-one-variant

Action Viewの修正です。

ActionView::Template#initializeの引数にvariantを追加、及び、元々variantをArrayで保持していたのを一つのみ保持するよう修正しています。実際は一つの値のみしか保持する事は無かった為。


[ci skip] Renamed formats -> format in test after #35406

actionview/test/template/text_test.rbの修正です。

テスト対象のメソッド名とテスト名が一致していなかったのを修正しています。

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

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

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

activerecord/CHANGELOG.md


[ci skip] activejob typo fix.

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

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


Ensure update_all series cares about optimistic locking

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

update_allupdate_counters等のメソッドでoptimistic lockingに関するケア(lock columnの更新処理)を行うよう修正しています。


activemodel typo fix.

activemodel/test/cases/validations/conditional_validation_test.rbの修正です。

falsefelseに、combiningconbiningにタイポしていたのを修正しています。

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

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

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


minor grammar fix [ci skip]

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rbのdocの修正です。

add_referenceのメソッドのdoc内のタイポを修正しています。


Let Zeitwerk autoloaders inflect with Active Support

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

Zeitwerk autoloadersでclassをロードする際に、ActiveSupport::Inflectorで指定した内容がロード時に使用されるよう修正しています。


More exercise tests for distinct count with group by

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

distinct count + group byのテストを追加しています。


Add test case for unscope with unknown column

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

unknown columnに対してunscopeした場合のテストを追加しています。


Make this test stronger

railties/test/application/zeitwerk_integration_test.rbの修正です。

Zeitwerk autoloaders + ActiveSupport::Inflectorのテストで、定数が期待通りにロードされている事も確認するよう修正しています。


Merge pull request #35382 from janko/restore-io-copy-stream-compatibility-with-uploaded-file

actionpack/lib/action_dispatch/http/upload.rbの修正です。

ActionDispatch::Http::UploadedFile#to_ioメソッドをtempfileのto_ioを呼び出すよう修正しています。

元々はtempfileへのalasとして定義されていたのですが、Add implicit to path conversion to uploaded fileActionDispatch::Http::UploadedFile#to_pathメソッドが定義された事により、IO.copy_streamActionDispatch::Http::UploadedFileインスタンスを定義できない(IO.copy_streamto_pathが定義されているとto_ioを呼び出す、かつ、to_ioがIOのインスタンスであることを期待するが、tempfileは違う)という問題が発生していました。

元の挙動通り、IO.copy_streamインスタンスを指定出来るようにする為に上記修正を行っています。


How to use select is updated [ci skip]

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

Selecting Specific Fieldsの項に、selectの引数にSymbolを指定した場合のexampleを追加しています。


Disable available locale checks in Action Test test

actiontext/test/test_helper.rbの修正です。

Action Textのテストで、I18nのlocale追加時のlocale checkを行わないよう修正しています。

locale checkが有効化されていると、available locale初期化後にlocaleの追加が出来なくなってしまい、テスト内で動的にlocaleを追加する、という事が出来なくなってしまう為。


More exercise string attribute predicate tests for falsy strings

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

string attribute + predicate methodのテストで、falsy stringsを指定した場合のテストを追加しています。


Add test case for unscope with merge

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

unscopemergeを一緒に使用した場合のテストを追加しています。


Remove duplicated protected params definitions

Active Recordの修正です。

テストでprotected paramsが必要な際に、テスト毎に独自のクラスを定義していたのを、support/stubs/strong_parametersを共通で使用するよう修正しています。

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

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

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


[ci skip] Updated migration version to 6.0 in the getting started docs

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

migrationファイルのバージョンを5.0 -> 6.0に修正しています。

基本的にはguideのmigrationファイルのバージョン更新のPRは受け付けてないのですが、今回はGetting Started guideでの修正なのでマージしたとの事です。


Pass lookup context to the layout handlers

actionview/lib/action_view/layouts.rbactionview/lib/action_view/renderer/template_renderer.rbの修正です。

layout handlersのメソッドの引数にlookup_contextを追加しています。

lookup_contextの呼び出し回数を減らす為の対応を行っており、その一環との事です。


update callbacks skipping methods in guide [ci skip]

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

Skipping Callbacksの項にあるcallbackをskipするメソッドの一覧に誤りがあったのを修正しています。


Ensure that rendered templates always have a format

Action Viewの修正です。

元々formatを保持していなかったRenderedCollection::EmptyCollection classでもformatを保持するよう修正しています。

これにより、rendered templateが必ずformatを保持する状態になり、処理の分岐を削除 + lookup_contextの呼び出しを減らせるようになっています。


Skip test_select_with_subquery_in_from_uses_original_table_name on CI

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

Skip test_select_with_subquery_in_from_uses_original_table_name on Buildkite as wellでBuildkite上でもtest_select_with_subquery_in_from_uses_original_table_nameが実行されないようにする為に、ENV["BUILDKITE"]がある場合はテストをskipするようにしたのですが、それが期待通りに動作しなかった為、ENV["CI"]を見るよう修正しています。


Make test_select_with_subquery_in_from_uses_original_table_name work with old SQLite3

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

test_select_with_subquery_in_from_uses_original_table_nameがCIで通らなかったのは、古いSQLiteではsubquery flatteningにより正しいカラム名が取得出来ない、という問題があった為のようでした。

という訳で、subqueryにDISTINCTを追加してsubquery flatteningが行われないようにして対応しています。

参考:The SQLite Query Optimizer Overview


Merge pull request #35374 from rails/cache-inline-templates

Action Viewの修正です。

developmentで、inline templatesのrener処理でメモリリークが発生してしまっていたのを修正しています。

通常のテンプレートと異なりinline templateはcompile後の状態でのキャッシュが行えず、処理を行う度に正しく解放されないオブジェクトが生成されてしまっていました。で、これを避ける為に、inline templateについてはfinalizerを定義しrender時に使用したメソッドを削除し、オブジェクトの解放が正しく行われるよう修正しています。


Upgrade Zeitwerk to 1.3.1

activesupport/activesupport.gemspecの修正です。

Zeitwerk gemのバージョンを1.3.1に更新しています。