なるようになるブログ

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

rails commit log流し読み(2017/07/18)

2017/07/18分のコミットです。

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

activerecord/CHANGELOG.md

railties/CHANGELOG.md


Allow multiparameter assigned attributes to be used with text_field

Active Model, Active Recordの修正です。

multi-parameter assignmentで構築された値(e.g. written_on(1i)written_on(2i)written_on(3i))をtext_fieldhidden_fieldで使用出来るよう(使用した値をARで受け取れるよう)修正しています。


Merge pull request #27662 from nhorton/patch-1

rails guideのRails Routing from the Outside Inの修正です。

Adding Member Routesの項にあるroutes helperのexampleで、メソッド名に誤りがあったのを修正しています。


Scrub the invalid paramter value before using it in the error

actionpack/lib/action_dispatch/request/utils.rbの修正です。

paramsのencodingが不正な値だった場合のエラーの、エラーメッセージの修正、及び、対象のparamsscrubした値をメッセージに含むよう修正しています。

-            raise Rack::Utils::InvalidParameterError, "Non UTF-8 value: #{params}"
+            raise Rack::Utils::InvalidParameterError, "Invalid encoding for parameter: #{params.scrub}"

不正なencodingが含まれたままエラーメッセージを生成しようとすると、エラーメッセージ生成の際に違うEncodingエラーが発生してしまう為、scrubメソッドを使用して不正なバイト列を削除しています。


Add bootsnap to default Gemfile

railtiesの修正です。

デフォルトで生成されるGemfilebootsnapを含むよう修正しています。

config/boot.rbでbootsnapのsetup処理がrequireされるようになっています。


Merge pull request #29788 from kamipo/remove_unused_mutex_m

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

不要なmutex_mのrequireを削除しています。


Persistence#delete should not be affected by scoping

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

Persistence#deleteメソッドがscopingの影響を受け無いよう修正しています。


Fix where with a custom table

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

custom tableを使用している場合に、whereメソッドがエラーを返してしまうバグがあったのを修正しています。


Fix HashWithIndifferentAccess#default when include?(nil)

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

HashWithIndifferentAccessのデフォルト値にブロックを与えている時に、HashWithIndifferentAccess#defaultメソッドを引数無しで呼び出した場合、Hash#defaultメソッド同様にnilを返すよう修正しています。


Merge pull request #25564 from brewski/serializable_hash_fix

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

ActiveRecord::Serialization#serializable_hashにfreezeした値を渡した際に正常に動作する事を確認するテストを追加しています。


Merge pull request #29799 from kamipo/remove_outdated_test_scoped_responds_to_delegated_methods

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

RelationArrayarel_tabledelegateする事を確認するテストを削除しています。

Arrayへのdelegateはもう行われていない、delegateを確認する為に使われているメソッド(insertupdate等)はRelationに定義されており、もう意味が無いため削除したとの事です。


Merge pull request #29801 from kamipo/extract_null_relation_test

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

NullRelationに関するテストを別ファイルに切り出しています。


Rename helper to force_lazy_load_hooks.

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

load hookを実行する為だけのメソッドの名前をlazy_loadからforce_lazy_load_hooksに修正しています。


support - as an argument to rails runner

railties/lib/rails/commands/runner/runner_command.rbの修正です。

rails runnerコマンドで-引数を指定出来るようにしています。-を指定した場合はstdinから値を読むこむようになります。

# bin/count_users.rb
puts User.count

上記のようなスクリプトがあった場合に、cat bin/count_users.rb | bin/rails runner - のような使い方が出来るようになっています。

docker使っている場合に、cat script.rb | docker exec -it <container_id> rails runner -みたいな使い方が出来るなら便利かもですねえ。


[ci skip] CodeTriage badge

README.mdの修正です。

READMEにCodeTriageのbadgeを追加しています。


Merge pull request #29828 from kamipo/fix_using_custom_table_with_joins

Active Recordの修正です。

custom tableを使用しているmodelにjoin処理を行った場合に、エラーになってしまうバグがあったのを修正しています。


Remove incorrect comment

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

