Skip to content
This repository
Browse code

Allow ActionMailer subclasses to individually set their delivery meth…

…od (so two subclasses can have different delivery methods) (closes #10033) [zdennis]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8111 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 57cde631383a8a0fa8d231ac1ae85ea725e12cd5 1 parent 6b018e3
David Heinemeier Hansson authored November 07, 2007
2  actionmailer/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Allow ActionMailer subclasses to individually set their delivery method (so two subclasses can have different delivery methods) #10033 [zdennis]
  4
+
3 5
 * Update TMail to v1.1.0.  Use an updated version of TMail if available.  [mikel]
4 6
 
5 7
 * Introduce a new base test class for testing Mailers.  ActionMailer::TestCase [Koz]
4  actionmailer/lib/action_mailer/base.rb
@@ -248,8 +248,8 @@ class Base
248 248
     @@raise_delivery_errors = true
249 249
     cattr_accessor :raise_delivery_errors
250 250
 
251  
-    @@delivery_method = :smtp
252  
-    cattr_accessor :delivery_method
  251
+    superclass_delegating_accessor :delivery_method
  252
+    self.delivery_method = :smtp
253 253
     
254 254
     @@perform_deliveries = true
255 255
     cattr_accessor :perform_deliveries
9  actionmailer/test/abstract_unit.rb
@@ -38,3 +38,12 @@ def uses_mocha(test_name)
38 38
 rescue Gem::LoadError
39 39
   $stderr.puts "Skipping #{test_name} tests (Mocha >= 0.5 is required). `gem install mocha` and try again."
40 40
 end
  41
+
  42
+def set_delivery_method(delivery_method)
  43
+  @old_delivery_method = ActionMailer::Base.delivery_method
  44
+  ActionMailer::Base.delivery_method = delivery_method
  45
+end
  46
+
  47
+def restore_delivery_method
  48
+  ActionMailer::Base.delivery_method = @old_delivery_method
  49
+end
51  actionmailer/test/delivery_method_test.rb
... ...
@@ -0,0 +1,51 @@
  1
+require "#{File.dirname(__FILE__)}/abstract_unit"
  2
+
  3
+class DefaultDeliveryMethodMailer < ActionMailer::Base
  4
+end
  5
+
  6
+class NonDefaultDeliveryMethodMailer < ActionMailer::Base
  7
+  self.delivery_method = :sendmail
  8
+end
  9
+
  10
+class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
  11
+  def setup
  12
+    set_delivery_method :smtp
  13
+  end
  14
+  
  15
+  def teardown
  16
+    restore_delivery_method
  17
+  end
  18
+
  19
+  def test_should_be_the_default_smtp
  20
+    assert_equal :smtp, ActionMailer::Base.delivery_method
  21
+  end
  22
+end
  23
+
  24
+class DefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
  25
+  def setup
  26
+    set_delivery_method :smtp
  27
+  end
  28
+  
  29
+  def teardown
  30
+    restore_delivery_method
  31
+  end
  32
+  
  33
+  def test_should_be_the_default_smtp
  34
+    assert_equal :smtp, DefaultDeliveryMethodMailer.delivery_method
  35
+  end
  36
+end
  37
+
  38
+class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
  39
+  def setup
  40
+    set_delivery_method :smtp
  41
+  end
  42
+  
  43
+  def teardown
  44
+    restore_delivery_method
  45
+  end
  46
+
  47
+  def test_should_be_the_set_delivery_method
  48
+    assert_equal :sendmail, NonDefaultDeliveryMethodMailer.delivery_method
  49
+  end
  50
+end
  51
+
8  actionmailer/test/mail_helper_test.rb
@@ -60,13 +60,17 @@ def new_mail( charset="utf-8" )
60 60
   end
61 61
 
62 62
   def setup
63  
-    ActionMailer::Base.delivery_method = :test
  63
+    set_delivery_method :test
64 64
     ActionMailer::Base.perform_deliveries = true
65 65
     ActionMailer::Base.deliveries = []
66 66
 
67 67
     @recipient = 'test@localhost'
68 68
   end
69  
-
  69
+  
  70
+  def teardown
  71
+    restore_delivery_method
  72
+  end
  73
+  
70 74
   def test_use_helper
71 75
     mail = HelperMailer.create_use_helper(@recipient)
72 76
     assert_match %r{Mr. Joe Person}, mail.encoded
12  actionmailer/test/mail_render_test.rb
@@ -45,13 +45,17 @@ def share(recipient)
45 45
 
46 46
 class RenderHelperTest < Test::Unit::TestCase
47 47
   def setup
48  
-    ActionMailer::Base.delivery_method = :test
  48
+    set_delivery_method :test
49 49
     ActionMailer::Base.perform_deliveries = true
50 50
     ActionMailer::Base.deliveries = []
51 51
 
52 52
     @recipient = 'test@localhost'
53 53
   end
54 54
 
  55
+  def teardown
  56
+    restore_delivery_method
  57
+  end
  58
+
55 59
   def test_inline_template
56 60
     mail = RenderMailer.create_inline_template(@recipient)
57 61
     assert_equal "Hello, Earth", mail.body.strip
@@ -70,13 +74,17 @@ def test_rxml_template
70 74
 
71 75
 class FirstSecondHelperTest < Test::Unit::TestCase
72 76
   def setup
73  
-    ActionMailer::Base.delivery_method = :test
  77
+    set_delivery_method :test
74 78
     ActionMailer::Base.perform_deliveries = true
75 79
     ActionMailer::Base.deliveries = []
76 80
 
77 81
     @recipient = 'test@localhost'
78 82
   end
79 83
 
  84
+  def teardown
  85
+    restore_delivery_method
  86
+  end
  87
+
80 88
   def test_ordering
81 89
     mail = FirstMailer.create_share(@recipient)
82 90
     assert_equal "first mail", mail.body.strip
11  actionmailer/test/mail_service_test.rb
@@ -282,7 +282,7 @@ def new_mail( charset="utf-8" )
282 282
 
283 283
   # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
284 284
   def setup
285  
-    ActionMailer::Base.delivery_method = :test
  285
+    set_delivery_method :test
286 286
     ActionMailer::Base.perform_deliveries = true
287 287
     ActionMailer::Base.raise_delivery_errors = true
288 288
     ActionMailer::Base.deliveries = []
@@ -290,9 +290,10 @@ def setup
290 290
     @original_logger = TestMailer.logger
291 291
     @recipient = 'test@localhost'
292 292
   end
293  
-  
  293
+
294 294
   def teardown
295 295
     TestMailer.logger = @original_logger
  296
+    restore_delivery_method
296 297
   end
297 298
 
298 299
   def test_nested_parts
@@ -902,11 +903,15 @@ def send
902 903
   end
903 904
 
904 905
   def setup
905  
-    ActionMailer::Base.delivery_method = :test
  906
+    set_delivery_method :test
906 907
     ActionMailer::Base.perform_deliveries = true
907 908
     ActionMailer::Base.deliveries = []
908 909
   end
909 910
 
  911
+  def teardown
  912
+    restore_delivery_method
  913
+  end
  914
+
910 915
   def test_send_method
911 916
     assert_nothing_raised do
912 917
       assert_emails 1 do
6  actionmailer/test/url_test.rb
@@ -40,13 +40,17 @@ def new_mail( charset="utf-8" )
40 40
   end
41 41
 
42 42
   def setup
43  
-    ActionMailer::Base.delivery_method = :test
  43
+    set_delivery_method :test
44 44
     ActionMailer::Base.perform_deliveries = true
45 45
     ActionMailer::Base.deliveries = []
46 46
 
47 47
     @recipient = 'test@localhost'
48 48
   end
49 49
 
  50
+  def teardown
  51
+    restore_delivery_method
  52
+  end
  53
+
50 54
   def test_signed_up_with_url
51 55
     ActionController::Routing::Routes.draw do |map| 
52 56
       map.connect ':controller/:action/:id' 

0 notes on commit 57cde63

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