なるようになるブログ

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

rails commit log流し読み(2015/06/13)

2015/06/13分のコミットです。

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

activerecord/CHANGELOG.md

actionpack/CHANGELOG.md

railties/CHANGELOG.md


Merge pull request #20480 from senny/test_runner

コンポーネントのテストを実行するのに、rake testではなく、minitest pluginを使用したtest runnerで実行出来るよう修正しています。

Rails 5から追加されたrails testコマンドと同じ仕組みで実行しているようです。


Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes

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

ActiveSupport::Concern#class_methodsメソッドが親クラスに影響を与えてしまっていたのを、影響を与えないよう修正しています。

issueより。

require 'active_support/concern'

module Foo
  extend ActiveSupport::Concern

  class_methods do
    def foo

    end
  end
end

module Bar
  extend ActiveSupport::Concern

  class_methods do
    def bar

    end
  end
end

class Parent
  include Bar
end

class Child < Parent
  include Foo
end

Parent.respond_to? :foo # => true

定数が定義されているかどうかの確認にModule#const_defined?メソッドを使用しているのですが、第二引数を指定しておらず、親クラスや include したモジュールもチェック対象しとしていたのが問題だったようです。第二引数にfalseを指定する事で対応しています。

-      mod = const_defined?(:ClassMethods) ?
+      mod = const_defined?(:ClassMethods, false) ?

const_defined?の第二引数の取り扱い知りませんでした。ご参考: instance method Module#const_defined?


Don't crash when mutating attributes in a getter

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

ActiveRecordのattributeのgetterメソッド内で、そのattributeの値をupdate_attributeメソッド等を使用し変更した場合に、exceptionで落ちてしまうバグがあったのを修正しています。

issueより。

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.string :title
  end
end

class Post < ActiveRecord::Base
  def title
    if self[:title].blank?
      update_attribute :title, "test"
    end
    self[:title]
  end
end

post = Post.create!(title: "aa")
post.update_attribute :title, nil

post.title # => ここでエラー

attributes cache を消すタイミングが早かったのが問題だったようです。


Merge pull request #18365 from pocke/fix_datatime_compare

activesupport/lib/active_support/core_ext/date_time/calculations.rbの修正です。

DateTime#<=>の引数に不正な文字列を指定した際に、ArgumentErrorがおきてしまっていたのを、Ruby本体の挙動同様、nilを戻すよう修正しています。

# before
Time.now ==  'a'  # => false
Time.now <=> 'a'  # => invalid date (ArgumentError)

# after
p Time.now ==  'a'  # => false
p Time.now <=> 'a'  # => nil

Add a test to ensure serialize persists nil as NULL

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

serializeしたattributeにnilを指定し保存した場合に、DB上はNULLとして値が格納されている事を確認するテストを追加しています。


add doc about how empty serialization objects are persisted

activerecord/lib/active_record/attribute_methods/serialization.rbのdocの修正です。

serializeしたattributeに空のHashやArrayを追加した場合に、nilとして保存される説明を追加しています。


Copy-edits in 04c349659bfd97f9eaf5266ef7a64b4eb367c6a8

activerecord/lib/active_record/attribute_methods/serialization.rbのdocの修正です。

先のコミットで追加されたdocの適切な位置に改行を追加しています。


Handle param-parsing errors from Rack in ExceptionWrapper

actionpack/lib/action_dispatch/middleware/exception_wrapper.rbの修正です。

ExceptionWrapperクラスがデフォルトで対応するエラークラスの一覧にRack::Utils::ParameterTypeErrorRack::Utils::InvalidParameterErrorクラスを追加しています。

Rack::Utils::ParameterTypeErrorはstructural parametersにコンフリクトするタイプが定義されていた時に、`Rack::Utils::InvalidParameterErrorはstructural parametersに不正なバイト列が含まれていた時に、それぞれ起きるエラーのようです。

詳細は、 Exception: Rack::Utils::InvalidParameterError — Documentation for rack/rack (master)Exception: Rack::Utils::ParameterTypeError — Documentation for rack/rack (master) 参照。


Add fixture use case to testing.md.

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

複数のデータをfixtureのメソッドから取得する場合のexampleを追加しています。

# this will return an array containing the fixtures david and steve
users(:david, :steve)

let's use the latest bundler version.

Gemfile.lockの修正です。

最新のbundlerを使用する為、BUNDLED WITHの値を更新しています。


make it possible to customize the executable inside rereun snippets.

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

テスト実行結果表示用のクラスで、テスト再実行ファイル名をカスタマイズ出来るよう修正しています。

テストを実行しエラーが起きた際、エラーが起きたテストを再実行する為のスニペットが表示されるのですが、そこに表示される再実行ファイルが不正な値(rails本体のテストなので、railsアプリのテスト再実行ファイルが表示されてしまう)が表示されてしまう為、対応したようです。

# before:
Failed tests:

bin/rails test test/cases/adapters/postgresql/schema_test.rb:91

# after:
Failed tests:

bin/test test/cases/adapters/postgresql/schema_test.rb:91

[ci skip] Fix the API docs for Bi-directional associations

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

Finding inverse associations automatically の対応で、inverse associationsを自動で検知するようになったのですが、doc内の説明が古いままだったので、最新の内容に更新しています。