From 343ac48f45a433b2ef0dc67f9cd9d1245fb5ef2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 11:01:21 +0100 Subject: Moved deprecated_body.rb to deprecatead_api.rb --- actionmailer/lib/action_mailer.rb | 1 - actionmailer/lib/action_mailer/base.rb | 61 ---------- actionmailer/lib/action_mailer/deprecated_api.rb | 141 +++++++++++++++++----- actionmailer/lib/action_mailer/deprecated_body.rb | 53 -------- actionmailer/lib/action_mailer/mail_helper.rb | 5 + actionmailer/lib/action_mailer/tmail_compat.rb | 6 +- actionmailer/test/mail_service_test.rb | 12 -- 7 files changed, 118 insertions(+), 161 deletions(-) delete mode 100644 actionmailer/lib/action_mailer/deprecated_body.rb diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb index 1765aee9cc..37c95baea7 100644 --- a/actionmailer/lib/action_mailer.rb +++ b/actionmailer/lib/action_mailer.rb @@ -33,7 +33,6 @@ module ActionMailer autoload :AdvAttrAccessor autoload :Base autoload :DeliveryMethods - autoload :DeprecatedBody autoload :DeprecatedApi autoload :MailHelper autoload :Quoting diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 39ddafe7fe..0b3b44e326 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -263,7 +263,6 @@ module ActionMailer #:nodoc: include AbstractController::UrlFor helper ActionMailer::MailHelper - include ActionMailer::DeprecatedBody include ActionMailer::DeprecatedApi include ActionMailer::DeliveryMethods @@ -297,69 +296,11 @@ module ActionMailer #:nodoc: @@default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] cattr_accessor :default_implicit_parts_order - @@protected_instance_variables = %w(@parts @message) - cattr_reader :protected_instance_variables - - # Specify the BCC addresses for the message - adv_attr_accessor :bcc - - # Specify the CC addresses for the message. - adv_attr_accessor :cc - - # Specify the charset to use for the message. This defaults to the - # +default_charset+ specified for ActionMailer::Base. - adv_attr_accessor :charset - - # Specify the content type for the message. This defaults to text/plain - # in most cases, but can be automatically set in some situations. - adv_attr_accessor :content_type - - # Specify the from address for the message. - adv_attr_accessor :from - - # Specify the address (if different than the "from" address) to direct - # replies to this message. - adv_attr_accessor :reply_to - - # Specify additional headers to be added to the message. - adv_attr_accessor :headers - - # Specify the order in which parts should be sorted, based on content-type. - # This defaults to the value for the +default_implicit_parts_order+. - adv_attr_accessor :implicit_parts_order - - # Defaults to "1.0", but may be explicitly given if needed. - adv_attr_accessor :mime_version - - # The recipient addresses for the message, either as a string (for a single - # address) or an array (for multiple addresses). - adv_attr_accessor :recipients - - # The date on which the message was sent. If not set (the default), the - # header will be set by the delivery agent. - adv_attr_accessor :sent_on - - # Specify the subject of the message. - adv_attr_accessor :subject - - # Specify the template name to use for current message. This is the "base" - # template name, without the extension or directory, and may be used to - # have multiple mailer methods share the same template. - adv_attr_accessor :template - - # Override the mailer name, which defaults to an inflected version of the - # mailer's class name. If you want to use a template in a non-standard - # location, you can use this to specify that location. - adv_attr_accessor :mailer_name - # Expose the internal Mail message attr_reader :message # Pass calls to headers and attachment to the Mail#Message instance delegate :headers, :attachments, :to => :@message - - # Alias controller_path to mailer_name so render :partial in views work. - alias :controller_path :mailer_name class << self @@ -504,9 +445,7 @@ module ActionMailer #:nodoc: # rendered and a new Mail object created. def process(method_name, *args) initialize_defaults(method_name) - super - unless @mail_was_called # Create e-mail parts create_parts diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 19c94aee8d..32ea4162dc 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -3,8 +3,87 @@ module ActionMailer # hooks in ActionMailer::Base are removed as well. # # Moved here to allow us to add the new Mail API - module DeprecatedApi - extend ActionMailer::AdvAttrAccessor + module DeprecatedApi #:nodoc: + extend ActiveSupport::Concern + + included do + extend ActionMailer::AdvAttrAccessor + + @@protected_instance_variables = %w(@parts) + cattr_reader :protected_instance_variables + + # Specify the BCC addresses for the message + adv_attr_accessor :bcc + + # Specify the CC addresses for the message. + adv_attr_accessor :cc + + # Specify the charset to use for the message. This defaults to the + # +default_charset+ specified for ActionMailer::Base. + adv_attr_accessor :charset + + # Specify the content type for the message. This defaults to text/plain + # in most cases, but can be automatically set in some situations. + adv_attr_accessor :content_type + + # Specify the from address for the message. + adv_attr_accessor :from + + # Specify the address (if different than the "from" address) to direct + # replies to this message. + adv_attr_accessor :reply_to + + # Specify additional headers to be added to the message. + adv_attr_accessor :headers + + # Specify the order in which parts should be sorted, based on content-type. + # This defaults to the value for the +default_implicit_parts_order+. + adv_attr_accessor :implicit_parts_order + + # Defaults to "1.0", but may be explicitly given if needed. + adv_attr_accessor :mime_version + + # The recipient addresses for the message, either as a string (for a single + # address) or an array (for multiple addresses). + adv_attr_accessor :recipients + + # The date on which the message was sent. If not set (the default), the + # header will be set by the delivery agent. + adv_attr_accessor :sent_on + + # Specify the subject of the message. + adv_attr_accessor :subject + + # Specify the template name to use for current message. This is the "base" + # template name, without the extension or directory, and may be used to + # have multiple mailer methods share the same template. + adv_attr_accessor :template + + # Override the mailer name, which defaults to an inflected version of the + # mailer's class name. If you want to use a template in a non-standard + # location, you can use this to specify that location. + adv_attr_accessor :mailer_name + + # Define the body of the message. This is either a Hash (in which case it + # specifies the variables to pass to the template when it is rendered), + # or a string, in which case it specifies the actual text of the message. + adv_attr_accessor :body + + # Alias controller_path to mailer_name so render :partial in views work. + alias :controller_path :mailer_name + end + + def render(*args) + options = args.last.is_a?(Hash) ? args.last : {} + if options[:body] + ActiveSupport::Deprecation.warn(':body in render deprecated. Please call body ' << + 'with a hash instead', caller[0,1]) + + body options.delete(:body) + end + + super + end # Add a part to a multipart message, with the given content-type. The # part itself is yielded to the block so that other properties (charset, @@ -13,8 +92,6 @@ module ActionMailer params = {:content_type => params} if String === params if custom_headers = params.delete(:headers) - ActiveSupport::Deprecation.warn('Passing custom headers with :headers => {} is deprecated. ' << - 'Please just pass in custom headers directly.', caller[0,10]) params.merge!(custom_headers) end @@ -27,19 +104,38 @@ module ActionMailer # Add an attachment to a multipart message. This is simply a part with the # content-disposition set to "attachment". def attachment(params, &block) - super # Run deprecation hooks - params = { :content_type => params } if String === params + params[:content] ||= params.delete(:data) || params.delete(:body) + if params[:filename] params = normalize_file_hash(params) else params = normalize_nonfile_hash(params) end + part(params, &block) end - private + # Render a message but does not set it as mail body. Useful for rendering + # data for part and attachments. + # + # Examples: + # + # render_message "special_message" + # render_message :template => "special_message" + # render_message :inline => "<%= 'Hi!' %>" + # + def render_message(object) + case object + when String + render_to_body(:template => object) + else + render_to_body(object) + end + end + + private def normalize_nonfile_hash(params) content_disposition = "attachment;" @@ -109,25 +205,6 @@ module ActionMailer @message end - - # Render a message but does not set it as mail body. Useful for rendering - # data for part and attachments. - # - # Examples: - # - # render_message "special_message" - # render_message :template => "special_message" - # render_message :inline => "<%= 'Hi!' %>" - # - # TODO Deprecate me - def render_message(object) - case object - when String - render_to_body(:template => object) - else - render_to_body(object) - end - end # Set up the default values for the various instance variables of this # mailer. Subclasses may override this method to provide different @@ -139,18 +216,20 @@ module ActionMailer @mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version @mailer_name ||= self.class.mailer_name.dup - @delivery_method = self.class.delivery_method @template ||= method_name @parts ||= [] @headers ||= {} @sent_on ||= Time.now - - super # Run deprecation hooks + @body ||= {} end def create_parts #:nodoc: - super # Run deprecation hooks + if String === @body + self.response_body = @body + elsif @body.is_a?(Hash) && !@body.empty? + @body.each { |k, v| instance_variable_set(:"@#{k}", v) } + end if String === response_body @parts.unshift create_inline_part(response_body) @@ -179,7 +258,7 @@ module ActionMailer :body => body ) end - + def split_content_type(ct) #:nodoc: ct.to_s.split("/") end diff --git a/actionmailer/lib/action_mailer/deprecated_body.rb b/actionmailer/lib/action_mailer/deprecated_body.rb deleted file mode 100644 index daaf145327..0000000000 --- a/actionmailer/lib/action_mailer/deprecated_body.rb +++ /dev/null @@ -1,53 +0,0 @@ -module ActionMailer - # TODO Remove this module all together in a next release. Ensure that super - # hooks in ActionMailer::Base are removed as well. - module DeprecatedBody - extend ActionMailer::AdvAttrAccessor - - # Define the body of the message. This is either a Hash (in which case it - # specifies the variables to pass to the template when it is rendered), - # or a string, in which case it specifies the actual text of the message. - adv_attr_accessor :body - - def initialize_defaults(method_name) - @body ||= {} - end - - def attachment(params, &block) - if params[:data] - ActiveSupport::Deprecation.warn('attachment :data => "string" is deprecated. To set the body of an attachment ' << - 'please use :content instead, like attachment :content => "string"', caller[0,10]) - params[:content] = params.delete(:data) - end - if params[:body] - ActiveSupport::Deprecation.warn('attachment :data => "string" is deprecated. To set the body of an attachment ' << - 'please use :content instead, like attachment :content => "string"', caller[0,10]) - params[:content] = params.delete(:body) - end - end - - def create_parts - if String === @body - ActiveSupport::Deprecation.warn('body(String) is deprecated. To set the body with a text ' << - 'call render(:text => "body")', caller[0,10]) - self.response_body = @body - elsif @body.is_a?(Hash) && !@body.empty? - ActiveSupport::Deprecation.warn('body(Hash) is deprecated. Use instance variables to define ' << - 'assigns in your view', caller[0,10]) - @body.each { |k, v| instance_variable_set(:"@#{k}", v) } - end - end - - def render(*args) - options = args.last.is_a?(Hash) ? args.last : {} - if options[:body] - ActiveSupport::Deprecation.warn(':body in render deprecated. Please call body ' << - 'with a hash instead', caller[0,1]) - - body options.delete(:body) - end - - super - end - end -end diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index 701dc34431..702c9ba8f7 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -20,5 +20,10 @@ module ActionMailer def mailer #:nodoc: @controller end + + # Access the message instance. + def message + @message + end end end diff --git a/actionmailer/lib/action_mailer/tmail_compat.rb b/actionmailer/lib/action_mailer/tmail_compat.rb index da13aac677..d78332c135 100644 --- a/actionmailer/lib/action_mailer/tmail_compat.rb +++ b/actionmailer/lib/action_mailer/tmail_compat.rb @@ -3,7 +3,7 @@ module Mail def set_content_type(*args) ActiveSupport::Deprecation.warn('Message#set_content_type is deprecated, please just call ' << - 'Message#content_type with the same arguments.', caller[0,10]) + 'Message#content_type with the same arguments', caller[0,10]) content_type(*args) end @@ -11,7 +11,7 @@ module Mail def transfer_encoding(value = nil) if value ActiveSupport::Deprecation.warn('Message#transfer_encoding is deprecated, please call ' << - 'Message#content_transfer_encoding with the same arguments.', caller[0,10]) + 'Message#content_transfer_encoding with the same arguments', caller[0,10]) content_transfer_encoding(value) else old_transfer_encoding @@ -20,7 +20,7 @@ module Mail def original_filename ActiveSupport::Deprecation.warn('Message#original_filename is deprecated, ' << - 'please call Message#filename.', caller[0,10]) + 'please call Message#filename', caller[0,10]) filename end diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb index 51d722ea00..ec820e3464 100644 --- a/actionmailer/test/mail_service_test.rb +++ b/actionmailer/test/mail_service_test.rb @@ -301,13 +301,6 @@ class TestMailer < ActionMailer::Base render :text => "testing" end - def body_ivar(recipient) - recipients recipient - subject "Body as a local variable" - from "test@example.com" - body :body => "foo", :bar => "baz" - end - def subject_with_i18n(recipient) recipients recipient from "system@loudthinking.com" @@ -1080,11 +1073,6 @@ EOF assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s end - def test_body_is_stored_as_an_ivar - mail = TestMailer.create_body_ivar(@recipient) - assert_equal "body: foo\nbar: baz", mail.body.to_s - end - def test_starttls_is_enabled_if_supported ActionMailer::Base.delivery_settings[:smtp].merge!(:enable_starttls_auto => true) MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true) -- cgit v1.2.3 From bb9d71ff9e537597ff4d5962e7870ad99001f605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 11:10:37 +0100 Subject: Move class methods to deprecated stuff. --- actionmailer/lib/action_mailer/base.rb | 53 +++--------------------- actionmailer/lib/action_mailer/deprecated_api.rb | 48 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 0b3b44e326..2c1de0e3b4 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -253,7 +253,6 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base < AbstractController::Base include Quoting - extend AdvAttrAccessor include AbstractController::Logger include AbstractController::Rendering @@ -311,23 +310,6 @@ module ActionMailer #:nodoc: alias :controller_path :mailer_name - def respond_to?(method_symbol, include_private = false) #:nodoc: - matches_dynamic_method?(method_symbol) || super - end - - def method_missing(method_symbol, *parameters) #:nodoc: - if match = matches_dynamic_method?(method_symbol) - case match[1] - when 'create' then new(match[2], *parameters).message - when 'deliver' then new(match[2], *parameters).deliver! - when 'new' then nil - else super - end - else - super - end - end - # Receives a raw email, parses it into an email object, decodes it, # instantiates a new mailer, and passes the email object to the mailer # object's +receive+ method. If you want your mailer to be able to @@ -357,7 +339,6 @@ module ActionMailer #:nodoc: raise "no mail object available for delivery!" unless mail ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| - self.set_payload_for_mail(payload, mail) mail.delivery_method delivery_methods[delivery_method], @@ -396,18 +377,11 @@ module ActionMailer #:nodoc: payload[:date] = mail.date payload[:mail] = mail.encoded end - - private - - def matches_dynamic_method?(method_name) #:nodoc: - method_name = method_name.to_s - /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) - end end def mail(headers = {}) # Guard flag to prevent both the old and the new API from firing - # TODO - Move this @mail_was_called flag into deprecated_api.rb + # Should be removed when old API is deprecated @mail_was_called = true m = @message @@ -426,6 +400,11 @@ module ActionMailer #:nodoc: m.body.set_sort_order(headers[:parts_order] || @@default_implicit_parts_order) + # # Set the subject if not set yet + # @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], + # :default => method_name.humanize) + + # TODO: m.body.sort_parts! m end @@ -436,30 +415,10 @@ module ActionMailer #:nodoc: # method, for instance). def initialize(method_name=nil, *args) super() - @mail_was_called = false @message = Mail.new process(method_name, *args) if method_name end - # Process the mailer via the given +method_name+. The body will be - # rendered and a new Mail object created. - def process(method_name, *args) - initialize_defaults(method_name) - super - unless @mail_was_called - # Create e-mail parts - create_parts - - # Set the subject if not set yet - @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], - :default => method_name.humanize) - - # Build the mail object itself - create_mail - end - @message - end - # Delivers a Mail object. By default, it delivers the cached mail # object (from the create! method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 32ea4162dc..430b0cef4a 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -73,6 +73,37 @@ module ActionMailer alias :controller_path :mailer_name end + module ClassMethods + def respond_to?(method_symbol, include_private = false) #:nodoc: + matches_dynamic_method?(method_symbol) || super + end + + def method_missing(method_symbol, *parameters) #:nodoc: + if match = matches_dynamic_method?(method_symbol) + case match[1] + when 'create' then new(match[2], *parameters).message + when 'deliver' then new(match[2], *parameters).deliver! + when 'new' then nil + else super + end + else + super + end + end + + private + + def matches_dynamic_method?(method_name) #:nodoc: + method_name = method_name.to_s + /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) + end + end + + def initialize(*) + super() + @mail_was_called = false + end + def render(*args) options = args.last.is_a?(Hash) ? args.last : {} if options[:body] @@ -85,6 +116,23 @@ module ActionMailer super end + def process(method_name, *args) + initialize_defaults(method_name) + super + unless @mail_was_called + # Create e-mail parts + create_parts + + # Set the subject if not set yet + @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], + :default => method_name.humanize) + + # Build the mail object itself + create_mail + end + end + + # Add a part to a multipart message, with the given content-type. The # part itself is yielded to the block so that other properties (charset, # body, headers, etc.) can be set on it. -- cgit v1.2.3