なるようになるブログ

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

rails commit log流し読み(2014/06/04)

2014/06/04分のコミットです。

CHANGELOGにのったコミットは以下の通りです。この日は多かった…。

activerecord/CHANGELOG.md

actionpack/CHANGELOG.md


Return a null column when no column exists for an attribute

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

column_for_attributeメソッドに存在しないカラムを指定した場合に、NullColumnクラスを返すよう変更しています。

# before 
  t.column_for_attribute(:aa)
  => nil

# after 
 t.column_for_attribute(:aa)
  => #<ActiveRecord::ConnectionAdapters::NullColumn:0x007f80bcda4f78
  @cast_type=
    #<ActiveRecord::Type::Value:0x007f80bcda4f50
    @limit=nil,
    @precision=nil,
    @scale=nil>,
  @default=nil,
  @default_function=nil,
  @name="aa",
  @null=true,
  @sql_type=nil>

より詳細な情報が取れるようになったのですが、戻り値でif判定を行ったりしていた場合、NullColumnではtrueになってしまうので、注意が必要そうです。


test pg, rm unused requires.

PostgreSQLのConnectionAdapterのテストの修正です。

不要なrequireを削除しています。


pg, preserve money type when dumping schema and extract money default.

PostgreSQLのConnectionAdapterの修正です。

typeにmoneyを追加しています。

PostgreSQLの通貨型ですね。


test pg, remove unused column assignments. Follow up to 254cdf47 PostgreSQLのConnectionAdapterのテストの修正です。

不要な変数を削除しています。


Keep column defaults in type cast form

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

デフォルトのcolumnとvalueを返すraw_column_defaultsメソッドを新設し、ActiveRecordinitializeではそちらを使うよう修正しています。


Relax mail gem constraint from ~> 2.5.4 to ~> 2.5, >= 2.5.4

actionmailer/actionmailer.gemspecの修正です。

mailgemの2.6が使用出来るよう、gemspecのバージョン記載を修正しています。

-  s.add_dependency 'mail', '~> 2.5.4'
+  s.add_dependency 'mail', ['~> 2.5', '>= 2.5.4']

2.5.4以上、3.0未満ですね。


only look up the format option from the hash once

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

options[:format]の値を各メソッドの引数に追加し、一度参照した値を使いまわすよう修正しています。


shorten up IGNORE_OPTIONS

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

options[:format] -> options.delete :formatに修正し、optionsからformatを削除するようにしています。合わせて、IGNORE_OPTIONSから:formatを削除。


remove another value from IGNORE_OPTIONS

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

先ほどのformatと同じ事を、toオプションにも対応。


remove :as and :anchor from IGNORE_OPTIONS

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

:as:anchorオプションにも同様の対応。


add a test for missing "via" parameter

actionpack/test/dispatch/routing_test.rbの修正です。

viaオプションなしでmatchを使用した時のテストケースを追加しています。


test with an empty via

actionpack/test/dispatch/routing_test.rbの修正です。

viaオプションが空だった場合のストケースを追加しています。


always pull out a via variable and simplify logic

actionpack/lib/action_dispatch/routing/mapper.rb

:viaオプションの取り扱い処理についてリファクタリングしています。


always make :via a list

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

実際の値に関わらず、:viaオプションをArrayとして取り扱うよう修正しています。


pull up via extraction and remove it from options / IGNORE_OPTIONS

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

:viaの値をメソッドの引数に追加し、一度参照した値を使いまわすよう修正しています。参照する際、optionから削除するようにして、IGNORE_OPTIONSからも削除しています。


:on is removed from options before the Mapping is instantiated, so remove it from IGNORE_OPTIONS

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

IGNORE_OPTIONSから:onを削除。


disconnect the constraints method from the options and scope hashes

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

constraintsメソッドの引数にoption_constraintsscope_constraintsを追加し、constraintsメソッドの中ではoptionsscopeの値を参照しないよう修正しています。

optionsscopeを参照する箇所をinitializeメソッドだけにして、IGNORE_OPTIONSから消す為ですね。


disconnect options and scope from the blocks method

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

