なるようになるブログ

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

rails commit log流し読み(2018/09/14)

2018/09/14分のコミットです。

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

activerecord/CHANGELOG.md

activestorage/CHANGELOG.md


Update arguments.rb [ci skip]

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

IntrinsicInstrinsicにタイポしている箇所があったのを修正しています。


Merge pull request #33853 from yahonda/use_utf8mb4_only_if_available

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

サポートするMySQLのバージョンを5.1.10以上から5.5.8以上に修正しています。

Use utf8mb4 character set by default for MySQL database (#33608)でdefaultのcharsetがutf8mb4に変更されたのですが、5.1系ではutf8mb4は使用出来ないという問題がありました。その対応として、5.1系が使われた場合の対応を入れる事も検討されたのですが、5.1系は公式のサポートも切れているような古いバージョンなので、Railsも明確にサポートしないようになりました。

合わせて、innodb_large_prefixが有効になっていない古い環境(MySQLだと5.7.8以下)でcharsetが指定されていない場合にエラーをraiseするよう修正しています。indexのkey長の問題がある為。


Revert falsey with example reordered

activesupport/lib/active_support/core_ext/object/blank.rbのdocの修正です。

Object#blank?メソッドのdocの言い回しの修正、exampleの順番の調整等を行っています。


add any? to DatabaseConfigurations

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

ActiveRecord::DatabaseConfigurationsクラスでany?configurationsdelegateするよう修正しています。

Rails 6.0からActiveRecord::Base.configurationsが返す値がHashからActiveRecord::DatabaseConfigurationsクラスのインスタンスに変更になっているのですが、ActiveRecord::Base.configurations.any?という書き方をしている箇所がライブラリ(e.g. Spring)にあり、下位互換性の為にany?を使えるようにしています。


Merge pull request #33378 from numbata/subclass-redefine-autosave-callbacks

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

subclassで、associated recordsのautosave callbackを再定義出来るよう修正しています。

subclassで親に定義されているassociationを再定義 + accepts_nested_attributes_forを使用する、というような場合に正しく動作するようにする為。


SQLite3 adapter supports expression indexes

activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rbactiverecord/lib/active_record/connection_adapters/sqlite3_adapter.rbの修正です。

SQLite3 adapterにexpression indexesのサポートを追加しています。

例。

create_table :users do |t|
  t.string :email
end
 add_index :users, 'lower(email)', name: 'index_users_on_email', unique: true

Merge pull request #33569 from eric-hemasystems/conditional-flash-helper

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

Flash.add_flash_typesメソッドでhelper_methodを呼び出す前に、helper_methodが定義されているかチェックするよう修正しています。

helper_methodが無い状態(ActionController::Metalに直接ActionController::Flashをincludeして使う場合)でもエラーにならないようにする為。


Bump MySQL version requirement to 5.5.8 in the template files

railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml.ttrailties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.ttの修正です。

template fileに記載されているサポートしているMySQLのバージョンを5.5.8以上に修正しています。


Enable sudo only in the required build

.travis.ymlの修正です。

全てのbuildでsudoが有効になっていた(Sudo-enabled infrastructureが使われていた)のを、sudoが必要なbuildでだけsudoを有効化するよう修正しています。

Sudo-enabled infrastructureだとPostgresSQLのisolationテストがdiskの問題でエラーになってしまう為。


Make sure the flash method is defined even if helpers are not present

actionpack/lib/action_controller/metal/flash.rbactionpack/test/controller/flash_test.rbの修正です。

Merge pull request #33569 from eric-hemasystems/conditional-flash-helperのfollow upとして、helper_methodが無い状態でもflashのメソッドは呼べるよう対応しています。


Remove mysql2 gem version requirement "< 0.6.0"

activerecord/lib/active_record/connection_adapters/mysql2_adapter.rbrailties/lib/rails/generators/app_base.rbの修正です。

mysql2 gemを指定するのにバージョンロック(gem "mysql2", ">= 0.4.4", "< 0.6.0")を指定していたのを、上限の指定は外すよう修正しています。


SQLite3: Support multiple args function for expression indexes

activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rbactiverecord/lib/active_record/connection_adapters/sqlite3_adapter.rbの修正です。

SQLite3 adapterのexpression indexesに指定するfunctionに、引数を複数指定出来るよう修正しています。


Add :redis_cache_store to config.cache_store options [ci skip]

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

config.cache_store オプションに指定可能な値の一覧に:redis_cache_storeを追加しています。


Configure Active Storage route prefix

Active Storageの修正です。

Active Storageが生成するroutesのprefixが/rails/active_storage固定だったのを、任意のprefixを指定出来るよう修正しています。prefixはconfig.active_storage.routes_prefixで指定出来るようになっています。

config.active_storage.routes_prefix = '/files'
$ ./bin/rails routes -c active_storage
                   Prefix Verb URI Pattern                                                               Controller#Action
       rails_service_blob GET  /files/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET  /files/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET  /files/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT  /files/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST /files/direct_uploads(.:format)                                           active_storage/direct_uploads#create

rails commit log流し読み(2018/09/13)

2018/09/13分のコミットです。

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

actionmailer/CHANGELOG.md

activejob/CHANGELOG.md


Merge pull request #33832 from bogdanvlviv/follow-up-33756

Active Recordの修正です。

ActiveRecord::Base::filter_attributesを追加した、Merge pull request #33756 from piecehealth/prのfollow up対応として、各docの修正、共通処理のメソッドの切り出し、等を行っています。


Merge pull request #33854 from rigani/integer-multiple

activesupport/lib/active_support/core_ext/integer/multiple.rbの修正です。

Integer#multiple_of?メソッドで値のチェックに!=、及び、zero?を使用していたのを、どちらも==を使用するよう修正しています。==の方が高速な為。


Include test helpers when ActionDispatch::IntegrationTest is loaded

actionmailer/lib/action_mailer/railtie.rbactivejob/lib/active_job/railtie.rbの修正です。

ActionDispatch::IntegrationTestがデフォルトでActionMailer::TestHelperActiveJob::TestHelperをincludeするよう修正しています。

ActionDispatch::IntegrationTestはcontrollerが呼び出すすべての処理のテストを出来るようにする事を想定しており、mailやjob queueに対するassertもその一部なので、デフォルトでそれらに対するassertionが出来るようにする為にincludeするようになりました。詳細は説明はissue参照。


Add a test that exercice better the behavior we expect in the query cache

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

development env(eager loadがtrueになっていないenv)でも最初のrequestの実行前にquery cacheがloadされる事を確認するテストを追加しています。


Merge pull request #33860 from jhawthorn/faster_find_templates

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

ActionView::PathResolverでtemplateを検索するのに、ワイルドカードを使用するよう修正しています。

元々は、templateを取得するのに下記のようなコードが使用されていました。

Dir["app/views/users/show{.en,}{.html,.text,.js,.css,.ics,.csv,.vcf,.vtt,.png,.jpeg,.gif,.bmp,.tiff,.svg,.mpeg,.mp3,.ogg,.m4a,.webm,.mp4,.otf,.ttf,.woff,.woff2,.xml,.rss,.atom,.yaml,.multipart_form,.url_encoded_form,.json,.pdf,.zip,.gzip,}{}{.raw,.erb,.html,.builder,.ruby,}"]

これだとそれぞれのパターン(app/views/users/show.en.html.rawapp/views/users/show.en.html.erb等々)毎にファイルが存在するかのチェック処理が実行されてしまい、結果大量のsyscallsが実行されてしまっていました。

で、これを、下記のようにワイルドカードを使用するよう修正しています

Dir["app/views/users/show*"]

これによりsyscallの呼び出しが大量に軽減(PRにあるコメントだと、元が1682 syscallsだったのが、14 syscallsに軽減)し、高速にtemplateの検索が行われるようになっています。


Merge pull request #33844 from kamipo/too_many_eager_load_ids

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

大量のレコードを一度にロードしようとした際にActiveRecord::StatementInvalidのエラーになってしまうバグがあったのを修正しています。

Refactor Active Record to let Arel manage bind paramsでIN clauseでbind paramsが使われるようになったのですが、bind paramsの数には制限(mysql2 65535, pg 65535, sqlite3 250000)があり、それら以上の数のレコードをロードしようとすると、bind paramsの制限にひっかかりqueryの実行でエラーになるようになってしまいました。

それを避ける為、Arrayについてはbind paramsを使用しないよう修正しています。


Add changelog entries for #33849 [ci skip]

actionmailer/CHANGELOG.mdactivejob/CHANGELOG.mdの修正です。

ActionDispatch::IntegrationTestがデフォルトでActionMailer::TestHelperActiveJob::TestHelperをincludeするよう修正した対応のエントリーを追加しています。

rails commit log流し読み(2018/09/12)

2018/09/12分のコミットです。

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

activerecord/CHANGELOG.md

actionmailer/CHANGELOG.md


Use utf8mb4 character set by default for MySQL database (#33608)

Active Recordの修正です。

MySQL databaseのdefaultのcharsetがutf8からutf8mb4に変更になりました。

utf8 だと3-Byteなので、4-Byteの値(絵文字等)が扱えない為。なお、collationについては明示的には指定しないようになっています。


Merge pull request #33824 from yskkin/suppress_logging

actionmailer/lib/action_mailer/base.rbactionmailer/lib/action_mailer/log_subscriber.rbの修正です。

perform_deliveriesにfalseが指定されている場合に、mail deliver時のメッセージを変更(perform_deliveriesがfalseの場合、実際のメールの送信処理は行われないので、それがわかるメッセージに変更)、及び、deliver.action_mailer hookのpayloadにperform_deliveriesの値を追加しています。


Merge pull request #33785 from bogdanvlviv/follow-up-33773-33779

railties/CHANGELOG.mdの修正です。

test environmentのdefault cache storeを変更した対応のエントリーのフォーマット等の修正を行っています。


refacto: config_for with ActiveSupport::InheritableOptions and symbolized keys

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

config_forメソッドで、読み込んだファイルの値を、deep_symbolize_keysメソッドでkeyを全てSymbolに変換、及び、InheritableOptionsのオブジェクトに変換して、値をkey名のメソッドで取得出来るよう修正しています。


Move digest path calculation out of loop

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

collection cacheの生成処理で、ループの中で同じdigest pathを毎回生成していたのを、先にdigest pathを生成し、それを使い回すよう修正しています。


Merge pull request #33795 from marceloperini/marceloperini/33450-active-storage

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

Active StorageでserviceにS3を使用している、かつ、0バイトのファイルがアップロードした場合に、Aws::S3::Errors::InvalidRangeのエラーが発生してしまうバグがあったのを修正しています。


use "minitest" consistently

各doc、コード内のMinitestminitestに修正しています。小文字の"minitest"が正式名称な為。


Merge pull request #27792 from tjoyal/sandbox-tagged-logging

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

ActiveSupport::TaggedLogging.newが、引数に指定されたlogger objectの値を返していた(引数のlogger objectの値を直接変更していた)のを、新しいlogger instanceを生成して、そちらを返すよう修正しています。

newメソッドが引数のobjectの状態を変更して返す、newメソッドに期待する挙動と異なるだろう、ということで新しいinstanceを生成するようになりました。


Merge pull request #33483 from DylanReile/master

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

rails 内部のmail preview用controllerで、Content Security Policyを強制的に無効化するよう修正しています。

mailer previewで使用するJSが動作するようにする為。以前似たような対応行った筈だったのですが、対応足りて無かったか。


Merge pull request #33819 from kevindew/yarn-path

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

yarn::install taskで、yarnコマンドを実行する際に、カレントからの相対パスでコマンドを指定したいのを、Rails.rootからのパスを指定するよう修正しています。

rails engineでyarn::install taskが動作するようにする為。


Update action_mailer_basics.md [ci skip]

rails guideのAction Mailer Basicsの修正です。

Action Mailer Configurationの項のフォーマットの修正を行っています。


Update test_helper_test.rb

activejob/test/cases/test_helper_test.rbの修正です。

withwihtにタイポしている箇所があったのを修正しています。


Fix query cache to load before first request

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

最初のrequestの実行前にquery cacheがloadされるよう修正しています。

元々はActive Recordがloadされた際にquery cacheのloadを行うhookを実行するようになっていたのですが、それだと最初のrequestの際にquery cacheがloadされない状態になっていました(Acitve Recordのloadが最初のrequestの途中で行われる為)。

そのため、query cacheのloadをActive Recordがloadされた際にではなく、initializerで直接実行するようにして対応しています。

rails commit log流し読み(2018/09/11)

2018/09/11分のコミットです。

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

activesupport/CHANGELOG.md

activerecord/CHANGELOG.md


Update CHANGELOG.md

activerecord/CHANGELOG.mdの修正です。

inspectinsepctにタイポしている箇所があったのを修正しています。


Remove all references to slave in the codebase

railtiesのテストの修正です。

テストでmaster / slaveという言葉を使用していたのを、primary / replicaを使用するよう修正しています。

master / slaveは差別的な意味合いがある言葉の為。


Add #unfreeze_time to ActiveSupport::Testing::TimeHelpers

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

travel_backのaliasとしてunfreeze_timeメソッドを追加しています。

freeze_timeが既にあり、それと対称となるメソッドがあった方がわかりやすいだろう、ということで追加されています。


Merge pull request #33823 from stoodfarback/aj_backburner_fix_priority

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

Backburner adapterで、jobに指定したpriorityがbackend(Backburner::Worker)に渡されてなかったのを、渡すよう修正しています。


Oxford comma [ci skip]

activesupport/lib/active_support/testing/time_helpers.rbのdocの修正です。

travel_backメソッドのdocで、カンマのフォーマットをoxford commaに修正しています。


Use correct variable

railties/test/application/dbconsole_test.rbrailties/test/engine/commands_test.rbの修正です。

Remove all references to slave in the codebaseの対応で修正が漏れている箇所があったのを修正しています。


Fixes #33610

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

create_withメソッドにnested attributesを指定した場合に、重複したレコードが作成されてしまう(本来1レコードだけ作成される筈が2レコード作成されてしまう)バグがあったのを修正しています。


Remove unused existing arg in SelectManager#collapse

activerecord/lib/arel/select_manager.rbの修正です。

SelectManager#collapseメソッドから使用していないexisting引数を削除しています。


Remove redundant all.scoping

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

Scoping::Named#scopeメソッドから不要なscopingメソッドの呼び出しを削除しています。


Move scoping handling into klass level from relation

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

scopingメソッドの実装をRelationクラスからclass methodに移動しています。

後ほどinternal scopingの問題の解決の為に使用したい為、とのことです。


Don't expose current_scope for internal use

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

Scoping moduleのcurrent_scopecurrent_scope=メソッドの可視性をprivateに変更しています。内部でだけで使用するメソッドの為。


Refactor object creation from relation to avoid pushing scope attributes

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

Relationクラスの各オブジェクト生成処理でscope attributesの生成を行っていたのを、行わないよう修正しています。

元々はinheritance objectの生成処理に関するバグ修正の為にscope attributesの生成、及び、それらのattributeをklass.newに渡すようになっていたのですが、そちらの処理はInheritance moduleで行うようリファクタリングしています。


Don't expose instantiate_instance_of for internal use

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

Persistence moduleのinstantiate_instance_ofメソッドの可視性をprivateに変更しています。内部でだけで使用するメソッドの為。

rails commit log流し読み(2018/09/09)

2018/09/09分のコミットです。

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


Use visitor.compile instead of constructing by connection itself

Active Recordの修正です。

column nameを生成するのに独自メソッドを使用していたのを、visitorにあるその為のメソッド(#compile)を使用するよう修正しています。


Revert a writer for BindParam#value

Arel::Nodes::BindParam classのvalue attributesをattr_accessor -> attr_readerに変更しています。

元々Active Recordのテストを通す為にwriterを使用するようになっていたのですが、今は不要になっている為。


Consistently use visitor.compile

activerecord/lib/active_record/connection_adapters/abstract/database_statements.rbactiverecord/lib/arel/visitors/to_sql.rbの修正です。

SQLの生成処理にvisitor.compileを使用するよう統一しています。


Add missing perform_enqueued_jobs to error logging test

activejob/test/cases/logging_test.rbの修正です。

error loggingのテストでenqueuしたjobの実行処理が漏れていて、実際には何もテスト出来ていなかったのを修正しています。


Fix sqlite3 url config in "Configuring Rails Applications" guide

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

Connection Preferenceの項にあるconfig/database.ymlのexampleで、SQLite3のURLの指定に誤りがある箇所があったのを修正しています。


Fix explanation of ActiveRecord::Base.configurations.configs_for in the CHANGELOG

activerecord/CHANGELOG.mdの修正です。

ActiveRecord::Base.configurations.configs_forメソッドの引数の指定に誤りがある箇所があったのを修正しています。


Fix ActiveRecord::DatabaseConfigurations's docs

activerecord/lib/active_record/database_configurations.rbactiverecord/lib/active_record/database_configurations/hash_config.rbのdocの修正です。

DatabaseConfigurationsクラスの各メソッドのdocで、戻り値のクラスや、メソッド名の説明に誤りがある箇所があったのを修正しています。


clarifies documentation around the attempts arugment to retry_on

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

retry_onメソッドのdocに、attemptsはリトライの回数ではなく、ジョブが実行された回数の合計である旨説明を追加しています。

rails commit log流し読み(2018/09/08)

2018/09/08分のコミットです。

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

activesupport/CHANGELOG.md


Merge pull request #33615 from Larochelle/i18n_full_message_with_nested_attributes

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

ActiveModel::Errors#full_messageメソッドでi18n用のkeyを生成する際に、[\d]を除去するよう修正しています

index_errorsにtrueを指定している場合に[\d](e.g. activemodel.attributes.person/contacts[0]/addresses[0].street)が含まれてしい、index_errorsを指定してない場合と同じi18nの値を取得出来るようにする為。


Merge pull request #33804 from yskkin/num_string

activemodel/lib/active_model/type/helpers/numeric.rbの修正です。

Numeric#non_numeric_string?の値が数値の文字列かどうかの正規表現に誤りがあり、FloatやDecimalの値を正しく判定出来てなかったバグがあったのを修正しています。


trace autoloads, and document hints for troubleshooting


Merge pull request #33615 from Larochelle/i18n_full_message_with_nested_attributes

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

ActiveModel::Errors#full_messageメソッドでi18n用のkeyを生成する際に、[\d]を除去するよう修正しています

index_errorsにtrueを指定している場合に[\d](e.g. activemodel.attributes.person/contacts[0]/addresses[0].street)が含まれてしい、index_errorsを指定してない場合と同じi18nの値を取得出来るようにする為。


Merge pull request #33804 from yskkin/num_string

activemodel/lib/active_model/type/helpers/numeric.rbの修正です。

Numeric#non_numeric_string?の値が数値の文字列かどうかの正規表現に誤りがあり、FloatやDecimalの値を正しく判定出来てなかったバグがあったのを修正しています。


trace autoloads, and document hints for troubleshooting

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

Autoloadのtrace機能を追加しています。ActiveSupport::Dependencies.logger、及び、ActiveSupport::Dependencies.verboseを指定することが、Autoloadされたconstant及びそのconstantが定義されたファイルがlogに出力出来るようになっています。

ActiveSupport::Dependencies.logger = Rails.logger
ActiveSupport::Dependencies.verbose = true

User
autoloading: constant ApplicationRecord autoloaded from /rails/master/app/models/application_record.rb
autoloading: constant User autoloaded from /rails/master/app/models/user.rb

Don’t allocate array on no args

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

ActionView::Digestor.digestdependencies引数のデフォルト値を[]からnilに変更、及び、digestメソッドでdependenciesnilの場合に不要な処理を行わないよう修正しています。

不要なArrayの生成を減らせるようにする為。


reflect c03bba4 in CHANGELOGs

activesupport/CHANGELOG.mdguides/CHANGELOG.mdの修正です。

先のAutoloadのtrace機能の対応についてCHANGELOGにエントリーを追加しています。


supports_xxx? returns whether a feature is supported by the backend

Active Recordの修正です。

supports_advisory_locks?メソッドがconfigの値を返すようになっていたのを、使用しているbackendがadvisory lockを使用出来るかどうかを返すよう修正しています。