なるようになるブログ

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

rails commit log流し読み(2017/01/31)

2017/01/31分のコミットです。

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

railties/CHANGELOG.md

activesupport/CHANGELOG.md


Simplify Postgres query for column_definitions()

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

column_definitionsメソッドでカラムの情報を取得するSQLにサブクエリーを使用していたのを、JOIN句を使用するよう修正しています。高速化の為。

PRより。

Before:

jordan=# explain SELECT a.attname,
  format_type(a.atttypid, a.atttypmod),
  pg_get_expr(d.adbin, d.adrelid),
  a.attnotnull,
  a.atttypid,
  a.atttypmod,
  (SELECT c.collname
   FROM pg_collation c, pg_type t
   WHERE c.oid = a.attcollation
   AND t.oid = a.atttypid
   AND a.attcollation <> t.typcollation),
   col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = 'a'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
                                                  QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Nested Loop Left Join  (cost=4.46..74.93 rows=3 width=119)
   Join Filter: ((a.attrelid = d.adrelid) AND (a.attnum = d.adnum))
   ->  Index Scan using pg_attribute_relid_attnum_index on pg_attribute a  (cost=0.28..12.26 rows=3 width=83)
         Index Cond: ((attrelid = '3525321'::oid) AND (attnum > 0))
         Filter: (NOT attisdropped)
   ->  Materialize  (cost=4.18..12.66 rows=4 width=38)
         ->  Bitmap Heap Scan on pg_attrdef d  (cost=4.18..12.64 rows=4 width=38)
               Recheck Cond: (adrelid = '3525321'::oid)
               ->  Bitmap Index Scan on pg_attrdef_adrelid_adnum_index  (cost=0.00..4.18 rows=4 width=0)
                     Index Cond: (adrelid = '3525321'::oid)
   SubPlan 1
     ->  Nested Loop  (cost=0.29..16.34 rows=1 width=64)
           ->  Index Scan using pg_collation_oid_index on pg_collation c  (cost=0.15..8.17 rows=1 width=64)
                 Index Cond: (oid = a.attcollation)
           ->  Index Scan using pg_type_oid_index on pg_type t  (cost=0.15..8.17 rows=1 width=0)
                 Index Cond: (oid = a.atttypid)
                 Filter: (a.attcollation <> typcollation)
(17 rows)

After:

jordan=# explain SELECT a.attname,
  format_type(a.atttypid, a.atttypmod),
  pg_get_expr(d.adbin, d.adrelid),
  a.attnotnull,
  a.atttypid,
  a.atttypmod,
  c.collname,
  col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_collation c
ON c.oid = a.attcollation
LEFT JOIN pg_type t
ON t.oid = a.atttypid AND a.attcollation <> t.typcollation
WHERE a.attrelid = 'collation_test'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
                                                           QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
 Merge Left Join  (cost=38.41..39.23 rows=3 width=179)
   Merge Cond: (a.attnum = d.adnum)
   Join Filter: (a.attrelid = d.adrelid)
   ->  Sort  (cost=25.73..25.73 rows=3 width=143)
         Sort Key: a.attnum
         ->  Hash Right Join  (cost=12.30..25.70 rows=3 width=143)
               Hash Cond: (c.oid = a.attcollation)
               ->  Seq Scan on pg_collation c  (cost=0.00..11.69 rows=269 width=68)
               ->  Hash  (cost=12.26..12.26 rows=3 width=83)
                     ->  Index Scan using pg_attribute_relid_attnum_index on pg_attribute a  (cost=0.28..12.26 rows=3 width=83)
                           Index Cond: ((attrelid = '3525342'::oid) AND (attnum > 0))
                           Filter: (NOT attisdropped)
   ->  Sort  (cost=12.68..12.69 rows=4 width=38)
         Sort Key: d.adnum
         ->  Bitmap Heap Scan on pg_attrdef d  (cost=4.18..12.64 rows=4 width=38)
               Recheck Cond: (adrelid = '3525342'::oid)
               ->  Bitmap Index Scan on pg_attrdef_adrelid_adnum_index  (cost=0.00..4.18 rows=4 width=0)
                     Index Cond: (adrelid = '3525342'::oid)