blocksメソッドの引数にoption_constraintsscope_blocksを追加し、blocksメソッドの中ではoptionsscopeの値を参照しないよう修正しています。先ほど同様ですかね。


disconnect normalize_defaults! from options[:constraints]

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

上の二つのコミットと同じ内容を、normalize_defaults!メソッドにも対応しています。


remove :constraints from IGNORE_OPTIONS

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

引数で渡すようにしたので、IGNORE_OPTIONSから:constraintsを削除しています。


remove :defaults from the IGNORE_OPTIONS list

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

引数で渡すようにしたので、IGNORE_OPTIONSから:defaultsを削除しています。

2014-06-04 03:33:52 +0900


:only is never used in Mapping, so rm the key

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

Mappingクラスでは:onlyオプション使用してなかったらしく、特に対応せずIGNORE_OPTIONSから削除しています。


remove IGNORE_OPTIONS

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

残っていたIGNORE_OPTIONSに定義されていたオプションも使用してなかったらしく、options.deleteを追加して、IGNORE_OPTIONS自体を削除しています。


only look up scope[:module] once

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

メソッドの引数にmodyouleを追加して、scope[:module]の値を使いまわすよう修正しています。


no longer need the scope ivar

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

使いまわすようになった:scopeをattr_readerから削除してます。


change defaults allocation to a one-liner

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

@defaultsの初期化処理を修正しています。

-          @defaults.merge!(scope[:defaults]) if scope[:defaults]
-          @defaults.merge!(options.delete(:defaults)) if options[:defaults]
+          @defaults = (scope[:defaults] || {}).merge options.delete(:defaults) || {}

ワンライナーにまとめた形に。


only loop through constraints once

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

constraintsに関するループを一回で済むようリファクタリングしています。


only loop over options once (hopefully)

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

optionsをチェックするループが一回で済むようリファクタリングしています。…hopefully。


return early from add_request_method

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

add_request_methodvia == [:all]の場合に、early resturnするよう修正しています。


only do Regexp === option once

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

Regexp === optionを一度だけ実行するようリファクタリングしています。


remove options as an ivar

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

メソッドの引数にoptionsを追加して、 attr_readerからoptionsを削除しています。


push options_constraints processing up

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

option_constraintsに関する処理を前の方に移動しています。

これにより、is_aチェックを減らせた、との事。


only do is_a checks on formatted once

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

formattedの型チェックを一度だけ行うようリファクタリング


move options_constraints tests next to each other

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

options_constraintsに関する処理を類似処理の箇所に移動しています。


add tests for nested lambda constraints

actionpack/test/controller/routing_test.rbの修正です。ああ、久し振りに違うファイルの修正が。

scopeでconstraintsオプションにlambda指定した場合のテストを追加しています。


push some options munging to an alternate constructor

そしてまたactionpack/lib/action_dispatch/routing/mapper.rbに戻る。

Mappingクラスにself.buildを追加し、options.delete等の処理をこちらに集約しています。


add tests for mixing :to and controller / action

actionpack/test/dispatch/routing_test.rbの修正です。

routingで:toオプションにcontroller/action混ぜた場合のテストケースを追加しています。


use the factory method to construct the mapping

actionpack/test/dispatch/mapper_test.rbの修正です。

Mapperインスタンスを作成するのに、newではなく、buildメソッドを使用するよう修正しています。


Fix regression on eager loading association based on SQL query rather

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

SQL queryを元に eager loadingを行った場合にエラーになるケースがあるのを修正しています。

belongs_to :last_pet, class_name: 'Pet'
scope :including_last_pet, -> {
   select(%q[
     owners.*, (
       select p.pet_id from pets p
       where p.owner_id = owners.owner_id
       order by p.name desc
       limit 1
     ) as last_pet_id
   ]).includes(:last_pet)
 }

こんな書き方出来るんですね。知らなかった。


Revert "test pg, we don't care about the internal state of column#default."

こちらのコミットをrevertしています。

Column#defaultのテストを削除するコミットでしたが、Column#defaultはpublic methodだろう、という事でテストを戻しています。


Routes specifying 'to:' must be a string that contains a "#" or a rack

