Action Mailer, go Proc thyself

2 May 2010

On my recent Rails Dispatch screen cast, I had a seemingly innocent example of a default header, using Time.now to insert a time stamp.

It was an innocent thing, and truthfully, I had it there more as an example than something I would do in production.

Nevertheless, good Ruby aficionados will recognise the mistake from a thousand miles away, the default hash inside of Action Mailer gets read, parsed and evaluated at compile time, not on a per message basis… which makes it basically useless as a Time stamp.

Rick DeNatale pointed this out in a question to me at RailsDispatch, he also asked if Action Mailer supports Proc objects inside of the default hash.

Sadly, the answer was no.

But then a few hours later it was yes!

So now you can pass in Proc objects to your Action Mailer class default method, like so:

class Notifier < ActionMailer::Base
  default :to => 'system@test.lindsaar.net',
          :subject => Proc.new { give_a_greeting }

  def welcome
    mail
  end

  private

  def give_a_greeting
    "This is a greeting at #{Time.now}"
  end
end

Which would then call the give_a_greeting method on your Mailer instance to create a value to assign to the subject field.

Enjoy!

blogLater

Mikel

results matching ""

    No results matching ""