(18 rows)

Docs: Specify return value of save!

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

save!メソッドのdocに、エラーがおきなければtrueを返すむね説明を追加しています。


Merge pull request #27787 from y-yagi/show_correct_class_name_in_migration_error

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

migration ファイルでActiveRecord::Migrationを直接継承した場合(バージョン情報を設定しなかった場合)に出力されるエラーメッセージに表示されるクラス名に誤りがあったのを修正しています。


Merge pull request #27784 from y-yagi/add_warnings_option_to_test_runner

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

test runnerにRubyのwarningを有効化する為のオプション(-w, --warnings)を追加しています。


Make internal classes internal for the documentation

actionmailer/lib/action_mailer/parameterized.rbのdocの修正です。

ActionMailer::Parameterized module配下の内部用のクラスに:nodoc:を設定しています。


Override the initializers instead of using tap

actionmailer/lib/action_mailer/parameterized.rbの修正です。

ActionMailer::Parameterized::MessageDelivery#initializeメソッドを追加して、必要なパラメータの設定をそこで行うよう修正しています。


Remove unneeded nodoc

actionmailer/lib/action_mailer/parameterized.rbのdocの修正です。

ActionMailer::Parameterized::DeliveryJob#performメソッドの:nodoc:を削除しています。DeliveryJobクラス自体に:nodoc:が設定されており、メソッドに:nodoc:を設定する必要は無いため。


Implement respond_to_missing? in the Parameterized::Mailer class

actionmailer/lib/action_mailer/parameterized.rbの修正です。

ActionMailer::Parameterized::Mailerクラスにrespond_to_missing?メソッドを追加しています。ActionMailer::Parameterized::Mailerクラスにはmethod_missingメソッドが定義されている為。


Avoid should in test names

actionmailer/test/parameterized_test.rbの修正です。

テスト名にshouldが使われていたのを削除しています。

-  test "should enqueue the email with params" do
+  test "enqueue the email with params" do

コミットログの通りで、テスト名にはshouldを避けるべきだから、という理由の為。


No need to advertise in the rdoc documentation

actionmailer/lib/action_mailer/parameterized.rbのdocの修正です。