routingの修正です。

routesでto:オプションを使用する場合、"#"を含むStringを指定する必要があるとの事です(SymbolはNG)。Symbolを使用したい場合、controller:又はaction:オプションを使用するように、との事です。

to:にSymbolを使用した場合、以下のワーニングが出力されます。

ActiveSupport::Deprecation.warn "defining a route where `to` is a symbol is deprecated.  Please change \"to: :#{to}\" to \"action: :#{to}\""

ロジック見る限り、勝手にoptionの入れ替えが行われるので、とりあえずは動作するようです。

case to
  when Symbol
    options[:action] = to
 when String
  if to =~ /#/
    options[:to] = to
  else
    options[:controller] = to
  end
else
  options[:to] = to
end

これは影響大きそうな。


oops! :bomb:

先ほどのコミットに全然関係ないコミットが含まれてしまっていたようで、削除しています。


Add default_i18n_subject to the guides

rails guideのRails Internationalization (I18n) APIの修正です。

default_i18n_subjectメソッドに関する説明を追加しています。

# user_mailer.rb
class UserMailer < ActionMailer::Base
  def welcome(user)
    mail(to: user.email, subject: default_i18n_subject(user: user.name))
  end
end
en:
  user_mailer:
    welcome:
      subject: "%{user}, welcome to Rails Guides!"

mailのi18n対応するときは便利そうです。


force table creation

activerecord/test/cases/adapters/postgresql/bit_string_test.rbの修正です。

テスト用のテーブルを作る際に、:force => trueを追加しています。


Pluralize params

rails guideのRails Internationalization (I18n) APIの修正です。

params -> parametersに修正。


Merge pull request #15172 from notEthan/active_record_pretty_print

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

pretty_printメソッドが追加されています。

# before 
  pp topic

  #<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "2003-07-16 14:28:11", bonus_time: "2000-01-01 14:28:00", last_read: "2004-04-15", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "2014-05-18 21:33:26", updated_at: "2014-05-18 21:33:26"> 

# after 
  pp topic

  #<Topic:0x007fb93696bcb0
   id: 1,
   title: "The First Topic",
   author_name: "David",
   author_email_address: "david@loudthinking.com",
   written_on: 2003-07-16 14:28:11 UTC,
   bonus_time: 2000-01-01 14:28:00 UTC,
   last_read: Thu, 15 Apr 2004,
   content: "Have a nice day",
   important: nil,
   approved: false,
   replies_count: 1,
   unique_replies_count: 0,
   parent_id: nil,
   parent_title: nil,
   type: nil,
   group: nil,
   created_at: 2014-05-18 21:31:53 UTC,
   updated_at: 2014-05-18 21:31:53 UTC> 

ppメソッドの実行結果がわかりやく。


Merge pull request #15349 from tgxworld/remove_duplicated_method_call

actionpack/lib/action_dispatch/http/parameters.rbの修正です。

重複してたwith_indifferent_accessメソッドの呼び出しを削除しています。


Partially revert deprecation of *_filter

*_filterコールバックをdeprecateにしたコミットをrevertしています。

deprecateにするのは、Rails 5からなので一旦revertしたようです。


Convert StrongParameters cache to a hash. This fixes an unbounded

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

StrongParametersのキャッシュをHashに格納するよう修正しています。

memory leakしているケースがあったもよう。詳しくはtenderloveのブログ参考。


reuse available belongs_to? method

ActiveRecordのassociationsの修正です。

through_reflection.macro == :belongs_toしている箇所をthrough_reflection.belongs_to?メソッドを使うよう修正しています。


Cleaned up duplicated CHANGELOG entry [ci skip]

activerecord/CHANGELOG.mdの修正です。

同一のエントリーがあったらしく、片方削除しています。


Change wording of explanation about precision & scale of decimal numbers [ci skip]

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

decimalに関する説明が誤っていたのを修正しています。


Use null column for association key types

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

association_key_typeメソッドcolumn_for_attributeメソッドを使用するよう修正しています。


Merge pull request #15463 from geopet/refactor_xml_compute_type

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

XML serializerの処理をリファクタリングしています。