なるようになるブログ

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

rails commit log流し読み(2015/07/18)

2015/07/18分のコミットです。

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

activesupport/CHANGELOG.md

activerecord/CHANGELOG.md


[skip ci] Lookup can be a noun but it is not a verb

各docの修正です。

lookup -> look upに修正、及び1行80文字に収まるよう適切な位置に改行を追加しています。


push key checking up

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

array_of_permitted_scalars_filterメソッド内で行っていたkeyチェック処理を、array_of_permitted_scalars_filterメソッド呼び出し前に行うように、array_of_permitted_scalars_filterメソッドではチェック処理を行わないよう修正しています


stop passing params to array_of_permitted_scalars_filter

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

元々array_of_permitted_scalars_filterメソッドではparamsと設定したい値を設定し、値がpermittedだった場合にparamsにその値を設定する、という処理を行っていたのを、 paramsの代わりにブロックを渡せるようにし、paramsへの値設定処理は呼び出し元で指定出来るよう修正しています。こうする事で、params objectが何のクラスであるかをarray_of_permitted_scalars_filterメソッド側では意識する必要が無くなる為との事です。なるほど。


remove useless function

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

array_of_permitted_scalars_filterメソッドarray_of_permitted_scalars?メソッドで実施している内容が重複していたので、array_of_permitted_scalars_filterメソッドを削除し、array_of_permitted_scalars?メソッドの方を使用するよう修正しています。


remove useless conditionals

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

fields_for_style?メソッドhash_filterメソッドから、elementHashかどうかのチェック処理を削除しています。

elementHashになることは無い為との事。


ActiveSupport::HashWithIndifferentAccess select and reject should return enumerator if called without block

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

HashWithIndifferentAccess#selectHashWithIndifferentAccess#rejectメソッドをブロックを指定せずに呼び出した場合に、enumeratorを返すよう修正しています。

Hash#selectHash#rejectメソッドと挙動を合わせる為との事。


remove useless conditional

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

hash_filterメソッドelementParameter以外のクラスの場合の処理を削除しています。

理由は先程のコミットと同様に、hash_filterメソッドの時点でelementは必ずParameterクラスの為。


push is_a checks up the stack

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

each_elementメソッドでelementsがParametersクラスかどうかチェックするよう修正しています。hash_filterメソッドの方で型を意識しないで済むように、との事です。


Merge pull request #20902 from sikachu/silence-association-reload-warning

ActiveRecordのテストの修正です。

テスト内でassociationのreload処理を行うのに明示的にreloadメソッドを呼び出すよう修正しています。

Deprecate force association reload by passing true の対応でassociation methodにtrueを渡すとdeprecationメッセージが表示されるようになったので、不要なdeprecationメッセージが表示されないよう修正したの事です。


push fields_for_style? in to a protected method

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

fields_for_style?メソッドの可視性を、to_unsafe_h経由せずに呼べるようにする為に、privateからprotectedに変更しています。


Add deprecation warning for render :text

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

renderメソッド:textオプションがdeprecateになりました。

:textという名前だったものの、レスポンスのMIMEresponseのMIMEがtext/plain`ではなく、紛らわしいので良くないのでは、という理由によりdeprecateになったようです。

text/plainを返したい場合はrender plain: 'plain text'を、text/htmlを返したい場合はrender html: '<strong>HTML</strong>'を使用するようにとの事です。


Stop using deprecated render :text in test

ActionPackのテストの修正です。

先のコミットでdeprecateになったrenderメソッドtextオプションを使用しないよう修正しています。


use plain option instead of deprecated text option

guides/bug_report_templates/action_controller_master.rbの修正です。

先のコミットでdeprecateになったrenderメソッドtextオプションを使用しないよう修正しています。


Replaced render :text with render :plain in AC gem bug report template

guides/bug_report_templates/action_controller_gem.rbの修正です。

先のコミットでdeprecateになったrenderメソッドtextオプションを使用しないよう修正しています。


Ensure that ActionController::Parameters can still be passed to AR

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

assign_nested_attributes_for_one_to_one_associationメソッドで引数のattributesが ActionController::Parametersだった際に、 attributesをHashに変換するよう修正しています。

nested attributesはHashクラスである事が期待されているのですが、Make AC::Parameters not inherited from Hashの対応によりActionController::ParametersHashクラスの子クラスで無くなった影響で処理が動かなくなってしまった為、とりあえずto_hでHashに変換して、処理が動作するように対応したようです。


Merge pull request #20763 from maurogeorge/default_scope_create-doc

rails guideのActive Record Query Interfaceの修正です。

Applying a default scopeの項に、default_scopeはレコードの新規作成時にも実行される旨説明を追加しています。


Ensure cyclic associations w/ autosave don't cause duplicate errors

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

define_autosave_validation_callbacksの後処理で、重複したエラーメッセージがあった場合に、重複したエラーメッセージを削除する処理を追加しています。

+      def _ensure_no_duplicate_errors
+        errors.messages.each_key do |attribute|
+          errors[attribute].uniq!
+        end
+      end

autosave: trueしたassociationがある場合に、同じvalidationが複数発生してしまう事があるらしく、その対応の為との事です。

class User < ActiveRecord::Base
  has_many :licenses

  validates :name, presence: true
  validates :email, presence: true
end

class License < ActiveRecord::Base
  belongs_to :user, autosave: true
end
user = User.new
user.licenses.build
user.save

user.errors # => #<ActiveModel::Errors:0x007fad846b4ac0 @base=#<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>, @messages={:name=>["can't be blank", "can't be blank"], :email=>["can't be blank", "can't be blank"], :licenses=>["is invalid"]}>