なるようになるブログ

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

rails commit log流し読み(2015/07/21)

2015/07/21分のコミットです。

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

activerecord/CHANGELOG.md


Correctly ignore mark_for_destruction without autosave

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

mark_for_destructionautosaveオプションにtrueが設定されている場合のみ動作する、とドキュメントに記載してあるのですが、実際はautosaveオプションの値に関わらずmark_for_destructionが動作してしまっていました。autosaveが設定されてない場合は動作しないよう修正しています。


Fix state being carried over from previous transaction

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

new_record?メソッドが、新規レコードではないのにtrueを返してしまうバグがあったのを修正しています。

例。nameアトリビュートが必須パラメータという場合。

# before
author = Author.create! name: 'foo'
author.name = nil
author.save        # => false
author.new_record? # => true

# after
author = Author.create! name: 'foo'
author.name = nil
author.save        # => false
author.new_record? # => false

Transactionsモジュールのwith_transaction_returning_statusメソッドで最終のtransaction statusをちゃんと返せていなかったのが問題だったようです。


Fix minor typo in test name

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

array型のテストのテスト名にタイポがあったのを修正しています


Fix minor typo in testing guide

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

Taking it furtherの項、editting -> editingにタイポを修正しています。


Ensure that microsecond precision is only used for version of mysql that support it. Fixes #19711

ActiveRecordのconnection adapterの修正です。

MySQLではmicrosecond precisionが使えるようになったのはv5.6.4 以降なのですが、Rails内部ではminorバージョンまでのチェックしかしておらず、 v5.6.3でエラーになってしまっていた為patchバージョンもチェックするよう修正しています。

同様に、indexのrename機能が使えるかどうかのチェックも厳密に行うよう修正しています。


titleizing the New Link

railties/lib/rails/generators/erb/scaffold/templates/index.html.erbの修正です。

generatorがデフォルトで生成するindex.html.erbの新規ページ作成用リンクのテキストをhuman_nameからsingular_table_name.titleizeを使用するよう修正しています。

他のページではsingular_table_name.titleizeを使用するようになっており、合わせる為にこちらも変更したとの事です。


Merge pull request #20751 from repinel/remove-unnecessary-dup

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

Mapper#add_routeメソッドから、不要なactiondup処理を削除しています。

actiondupを行っていたものの、移行の処理で、値が変更してからは参照される事が無い為、別にdupする必要性は無いだろう、という事で削除されたようです。


Deprecate and rename the keys for association restrict_dependent_destroy https://github.com/rails/rails/commit/b184398b5359da5b76367aa61551d0f3d7e99fc5

activerecord/lib/active_record/associations/has_many_association.rbactiverecord/lib/active_record/associations/has_one_association.rbの修正です。

依存しているレコードがある為にレコードが削除出来ない場合に表示されるエラーメッセージのi18nのキーが、 元々restrict_dependent_destroy.onerestrict_dependent_destroy.manyだったのを、それぞれ、 restrict_dependent_destroy.has_onerestrict_dependent_destroy.has_manyに修正しています。 古いkeyを使用した場合、deprecateメッセージが表示されます。Rails 5.1で削除予定

onemanyはそれぞれi18n内で特別意味を持つ単語の為、キーに使用するのは良くない、という事で変更になったようです。


Merge pull request #20819 from y-yagi/rails_api_gemfile

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

Rails APIの場合、デフォルトで生成されるGemfileにweb-consoleが記載されないよう修正しています。

Rails APIの場合viewが無いので、web-consoleは不要だろう、という事で対応しています。


Merge pull request #20926 from rsanheim/fail-fast-bin-setup

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

bin/setupコマンドに、systemが失敗した場合に即時にabortするsystem!メソッドを定義して、各コマンドの実行にそのメソッドを使用するよう修正しています。

各コマンド実行時に、即処理が終了した方が良いだろう、との事で修正したとの事です。


Extra caller details added to ActiveRecord::RecordNotFound

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

ActiveRecord::RecordNotFoundクラスに、エラーが発生した際のmodel名、primary_key、idが保持するよう修正しています。

これにより、RecordNotFoundが発生したmodel/idによって処理を分岐する、という事が出来るようになっています。例。

class SomeAbstractController < ActionController::Base
  rescue_from ActiveRecord::RecordNotFound, with: :redirect_to_404

  private
  def redirect_to_404(e)
    return redirect_to(posts_url) if e.model == 'Post'
    raise
  end
end