ActiveRecord::AttributeSetattributesがprotectedになっている理由は、privateにしてしまうとRuby 2.2系でwarning(private attribute?)が出てしまう為とのコメントがあったのですが、ここではprotectedにしているのは左記とは別の理由のため、該当のコメントを削除しています。


Remove deprecated code concerning dirty methods in after callbacks

Active Recordの修正です。

deprecatedになっていたafter callbacksでdirty methodsを使用した場合の為の処理を削除しています。


Remove deprecated code concerning non-attributes and *_will_change!

activerecord/lib/active_record/attribute_methods/dirty.rbactiverecord/lib/active_record/attribute_mutation_tracker.rbの修正です。

deprecatedになっていたattribute_will_change!にvirtual attributesを指定した場合の処理を削除しています。

rails commit log流し読み(2017/07/17)

2017/07/17分のコミットです。

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

activesupport/CHANGELOG.md

activerecord/CHANGELOG.md


Fix code formatting for QueryMethods#select

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

QueryMethods#selectメソッドのdocのフォーマットの修正を行っています。


explain why require_relative is not used here [ci skip]

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

retrieve_store_classメソッドでrequire_relativeではなくrequireを使用している理由についての説明を追加しています。


Fix root not being defined on Travis.

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

TestUnitReporter#app_rootメソッドでRails.rootを取得する前に、Railsrootメソッドが定義されているか確認するよう修正しています。

railsの各コンポーネントのテストを実行する際、Rails moduleは定義されているが、rootメソッドが定義されていない事がありえる為。


Merge pull request #29819 from kirs/frozen-activemodel

Active Modelの修正です。

各ファイルの先頭にfrozen_string_literal: trueを追加しています。


Set RAILS_ENV before load application file

railties/lib/rails/commands/dbconsole/dbconsole_command.rbの修正です。

require_application_and_environment!メソッドを呼び出す前に

ENV["RAILS_ENV"]に値を設定するよう修正しています。RAILS_ENVに値を設定する前にconfig/applicationを読み込みむとデフォルトのenv(development)のファイルが読み込まれてしまう為。


Enable Layout/FirstParameterIndentation cop

.rubocop.ymlLayout/FirstParameterIndentation copを追加、及び各ファイルにcopを反映しています。


Remove useless arel_engine

Active Recordの修正です。

不要なarel_engineメソッドを削除しています。

元々、arel_engineメソッドはFinderMethods#raise_record_not_found_exception!メソッドでのみ使用していたのですが、engineではなくklass.connectionを渡しても正常に動作する為、削除したとの事です。


Merge pull request #28077 from Erol/make-reverse-merge-bang-order-consistent

activesupport/lib/active_support/core_ext/hash/reverse_merge.rbactivesupport/lib/active_support/hash_with_indifferent_access.rbの修正です。

Hash#reverse_merge!メソッドの結果のオーダーが、Hash#reverse_mergeHashWithIndifferentAccess#reverse_merge、#reverse_merge!`と異なっていたのを、全て同じオーダーになるよう修正しています。

# before
ab = {a: 1, b: 2}
c = {c: 3}

indifferent_ab = HashWithIndifferentAccess.new(a: 1, b: 2)
indifferent_c = HashWithIndifferentAccess.new(c: 3)

ab.reverse_merge(c) #=> {c: 3, a: 1, b: 2}
ab.reverse_merge!(c) #=> {a: 1, b: 2, c: 3}
indifferent_ab.reverse_merge(c) #=> {c: 3, a: 1, b: 2}
indifferent_ab.reverse_merge!(c) #=> {c: 3, a: 1, b: 2}

# after
ab.reverse_merge!(c) #=> {c: 3, a: 1, b: 2}

Post.joins(:users) should not be affected by User.current_scope

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

Relation#joinsでtarget modelのcurrent_scopeに影響を受けないよう修正しています。ただ、unscopedの情報については使用されるようになっています。


Add documentation for class_attribute options

activesupport/lib/active_support/core_ext/class/attribute.rbのdocの修正です。

class_attributeメソッドのdocにオプションについての説明を追加しています。

