なるようになるブログ

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

rails commit log流し読み(2015/10/30)

2015/10/30分のコミットです。

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

activerecord/CHANGELOG.md

activesupport/CHANGELOG.md

actionview/CHANGELOG.md


Set scope.reordering_value to true if :reordering values are specified

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

associationをpreloadする際に、reorderメソッドが正しく実行されないバグがあったのを修正しています。

class Post < ActiveRecord::Base
  has_many :comments

  has_one :oldest_comment, -> { reorder(:created_at) }, class_name: 'Comment'
end

class Comment < ActiveRecord::Base
  default_scope { order('updated_at desc') }
end

上記のようなmodelがあった場合に、oldest_commentをpreloadした際にreorderが正しく実行されてなかったとの事です。


Fix merge conflicts from #19501

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

sti classのチェック処理のリファクタリングを行った、DRY up STI subclass logic by ccutrer · Pull Request #19501 · rails/railsの対応を手動をマージしています。

手動で対応しているのは、そのままマージするとテストがコケてしまうのと、一部スタイル調整を行う為、との事です。

ちなみに、別々のメソッドで行っていいたsti classのチェック処理を、一つのメソッド(find_sti_class)で行うようリファクタリングしています。


Fix test failures caused by #19501

activerecord/lib/active_record/inheritance.rbactiverecord/test/cases/adapters/mysql/schema_test.rbactiverecord/test/cases/adapters/mysql2/schema_test.rbの修正です。

先の対応で、subclassが見つからなかった場合に表示するエラーメッセージに使用するメソッドが誤っていたのを修正、及び、schemaのテストがコケるようになってしまっていたのを修正しています。

STIの影響でコケるようになってしまったらしく、STIを無効化にして対応しています。詳細わからなかったです。


Fix a stylistic nitpick in #19501

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

SubclassNotFoundをraiseする際のエラーメッセージを生成する際に、不要な文字列の連結を行っていたのを削除しています。


Merge pull request #16357 from gchan/hwia-respects-to-hash-default

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

HashWithIndifferentAccess.newメソッドが、引数のオブジェクトのto_hashが返すデフォルトのvalue及びprocを自身のオブジェクトに設定するよう修正しています。

元々は、元のデフォルトの値を反映させるには、HashWithIndifferentAccess.new_from_hash_copying_defaultメソッドを使う必要があったのですが、この対応によりnewメソッドの方で対応を行うように修正されています。

なお、new_from_hash_copying_defaultはもう不要とり、のちほどdeprecateになっています。


Properly indent '' and '' within ''

railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.ttの修正です。

デフォルトで生成されるapplication.html.erb<head>タグ及び<body>タグのインデントがおかしかった(親タグである<html>タグと同じインデントになっていた)のを修正しています。


Deprecate HWIDA.new_from_hash_copying_default

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

HashWithIndifferentAccess.new_from_hash_copying_defaultメソッドがdeprecateになりました。今後は、HashWithIndifferentAccess.newを使うだけでOKです。


Revert "Merge pull request #22026 from akihiro17/fix-preload-association"

associationをpreloadする際にreorderメソッドが正しく実行されないバグがあったのを修正した、Set scope.reordering_value to true if :reordering values are specified をrevertしています。buildが壊れてしまった為との事です。後ほど、テストを修正し再度コミットされています。


Merge pull request #22101 from gocardless/ar-pgbouncer-fix

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

standard_conforming_stringsを設定する際、SETで実行時パラメータに設定していたのを、pg_settingsテーブル自体の値を更新するよう修正しています。

-        old, self.client_min_messages = client_min_messages, 'panic'
-        execute('SET standard_conforming_strings = on', 'SCHEMA') rescue nil
-      ensure
-        self.client_min_messages = old
+        execute(<<-SQL, 'SCHEMA')
+          UPDATE pg_settings
+          SET setting = 'on'
+          WHERE name = 'standard_conforming_strings' AND context = 'user'
+        SQL

