なるようになるブログ

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

rails commit log流し読み(2018/02/15)

2018/02/15分のコミットです。

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

activerecord/CHANGELOG.md


Don't force people to upgrade i18n gem

activesupport/activesupport.gemspecの修正です。

Bump i18n to 1.0i18n gemを更新した際、バージョン1系を強制するようになっていたのですが、0.x系も使用できるよう、バージョン指定を緩めています。


Rdoc formatting fix: b instead of MD-style asterisks

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

find_or_create_byのdocでフォーマットが崩れていた箇所があったのを修正しています。


Merge pull request #30941 from toptal/introduce-custom-serializers-to-activejob-arguments

Active Jobの修正です。

jobの引数に任意のオブジェクトを渡せるようにするための仕組みを追加しています。

元々はRails側で管理されているクラス(String、Active Recordのmodel、HashWithIndifferentAccess等々)のオブジェクトしかjobの引数にしか渡せませんでした。ただ、ユーザが作成した独自のクラスのオブジェクト渡せないのは不便だろう、という事で、独自のクラスのオブジェクトも渡せるようになりました。

独自のクラスのオブジェクトを渡したい場合、そのオブジェクトのserialize / deserialize処理を定義した独自のserializerクラスを作る必要があります。

例。

class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
  # Check if this object should be serialized using this serializer.
  def serialize?(argument)
    argument.is_a? Money
  end

  # Convert an object to a simpler representative using supported object types.
  # The recommended representative is a Hash with a specific key. Keys can be of basic types only.
  # You should call `super` to add the custom serializer type to the hash
  def serialize(object)
    super(
      "cents" => object.cents,
      "currency" => object.currency
    )
  end

  # Convert serialized value into a proper object
  def deserialize(hash)
    Money.new hash["cents"], hash["currency"]
  end
end

上記のように作成した serializerクラスを、Rails.application.config.active_job.custom_serializers << MoneySerializer として、custom_serializersに追加すればOKです(今ちょっと挙動バグっているようですが)。


Use require_dependency inside Active Storage

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

Active Storage内部で使用しているクラスをロードするのにrequire_dependencyを使用してファイルを明示的に指定するよう修正しています。

同じ名前のクラスがアプリやgemにあった場合に、そちらのクラスが読み込まれてしまう事がある為。


Merge pull request #31866 from fatkodima/redis_cache-connection_pool

Active Supportの修正です。

再度redis cache storeにconnection poolingのサポートを追加しています。

driverにRedis::Distributedを指定した場合に正しく動作しないバグがあった為、Revert "Merge pull request #31447 from fatkodima/redis_cache-connection_pool"でrevertされていましたが、左記バグの対応が完了した為。


Add #create_or_find_by to lean on unique constraints (#31989)

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

ActiveRecord::Relationcreate_or_find_byメソッド、及び、create_or_find_by!メソッドを追加しています。名前の通り、左記にcreateしてレコードが既にある場合(ActiveRecord::RecordNotUniqueでエラーになった場合)にfindを実行するメソッドです。

def create_or_find_by(attributes, &block)
  transaction(requires_new: true) { create(attributes, &block) }
rescue ActiveRecord::RecordNotUnique
  find_by!(attributes)
end

find_or_create_byだとfind(select)'とcreate(insert)`の間にレコードが追加されてしまった場合にエラーになってしまうので、そのようなケースを避ける為に追加されています。


Missing backquote, extra end keyword [ci skip]

rails guideのActive Job Basicsの修正です。

Supported types for argumentsの項でクォートが不足していたのを修正、及び、Serializersの項のexampleコードで不要なendがあったのを削除しています。