Action Mailer, go Proc thyself

Mon May 03 10:11:00 -0700 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

  1. sabieaspo Says:

    Your post has been linked at the Drink Rails blog.

  2. Sohan Says:

    Your post has been linked at the Drink Rails blog.

  3. Martin Says:

    What about

    default :subject => :give_a_greeting

    as a shortcut for

    default :subject => Proc.new { give_a_greeting }

  4. Culinar Says:

    I have tried your code and works perfect. Thanks a lot mate :)

  5. natural mole removal Says:

    Interesting post and thanks for sharing. Some things in here I have not thought about before.Thanks for making such a cool post which is really very well written. Will be referring a lot of friends about this. Keep blogging

  6. How to Get Scholarships Says:

    Thanks for making such a cool post which is really very well written. How to Get Scholarships Will be referring a lot of friends about this. Keep blogging

  7. Diablo 3 leveling guide reviews Says:

    Youtube . com megapersonalities can be any well-known 1. It will affect Bieber assault. Therefore, the particular followers foundation regarding him is several and throughout the world.

  8. bestowadeel Says:

    This is really a nice blog. I have read many blogs but this one really satisfies your real needs. I just love to bookmark this page. Really was not expecting that when I started off studying.Security in Slough | Security in St Albans | Security Companies London | Security in Crawley | Security in St Davids | Car breakers in Birmingham

  9. jacky32 Says:
    They often come about when the hair is cut under the skin during shaving, waxing, or any other hair removal procedure. It will then not be able to grow out of the skin and this result in ingrown hairs. There are also cases where the hair curls back to the follicles. Swelling and redness on the area soon follow. An ingrown hair infection may range from mild to severe, in some cases, and may need medical attention. getiing rid of Ingrown hairs
  10. jacky32 Says:
    They often come about when the hair is cut under the skin during shaving, waxing, or any other hair removal procedure. It will then not be able to grow out of the skin and this result in ingrown hairs. There are also cases where the hair curls back to the follicles. Swelling and redness on the area soon follow. An ingrown hair infection may range from mild to severe, in some cases, and may need medical attention. getiing rid of Ingrown hairs
  11. music production Says:

    Congratulations. Really astounded with the caliber of the advice are presented. I sincerely hope that you keep up with the brilliant work accomplished.

  12. profiletimelinecover.com Says:

    I will try to adapt it. I am sure that it will be possible,

  13. charter greece Says:

    For me this is a really nice argument and I will use the feature for sure in the near future.

Leave a Reply