Skip to content
This repository
Browse code

use autoload instead of explicit requires for ActionMailer

  • Loading branch information...
commit e201fc750bf4b7dff1875b7fcdd47f1686ef2052 1 parent 04d2d04
Joshua Peek authored November 23, 2008
38  actionmailer/lib/action_mailer.rb
@@ -31,22 +31,32 @@
31 31
   end
32 32
 end
33 33
 
34  
-require 'action_mailer/vendor'
35  
-require 'tmail'
36  
-
37  
-require 'action_mailer/base'
38  
-require 'action_mailer/helpers'
39  
-require 'action_mailer/mail_helper'
40  
-require 'action_mailer/quoting'
41  
-require 'action_mailer/test_helper'
  34
+module ActionMailer
  35
+  def self.load_all!
  36
+    [Base, Part, ::Text::Format, ::Net::SMTP]
  37
+  end
42 38
 
43  
-require 'net/smtp'
  39
+  autoload :AdvAttrAccessor, 'action_mailer/adv_attr_accessor'
  40
+  autoload :Base, 'action_mailer/base'
  41
+  autoload :Helpers, 'action_mailer/helpers'
  42
+  autoload :Part, 'action_mailer/part'
  43
+  autoload :PartContainer, 'action_mailer/part_container'
  44
+  autoload :Quoting, 'action_mailer/quoting'
  45
+  autoload :TestCase, 'action_mailer/test_case'
  46
+  autoload :TestHelper, 'action_mailer/test_helper'
  47
+  autoload :Utils, 'action_mailer/utils'
  48
+end
44 49
 
45  
-ActionMailer::Base.class_eval do
46  
-  include ActionMailer::Quoting
47  
-  include ActionMailer::Helpers
  50
+module Text
  51
+  autoload :Format, 'action_mailer/vendor/text_format'
  52
+end
48 53
 
49  
-  helper MailHelper
  54
+module Net
  55
+  autoload :SMTP, 'net/smtp'
50 56
 end
51 57
 
52  
-silence_warnings { TMail::Encoder.const_set("MAX_LINE_LEN", 200) }
  58
+autoload :MailHelper, 'action_mailer/mail_helper'
  59
+autoload :TMail, 'action_mailer/vendor/tmail'
  60
+
  61
+# TODO: Don't explicitly load entire lib
  62
+ActionMailer.load_all!
15  actionmailer/lib/action_mailer/base.rb
... ...
@@ -1,7 +1,3 @@
1  
-require 'action_mailer/adv_attr_accessor'
2  
-require 'action_mailer/part'
3  
-require 'action_mailer/part_container'
4  
-require 'action_mailer/utils'
5 1
 require 'tmail/net'
6 2
 
7 3
 module ActionMailer #:nodoc:
@@ -245,7 +241,7 @@ module ActionMailer #:nodoc:
245 241
   #   and appear last in the mime encoded message. You can also pick a different order from inside a method with
246 242
   #   +implicit_parts_order+.
247 243
   class Base
248  
-    include AdvAttrAccessor, PartContainer
  244
+    include AdvAttrAccessor, PartContainer, Quoting, Utils
249 245
     if Object.const_defined?(:ActionController)
250 246
       include ActionController::UrlWriter
251 247
       include ActionController::Layout
@@ -648,11 +644,11 @@ def create_mail
648 644
 
649 645
         if @parts.empty?
650 646
           m.set_content_type(real_content_type, nil, ctype_attrs)
651  
-          m.body = Utils.normalize_new_lines(body)
  647
+          m.body = normalize_new_lines(body)
652 648
         else
653 649
           if String === body
654 650
             part = TMail::Mail.new
655  
-            part.body = Utils.normalize_new_lines(body)
  651
+            part.body = normalize_new_lines(body)
656 652
             part.set_content_type(real_content_type, nil, ctype_attrs)
657 653
             part.set_content_disposition "inline"
658 654
             m.parts << part
@@ -698,4 +694,9 @@ def perform_delivery_test(mail)
698 694
         deliveries << mail
699 695
       end
700 696
   end
  697
+
  698
+  Base.class_eval do
  699
+    include Helpers
  700
+    helper MailHelper
  701
+  end
701 702
 end
2  actionmailer/lib/action_mailer/mail_helper.rb
... ...
@@ -1,5 +1,3 @@
1  
-require 'text/format'
2  
-
3 1
 module MailHelper
4 2
   # Uses Text::Format to take the text and format it, indented two spaces for
5 3
   # each line, and wrapped at 72 columns.
10  actionmailer/lib/action_mailer/part.rb
... ...
@@ -1,15 +1,10 @@
1  
-require 'action_mailer/adv_attr_accessor'
2  
-require 'action_mailer/part_container'
3  
-require 'action_mailer/utils'
4  
-
5 1
 module ActionMailer