ActionMailer::Parameterized moduleのdocにActionMailer::Parameterizedについての広告(That's a big improvement!…という文)が入っていたのを削除しています。


:scissors:

actionmailer/lib/action_mailer/parameterized.rbのdocの修正です。

ActionMailer::Parameterized moduleのdoc内のexampleコードから不要なスペースを削除しています。


Document with with an example an link to Parameterized

actionmailer/lib/action_mailer/parameterized.rbの修正です。

ActionMailer::Parameterized.withメソッドにdocを追加、及びActionMailer::Parameterized moduleのdocへのリンクを追加しています。


Merge pull request #27822 from mtsmfm/bump-unicode-version

Active Supportの修正です。

Active Supportで保持しているunicode tableのデータを、8.0.0から9.0.0に更新しています。

合わせて、ActiveSupport::Multibyte::Unicode.unpack_graphemesメソッドの処理を、Unicode 9.0.0のUNICODE TEXT SEGMENTATIONの仕様に合わせて更新しています。


Merge pull request #27851 from tenderlove/scope-chain

Active Recordの修正です。

scopeを取得するのにscope chainを保持する為の配列を作成し使用していたのを、各reflctionクラスにscopesメソッドを追加し、reflctionからscopeを取得出来るよう修正しています。


Added warning for reserved YAML keywords.

railties/lib/rails/generators/rails/app/templates/config/locales/en.yml、 Rais GuideのRails Internationalization (I18n) APIの修正です。

デフォルトで生成されるen.yml、及びRail GuideにYAMLのキーワードはそのまま使えず、エスケープが必要である旨説明を追加しています。具体的には、true, on, yes, false, off, no


Avoid lambda scopes when possible

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

ThroughReflection#source_type_scopeメソッドでsource_type scopeを取得するのに、lambda scopesを使用していたのを、through_reflection経由で取得するよう修正しています。

      def source_type_scope
-        type = foreign_type
-        source_type = options[:source_type]
-        lambda { |object| where(type => source_type) }
+        through_reflection.klass.where(foreign_type => options[:source_type])
       end

この時点でreflection経由で正しいscopeを取得出来る為そちらを使用するようにしたようです。多分。コミットメッセージにあったLambda scopes require a bunch more workの意味がよくわからない…。


Merge pull request #27797 from y-yagi/correctly_check_error_message

各テストで、assert_raiseの第二引数にエラーメッセージを指定している箇所があったのですが、assert_raiseはエラーメッセージのチェックは行わずメッセージについてのテストが正しく出来てなかったので、別途assertionを追加し、メッセージについてはそちらで確認するよう修正しています。


add default value to deliver_later_queue_name option [ci skip]

actionmailer/lib/action_mailer/base.rbのdocの修正です。

ActionMailer::Baseクラスのdoc内のdeliver_later_queue_nameオプションにデフォルト値についての説明を追加しています。


Merge pull request #27647 from Shopify/fully-eagerload-journey

Action Pack / railtiesの修正です。

最初のrequestの際にroutesの初期化処理を行っていたのを、server boot時にroutesの初期化処理を行うよう修正しています(config.eager_loadがtrueの場合のみ)。最初のresponseが遅くなるのを避けれるようにする為。


Merge pull request #27738 from kenta-s/remove-unused-argument-formats

actionview/lib/action_view/template/resolver.rbactionview/lib/action_view/testing/resolvers.rbの修正です。

ActionView::PathResolver#extract_handler_and_format_and_variantメソッドから使用していないdefault_formats引数を削除しています。


Merge pull request #27849 from joevandyk/patch-2

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

LegacyYamlAdapter moduleの読み込みをeager_autoloadブロック内で行うよう修正しています。

ActiveRecord::Core#init_withLegacyYamlAdapterを使用しているのですが、時折LegacyYamlAdapter moduleが存在しないエラーが発生してしまう為、確実に読み込み処理が行われるよ移動したようです。


Don’t mutate raw_source in mailer preview interceptor

actionmailer/lib/action_mailer/inline_preview_interceptor.rbの修正です。

mailer previewで画像の表示を行う際に、Mail::Body#raw_sourceメソッドの結果の値を変化させていたのを、変化させないよう修正しています。

raw_sourceはbodyの作成使用された際の値を返すようになっており、その値を変化させると、本来想定している値と違う値がraw_sourceかえされる事になってしまう為、それを避ける為修正したとの事です。


Merge pull request #27824 from kenta-s/raise-an-error-if-no-block-given

activesupport/lib/active_support/evented_file_update_checker.rbactivesupport/lib/active_support/file_update_checker.rbの修正です。

EventedFileUpdateChecker#executeFileUpdateChecker#executeメソッドで処理を実行する際に、@blocknilの場合にArgumentErrorをraiseするよう修正しています。

@blockはクラスの初期化時点で渡すので、後ほどinitializeでblockが渡されるかどうかチェックするよう修正されています。


Raise in the initialize not in the execute

activesupport/lib/active_support/evented_file_update_checker.rbactivesupport/lib/active_support/file_update_checker.rbの修正です。

EventedFileUpdateChecker#initializeFileUpdateChecker#initializeの引数にblockが渡されていない場合に、ArgumentErrorをraiseするよう修正しています。


Merge pull request #27838 from kamipo/reload_destroyed_through_record

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

Associations::HasOneThroughAssociation#create_through_recordメソッドでthrough_recordがdestroy済みの場合に、through_recordの値をreloadするよう修正しています。

reloadせずに値を追加しようとすると、エラーに(RuntimeError: Can't modify frozen hash)なってしまう為、reloadするよう修正したとの事です。


Remove redundant right parentheses in number_helper [ci skip]

activesupport/lib/active_support/number_helper.rbのdocの修正。

number_to_phoneメソッドのexampleコードに閉じカッコが多かったのを削除しています。


Add test for variable_size_secure_compare

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

ActiveSupport::SecurityUtils.variable_size_secure_compareメソッドのテストを追加しています。


Merge pull request #27855 from kevinzwhuang/finder-methods-typo-fix

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

findメソッドのdoc内のグラマーの修正を行っています。