Skip to content
This repository
Browse code

remove AM delegating register_observer and register_interceptor to Ma…

…il and instead implement smarter versions allowing for string class names, also added proper Railtie support with tests

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit e6bc81837fd14e365a9ea7b737b13889b8536105 1 parent 30264bb
Josh Kalderimis authored April 04, 2011 josevalim committed April 05, 2011
29  actionmailer/lib/action_mailer/base.rb
@@ -4,6 +4,7 @@
4 4
 require 'active_support/core_ext/array/wrap'
5 5
 require 'active_support/core_ext/object/blank'
6 6
 require 'active_support/core_ext/proc'
  7
+require 'active_support/core_ext/string/inflections'
7 8
 require 'action_mailer/log_subscriber'
8 9
 
9 10
 module ActionMailer #:nodoc:
@@ -348,9 +349,6 @@ class Base < AbstractController::Base
348 349
     include ActionMailer::OldApi
349 350
     include ActionMailer::DeprecatedApi
350 351
 
351  
-    delegate :register_observer, :to => Mail
352  
-    delegate :register_interceptor, :to => Mail
353  
-
354 352
     private_class_method :new #:nodoc:
355 353
 
356 354
     class_attribute :default_params
@@ -362,6 +360,31 @@ class Base < AbstractController::Base
362 360
     }.freeze
363 361
 
364 362
     class << self
  363
+      # Register one or more Observers which will be notified when mail is delivered.
  364
+      def register_observers(*observers)
  365
+        observers.flatten.compact.each { |observer| register_observer(observer) }
  366
+      end
  367
+
  368
+      # Register one or more Interceptors which will be called before mail is sent.
  369
+      def register_interceptors(*interceptors)
  370
+        interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) }
  371
+      end
  372
+
  373
+      # Register an Observer which will be notified when mail is delivered.
  374
+      # Either a class or a string can be passed in as the Observer. If a string is passed in
  375
+      # it will be <tt>constantize</tt>d.
  376
+      def register_observer(observer)
  377
+        delivery_observer = (observer.is_a?(String) ? observer.constantize : observer)
  378
+        Mail.register_observer(delivery_observer)
  379
+      end
  380
+
  381
+      # Register an Inteceptor which will be called before mail is sent.
  382
+      # Either a class or a string can be passed in as the Observer. If a string is passed in
  383
+      # it will be <tt>constantize</tt>d.
  384
+      def register_interceptor(interceptor)
  385
+        delivery_interceptor = (interceptor.is_a?(String) ? interceptor.constantize : interceptor)
  386
+        Mail.register_interceptor(delivery_interceptor)
  387
+      end
365 388
 
366 389
       def mailer_name
367 390
         @mailer_name ||= name.underscore
4  actionmailer/lib/action_mailer/railtie.rb
@@ -19,6 +19,10 @@ class Railtie < Rails::Railtie
19 19
 
20 20
       ActiveSupport.on_load(:action_mailer) do
21 21
         include app.routes.url_helpers
  22
+
  23
+        register_interceptors(options.delete(:interceptors))
  24
+        register_observers(options.delete(:observers))
  25
+
22 26
         options.each { |k,v| send("#{k}=", v) }
23 27
       end
24 28
     end
40  actionmailer/test/base_test.rb
@@ -493,6 +493,11 @@ def self.delivered_email(mail)
493 493
     end
494 494
   end
495 495
 
  496
+  class MySecondObserver
  497
+    def self.delivered_email(mail)
  498
+    end
  499
+  end
  500
+
496 501
   test "you can register an observer to the mail object that gets informed on email delivery" do
497 502
     ActionMailer::Base.register_observer(MyObserver)
498 503
     mail = BaseMailer.welcome
@@ -500,11 +505,31 @@ def self.delivered_email(mail)
500 505
     mail.deliver
501 506
   end
502 507
 
  508
+  test "you can register an observer using its stringified name to the mail object that gets informed on email delivery" do
  509
+    ActionMailer::Base.register_observer("BaseTest::MyObserver")
  510
+    mail = BaseMailer.welcome
  511
+    MyObserver.expects(:delivered_email).with(mail)
  512
+    mail.deliver
  513
+  end
  514
+
  515
+  test "you can register multiple observers to the mail object that both get informed on email delivery" do
  516
+    ActionMailer::Base.register_observers("BaseTest::MyObserver", MySecondObserver)
  517
+    mail = BaseMailer.welcome
  518
+    MyObserver.expects(:delivered_email).with(mail)
  519
+    MySecondObserver.expects(:delivered_email).with(mail)
  520
+    mail.deliver
  521
+  end
  522