SETだと、一度onしたものを再度onにしようとするとエラーになってしまうのですが、PgBouncerを使用していた場合に、どこかのコネクションで一度standard_conforming_stringsの設定をoffに処理するが失敗してしまった場合に、以降ずっとstandard_conforming_stringsをonに出来なくなってしまうという問題があった為、直接テーブルを更新するようにしたとの事です。

が、これは後ほど再度修正されて、最終的にはSETを使う形に戻っています。


Revert "Revert "Merge pull request #22026 from akihiro17/fix-preload-association"" https://github.com/rails/rails/commit/857a34a41622300457c0a52885d53344a3e36505

associationをpreloadする際にreorderメソッドが正しく実行されないバグがあったのを修正した、Set scope.reordering_value to true if :reordering values are specified について、テストを修正し、再度コミットしています。


making selected value to accept Hash like the default option. E.g. selected: {day: params[:day].to_i, month: params[:month].to_id}

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

date_selectメソッドselectedオプションに、Hashを指定出来るように対応しています。

例。

<%= date_select "document", "created_at", {
      start_year: Date.today.year,
      end_year: Date.today.year - 5,
      prompt: { day: 'Please select day', month: 'Please select month'},
      selected: {
        day: params[:document]["created_at(3i)"].to_i,
        month: params[:document]["created_at(2i)"].to_i}
    }, class: 'form-control col-sm-3' %>

これは割と便利そう。


Fix style issues with #16252

actionview/CHANGELOG.mdactionview/lib/action_view/helpers/date_helper.rbの修正です。

CHANGELOGのentryを先頭に追加、及びif/else複数使用していた箇所を、case/whenを使用するよう修正しています。


Add CHANGELOG entry for #22101 [ci skip]

activerecord/CHANGELOG.mdの修正です。

standard_conforming_stringsを設定する際、SETで実行時パラメータに設定していたのを、pg_settingsテーブル自体の値を更新するよう修正した、Merge pull request #22101 from gocardless/ar-pgbouncer-fixについて、CHANGELOGに追記しています。


FixtureSet.fixture_class_names should have no default value

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

FixtureSet.fixture_class_namesのデフォルト値を設定しないよう修正しています。

-      self.fixture_class_names = Hash.new do |h, fixture_set_name|
-        h[fixture_set_name] = ActiveRecord::FixtureSet.default_fixture_model_name(fixture_set_name, self.config)
-      end
+      self.fixture_class_names = {}

set_fixture_classに存在しないidentifiersを指定した場合に、今のデフォルトのprocでは最終的に正しい値を返せない(正しくinsert出来ない値が返ってきてしまう)ので、正しくない値を返せないのであれば、デフォルト値として意味が無いだろう、という事で修正されたようです。


Add documentation for TimeWithZone #ago and #advance.

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

TimeWithZone#agoTimeWithZone#advanceメソッドにdocを追加しています。


Fix the example given in the documentation for TimeWithZone#-

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

TimeWithZone#-メソッドのdoc内のexampleに、実際の実行結果と異なる箇所があったのを修正しています。


Never pass nil to order

Active Recordの修正です。

orderメソッドnilが渡された場合の対応を削除しています。

orderメソッドnilを渡す事は普通無く、サポートする必要は無いだろう、という事で削除したとの事です。


Don't disable errors when turning standard_conforming_strings on

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

standard_conforming_stringsの設定処理を、再度SETで行うよう修正しています。

エラーが起きても無視する、という形になっています。

# before
def set_standard_conforming_strings
  old, self.client_min_messages = client_min_messages, 'panic'
  execute('SET standard_conforming_strings = on', 'SCHEMA') rescue nil
ensure
  self.client_min_messages = old
end

# after
def set_standard_conforming_strings
  execute('SET standard_conforming_strings = on', 'SCHEMA')
end