rails commit log流し読み(2017/07/16)

2017/07/16分のコミットです。

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

railties/CHANGELOG.md


Remove redundant instances of the word ‘simply’ from documentation, on the basis that if it was simple, the user wouldn’t be reading the docs

rails guideのA Guide to Testing Rails Applicationsの修正です。

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


Merge pull request #29770 from y-yagi/fix_boolean_column_migration_script

Active Recordの修正です。

SQLite 3 adapterでbooleanの値を保持するのにStringのt / fを使用していたのを、Integer(1 / 0)を使用するよう移行する為のスクリプトにタイポがあったのを修正しています。


Merge pull request #29769 from kamipo/remove_extra_order_for_first

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

テストでレコードの最初の行を取得するのに、all.merge!(order: "id").firstorderしていたのを、単純にfirstを使用するよう修正しています。

Made `first` finder consistent among database engines by adding a default order clause からRelation#firstはprimary keyでorderした値を返すようになっており、明示的にorderする必要は無い為。


Merge pull request #29679 from kamipo/add_test_case_for_27724

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

associationに既に存在しているconditionを上書き出来る事を確認するテストを追加しています。


Remove blank lines in setup

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

bin/setupbin/yarnを実行する処理の後に不要な空行があったのを削除しています。


Do not generate unused components contents in app:update task

railtiesの修正です。

app:update taskで、使用していないコンポーネントに関するファイル、configは生成しないよう修正しています。

元々はアプリの状態に関わらず全てのファイル、configを生成するようになっていたのですが、例えば--skip-action-cableを指定して作成したアプリの場合、Action Cablenに関するファイル、configは生成しないようになります。


Merge pull request #29771 from kamipo/fix_create_with_using_both_string_and_symbol

Active Recordの修正です。

create_withに同じkeyをString、Symbolでそれぞれで指定した場合に、後から設定した値が正しく反映されないバグがあったのを修正しています。


Merge pull request #29809 from kamipo/remove_unused_ivars

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

ActiveRecord::Relation#resetメソッドから使用していない変数を削除しています。


Remove unused requires

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

使用していないファイルのrequireを削除しています。


Set represent_boolean_as_integer via configuration

Active Recordの修正です。

SQLite 3 adapterでbooleanの値を保持するのに、String又はIntegerを使用するかを指定する為のフラグをconfig経由で指定出来るよう修正しています。

元々はActiveRecord::ConnectionAdapters::SQLite3Adapterのattributeを直接参照するようになっていたのですが、ActiveRecord::ConnectionAdapters::SQLite3Adapterはinitiaiizer実行時点ではloadされておらず、initiaiizerから値が設定出来ない為、config経由で値を指定出来るようにしています。


add helper method for explicit lazy load

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

lazy load hookを呼び出す為のヘルパーメソッドを追加しています。


Fix create_with with multiparameter attributes

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

create_withメソッドにmultiparameter attributes(date型のwritten_on(1i)written_on(2i)等)を指定出来るよう修正しています。


Don’t cache scope_for_create

Active Recordの修正です。

scope_for_createの結果をcacheしていたのを、cacheしないよう修正しています。通常の処理でcacheされたscope_for_createが使われる事は無く、cacheする意味が無いため。


Allow to pass a connection to the dbconsole command

railties/lib/rails/commands/dbconsole/dbconsole_command.rbの修正です。

dbconsoleコマンドでconnection name(DB configファイルの3階層目の値)を指定出来るよう修正しています。

bin/rails dbconsole -c replicaのように、-cオプション経由で指定出来るようになっています。


Deprecate environment as an argument for dbconsole and console

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

dbconsoleconsoleコマンドのregular argumentとしてenvironment’s nameを指定するのがdeprecateになりました。今後はenvironment’s nameを指定するのに必ず-eオプションを指定する必要があります。

# before
$ bin/rails dbconsole production

# after
$ bin/rails dbconsole -e production

Properly expand the environment’s name

railtiesの修正です。

dbconsoleconsoleコマンドの-eオプションでenvironment’s nameを指定する際に、自動で補完(prodと指定した場合、productionにしてくれる)するよう修正しています。


