Skip to content
This repository
Browse code

Deprecate old defaults API.

  • Loading branch information...
commit 39a1b06f13221e00ab38dff960155cb5fe9eaabb 1 parent 9dd65c3
authored January 26, 2010
31  actionmailer/lib/action_mailer/base.rb
@@ -262,24 +262,6 @@ class Base < AbstractController::Base
262 262
       :parts_order  => [ "text/plain", "text/enriched", "text/html" ]
263 263
     }
264 264
 
265  
-    extlib_inheritable_accessor :default_charset
266  
-    self.default_charset = "utf-8"
267  
-
268  
-    extlib_inheritable_accessor :default_content_type
269  
-    self.default_content_type = "text/plain"
270  
-
271  
-    extlib_inheritable_accessor :default_mime_version
272  
-    self.default_mime_version = "1.0"
273  
-
274  
-    # This specifies the order that the parts of a multipart email will be.  Usually you put
275  
-    # text/plain at the top so someone without a MIME capable email reader can read the plain
276  
-    # text of your email first.
277  
-    #
278  
-    # Any content type that is not listed here will be inserted in the order you add them to
279  
-    # the email after the content types you list here.
280  
-    extlib_inheritable_accessor :default_implicit_parts_order
281  
-    self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ]
282  
-
283 265
     class << self
284 266
 
285 267
       def mailer_name
@@ -498,7 +480,7 @@ def mail(headers={}, &block)
498 480
       quote_fields!(headers, charset)
499 481
 
500 482
       # Render the templates and blocks
501  
-      responses, explicit_order = collect_responses_and_sort_order(headers, &block)
  483
+      responses, explicit_order = collect_responses_and_parts_order(headers, &block)
502 484
       create_parts_from_responses(m, responses, charset)
503 485
 
504 486
       # Finally setup content type and parts order
@@ -554,18 +536,18 @@ def quote_fields!(headers, charset) #:nodoc:
554 536
       m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to]
555 537
     end
556 538
 
557  
-    def collect_responses_and_sort_order(headers) #:nodoc:
558  
-      responses, sort_order = [], nil
  539
+    def collect_responses_and_parts_order(headers) #:nodoc:
  540
+      responses, parts_order = [], nil
559 541
 
560 542
       if block_given?
561 543
         collector = ActionMailer::Collector.new(self) { render(action_name) }
562 544
         yield(collector)
563  
-        sort_order = collector.responses.map { |r| r[:content_type] }
  545
+        parts_order = collector.responses.map { |r| r[:content_type] }
564 546
         responses  = collector.responses
565 547
       elsif headers[:body]
566 548
         responses << {
567 549
           :body => headers[:body],
568  
-          :content_type => self.class.default_content_type.dup
  550
+          :content_type => self.class.defaults[:content_type] || "text/plain"
569 551
         }
570 552
       else
571 553
         each_template do |template|
@@ -576,7 +558,7 @@ def collect_responses_and_sort_order(headers) #:nodoc:
576 558
         end
577 559
       end
578 560
 
579  
-      [responses, sort_order]
  561
+      [responses, parts_order]
580 562
     end
581 563
 
582 564
     def each_template(&block) #:nodoc:
@@ -594,7 +576,6 @@ def each_template(&block) #:nodoc:
594 576
     def create_parts_from_responses(m, responses, charset) #:nodoc:
595 577
       if responses.size == 1 && !m.has_attachments?
596 578
         responses[0].each { |k,v| m[k] = v }
597  
-        return responses[0][:content_type]
598 579
       elsif responses.size > 1 && m.has_attachments?
599 580
         container = Mail::Part.new
600 581
         container.content_type = "multipart/alternative"
5  actionmailer/lib/action_mailer/delivery_methods.rb
@@ -65,7 +65,10 @@ def wrap_delivery_behavior(mail, method=nil) #:nodoc:
65 65
         method ||= self.delivery_method
66 66
         mail.delivery_handler = self
67 67
 
68  
-        if method.is_a?(Symbol)
  68
+        case method
  69
+        when NilClass
  70
+          raise "Delivery method cannot be nil"
  71
+        when Symbol
69 72
           if klass = delivery_methods[method.to_sym]
70 73
             mail.delivery_method(klass, send(:"#{method}_settings"))
71 74
           else
29  actionmailer/lib/action_mailer/deprecated_api.rb
@@ -5,8 +5,25 @@ module ActionMailer
5 5
   module DeprecatedApi #:nodoc:
6 6
     extend ActiveSupport::Concern
7 7
 
8  
-    module ClassMethods
  8
+    included do
  9
+      [:charset, :content_type, :mime_version, :implicit_parts_order].each do |method|
  10
+        class_eval <<-FILE, __FILE__, __LINE__ + 1
  11
+          def self.default_#{method}
  12
+            @@default_#{method}
  13
+          end
  14
+
  15
+          def self.default_#{method}=(value)
  16
+            ActiveSupport::Deprecation.warn "ActionMailer::Base.default_#{method}=value is deprecated, " <<
  17
+              "use defaults :#{method} => value instead"
  18
+            @@default_#{method} = value
  19
+          end
  20
+
  21
+          @@default_#{method} = nil
  22
+        FILE
  23
+      end
  24
+    end
9 25
 
  26
+    module ClassMethods
10 27
       # Deliver the given mail object directly. This can be used to deliver
11 28
       # a preconstructed mail object, like:
12 29
       #
@@ -99,7 +116,15 @@ def render_message(*args)
99 116
     end
100 117
 
101 118
   private
102  
-    
  119
+
  120
+    def initialize_defaults(*)
  121
+      @charset              ||= self.class.default_charset.try(:dup)
  122
+      @content_type         ||= self.class.default_content_type.try(:dup)
  123
+      @implicit_parts_order ||= self.class.default_implicit_parts_order.try(:dup)
  124
+      @mime_version         ||= self.class.default_mime_version.try(:dup)
  125
+      super
  126
+    end
  127
+
103 128
     def create_parts
104 129
       if @body.is_a?(Hash) && !@body.empty?
105 130
         ActiveSupport::Deprecation.warn "Giving a hash to body is deprecated, please use instance variables instead", caller[0,2]
10  actionmailer/lib/action_mailer/old_api.rb
... ...
@@ -1,3 +1,5 @@
  1
+require 'active_support/core_ext/object/try'
  2
+
1 3
 module ActionMailer
2 4
   module OldApi #:nodoc:
3 5
     extend ActiveSupport::Concern
@@ -185,10 +187,10 @@ def create_mail
185 187
     # mailer. Subclasses may override this method to provide different
186 188
     # defaults.
187 189
     def initialize_defaults(method_name) 
188  
-      @charset              ||= self.class.default_charset.dup
189  
-      @content_type         ||= self.class.default_content_type.dup
190  
-      @implicit_parts_order ||= self.class.default_implicit_parts_order.dup
191  
-      @mime_version         ||= self.class.default_mime_version.dup if self.class.default_mime_version
  190
+      @charset              ||= self.class.defaults[:charset].try(:dup)
  191
+      @content_type         ||= self.class.defaults[:content_type].try(:dup)
  192
+      @implicit_parts_order ||= self.class.defaults[:parts_order].try(:dup)
  193
+      @mime_version         ||= self.class.defaults[:mime_version].try(:dup)
192 194
 
193 195
       @mailer_name   ||= self.class.mailer_name.dup
194 196
       @template      ||= method_name

0 notes on commit 39a1b06

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