Merge pull request #19511 from larskanis/replace_const_conn_params

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

有効なconnection parametersをmodule内の定数として定義していたのを、libpqが提供しているメソッド(PGconn.conndefaults_hash)から値を取得するよう修正しています。


Remove unnecessary method

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

使用していないFunctional#set_response!メソッドを削除しています。


use dispatch instead of process to run requests though

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

TestCase#processメソッドで、requestを実行するのに、processメソッドではなくdispatchメソッドを使用するよう修正しています。

processメソッドだと、使う側でcontrollerにrequest、responseを設定する必要があるのですが、dispatchメソッドだと自動で設定してくれる為、との事です。


Merge pull request #13008 from ktheory/sanitize_order

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

ActiveRecord::QueryMethods#orderメソッドの引数にArrayを渡した場合に、サニタイズ処理を行うよう修正しています。

Tag.order(['field(id, ?)', [1,3,2]].to_sql
# => SELECT "tags".* FROM "tags"   ORDER BY field(id, 1,3,2)

Add a changelog entry for #13008

activerecord/CHANGELOG.mdの修正です。

先のサニタイズ処理について、CHANGELOGに追記しています。


switch to JSON.dump to avoid infinite recursion

actionpack/test/controller/test_case_test.rbの修正です。

test_headerメソッドで、request.headers.envjsonに変換するのに、to_jsonメソッドではなくJSON.dumpを使用するよう修正しています。

-      render plain: request.headers.env.to_json
+      render plain: ::JSON.dump(request.headers.env)

再帰が無限に行われるのを防ぐため、との事なのですが、詳細がちょっとわからなかったっです…。


ActionController::Base#process() now only takes an action name

actionpack/lib/abstract_controller/base.rbの修正です。

ActionController::Base#processメソッドの引数が、アクション名のみになりました。

-    def process(action, *args)
+    def process(action)

通常のアプリケーションでは、controllerで引数を使用する事は無い為、との事です。 ……が、後ほどすぐにrevertされています。


Ensure has_and_belongs_to_many works with belongs_to_required_by_default

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

belongs_to_required_by_defaultにtrueを設定していた場合に、has_and_belongs_to_manyが正しく動作しないバグがあったのを修正しています。

associationを生成する際にinverse_ofが動作しない為に、validationでひっかかってしまいエラーになってしまっていた為との事です。該当箇所では、required: falseを明示的に設定する事で回避しています。


Revert "ActionController::Base#process() now only takes an action name"

ActionController::Base#processメソッドの引数をアクション名のみに対応した、ActionController::Base#process() now only takes an action name をrevertしています。

revertコミットに説明が無いので、理由は不明です…。


Add version constraint to database gem in generated application

railties/lib/rails/generators/app_base.rbrailties/lib/rails/generators/rails/plugin/templates/%name%.gemspecrailties/lib/rails/generators/rails/plugin/templates/Gemfileの修正です。

Rails application / pluginを生成する際に作成されるGemfileで、使用するDB関係のgemのバージョンを固定するよう修正しています。

+        when "oracle"         then ["ruby-oci8", nil]
+        when "postgresql"     then ["pg", ["~> 0.18"]]
+        when "frontbase"      then ["ruby-frontbase", nil]
+        when "mysql"          then ["mysql2", [">= 0.3.18", "< 0.5"]]
+        when "sqlserver"      then ["activerecord-sqlserver-adapter", nil]
+        when "jdbcmysql"      then ["activerecord-jdbcmysql-adapter", nil]
+        when "jdbcsqlite3"    then ["activerecord-jdbcsqlite3-adapter", nil]
+        when "jdbcpostgresql" then ["activerecord-jdbcpostgresql-adapter", nil]
+        when "jdbc"           then ["activerecord-jdbc-adapter", nil]

gemのバージョンが上がってしまう事で、applicationが動かなくなってしまったりするのを防ぐ為、とのことです。