なるようになるブログ

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

rails commit log流し読み(2016/04/21)

2016/04/21分のコミットです。

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

activerecord/CHANGELOG.md


Merge pull request #23557 from kamipo/dump_indexes_in_create_for_generates_sql_in_one_query

Acitve Recordの修正です。

schemaファイルを生成する際、indexの生成にadd_indexメソッドを使用するようになっていたのを、create_table内でindexメソッドを使用するよう修正しています。

# before
ActiveRecord::Schema.define(version: 20160419063525) do
  create_table "events", force: :cascade do |t|
    t.string   "name",       limit: 5
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
  end

  add_index "events", ["name"], name: "index_events_on_name"
end
# after
ActiveRecord::Schema.define(version: 20160419063525) do
  create_table "events", force: :cascade do |t|
    t.string   "name",       limit: 5
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
    t.index ["name"], name: "index_events_on_name"
  end
end

体裁が整いこちらの方が見やすいだろう、というのと、MySQLの場合はこれにより不要なindex設定のクエリーが実行されない(create tableのqueryでindexの設定も行える為)というメリットがあるため、との事です。


- [ci skip] regardless is usually followed by of and not by if .. or not.

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

ActiveModel::Conversion#to_keyメソッドのグラマーの修正を行っています。


Merge pull request #24221 from gregmolnar/uuid

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

create_join_tableメソッドcolumn_optionsオプションに、primary keyの型を指定出来るよう対応しています。

create_join_table :artists, :musics, column_options: { type: :uuid }

Speed up String#blank? Regex

activesupport/lib/active_support/core_ext/object/blank.rbの修正です。

String#blank?の空白チェック用の正規表現の見直しを行っています。

  BLANK_RE = /\A[[:space:]]*\z/

  def blank?
-    # In practice, the majority of blank strings are empty. As of this writing
-    # checking for empty? is about 3.5x faster than matching against the regexp
-    # in MRI, so we call the predicate first, and then fallback.
-    #
-    # The penalty for blank strings with whitespace or present ones is marginal.
-    empty? || BLANK_RE === self
+    # Regex check is slow, only check non-empty strings.
+    # A string not blank if it contains a single non-space string.
+    empty? || !(/[[:^space:]]/ === self)
   end

文字列が全て空かどうかではなく、文字列中に空じゃない文字があるかどうか、でチェックするようにしたんですねえ。こちらの方が高速との事です。コミットログにあったベンチマークは以下の通りです。

require 'benchmark/ips'

def string_generate
  str = " abcdefghijklmnopqrstuvwxyz\t".freeze
  str[rand(0..(str.length - 1))] * rand(0..23)
end

strings = 100.times.map { string_generate }

ALL_WHITESPACE_STAR = /\A[[:space:]]*\z/

Benchmark.ips do |x|
  x.report('current regex            ') { strings.each {|str| str.empty? || ALL_WHITESPACE_STAR === str } }
  x.report('+ instead of *           ') { strings.each {|str| str.empty? || /\A[[:space:]]+\z/ === str } }
  x.report('not a non-whitespace char') { strings.each {|str| str.empty? || !(/[[:^space:]]/ === str) } }
  x.compare!
end

# Warming up --------------------------------------
# current regex
#                          1.744k i/100ms
# not a non-whitespace char
#                          2.264k i/100ms
# Calculating -------------------------------------
# current regex
#                          18.078k (± 8.9%) i/s -     90.688k
# not a non-whitespace char
#                          23.580k (± 7.1%) i/s -    117.728k

# Comparison:
# not a non-whitespace char:    23580.3 i/s
# current regex            :    18078.2 i/s - 1.30x slower

Fix small typos [ci skip]

rails guideのAutoloading and Reloading Constantsの修正です。

Top-Level Constants及びQualified Referencesの項のグラマーの修正を行っています。


Remove unused BLANK_RE

activesupport/lib/active_support/core_ext/object/blank.rbの修正です。

先のString#blank?の空白チェック用の正規表現の見直しを行った対応により不要になったBLANK_RE定数を削除しています。


Update delegate to use newer Ruby syntax

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

delegateメソッドの引数の指定に、キーワード引数を使用するよう修正しています。


add doc for :type option of #create_join_table [ci skip]

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rbのdocの修正です。

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

が、オプションを説明する箇所に誤りがあり、後ほどrevertされています。


Revert "add doc for :type option of #create_join_table [ci skip]"

という訳で、直前の修正をrevertしています。


Merge pull request #24657 from maclover7/fix-23643-1

rails guideのRuby on Rails 5.0 Release Notesの修正です。

ActionDispatch::IntegrationTest及びActionController::TestCaseのHTTP request methodsの引数をキーワード引数を指定出来るようにした対応(Use kwargs in ActionController::TestCase and ActionDispatch::Integration HTTP methods by kirs · Pull Request #18323 · rails/rails)について、Deprecationsの項にentryを追加しています。


restores code comments in String#blank? [ci skip]

activesupport/lib/active_support/core_ext/object/blank.rbのdocの修正です。

String#blank?メソッドのコード内に、処理内容についての説明のコメントを追加しています。


Minor space bump.

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

has_manyのdoc内のCollection associations項のタイトル部分に、スペースを追加しています。


Merge pull request #24664 from y-yagi/use_wrapper_file_to_add_rake_task_for_engines

railties/lib/rails/generators/rails/plugin/templates/Rakefileの修正です。

rails engine用のRakefileで、gem用のリリースタスクを追加するのにBundler::GemHelper.install_tasksメソッドを直接呼び出していたのを、bundlerが提供しているラッパーファイル(bundler/gem_tasks)を使用するよう修正しています。bundler側で何かtaskを追加した場合に、Rakefileを修正せずにそのtaskを使用する事が出来る為。


Merge pull request #24661 from maclover7/fix-22975

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

ActionController::Renderer.forメソッドの第二引数のdefaultsのデフォルト値に、DEFAULTS定数を使用しているのですが、その値を設定する際にdupした値を使用するよう修正しています。

    # Create a new renderer instance for a specific controller class.
-    def self.for(controller, env = {}, defaults = DEFAULTS)
+    def self.for(controller, env = {}, defaults = DEFAULTS.dup)
       new(controller, env, defaults)

DEFAULTSはfreezeしているので、そのまま値を使用すると、ApplicationController.renderer.defaults.merge!を使用した際にエラーになってしまう為、対応したとの事です。