なるようになるブログ

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

rails commit log流し読み(2015/02/12)

2015/02/12分のコミットです。

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

actionpack/CHANGELOG.md

activerecord/CHANGELOG.md

activesupport/CHANGELOG.md


Remove most PG specific type subclasses

PostgreSQLのConnectionAdaptersの修正です。

元々、各種型用のサブクラス(OID::IntegerOID::Time等)をActiveRecordで定義していたのですが、最新のpg gemでは、gemの方で変換用クラスがあり、Cで書かれている為、そちらの方が高速に動作すると思われるので、使用出来るものについてはpgで定義されているクラス(PG::TextDecoder::IntegerPG::TextDecoder::Boolean)を使用するよう修正しています。


Remove an unused option that I didn't mean to commit [ci skip]

activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rbの修正です。

先のコミットで誤って追加してしまったTypeMapInitializerコンストラクタrun_complex_typesオプションを削除しています。


Reduce object allocations in ActiveSupport::Subscriber

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

finishメソッドで引数のnameを分割する際に使用する区切り文字を、freezeしています。 オブジェクト生成数の軽減の為との事。


Remove the SQLite3 Binary subclass

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

ConnectionAdapters::SQLite3Binaryクラスを削除しています。値のエンコーディングEncoding::ASCII_8BIT以外だった場合に、強制的にEncoding::ASCII_8BITに変換する処理が行われていたのですが、他の場所で既に行われており、ここでの処理は不要な為、削除したようです。多分。


Fixes wording of test description

activesupport/test/xml_mini_test.rbの修正です。

テスト名が実際の内容と異なっていたので、適切な内容にテスト名を修正しています。


Revert 88d08f2ec9f89ba431cba8d0c06ac9ebc204bbbb

activesupport/lib/active_support/core_ext/module/delegation.rbの修正です。

Fix confusing exception in ActiveSupport delegationをrevertしています。

Module#delegateを使用している時に、delegate先のメソッドNoMethodErrorが起きた際に、NoMethodErrorではなくRuntimeErrorが発生してしまう問題があったのを直したコミットだったのですが、integration testの性能劣化の原因になってしまっていたらしく、revertされています。


Merge pull request #18374 from claudiob/add-collection-to-fresh-when

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

ActionController::ConditionalGet#fresh_whenstale?メソッドにrecordのcollectionを渡せるよう対応しています。collectionを渡した場合は、collection内の最大のupdated_atの値をlast_modifiedに設定してくれます。

# Before
def index
  @articles = Article.all
  fresh_when(etag: @articles, last_modified: @articles.maximum(:updated_at))
end

# After
def index
  @articles = Article.all
  fresh_when(@articles)
end

これは便利。


Merge pull request #18888 from kamipo/refactor_quote_default_expression

ActiveRecordの修正です。

quote_default_expressionメソッドリファクタリング(Quoting#quote_default_value -> Quoting#quote_default_expressionメソッド名の変更、及び重複コードの削除)を行っています。


current_scope shouldn't pollute sibling STI classes

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

STIを使用している際、同じ親クラスを持つ全てのクラスのscopeが参照されてしまっていたバグがあったのを修正しています。

PRから。

# schema
create_table :sti_roots, force: true do |t|
  t.string :type
  t.belongs_to :owner
end

create_table :owners, force: true do |t|
end
class StiRoot < ActiveRecord::Base
  belongs_to :owner
  scope :of_type, ->(cls) do
    all.merge cls.all
  end
end

class StiLeaf1 < StiRoot
end

class StiAbstract < StiRoot
end

class StiLeaf2 < StiAbstract
end

class StiLeaf3 < StiAbstract
end

class Owner < ActiveRecord::Base
  has_many :sti_roots
end

class BugTest < Minitest::Test
  def test_association_stuff
    owner = Owner.create!

    owner.sti_roots << StiLeaf1.create!
    owner.sti_roots << StiLeaf2.create!
    owner.sti_roots << StiLeaf3.create!

    assert_equal 2, owner.sti_roots.of_type(StiAbstract).count  # =>  Expected: 2  Actual: 3
  end
end

親であるStiRootof_type scopeでは、本来は引数に指定したStiAbstractクラスの値のみ取得出来なければなけないのですが、current_scopeメソッドScopeRegistry.value_forの引数にbase classの値を指定していたので、同じ親を持つStiLeaf1クラスの値も取得されてしまった、値がおかしくなっていた、というバグとの事です。…合ってるかなあ。


Refactor enum to be defined in terms of the attributes API

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

attributes APIの規約にしたがって、enumに関する処理をリファクタしています。

合わせて、whereに直接enumの値を指定出来るよう対応しています。

class Thing < ActiveRecord::Base
  enum state: [:working, :broken]
end
Thing.create!(state: :broken)

Thing.where(state: Thing.states[:broken])  # => #<Thing id: 1, state: "broken", created_at: "2015-02-12 07:43:12", updated_at: "2015-02-12 07:43:12">
Thing.where(state: 'broken') # => #<Thing id: 1, state: "broken", created_at: "2015-02-12 07:43:12", updated_at: "2015-02-12 07:43:12">

直接値を指定出来るのは便利ですねえ。


Don't break enum on PG

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

Enum#type_cast_from_databaseメソッドでIntegerへの変換処理が不足していたのを修正しています。


Properly dump primitive-like AS::SafeBuffer strings as YAML

activesupport/lib/active_support/core_ext/string/output_safety.rbの修正です。

ActiveSupport::SafeBuffer#to_yamlを呼び出した際、primitiveな値についても、Stringの値を返すよう修正しています。

# Before:
YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml  # => "Hello"
YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml   # => true
YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml  # => false
YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml      # => 1
YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml    # => 1.1

# After:
YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml  # => "Hello"
YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml   # => "true"
YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml  # => "false"
YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml      # => "1"
YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml    # => "1.1"

PHPJavaScriptの振る舞いに合わせた、との事。


fix typo in fresh_when example [ci skip]

actionpack/CHANGELOG.mdの修正です。

先にコミットされたfresh_whenの対応で記載されたexampleにタイポがあったのを修正しています。


tests, remove unused requires.

activerecord/test/cases/migration/foreign_key_test.rbactiverecord/test/cases/migration_test.rbの修正です。

不要なactive_support/testing/streamのrequireを削除しています。


pg tests, be clear about the missing type that causes a test skip.

activerecord/test/cases/adapters/postgresql/json_test.rbactiverecord/test/cases/migration_test.rbの修正です。

json型のテストをskipする際、表示されるメッセージをより分かりやすい内容に修正、及びテスト用のtableをdropする際、if_existsオプションでチェックするよう修正しています。


get rid of transaction warning when running PG tests.

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

"WARNING: there is no transaction in progress"が出力されてしまっていたので、トランザクション実行中のみコミットするよう修正しています。