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
Leave a Reply
Latest posts
- Encrypting Another Partition Using FileVault 2 on OSX Lion
- Installing Home Folder on Second Drive on OSX Lion
- undefined local variable or method `version_requirements'
- A New World of Resources
- Rails Static Pages
- Twitter Replacing Rails? So?
- Engine Yard Cloud Backups Generating Zero Length Backups
- Our Rails Rumble Entry - StillAlive.com
- Renaming a controller and redirection in Rails 3
- Updating RailsPlugins.org to Rails 3 - Part 1
- A new protocol for social interaction
- What is a distributed social network?
- Bundler and Public Git Sources
- Getting Heroku, memcached and Rails 3 working
- Why Bundler?
- Rails Commit Access
- Introducing TellThemWhen
- rake RSpec & Cucumber uninitialized constant Rails::Boot::Bundler
- This Relationship is Worth Nothing
- Thank YOU...
- Inline Attachments for ActionMailer
- Upgrading RailsPlugins.org to Rails 3 - Part 1
- Stripping dollar signs and commas from a string
- Getting Rails 3 Edge with jQuery, RSpec and Cucumber using RVM
- The Real News Donation Drive
- ActionMailer ScreenCast and Article
- Installing RSpec for Rails 3
- I am speaking at RailsConf 2010
- Rails 3 Session Secret and Session Store
- If you're lazy and you know it write your specs!
- Bundler - uninitialized constant ActionController
- Bundle Me Some Sanity
- How to use Mail / ActionMailer 3 with GMail SMTP
- Put your mailer where the action is!
- Why Force a Choice?
- How to make an RSS feed in Rails
- Rails 3 Routing with Rack
- Bundle me some Rails
- Helping out in Haiti
- Watch your self
- Is Rails 3.0 a Game Changer?
- Where did the scripts go?
- validates :rails_3, :awesome => true
- New Rails Version 3.0 Guides Online
- New ActionMailer API in Rails 3.0
- Mail gem version 2 released
- How to rename a Rails 3 Application
- Rails 3.0 Examples
- DECCA Driving Day
Latest comments
- Quantspring.com
This is a really great post I f...
- Maxine
Maybe it has a connection with ...
- Axel W
Had the same problem lots of ti...
- cloud computing anbieter
I was there and I see you confe...
- cloud computing anbieter
I was there and I see you confe...
Categories
Tag Cloud
AJAX ARGH! ActiveRecord Ajax Apache Apple Asterisk Australia Copy Database Development Feedburner Gem server Google Human Rights Javascript L. Ron Hubbard MS SQL Server MacOSX Mail Mephisto Not Programming OpenBSD Opensource Performance Personal Integrity PostgreSQL Programming Prototype Puzzle RDoc REST RESTful Rails RSPec RSpec Rails Rails Tips Rspec Ruby Ruby on Rails Ruby on Rails Tips Ruby on rails Tips SQL SQLServer SVN Scientologist Scientology Site Stats Soekris Soekris net5501 TMail Textmate Tips Windows World about mikel anti drug apache contributing daemon documentation drugs illustrator javascript lambda mail mephisto newspapers nitro open source opensource photoshop productivity programming railscasts rspec ruby ruby on rails rubyforge scientology seo sitemap sqlserver tips tmail tom cruise unix tricks vector graphicsArchives
- November 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (0)
- July 2009 (1)
- June 2009 (0)
- May 2009 (1)
- April 2009 (0)
- March 2009 (0)
- February 2009 (0)
- January 2009 (2)
- December 2008 (0)
- November 2008 (5)
- October 2008 (0)
- September 2008 (1)
- August 2008 (0)
- July 2008 (2)
- June 2008 (13)
- May 2008 (7)
- April 2008 (18)
- March 2008 (8)
- February 2008 (5)
- January 2008 (7)
- December 2007 (20)
- November 2007 (22)




Tue Nov 08 19:41:28 -0800 2011
Your post has been linked at the Drink Rails blog.
Mon May 03 08:46:43 -0700 2010
Your post has been linked at the Drink Rails blog.
Mon May 03 00:04:23 -0700 2010
What about
default :subject => :give_a_greeting
as a shortcut for
default :subject => Proc.new { give_a_greeting }
Fri Oct 07 00:21:33 -0700 2011
I have tried your code and works perfect. Thanks a lot mate :)
Mon Feb 27 23:48:47 -0800 2012
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
Wed Feb 29 20:36:24 -0800 2012
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
Thu Mar 15 16:41:27 -0700 2012
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.
Sun Mar 25 02:50:18 -0700 2012
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
Tue Mar 27 01:24:31 -0700 2012 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
Tue Mar 27 01:24:48 -0700 2012 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
Tue Mar 27 08:10:47 -0700 2012
Congratulations. Really astounded with the caliber of the advice are presented. I sincerely hope that you keep up with the brilliant work accomplished.
Sun Apr 22 01:34:44 -0700 2012
I will try to adapt it. I am sure that it will be possible,
Mon Apr 23 01:47:39 -0700 2012
For me this is a really nice argument and I will use the feature for sure in the near future.