From 99da42c29944beed0cb9e892ae90bfeb2590c57e Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 2 Apr 2011 00:35:33 -0300 Subject: Gem::Specification#has_rdoc= is deprecated since rubygems 1.7.0 --- actionmailer/actionmailer.gemspec | 2 -- 1 file changed, 2 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/actionmailer.gemspec b/actionmailer/actionmailer.gemspec index 2ae85f8b57..ee02cf6945 100644 --- a/actionmailer/actionmailer.gemspec +++ b/actionmailer/actionmailer.gemspec @@ -17,8 +17,6 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - s.has_rdoc = true - s.add_dependency('actionpack', version) s.add_dependency('mail', '~> 2.2.15') end -- cgit v1.2.3 From 9bf5cddd2835ba4f5d5597f421acf8c9f7385c3b Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 3 Apr 2011 16:59:37 +0900 Subject: s/ERb/ERB/g (part II) --- actionmailer/README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionmailer') diff --git a/actionmailer/README.rdoc b/actionmailer/README.rdoc index 0fa18d751b..14d20bb08d 100644 --- a/actionmailer/README.rdoc +++ b/actionmailer/README.rdoc @@ -32,7 +32,7 @@ This can be as simple as: end The body of the email is created by using an Action View template (regular -ERb) that has the instance variables that are declared in the mailer action. +ERB) that has the instance variables that are declared in the mailer action. So the corresponding body template for the method above could look like this: -- cgit v1.2.3 From 284ca810c17a89d15d809ba203289487044fcff9 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Sat, 2 Apr 2011 10:51:47 +0200 Subject: remove AM delegating register_observer and register_interceptor to Mail and instead implement smarter versions allowing for string class names, also added proper Railtie support with tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionmailer/lib/action_mailer/base.rb | 30 ++++++++++++++++++++--- actionmailer/lib/action_mailer/railtie.rb | 8 +++++-- actionmailer/test/base_test.rb | 40 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 16fcf112b7..c21fd764b9 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -4,6 +4,7 @@ require 'action_mailer/collector' require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/proc' +require 'active_support/core_ext/string/inflections' require 'action_mailer/log_subscriber' module ActionMailer #:nodoc: @@ -349,9 +350,6 @@ module ActionMailer #:nodoc: helper ActionMailer::MailHelper include ActionMailer::OldApi - delegate :register_observer, :to => Mail - delegate :register_interceptor, :to => Mail - private_class_method :new #:nodoc: class_attribute :default_params @@ -363,6 +361,32 @@ module ActionMailer #:nodoc: }.freeze class << self + # Register one or more Observers which will be notified when mail is delivered. + def register_observers(*observers) + observers.flatten.compact.each { |observer| register_observer(observer) } + end + + # Register one or more Interceptors which will be called before mail is sent. + def register_interceptors(*interceptors) + interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) } + end + + # Register an Observer which will be notified when mail is delivered. + # Either a class or a string can be passed in as the Observer. If a string is passed in + # it will be constantized. + def register_observer(observer) + delivery_observer = (observer.respond_to?(:delivered_email) ? observer : observer.constantize) + Mail.register_observer(delivery_observer) + end + + # Register an Inteceptor which will be called before mail is sent. + # Either a class or a string can be passed in as the Observer. If a string is passed in + # it will be constantized. + def register_interceptor(interceptor) + delivery_interceptor = (interceptor.respond_to?(:delivering_email) ? interceptor : interceptor.constantize) + Mail.register_interceptor(delivery_interceptor) + end + def mailer_name @mailer_name ||= name.underscore end diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb index 4ec478067f..444754d0e9 100644 --- a/actionmailer/lib/action_mailer/railtie.rb +++ b/actionmailer/lib/action_mailer/railtie.rb @@ -19,13 +19,17 @@ module ActionMailer options.stylesheets_dir ||= paths["public/stylesheets"].first # make sure readers methods get compiled - options.asset_path ||= app.config.asset_path - options.asset_host ||= app.config.asset_host + options.asset_path ||= app.config.asset_path + options.asset_host ||= app.config.asset_host ActiveSupport.on_load(:action_mailer) do include AbstractController::UrlFor extend ::AbstractController::Railties::RoutesHelpers.with(app.routes) include app.routes.mounted_helpers + + register_interceptors(options.delete(:interceptors)) + register_observers(options.delete(:observers)) + options.each { |k,v| send("#{k}=", v) } end end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 6a7931da8c..9fdd0e1ced 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -478,6 +478,11 @@ class BaseTest < ActiveSupport::TestCase end end + class MySecondObserver + def self.delivered_email(mail) + end + end + test "you can register an observer to the mail object that gets informed on email delivery" do ActionMailer::Base.register_observer(MyObserver) mail = BaseMailer.welcome @@ -485,11 +490,31 @@ class BaseTest < ActiveSupport::TestCase mail.deliver end + test "you can register an observer using its stringified name to the mail object that gets informed on email delivery" do + ActionMailer::Base.register_observer("BaseTest::MyObserver") + mail = BaseMailer.welcome + MyObserver.expects(:delivered_email).with(mail) + mail.deliver + end + + test "you can register multiple observers to the mail object that both get informed on email delivery" do + ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver) + mail = BaseMailer.welcome + MyObserver.expects(:delivered_email).with(mail) + MySecondObserver.expects(:delivered_email).with(mail) + mail.deliver + end + class MyInterceptor def self.delivering_email(mail) end end + class MySecondInterceptor + def self.delivering_email(mail) + end + end + test "you can register an interceptor to the mail object that gets passed the mail object before delivery" do ActionMailer::Base.register_interceptor(MyInterceptor) mail = BaseMailer.welcome @@ -497,6 +522,21 @@ class BaseTest < ActiveSupport::TestCase mail.deliver end + test "you can register an interceptor using its stringified name to the mail object that gets passed the mail object before delivery" do + ActionMailer::Base.register_interceptor("BaseTest::MyInterceptor") + mail = BaseMailer.welcome + MyInterceptor.expects(:delivering_email).with(mail) + mail.deliver + end + + test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do + ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor) + mail = BaseMailer.welcome + MyInterceptor.expects(:delivering_email).with(mail) + MySecondInterceptor.expects(:delivering_email).with(mail) + mail.deliver + end + test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do mail1 = ProcMailer.welcome yesterday = 1.day.ago -- cgit v1.2.3 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 From cb9e501a2893ec7e210a1de2550cb1ecf16d5c90 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Mon, 4 Apr 2011 19:26:41 +0800 Subject: AM register_interceptor and register_observer only constantize if the argument is a String --- actionmailer/lib/action_mailer/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionmailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index c21fd764b9..cd76383931 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -375,7 +375,7 @@ module ActionMailer #:nodoc: # Either a class or a string can be passed in as the Observer. If a string is passed in # it will be constantized. def register_observer(observer) - delivery_observer = (observer.respond_to?(:delivered_email) ? observer : observer.constantize) + delivery_observer = (observer.is_a?(String) ? observer.constantize : observer) Mail.register_observer(delivery_observer) end @@ -383,7 +383,7 @@ module ActionMailer #:nodoc: # Either a class or a string can be passed in as the Observer. If a string is passed in # it will be constantized. def register_interceptor(interceptor) - delivery_interceptor = (interceptor.respond_to?(:delivering_email) ? interceptor : interceptor.constantize) + delivery_interceptor = (interceptor.is_a?(String) ? interceptor.constantize : interceptor) Mail.register_interceptor(delivery_interceptor) end -- cgit v1.2.3