6 2
   # Represents a subpart of an email message. It shares many similar
7 3
   # attributes of ActionMailer::Base.  Although you can create parts manually
8 4
   # and add them to the +parts+ list of the mailer, it is easier
9 5
   # to use the helper methods in ActionMailer::PartContainer.
10 6
   class Part
11  
-    include ActionMailer::AdvAttrAccessor
12  
-    include ActionMailer::PartContainer
  7
+    include AdvAttrAccessor, PartContainer, Utils
13 8
 
14 9
     # Represents the body of the part, as a string. This should not be a
15 10
     # Hash (like ActionMailer::Base), but if you want a template to be rendered
@@ -64,7 +59,7 @@ def to_mail(defaults)
64 59
           when "base64" then
65 60
             part.body = TMail::Base64.folding_encode(body)
66 61
           when "quoted-printable"
67  
-            part.body = [Utils.normalize_new_lines(body)].pack("M*")
  62
+            part.body = [normalize_new_lines(body)].pack("M*")
68 63
           else
69 64
             part.body = body
70 65
         end
@@ -102,7 +97,6 @@ def to_mail(defaults)
102 97
     end
103 98
 
104 99
     private
105  
-
106 100
       def squish(values={})
107 101
         values.delete_if { |k,v| v.nil? }
108 102
       end
2  actionmailer/lib/action_mailer/test_case.rb
@@ -10,7 +10,7 @@ def initialize(name)
10 10
   end
11 11
 
12 12
   class TestCase < ActiveSupport::TestCase
13  
-    include ActionMailer::Quoting
  13
+    include Quoting, TestHelper
14 14
 
15 15
     setup :initialize_test_deliveries
16 16
     setup :set_expected_mail
1  actionmailer/lib/action_mailer/test_helper.rb
@@ -58,6 +58,7 @@ def assert_no_emails(&block)
58 58
   end
59 59
 end
60 60
 
  61
+# TODO: Deprecate this
61 62
 module Test
62 63
   module Unit
63 64
     class TestCase
1  actionmailer/lib/action_mailer/utils.rb
@@ -3,6 +3,5 @@ module Utils #:nodoc:
3 3
     def normalize_new_lines(text)
4 4
       text.to_s.gsub(/\r\n?/, "\n")
5 5
     end
6  
-    module_function :normalize_new_lines
7 6
   end
8 7
 end
14  actionmailer/lib/action_mailer/vendor.rb
... ...
@@ -1,14 +0,0 @@
1  
-# Prefer gems to the bundled libs.
2  
-require 'rubygems'
3  
-
4  
-begin
5  
-  gem 'tmail', '~> 1.2.3'
6  
-rescue Gem::LoadError
7  
-  $:.unshift "#{File.dirname(__FILE__)}/vendor/tmail-1.2.3"
8  
-end
9  
-
10  
-begin
11  
-  gem 'text-format', '>= 0.6.3'
12  
-rescue Gem::LoadError
13  
-  $:.unshift "#{File.dirname(__FILE__)}/vendor/text-format-0.6.3"
14  
-end
10  actionmailer/lib/action_mailer/vendor/text_format.rb
... ...
@@ -0,0 +1,10 @@
  1
+# Prefer gems to the bundled libs.
  2
+require 'rubygems'
  3
+
  4
+begin
  5
+  gem 'text-format', '>= 0.6.3'
  6
+rescue Gem::LoadError
  7
+  $:.unshift "#{File.dirname(__FILE__)}/text-format-0.6.3"
  8
+end
  9
+
  10
+require 'text/format'
17  actionmailer/lib/action_mailer/vendor/tmail.rb
... ...
@@ -0,0 +1,17 @@
  1
+# Prefer gems to the bundled libs.
  2
+require 'rubygems'
  3
+
  4
+begin
  5
+  gem 'tmail', '~> 1.2.3'
  6
+rescue Gem::LoadError
  7
+  $:.unshift "#{File.dirname(__FILE__)}/tmail-1.2.3"
  8
+end
  9
+
  10
+module TMail
  11
+end
  12
+
  13
+require 'tmail'
  14
+
  15
+silence_warnings do
  16
+  TMail::Encoder.const_set("MAX_LINE_LEN", 200)
  17
+end

5 notes on commit e201fc7

David Reese

Had to comment out “require ‘tmail/net’ in actionmailer/base.rb” to get rails to start after this commit.

Lourens Naudé

same here

Joshua Peek
Collaborator

hmm, can’t seem to reproduce the issue. what exception is getting raised?

David Reese
  1. no such file to load — tmail/net (RuntimeError)

ActionMailer seems to work without it, interestingly enough.

Joshua Peek
Collaborator

Jeremy just remove the line a few mins ago. It should be fine for now.

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