From f323a8fed43696c082c326d795f28dee5a68a480 Mon Sep 17 00:00:00 2001 From: simply-phi Date: Sun, 3 Apr 2011 04:17:26 -0700 Subject: Added information about default values --- actionmailer/README.rdoc | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'actionmailer') diff --git a/actionmailer/README.rdoc b/actionmailer/README.rdoc index 14d20bb08d..3789a75021 100644 --- a/actionmailer/README.rdoc +++ b/actionmailer/README.rdoc @@ -72,6 +72,87 @@ Or you can just chain the methods together like: Notifier.welcome.deliver # Creates the email and sends it immediately +== Setting defaults + +Sometimes you have an Action Mailer class with more than one method for sending e-mails. Think of an authentication system in which you would like to send users a welcome message after sign up, a forgot your password message and a message to send when the user closes his account. Your class would look something like this. + +Example: + + class Authenticationmailer < ActionMailer::Base + def signed_up(user) + # prepare the view + .... + + # and send the e-mail + mail(:to => user.email, + :subject => "Welcome to our awesome application!", + :from => "awesome@application.com") + end + + def forgot_password(user) + # prepare the view + .... + + mail(:to => user.email, + :subject => "Forgot your password? No worry, we're awesome at that too!", + :from => "awesome@application.com") + end + + def closed_account(user) + # prepare the view + .... + + mail(:to => user.email, + :subject => "Closing your account, are you? That's not awesome, dude!", + :from => "awesome@application.com") + end + end + +Now this works fine, but it would be nice if we could remove the :from from the method, seeing that it is a static value that is the same across all the methods, and just assign it once. Introducing the default method. With this method you can assign default values that will be used by all of the mail methods. Now you can refactor the above example to just assign the :from value only once. + +Example: + + class Authenticationmailer < ActionMailer::Base + default :from => "awesome@application.com" + + def signed_up(user) + # prepare the view + .... + + # and send the e-mail + mail(:to => user.email, + :subject => "Welcome to our awesome application!") + end + + def forgot_password(user) + # prepare the view + .... + + mail(:to => user.email, + :subject => "Forgot your password? No worry, we're awesome at that too!") + end + + def closed_account(user) + # prepare the view + .... + + mail(:to => user.email, + :subject => "Closing your account, are you? That's not awesome, dude!") + end + end + +The default method takes a Hash, so it is possible to assign more values in one method. + +Example: + + class Authenticationmailer < ActionMailer::Base + default :from => "awesome@application.com", :subject => "Default subject" + + ..... + end + +The default value is overwritten if you use them in the mail method. + == Receiving emails To receive emails, you need to implement a public instance method called receive that takes an -- cgit v1.2.3 From ac07da8fc72b7a57fd4a60c0dcb5b777d85f9eb7 Mon Sep 17 00:00:00 2001 From: simply-phi Date: Sun, 3 Apr 2011 09:12:07 -0700 Subject: Made the defaults section a little more readable and more to the point, giving a overview of the possibilities. --- actionmailer/README.rdoc | 74 ++---------------------------------------------- 1 file changed, 3 insertions(+), 71 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/README.rdoc b/actionmailer/README.rdoc index 3789a75021..9b206fbcc7 100644 --- a/actionmailer/README.rdoc +++ b/actionmailer/README.rdoc @@ -74,85 +74,17 @@ Or you can just chain the methods together like: == Setting defaults -Sometimes you have an Action Mailer class with more than one method for sending e-mails. Think of an authentication system in which you would like to send users a welcome message after sign up, a forgot your password message and a message to send when the user closes his account. Your class would look something like this. +It is possible to set default values that will be used in every method in your Action Mailer class. To implement this functionality, you just call the public class method default which you get for free from ActionMailer::Base. This method accepts a Hash as the parameter. You can use any of the headers e-mail messages has, like :from as the key. You can also pass in a string as the key, like "Content-Type", but Action Mailer does this out of the box for you, so you wont need to worry about that. Finally it is also possible to pass in a Proc that will get evaluated when it is needed. -Example: - - class Authenticationmailer < ActionMailer::Base - def signed_up(user) - # prepare the view - .... - - # and send the e-mail - mail(:to => user.email, - :subject => "Welcome to our awesome application!", - :from => "awesome@application.com") - end - - def forgot_password(user) - # prepare the view - .... - - mail(:to => user.email, - :subject => "Forgot your password? No worry, we're awesome at that too!", - :from => "awesome@application.com") - end - - def closed_account(user) - # prepare the view - .... - - mail(:to => user.email, - :subject => "Closing your account, are you? That's not awesome, dude!", - :from => "awesome@application.com") - end - end - -Now this works fine, but it would be nice if we could remove the :from from the method, seeing that it is a static value that is the same across all the methods, and just assign it once. Introducing the default method. With this method you can assign default values that will be used by all of the mail methods. Now you can refactor the above example to just assign the :from value only once. +Note that every value you set with this method will get over written if you use the same key in your mailer method. Example: class Authenticationmailer < ActionMailer::Base - default :from => "awesome@application.com" - - def signed_up(user) - # prepare the view - .... - - # and send the e-mail - mail(:to => user.email, - :subject => "Welcome to our awesome application!") - end - - def forgot_password(user) - # prepare the view - .... - - mail(:to => user.email, - :subject => "Forgot your password? No worry, we're awesome at that too!") - end - - def closed_account(user) - # prepare the view - .... - - mail(:to => user.email, - :subject => "Closing your account, are you? That's not awesome, dude!") - end - end - -The default method takes a Hash, so it is possible to assign more values in one method. - -Example: - - class Authenticationmailer < ActionMailer::Base - default :from => "awesome@application.com", :subject => "Default subject" - + default :from => "awesome@application.com", :subject => Proc.new { "E-mail was generated at #{Time.now}" } ..... end -The default value is overwritten if you use them in the mail method. - == Receiving emails To receive emails, you need to implement a public instance method called receive that takes an -- cgit v1.2.3