Replace Erubis with Erubi in a part of guide [ci skip]

rails guideのThe Rails Initialization Processの修正です。

Railsが依存しているgemの一覧にerubisが残っていたのを、erubiに修正しています。

rails commit log流し読み(2017/07/15)

2017/07/15分のコミットです。

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


Fix doc format for ActiveSupport::Testing::TimeHelpers [ci skip]

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

ActiveSupport::Testing::TimeHelpers moduleの各メソッドのdocのフォーマットの修正を行っています。


Remove useless aliased_table_name in JoinDependency

Active Recordの修正です。

JoinDependencyクラスから不要なaliased_table_nameメソッドを削除しています。 tableメソッドの方を使用すれば良い為、との事です。


Remove encoding utf-8 magic comment

activesupport/test/multibyte_grapheme_break_conformance_test.rbactivesupport/test/multibyte_normalization_conformance_test.rbの修正です。

ファイルのエンコーディング用マジックコメント(encoding: utf-8)を削除しています。

rails commit log流し読み(2017/07/14)

2017/07/14分のコミットです。

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


[ci skip]Remove options of javascript_link_tag and stylesheet_link_tag in guides

rails guideのAction View Overviewの修正です。

javascript_pathstylesheet_include_tagメソッドについての説明から既に存在しないオプションについての説明を削除しています。

rails commit log流し読み(2017/07/13)

2017/07/13分のコミットです。

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


Response#charset= uses default_charset when nil is passed

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

Response#charset=nilを渡した場合、utf-8が設定される旨説明が記載されたいたのですが、実際はdefault_charsetが使用されるので、その旨docを修正しています。


Merge pull request #29772 from y-yagi/allow_to_edit_secrets_in_mutiple_applications_at_the_same_time

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

encrypted secretsをeditする際に使用する一時ファイル名にプロセスIDを加えるよう修正しています。元々は固定の文字列(secrets.yml.enc)だった為、複数のRailsアプリで同時にedit出来なかった為、プロセスIDを加えてファイル名がバッティングしないようにしています。

rails commit log流し読み(2017/07/12)

2017/07/12分のコミットです。

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

activerecord/CHANGELOG.md


Fix english for the rescue_from warning [ci skip]

rails guideのAction Controller Overviewの修正です。

rescue_fromについての注意文言の言い回しを修正しています。


Merge pull request #29748 from koic/enable_layout_empty_line_after_magic_comment

rubocop.ymlLayout/EmptyLineAfterMagicCommentのcopを追加、及び各ファイルに設定を反映しています。


Merge pull request #29746 from kamipo/extract_fake_klass

activerecord/test/cases/relation/mutation_test.rbactiverecord/test/cases/relation_test.rbの修正です。

relation_test.rbrelation/mutation_test.rbそれぞれで定義していたテスト用のクラス(FakeKlass)が、内容がほぼ同じだった為、共通で使用出来るよう別ファイルに切り出しています。


normalize_path used to be nil tolerant

actionpack/lib/action_dispatch/journey/router/utils.rbの修正です。

Router::Utils.normalize_pathの引数にnilを指定した場合NoMethodErrorが発生してしまうバグがあったのを修正しています。


Fix configuring third-party cache stores such as ActiveSupport::Cache::RedisStore

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

retrieve_store_classメソッドでcache store classのファイルを読み込む際、require_relativeを使用していたのをrequireを使用するよう修正しています。

require_relativeだとthird-partyのライブラリの読み込みに失敗する為。


Merge pull request #29699 from lugray/represent_boolean_as_integer

Active Recordの修正です。

SQLite 3 adapterでbooleanの値を保持するのにStringのt / fを使用していたのを、Integer(1 / 0)を使用するよう修正しています。

1と0はSQLite側でも true / falseと認識する為(t / fではtrue / falseとは認識されない)。

元々t / fで保持していた値は、手動で移行する必要があります(e.g. ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1))。デフォルトではt / fを使用するようになっており、Integerで値を保持するようにしたい場合は、ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integertrueを指定すればOKです。