なるようになるブログ

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

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

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

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

actionmailer/CHANGELOG.md


Fix broken sample code for EventedFileUpdateChecker [ci skip]

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

ActiveSupport::EventedFileUpdateCheckerクラスのdoc内にあるexampleコードで、newメソッドの引数の渡し方がおかしかったのを修正、及び、クラス名をnamespace付きで記載するよう修正しています。


add combination of Ruby 2.4 + aj:integration to CI

.travis.ymlの修正です。

CIでRuby 2.4 + Active Job Integrationのテストが動いてなかった為、左記組み合わせのテストが動くよう修正しています。


make backburner integration test to work

activejob/test/support/integration/adapters/backburner.rbの修正です。

backburner adapterを使用してのActive Jobのintegration testで、beanstalkdが起動しているかどうかの確認にBackburner::Worker.connectionメソッドを使用していたのを、Backburner::Worker.newでworkerのインスタンス作成処理でチェックするよう修正しています。

Backburner::Worker.connectionメソッドがbackburner 1.2.0で削除されてしまい、beanstalkdが起動しているかどうかに関わらず必ずチェック処理が失敗するようになってしまっていた為、正しいメソッドでチェックするよう修正しています。


Offer the option to use parameterization for shared processing of headers and ivars (#27825) https://github.com/rails/rails/commit/1cec84ad2ddd843484ed40b1eb7492063ce71baf

Action Mailerの修正です。

2つの対応がまとめてコミットされています。まず、default HashにProcに加えてlambdaが使用出来るよう対応しています。

class NotifierMailer < ApplicationMailer
  default 'X-Special-Header' => Proc.new { my_method }, to: -> { @inviter.email_address }
end

もう一つ、mailerのメソッド間で共通の変数、前処理を使用出来るにする為のparameterized moduleを追加しています。

doc内のexampleより。

# parameterized module使用前

class InvitationsMailer < ApplicationMailer
  def account_invitation(inviter, invitee)
    @account = inviter.account
    @inviter = inviter
    @invitee = invitee

    subject = "#{@inviter.name} invited you to their Basecamp (#{@account.name})"

    mail \
      subject:   subject,
      to:        invitee.email_address,
      from:      common_address(inviter),
      reply_to:  inviter.email_address_with_name
  end

  def project_invitation(project, inviter, invitee)
    @account = inviter.account
    @project = project
    @inviter = inviter
    @invitee = invitee
    @summarizer = ProjectInvitationSummarizer.new(@project.bucket)

    subject = "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"

    mail \
      subject:   subject,
      to:        invitee.email_address,
      from:      common_address(inviter),
      reply_to:  inviter.email_address_with_name
  end

  def bulk_project_invitation(projects, inviter, invitee)
    @account  = inviter.account
    @projects = projects.sort_by(&:name)
    @inviter  = inviter
    @invitee  = invitee

    subject = "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})"

    mail \
      subject:   subject,
      to:        invitee.email_address,
      from:      common_address(inviter),
      reply_to:  inviter.email_address_with_name
  end
end

InvitationsMailer.account_invitation(person_a, person_b).deliver_later

それぞれのメソッドでインスタンス変数を初期化するようになっていますが、parameterized moduleを使用すると下記のように書きなおす事が出来ます。

# parameterized module使用前

class InvitationsMailer < ApplicationMailer
  before_action { @inviter, @invitee = params[:inviter], params[:invitee] }
  before_action { @account = params[:inviter].account }

  default to:       -> { @invitee.email_address },
          from:     -> { common_address(@inviter) },
          reply_to: -> { @inviter.email_address_with_name }

  def account_invitation
    mail subject: "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
  end

  def project_invitation
    @project    = params[:project]
    @summarizer = ProjectInvitationSummarizer.new(@project.bucket)

     mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})"
  end

  def bulk_project_invitation
    @projects = params[:projects].sort_by(&:name)

    mail subject: "#{@inviter.name.familiar} added you to some new stuff in Basecamp (#{@account.name})"
  end
end

InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later

.withメソッドにmailer classの各メソッドで共通で使用出来る変数を指定出来るようになっています。また、withの引数に指定した値は、paramsメソッド経由で取得出来るようになっています。ぱっと見た時よくわからなかったのですが、慣れると便利そう。


Fix judgement spelling [ci skip]

rails guideのContributing to Ruby on Railsの修正です。

What about Feature Requests?の項のjudgementjudgmentに修正しています。Railsのdocはアメリカ英語を使用するようになっており、アメリカ英語ではjudgmentな為。なるほど。

参考:Judgement | Definition of Judgement by Merriam-Webster