なるようになるブログ

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

rails commit log流し読み(2017/11/14)

2017/11/14分のコミットです。

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

activerecord/CHANGELOG.md

activesupport/CHANGELOG.md


Use .tt extension to all the template files

railtiesの修正です。

generatorで使用するtemplates filesの拡張子をまとめてttに更新しています。

元はttだったりrbだったりjsだったり色々混ざっていたのですが、rbjsだとエディタで開いた時や静的解析のときに困る(拡張子的には正しいRubyやJSのファイルのように思えるが、実際の中身は違う為)ので、すべてThorのtemplateである事を表すttを使用するようにしています。


Merge pull request #30980 from sobrinho/sobrinho/arel-star-ignored-columns

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

ignored_columnsに指定したcolumnに対するattribute methodsが、modelのreload後に定義されてしまうバグがあったのを修正しています。


Properly cast input in update_all

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

update_allの引数に指定された値をType#serializeに渡す前にcastするよう修正しています。

これにより、update_all(foo: 'true')のようにStringの"true"を渡した際にb、ooleanのtrueとして評価されるようになります。


Merge pull request #31117 from renuo/fix_errors_added

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

ActiveModel::Errors#added?メソッドが、message引数にSymbolを指定した、かつ、既に同じattributeで別のエラーメッセージが格納されている場合にtrueを返してしまうバグがあったのを修正しています。


Preserve existing metadata when analyzing a blob

activestorage/app/models/active_storage/blob.rbの修正です。

blobのanalyze処理を行った際に、元々保持していたmetadataが失われしまった(analyze処理で設定されたmetadataで上書きされてしまっていた)のを、元のmetadataも保持するよう修正しています。


Sort mailer previews

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

mailer preivewを表示する際に、アルファベット順にソートした値を表示するよう修正しています。


Merge pull request #30782 from NickLaMuro/improve_performance_of_inflections

Active Supportの修正です。

ActiveSupport::Inflector.inflectionsで使用する正規表現を、メソッド呼び出しごとに毎回生成していたのを、変数で保持して値をキャッシュするよう修正しています。

合わせて、不要なActiveSupport::Inflector.inflections.acronym_regex attributeをdeprecateにしています。内部でだけで使用している想定のattributeだったのですが、どうにも誤ってdocに乗ってしまった為、削除前にdeprecateにしています。


Built-in Redis cache store

Active Supportの修正です。

Railsがデフォルトでサポートするcache storeに、Redis cache storeを追加しています。

他のcache store同様に、:namespace、:compress、:compress_threshold, :expires_in, :race_condition_tool等のオプションが指定出来るようになっています。

また、driverが指定出来るようになっており、redis-rbだけでなく、hiredis, Redis::Distributedが指定出来るようになっています。

config.cache_store = :redis_cache_store, driver: :hiredis,
  namespace: 'myapp-cache', compress: true, timeout: 1,
  url: "redis://:#{cache_password}@myapp-cache-1:6379/0"

rails guideのCaching with Rails: An Overviewに説明が追加されているので、詳細はそちらを参照。Caching with Rails: An Overview


Add environment as dependency of load_config (#31135)

activerecord/lib/active_record/railties/databases.rakeの修正です。

load_config taskのdependencyenvironment taskを追加しています。

元々一部taskではenvironment taskが実行されていなかった(environmentファイルがロードされてなかった)のですが、それだと、例えばdatabase.ymlにencrypted secretsを使用している場合に問題になる(environmentファイルがロードされない、という事はread_encrypted_secretsがtrueになる事が無い)為、一通りのtaskでenvironmentファイルのロード処理が行われるようにする為に、load_config taskのdependencyenvironment taskを追加しています。


Cache: Enable compression by default for values > 1kB.

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

cache storeで、cacheされる値が1kb以上の場合デフォルトでcompress処理を行うよう修正しています。

元々はCPUコストを気にしてデフォルトはdisableになっていたのですが、今はそこのコストが低くなったのを、よくcacheに入れるHTMLやJSONは圧縮率が良い為、デフォルトでenableにした、との事です。


Merge pull request #27947 from mastahyeti/unsafe_raw_sql

Active Recordの修正です。

orderpluckの引数に生SQLをそのまま渡すのがdeprecateになりました。

User.order("LENGTH(name)")
# => DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "LENGTH(name)". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql().

上記のような事をやりたい場合、Arel.sqlメソッドでラップしてあげる必要があります。

User.order(Arel.sql("LENGTH(name)"))

Article.order(params[:my_order])のような形でquery methodを使用した場合に、SQL injectionが起こるのを防ぐ為、との事です。

5.2時点ではdeprecateメッセージが表示されるだけですが、6.0ではUnknownAttributeReferenceがraiseされるようになります。引続き生SQLを渡すようにしたい場合、ActiveRecord::Base.allow_unsafe_raw_sql:disabledを指定すればOKになっています。


Update layouts_and_rendering.md [ci skip]

rails guideのLayouts and Rendering in Railsの修正です。

Local Variablesの項のcounter変数についての説明の言い回しを修正しています。


Update exists? documentation

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

exists?メソッドのdocにrelationとチェインして使用した場合のexample(Person.where(name: 'Spartacus', rating: 4).exists?)を追加しています。