+
503 523
   class MyInterceptor
504 524
     def self.delivering_email(mail)
505 525
     end
506 526
   end
507 527
 
  528
+  class MySecondInterceptor
  529
+    def self.delivering_email(mail)
  530
+    end
  531
+  end
  532
+
508 533
   test "you can register an interceptor to the mail object that gets passed the mail object before delivery" do
509 534
     ActionMailer::Base.register_interceptor(MyInterceptor)
510 535
     mail = BaseMailer.welcome
@@ -512,6 +537,21 @@ def self.delivering_email(mail)
512 537
     mail.deliver
513 538
   end
514 539
 
  540
+  test "you can register an interceptor using its stringified name to the mail object that gets passed the mail object before delivery" do
  541
+    ActionMailer::Base.register_interceptor("BaseTest::MyInterceptor")
  542
+    mail = BaseMailer.welcome
  543
+    MyInterceptor.expects(:delivering_email).with(mail)
  544
+    mail.deliver
  545
+  end
  546
+
  547
+  test "you can register multiple interceptors to the mail object that both get passed the mail object before delivery" do
  548
+    ActionMailer::Base.register_interceptors("BaseTest::MyInterceptor", MySecondInterceptor)
  549
+    mail = BaseMailer.welcome
  550
+    MyInterceptor.expects(:delivering_email).with(mail)
  551
+    MySecondInterceptor.expects(:delivering_email).with(mail)
  552
+    mail.deliver
  553
+  end
  554
+
515 555
   test "being able to put proc's into the defaults hash and they get evaluated on mail sending" do
516 556
     mail1 = ProcMailer.welcome
517 557
     yesterday = 1.day.ago
65  railties/test/application/configuration_test.rb
... ...
@@ -1,5 +1,18 @@
1 1
 require "isolation/abstract_unit"
2 2
 
  3
+class ::MyMailInterceptor
  4
+  def self.delivering_email(email); email; end
  5
+end
  6
+
  7
+class ::MyOtherMailInterceptor < ::MyMailInterceptor; end
  8
+
  9
+class ::MyMailObserver
  10
+  def self.delivered_email(email); email; end
  11
+end
  12
+
  13
+class ::MyOtherMailObserver < ::MyMailObserver; end
  14
+
  15
+
3 16
 module ApplicationTests
4 17
   class ConfigurationTest < Test::Unit::TestCase
5 18
     include ActiveSupport::Testing::Isolation
@@ -257,6 +270,58 @@ def index
257 270
       assert_equal res, last_response.body # value should be unchanged
258 271
     end
259 272
 
  273
+    test "registers interceptors with ActionMailer" do
  274
+      add_to_config <<-RUBY
  275
+        config.action_mailer.interceptors = MyMailInterceptor
  276
+      RUBY
  277
+
  278
+      require "#{app_path}/config/environment"
  279
+      require "mail"
  280
+
  281
+      ActionMailer::Base
  282
+
  283
+      assert_equal [::MyMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
  284
+    end
  285
+
  286
+    test "registers multiple interceptors with ActionMailer" do
  287
+      add_to_config <<-RUBY
  288
+        config.action_mailer.interceptors = [MyMailInterceptor, "MyOtherMailInterceptor"]
  289
+      RUBY
  290
+
  291
+      require "#{app_path}/config/environment"
  292
+      require "mail"
  293
+
  294
+      ActionMailer::Base
  295
+
  296
+      assert_equal [::MyMailInterceptor, ::MyOtherMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
  297
+    end
  298
+
  299
+    test "registers observers with ActionMailer" do
  300
+      add_to_config <<-RUBY
  301
+        config.action_mailer.observers = MyMailObserver
  302
+      RUBY
  303
+
  304
+      require "#{app_path}/config/environment"
  305
+      require "mail"
  306
+
  307
+      ActionMailer::Base
  308
+
  309
+      assert_equal [::MyMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
  310
+    end
  311
+
  312
+    test "registers multiple observers with ActionMailer" do
  313
+      add_to_config <<-RUBY
  314
+        config.action_mailer.observers = [MyMailObserver, "MyOtherMailObserver"]
  315
+      RUBY
  316
+
  317
+      require "#{app_path}/config/environment"
  318
+      require "mail"
  319
+
  320
+      ActionMailer::Base
  321
+
  322
+      assert_equal [::MyMailObserver, ::MyOtherMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
  323
+    end
  324
+
260 325
     test "config.action_controller.perform_caching = false" do
261 326
       make_basic_app do |app|
262 327
         app.config.action_controller.perform_caching = false

0 notes on commit e6bc818

Please sign in to comment.
Something went wrong with that request. Please try again.