From 8cbf825425dc8ad3770881ea4e100b9023c69ce2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 14 Oct 2009 19:50:06 -0500 Subject: Rename Orchestra to Notifications [#3321 state:resolved] --- actionmailer/lib/action_mailer/base.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index b3005c2e5e..d8b08116eb 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -394,7 +394,7 @@ module ActionMailer #:nodoc: def controller_path self.class.controller_path end - + def formats @template.formats end @@ -481,7 +481,7 @@ module ActionMailer #:nodoc: # Initialize the mailer via the given +method_name+. The body will be # rendered and a new TMail::Mail object created. def create!(method_name, *parameters) #:nodoc: - ActiveSupport::Orchestra.instrument(:create_mail, :name => method_name) do + ActiveSupport::Notifications.instrument(:create_mail, :name => method_name) do initialize_defaults(method_name) __send__(method_name, *parameters) @@ -550,7 +550,7 @@ module ActionMailer #:nodoc: logger.debug "\n#{mail.encoded}" end - ActiveSupport::Orchestra.instrument(:deliver_mail, :mail => @mail) do + ActiveSupport::Notifications.instrument(:deliver_mail, :mail => @mail) do begin __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries rescue Exception => e # Net::SMTP errors or sendmail pipe errors @@ -583,9 +583,9 @@ module ActionMailer #:nodoc: if template.respond_to?(:mime_type) @current_template_content_type = template.mime_type && template.mime_type.to_sym.to_s end - + @template = initialize_template_class(body) - layout = _pick_layout(layout, true) unless + layout = _pick_layout(layout, true) unless ActionController::Base.exempt_from_layout.include?(template.handler) @template._render_template(template, layout, {}) ensure @@ -600,16 +600,16 @@ module ActionMailer #:nodoc: def render(opts) layout, file = opts.delete(:layout), opts[:file] - + begin @template = initialize_template_class(opts.delete(:body)) - + if file prefix = mailer_name unless file =~ /\// template = view_paths.find(file, {:formats => formats}, prefix) end - layout = _pick_layout(layout, + layout = _pick_layout(layout, !template || ActionController::Base.exempt_from_layout.include?(template.handler)) if template @@ -648,7 +648,7 @@ module ActionMailer #:nodoc: def sort_parts(parts, order = []) order = order.collect { |s| s.downcase } - + parts = parts.sort do |a, b| a_ct = a.content_type.downcase b_ct = b.content_type.downcase @@ -689,7 +689,7 @@ module ActionMailer #:nodoc: headers.each { |k, v| m[k] = v } real_content_type, ctype_attrs = parse_content_type - + if @parts.empty? m.set_content_type(real_content_type, nil, ctype_attrs) m.body = normalize_new_lines(body) -- cgit v1.2.3 From 5b8e6279acaa46f22118122c49350e1b08c7371a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 14 Oct 2009 20:59:33 -0700 Subject: AV expects options[:locals] to be a Hash --- actionmailer/lib/action_mailer/base.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index d8b08116eb..24be66d197 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -599,6 +599,7 @@ module ActionMailer #:nodoc: end def render(opts) + opts[:locals] ||= {} layout, file = opts.delete(:layout), opts[:file] begin -- cgit v1.2.3 From 8b340ab2f62bac2af9d5917e296bb4101530282a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 18:06:15 -0300 Subject: Revert "Rename Orchestra to Notifications [#3321 state:resolved]" This reverts commit 8cbf825425dc8ad3770881ea4e100b9023c69ce2. --- actionmailer/lib/action_mailer/base.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 24be66d197..c0c04af51c 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -394,7 +394,7 @@ module ActionMailer #:nodoc: def controller_path self.class.controller_path end - + def formats @template.formats end @@ -481,7 +481,7 @@ module ActionMailer #:nodoc: # Initialize the mailer via the given +method_name+. The body will be # rendered and a new TMail::Mail object created. def create!(method_name, *parameters) #:nodoc: - ActiveSupport::Notifications.instrument(:create_mail, :name => method_name) do + ActiveSupport::Orchestra.instrument(:create_mail, :name => method_name) do initialize_defaults(method_name) __send__(method_name, *parameters) @@ -550,7 +550,7 @@ module ActionMailer #:nodoc: logger.debug "\n#{mail.encoded}" end - ActiveSupport::Notifications.instrument(:deliver_mail, :mail => @mail) do + ActiveSupport::Orchestra.instrument(:deliver_mail, :mail => @mail) do begin __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries rescue Exception => e # Net::SMTP errors or sendmail pipe errors @@ -583,9 +583,9 @@ module ActionMailer #:nodoc: if template.respond_to?(:mime_type) @current_template_content_type = template.mime_type && template.mime_type.to_sym.to_s end - + @template = initialize_template_class(body) - layout = _pick_layout(layout, true) unless + layout = _pick_layout(layout, true) unless ActionController::Base.exempt_from_layout.include?(template.handler) @template._render_template(template, layout, {}) ensure @@ -601,16 +601,16 @@ module ActionMailer #:nodoc: def render(opts) opts[:locals] ||= {} layout, file = opts.delete(:layout), opts[:file] - + begin @template = initialize_template_class(opts.delete(:body)) - + if file prefix = mailer_name unless file =~ /\// template = view_paths.find(file, {:formats => formats}, prefix) end - layout = _pick_layout(layout, + layout = _pick_layout(layout, !template || ActionController::Base.exempt_from_layout.include?(template.handler)) if template @@ -649,7 +649,7 @@ module ActionMailer #:nodoc: def sort_parts(parts, order = []) order = order.collect { |s| s.downcase } - + parts = parts.sort do |a, b| a_ct = a.content_type.downcase b_ct = b.content_type.downcase @@ -690,7 +690,7 @@ module ActionMailer #:nodoc: headers.each { |k, v| m[k] = v } real_content_type, ctype_attrs = parse_content_type - + if @parts.empty? m.set_content_type(real_content_type, nil, ctype_attrs) m.body = normalize_new_lines(body) -- cgit v1.2.3 From 2d7abe245e7a2b1717e48ef550e4083318fd7ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 18:51:51 -0300 Subject: Renamed Orchestra to Notifications once again [#3321 state:resolved] --- actionmailer/lib/action_mailer/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index c0c04af51c..df3bfb3620 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -481,7 +481,7 @@ module ActionMailer #:nodoc: # Initialize the mailer via the given +method_name+. The body will be # rendered and a new TMail::Mail object created. def create!(method_name, *parameters) #:nodoc: - ActiveSupport::Orchestra.instrument(:create_mail, :name => method_name) do + ActiveSupport::Notifications.instrument(:create_mail, :name => method_name) do initialize_defaults(method_name) __send__(method_name, *parameters) @@ -550,7 +550,7 @@ module ActionMailer #:nodoc: logger.debug "\n#{mail.encoded}" end - ActiveSupport::Orchestra.instrument(:deliver_mail, :mail => @mail) do + ActiveSupport::Notifications.instrument(:deliver_mail, :mail => @mail) do begin __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries rescue Exception => e # Net::SMTP errors or sendmail pipe errors -- cgit v1.2.3 From f4f76772fb5c25357a54baaa9cd20f7e9a1cd653 Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs Date: Wed, 28 Oct 2009 09:17:59 +0000 Subject: abstract all of the ActionMailer delivery methods into their own classes. thereby the following are equivalent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.delivery_method = ActionMailer::DeliveryMethod::Smtp we could equally set our own custom object as long as it provides the instance method :perform_delivery(mail) eg. class MySmsDeliveryMethod def perform_delivery(mail) Sms.send(mail['to'], mail['body']) end end MySmsMailer.delivery_method = MySmsDeliveryMethod.new Signed-off-by: José Valim --- actionmailer/lib/action_mailer/base.rb | 75 ++++------------------ actionmailer/lib/action_mailer/delivery_method.rb | 58 +++++++++++++++++ .../lib/action_mailer/delivery_method/file.rb | 21 ++++++ .../lib/action_mailer/delivery_method/sendmail.rb | 22 +++++++ .../lib/action_mailer/delivery_method/smtp.rb | 31 +++++++++ .../lib/action_mailer/delivery_method/test.rb | 12 ++++ 6 files changed, 157 insertions(+), 62 deletions(-) create mode 100644 actionmailer/lib/action_mailer/delivery_method.rb create mode 100644 actionmailer/lib/action_mailer/delivery_method/file.rb create mode 100644 actionmailer/lib/action_mailer/delivery_method/sendmail.rb create mode 100644 actionmailer/lib/action_mailer/delivery_method/smtp.rb create mode 100644 actionmailer/lib/action_mailer/delivery_method/test.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index df3bfb3620..898356075f 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -1,5 +1,3 @@ -require 'tmpdir' - require "active_support/core_ext/class" # Use the old layouts until actionmailer gets refactored require "action_controller/legacy/layout" @@ -232,7 +230,7 @@ module ActionMailer #:nodoc: # * raise_delivery_errors - Whether or not errors should be raised if the email fails to be delivered. # # * delivery_method - Defines a delivery method. Possible values are :smtp (default), :sendmail, :test, - # and :file. + # and :file. Or you may provide a custom delivery method object eg. MyOwnDeliveryMethodClass.new # # * perform_deliveries - Determines whether deliver_* methods are actually carried out. By default they are, # but this can be turned off to help functional testing. @@ -270,35 +268,21 @@ module ActionMailer #:nodoc: cattr_accessor :logger - @@smtp_settings = { - :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true, - } - cattr_accessor :smtp_settings - - @@sendmail_settings = { - :location => '/usr/sbin/sendmail', - :arguments => '-i -t' - } - cattr_accessor :sendmail_settings - - @@file_settings = { - :location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" - } - - cattr_accessor :file_settings + class << self + delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::File, :prefix => :file + delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Sendmail, :prefix => :sendmail + delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Smtp, :prefix => :smtp + + def delivery_method=(method_name) + @delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name) + end + end + self.delivery_method = :smtp + superclass_delegating_reader :delivery_method @@raise_delivery_errors = true cattr_accessor :raise_delivery_errors - superclass_delegating_accessor :delivery_method - self.delivery_method = :smtp - @@perform_deliveries = true cattr_accessor :perform_deliveries @@ -552,7 +536,7 @@ module ActionMailer #:nodoc: ActiveSupport::Notifications.instrument(:deliver_mail, :mail => @mail) do begin - __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries + self.delivery_method.perform_delivery(mail) if perform_deliveries rescue Exception => e # Net::SMTP errors or sendmail pipe errors raise e if raise_delivery_errors end @@ -720,39 +704,6 @@ module ActionMailer #:nodoc: @mail = m end - def perform_delivery_smtp(mail) - destinations = mail.destinations - mail.ready_to_send - sender = (mail['return-path'] && mail['return-path'].spec) || mail['from'] - - smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port]) - smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto) - smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password], - smtp_settings[:authentication]) do |smtp| - smtp.sendmail(mail.encoded, sender, destinations) - end - end - - def perform_delivery_sendmail(mail) - sendmail_args = sendmail_settings[:arguments] - sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path'] - IO.popen("#{sendmail_settings[:location]} #{sendmail_args}","w+") do |sm| - sm.print(mail.encoded.gsub(/\r/, '')) - sm.flush - end - end - - def perform_delivery_test(mail) - deliveries << mail - end - - def perform_delivery_file(mail) - FileUtils.mkdir_p file_settings[:location] - - (mail.to + mail.cc + mail.bcc).uniq.each do |to| - File.open(File.join(file_settings[:location], to), 'a') { |f| f.write(mail) } - end - end end Base.class_eval do diff --git a/actionmailer/lib/action_mailer/delivery_method.rb b/actionmailer/lib/action_mailer/delivery_method.rb new file mode 100644 index 0000000000..ffba3c418c --- /dev/null +++ b/actionmailer/lib/action_mailer/delivery_method.rb @@ -0,0 +1,58 @@ +require "active_support/core_ext/class" +module ActionMailer + module DeliveryMethod + + autoload :File, 'action_mailer/delivery_method/file' + autoload :Sendmail, 'action_mailer/delivery_method/sendmail' + autoload :Smtp, 'action_mailer/delivery_method/smtp' + autoload :Test, 'action_mailer/delivery_method/test' + + # Creates a new DeliveryMethod object according to the given options. + # + # If no arguments are passed to this method, then a new + # ActionMailer::DeliveryMethod::Stmp object will be returned. + # + # If you pass a Symbol as the first argument, then a corresponding + # delivery method class under the ActionMailer::DeliveryMethod namespace + # will be created. + # For example: + # + # ActionMailer::DeliveryMethod.lookup_method(:sendmail) + # # => returns a new ActionMailer::DeliveryMethod::Sendmail object + # + # If the first argument is not a Symbol, then it will simply be returned: + # + # ActionMailer::DeliveryMethod.lookup_method(MyOwnDeliveryMethod.new) + # # => returns MyOwnDeliveryMethod.new + def self.lookup_method(delivery_method) + case delivery_method + when Symbol + method_name = delivery_method.to_s.camelize + method_class = ActionMailer::DeliveryMethod.const_get(method_name) + method_class.new() + when nil + Smtp.new + else + delivery_method + end + end + + # An abstract delivery method class. There are multiple delivery method + # classes, documented under + # See the classes under the ActionMailer::DeliveryMethod, e.g. + # ActionMailer::DeliveryMethod::Smtp. + # Smtp is the default delivery method for production + # while Test is used in testing. + # + # each delivery method exposes just one method + # + # delivery_method = ActionMailer::DeliveryMethod::Smtp.new + # + # delivery_method.perform_delivery(mail) # send the mail via smtp + class Method + superclass_delegating_accessor :settings + self.settings = {} + end + + end +end diff --git a/actionmailer/lib/action_mailer/delivery_method/file.rb b/actionmailer/lib/action_mailer/delivery_method/file.rb new file mode 100644 index 0000000000..8807a05221 --- /dev/null +++ b/actionmailer/lib/action_mailer/delivery_method/file.rb @@ -0,0 +1,21 @@ +require 'tmpdir' +module ActionMailer + module DeliveryMethod + + # A delivery method implementation which writes all mails to a file. + class File < Method + + self.settings = { + :location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" + } + + def perform_delivery(mail) + FileUtils.mkdir_p settings[:location] + + (mail.to + mail.cc + mail.bcc).uniq.each do |to| + ::File.open(::File.join(settings[:location], to), 'a') { |f| f.write(mail) } + end + end + end + end +end diff --git a/actionmailer/lib/action_mailer/delivery_method/sendmail.rb b/actionmailer/lib/action_mailer/delivery_method/sendmail.rb new file mode 100644 index 0000000000..34e03b8060 --- /dev/null +++ b/actionmailer/lib/action_mailer/delivery_method/sendmail.rb @@ -0,0 +1,22 @@ +module ActionMailer + module DeliveryMethod + + # A delivery method implementation which sends via sendmail. + class Sendmail < Method + + self.settings = { + :location => '/usr/sbin/sendmail', + :arguments => '-i -t' + } + + def perform_delivery(mail) + sendmail_args = settings[:arguments] + sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path'] + IO.popen("#{settings[:location]} #{sendmail_args}","w+") do |sm| + sm.print(mail.encoded.gsub(/\r/, '')) + sm.flush + end + end + end + end +end diff --git a/actionmailer/lib/action_mailer/delivery_method/smtp.rb b/actionmailer/lib/action_mailer/delivery_method/smtp.rb new file mode 100644 index 0000000000..e39f97330c --- /dev/null +++ b/actionmailer/lib/action_mailer/delivery_method/smtp.rb @@ -0,0 +1,31 @@ +module ActionMailer + module DeliveryMethod + + # A delivery method implementation which sends via smtp. + class Smtp < Method + + self.settings = { + :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true, + } + + def perform_delivery(mail) + destinations = mail.destinations + mail.ready_to_send + sender = (mail['return-path'] && mail['return-path'].spec) || mail['from'] + + smtp = Net::SMTP.new(settings[:address], settings[:port]) + smtp.enable_starttls_auto if settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto) + smtp.start(settings[:domain], settings[:user_name], settings[:password], + settings[:authentication]) do |smtp| + smtp.sendmail(mail.encoded, sender, destinations) + end + end + end + end +end diff --git a/actionmailer/lib/action_mailer/delivery_method/test.rb b/actionmailer/lib/action_mailer/delivery_method/test.rb new file mode 100644 index 0000000000..e63e0abbb8 --- /dev/null +++ b/actionmailer/lib/action_mailer/delivery_method/test.rb @@ -0,0 +1,12 @@ +module ActionMailer + module DeliveryMethod + + # A delivery method implementation designed for testing, which just appends each record to the :deliveries array + class Test < Method + + def perform_delivery(mail) + ActionMailer::Base.deliveries << mail + end + end + end +end -- cgit v1.2.3 From a9751a7034c5a2a49fd90e9f79ad5fcae103487b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 17 Oct 2009 10:31:11 -0300 Subject: Refactor ActionMailer layout and remove legacy one. --- actionmailer/lib/action_mailer/base.rb | 42 ++++++++++------------ actionmailer/lib/action_mailer/delivery_method.rb | 17 +++++---- .../lib/action_mailer/delivery_method/file.rb | 2 +- .../lib/action_mailer/delivery_method/sendmail.rb | 2 +- .../lib/action_mailer/delivery_method/smtp.rb | 2 +- .../lib/action_mailer/delivery_method/test.rb | 2 +- 6 files changed, 31 insertions(+), 36 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 898356075f..29afa2692a 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -1,6 +1,4 @@ -require "active_support/core_ext/class" -# Use the old layouts until actionmailer gets refactored -require "action_controller/legacy/layout" +require 'active_support/core_ext/class' module ActionMailer #:nodoc: # Action Mailer allows you to send email from your application using a mailer model and views. @@ -254,11 +252,12 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base include AdvAttrAccessor, PartContainer, Quoting, Utils - extend AbstractController::RenderingController + + include AbstractController::RenderingController + include AbstractController::Layouts if Object.const_defined?(:ActionController) include ActionController::UrlWriter - include ActionController::Layout end private_class_method :new #:nodoc: @@ -569,8 +568,7 @@ module ActionMailer #:nodoc: end @template = initialize_template_class(body) - layout = _pick_layout(layout, true) unless - ActionController::Base.exempt_from_layout.include?(template.handler) + layout = _layout_for_option(:default, :formats => formats) @template._render_template(template, layout, {}) ensure @current_template_content_type = nil @@ -583,25 +581,23 @@ module ActionMailer #:nodoc: end def render(opts) + file = opts[:file] opts[:locals] ||= {} - layout, file = opts.delete(:layout), opts[:file] - - begin - @template = initialize_template_class(opts.delete(:body)) - - if file - prefix = mailer_name unless file =~ /\// - template = view_paths.find(file, {:formats => formats}, prefix) - end - layout = _pick_layout(layout, - !template || ActionController::Base.exempt_from_layout.include?(template.handler)) + @template = initialize_template_class(opts.delete(:body)) - if template - @template._render_template(template, layout, opts) - elsif inline = opts[:inline] - @template._render_inline(inline, layout, opts) - end + if file + prefix = mailer_name unless file =~ /\// + template = view_paths.find(file, {:formats => formats}, prefix) + end + + layout = opts.key?(:layout) ? opts.delete(:layout) : :default + layout = _layout_for_option(layout, :formats => formats) + + if template + @template._render_template(template, layout, opts) + elsif inline = opts[:inline] + @template._render_inline(inline, layout, opts) end end diff --git a/actionmailer/lib/action_mailer/delivery_method.rb b/actionmailer/lib/action_mailer/delivery_method.rb index ffba3c418c..29a51afdc3 100644 --- a/actionmailer/lib/action_mailer/delivery_method.rb +++ b/actionmailer/lib/action_mailer/delivery_method.rb @@ -2,10 +2,10 @@ require "active_support/core_ext/class" module ActionMailer module DeliveryMethod - autoload :File, 'action_mailer/delivery_method/file' + autoload :File, 'action_mailer/delivery_method/file' autoload :Sendmail, 'action_mailer/delivery_method/sendmail' - autoload :Smtp, 'action_mailer/delivery_method/smtp' - autoload :Test, 'action_mailer/delivery_method/test' + autoload :Smtp, 'action_mailer/delivery_method/smtp' + autoload :Test, 'action_mailer/delivery_method/test' # Creates a new DeliveryMethod object according to the given options. # @@ -27,18 +27,17 @@ module ActionMailer def self.lookup_method(delivery_method) case delivery_method when Symbol - method_name = delivery_method.to_s.camelize + method_name = delivery_method.to_s.camelize method_class = ActionMailer::DeliveryMethod.const_get(method_name) - method_class.new() - when nil + method_class.new + when nil # default Smtp.new else delivery_method end end - # An abstract delivery method class. There are multiple delivery method - # classes, documented under + # An abstract delivery method class. There are multiple delivery method classes. # See the classes under the ActionMailer::DeliveryMethod, e.g. # ActionMailer::DeliveryMethod::Smtp. # Smtp is the default delivery method for production @@ -47,8 +46,8 @@ module ActionMailer # each delivery method exposes just one method # # delivery_method = ActionMailer::DeliveryMethod::Smtp.new - # # delivery_method.perform_delivery(mail) # send the mail via smtp + # class Method superclass_delegating_accessor :settings self.settings = {} diff --git a/actionmailer/lib/action_mailer/delivery_method/file.rb b/actionmailer/lib/action_mailer/delivery_method/file.rb index 8807a05221..587ae37ffa 100644 --- a/actionmailer/lib/action_mailer/delivery_method/file.rb +++ b/actionmailer/lib/action_mailer/delivery_method/file.rb @@ -1,10 +1,10 @@ require 'tmpdir' + module ActionMailer module DeliveryMethod # A delivery method implementation which writes all mails to a file. class File < Method - self.settings = { :location => defined?(Rails) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" } diff --git a/actionmailer/lib/action_mailer/delivery_method/sendmail.rb b/actionmailer/lib/action_mailer/delivery_method/sendmail.rb index 34e03b8060..db55af79f1 100644 --- a/actionmailer/lib/action_mailer/delivery_method/sendmail.rb +++ b/actionmailer/lib/action_mailer/delivery_method/sendmail.rb @@ -3,7 +3,6 @@ module ActionMailer # A delivery method implementation which sends via sendmail. class Sendmail < Method - self.settings = { :location => '/usr/sbin/sendmail', :arguments => '-i -t' @@ -18,5 +17,6 @@ module ActionMailer end end end + end end diff --git a/actionmailer/lib/action_mailer/delivery_method/smtp.rb b/actionmailer/lib/action_mailer/delivery_method/smtp.rb index e39f97330c..86b0ae8329 100644 --- a/actionmailer/lib/action_mailer/delivery_method/smtp.rb +++ b/actionmailer/lib/action_mailer/delivery_method/smtp.rb @@ -1,6 +1,5 @@ module ActionMailer module DeliveryMethod - # A delivery method implementation which sends via smtp. class Smtp < Method @@ -27,5 +26,6 @@ module ActionMailer end end end + end end diff --git a/actionmailer/lib/action_mailer/delivery_method/test.rb b/actionmailer/lib/action_mailer/delivery_method/test.rb index e63e0abbb8..6e3239d52a 100644 --- a/actionmailer/lib/action_mailer/delivery_method/test.rb +++ b/actionmailer/lib/action_mailer/delivery_method/test.rb @@ -3,10 +3,10 @@ module ActionMailer # A delivery method implementation designed for testing, which just appends each record to the :deliveries array class Test < Method - def perform_delivery(mail) ActionMailer::Base.deliveries << mail end end + end end -- cgit v1.2.3 From 684c2dc20801b7fcc941ec9478d33d3bf7c74551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 18 Oct 2009 20:20:14 -0200 Subject: Remove ActionMailer helpers and rely on AbstractController one. --- actionmailer/lib/action_mailer/base.rb | 30 ++++---- actionmailer/lib/action_mailer/helpers.rb | 114 ------------------------------ 2 files changed, 14 insertions(+), 130 deletions(-) delete mode 100644 actionmailer/lib/action_mailer/helpers.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 29afa2692a..d01120d0d8 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -256,6 +256,9 @@ module ActionMailer #:nodoc: include AbstractController::RenderingController include AbstractController::Layouts + include AbstractController::Helpers + helper MailHelper + if Object.const_defined?(:ActionController) include ActionController::UrlWriter end @@ -442,6 +445,7 @@ module ActionMailer #:nodoc: self.view_paths && self.view_paths.first end + # Should template root overwrite the whole view_paths? def template_root=(root) self.view_paths = ActionView::Base.process_view_paths(root) end @@ -470,6 +474,11 @@ module ActionMailer #:nodoc: # If an explicit, textual body has not been set, we check assumptions. unless String === @body + # TODO Hax + @body.each do |k, v| + instance_variable_set(:"@#{k}", v) + end + # First, we look to see if there are any likely templates that match, # which include the content-type in their file name (i.e., # "the_template_file.text.html.erb", etc.). Only do this if parts @@ -480,7 +489,7 @@ module ActionMailer #:nodoc: :content_type => template.mime_type ? template.mime_type.to_s : "text/plain", :disposition => "inline", :charset => charset, - :body => render_template(template, @body) + :body => render_to_string(:_template => template) ) end @@ -562,16 +571,10 @@ module ActionMailer #:nodoc: @sent_on ||= Time.now end - def render_template(template, body) - if template.respond_to?(:mime_type) - @current_template_content_type = template.mime_type && template.mime_type.to_sym.to_s - end - - @template = initialize_template_class(body) - layout = _layout_for_option(:default, :formats => formats) - @template._render_template(template, layout, {}) - ensure - @current_template_content_type = nil + def _determine_template(options) + super + layout = options.key?(:layout) ? options[:layout] : :default + options[:_layout] = _layout_for_option(layout, options[:_template].details) end def render_message(method_name, body) @@ -701,9 +704,4 @@ module ActionMailer #:nodoc: end end - - Base.class_eval do - include Helpers - helper MailHelper - end end diff --git a/actionmailer/lib/action_mailer/helpers.rb b/actionmailer/lib/action_mailer/helpers.rb deleted file mode 100644 index ecd8f0f5b6..0000000000 --- a/actionmailer/lib/action_mailer/helpers.rb +++ /dev/null @@ -1,114 +0,0 @@ -require 'active_support/dependencies' - -module ActionMailer - module Helpers #:nodoc: - def self.included(base) #:nodoc: - # Initialize the base module to aggregate its helpers. - base.class_inheritable_accessor :master_helper_module - base.master_helper_module = Module.new - - # Extend base with class methods to declare helpers. - base.extend(ClassMethods) - - base.class_eval do - # Wrap inherited to create a new master helper module for subclasses. - class << self - alias_method_chain :inherited, :helper - end - - # Wrap initialize_template_class to extend new template class - # instances with the master helper module. - alias_method_chain :initialize_template_class, :helper - end - end - - module ClassMethods - # Makes all the (instance) methods in the helper module available to templates rendered through this controller. - # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules - # available to the templates. - def add_template_helper(helper_module) #:nodoc: - master_helper_module.module_eval "include #{helper_module}" - end - - # Declare a helper: - # helper :foo - # requires 'foo_helper' and includes FooHelper in the template class. - # helper FooHelper - # includes FooHelper in the template class. - # helper { def foo() "#{bar} is the very best" end } - # evaluates the block in the template class, adding method +foo+. - # helper(:three, BlindHelper) { def mice() 'mice' end } - # does all three. - def helper(*args, &block) - args.flatten.each do |arg| - case arg - when Module - add_template_helper(arg) - when String, Symbol - file_name = arg.to_s.underscore + '_helper' - class_name = file_name.camelize - - require_dependency(file_name, "Missing helper file helpers/%s.rb") - # begin - # require_dependency(file_name) - # rescue LoadError => load_error - # requiree = / -- (.*?)(\.rb)?$/.match(load_error.message).to_a[1] - # msg = (requiree == file_name) ? "Missing helper file helpers/#{file_name}.rb" : "Can't load file: #{requiree}" - # raise LoadError.new(msg).copy_blame!(load_error) - # end - - add_template_helper(class_name.constantize) - else - raise ArgumentError, 'helper expects String, Symbol, or Module argument' - end - end - - # Evaluate block in template class if given. - master_helper_module.module_eval(&block) if block_given? - end - - # Declare a controller method as a helper. For example, - # helper_method :link_to - # def link_to(name, options) ... end - # makes the link_to controller method available in the view. - def helper_method(*methods) - methods.flatten.each do |method| - master_helper_module.module_eval <<-end_eval - def #{method}(*args, &block) - controller.__send__(%(#{method}), *args, &block) - end - end_eval - end - end - - # Declare a controller attribute as a helper. For example, - # helper_attr :name - # attr_accessor :name - # makes the name and name= controller methods available in the view. - # The is a convenience wrapper for helper_method. - def helper_attr(*attrs) - attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") } - end - - private - def inherited_with_helper(child) - inherited_without_helper(child) - begin - child.master_helper_module = Module.new - child.master_helper_module.__send__(:include, master_helper_module) - child.helper child.name.to_s.underscore - rescue MissingSourceFile => e - raise unless e.is_missing?("#{child.name.to_s.underscore}_helper") - end - end - end - - private - # Extend the template class instance with our controller's helper module. - def initialize_template_class_with_helper(assigns) - initialize_template_class_without_helper(assigns).tap do |template| - template.extend self.class.master_helper_module - end - end - end -end -- cgit v1.2.3 From 43d5504f0a6a831474d149aa5f1ebb2545790152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 18 Oct 2009 22:52:36 -0200 Subject: Move all render and layout pieces required in ActionMailer from ActionController to AbstractController. --- actionmailer/lib/action_mailer/base.rb | 168 ++++++++++++++------------------- 1 file changed, 70 insertions(+), 98 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index d01120d0d8..3009d8cdfc 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -376,13 +376,19 @@ module ActionMailer #:nodoc: # The mail object instance referenced by this mailer. attr_reader :mail attr_reader :template_name, :default_template_name, :action_name + attr_internal :response_body def controller_path self.class.controller_path end - + def formats - @template.formats + [:"*/*"] + end + + # Refactor out all mailer_name + def _prefix + mailer_name end class << self @@ -468,67 +474,68 @@ module ActionMailer #:nodoc: # Initialize the mailer via the given +method_name+. The body will be # rendered and a new TMail::Mail object created. def create!(method_name, *parameters) #:nodoc: - ActiveSupport::Notifications.instrument(:create_mail, :name => method_name) do - initialize_defaults(method_name) - __send__(method_name, *parameters) + initialize_defaults(method_name) + __send__(method_name, *parameters) + + # Check if render was called. + @body = self.response_body if @body.is_a?(Hash) && @body.empty? - # If an explicit, textual body has not been set, we check assumptions. - unless String === @body - # TODO Hax + # If an explicit, textual body has not been set, we check assumptions. + unless String === @body + # TODO Fix me. Deprecate assigns to be given as a :body hash + if @body.is_a?(Hash) @body.each do |k, v| instance_variable_set(:"@#{k}", v) end - - # First, we look to see if there are any likely templates that match, - # which include the content-type in their file name (i.e., - # "the_template_file.text.html.erb", etc.). Only do this if parts - # have not already been specified manually. - # if @parts.empty? - template_root.find_all(@template, {}, template_path).each do |template| - @parts << Part.new( - :content_type => template.mime_type ? template.mime_type.to_s : "text/plain", - :disposition => "inline", - :charset => charset, - :body => render_to_string(:_template => template) - ) - end - - if @parts.size > 1 - @content_type = "multipart/alternative" if @content_type !~ /^multipart/ - @parts = sort_parts(@parts, @implicit_parts_order) - end - # end - - # Then, if there were such templates, we check to see if we ought to - # also render a "normal" template (without the content type). If a - # normal template exists (or if there were no implicit parts) we render - # it. - # ==== - # TODO: Revisit this - # template_exists = @parts.empty? - # template_exists ||= template_root.find("#{mailer_name}/#{@template}") - # @body = render_message(@template, @body) if template_exists - - # Finally, if there are other message parts and a textual body exists, - # we shift it onto the front of the parts and set the body to nil (so - # that create_mail doesn't try to render it in addition to the parts). - # ==== - # TODO: Revisit this - # if !@parts.empty? && String === @body - # @parts.unshift Part.new(:charset => charset, :body => @body) - # @body = nil - # end end - # If this is a multipart e-mail add the mime_version if it is not - # already set. - @mime_version ||= "1.0" if !@parts.empty? + # First, we look to see if there are any likely templates that match, + # which include the content-type in their file name (i.e., + # "the_template_file.text.html.erb", etc.). Only do this if parts + # have not already been specified manually. + # if @parts.empty? + template_root.find_all(@template, {}, template_path).each do |template| + @parts << Part.new( + :content_type => template.mime_type ? template.mime_type.to_s : "text/plain", + :disposition => "inline", + :charset => charset, + :body => render_to_body(:_template => template) + ) + end - # build the mail object itself - @mail = create_mail + if @parts.size > 1 + @content_type = "multipart/alternative" if @content_type !~ /^multipart/ + @parts = sort_parts(@parts, @implicit_parts_order) + end + # end + + # Then, if there were such templates, we check to see if we ought to + # also render a "normal" template (without the content type). If a + # normal template exists (or if there were no implicit parts) we render + # it. + # ==== + # TODO: Revisit this + # template_exists = @parts.empty? + # template_exists ||= template_root.find("#{mailer_name}/#{@template}") + # @body = render_message(@template, @body) if template_exists + + # Finally, if there are other message parts and a textual body exists, + # we shift it onto the front of the parts and set the body to nil (so + # that create_mail doesn't try to render it in addition to the parts). + # ==== + # TODO: Revisit this + # if !@parts.empty? && String === @body + # @parts.unshift Part.new(:charset => charset, :body => @body) + # @body = nil + # end end - @mail + # If this is a multipart e-mail add the mime_version if it is not + # already set. + @mime_version ||= "1.0" if !@parts.empty? + + # build the mail object itself + @mail = create_mail end # Delivers a TMail::Mail object. By default, it delivers the cached mail @@ -537,7 +544,7 @@ module ActionMailer #:nodoc: def deliver!(mail = @mail) raise "no mail object available for delivery!" unless mail - unless logger.nil? + if logger logger.info "Sent mail to #{Array(recipients).join(', ')}" logger.debug "\n#{mail.encoded}" end @@ -571,45 +578,16 @@ module ActionMailer #:nodoc: @sent_on ||= Time.now end - def _determine_template(options) - super - layout = options.key?(:layout) ? options[:layout] : :default - options[:_layout] = _layout_for_option(layout, options[:_template].details) - end - - def render_message(method_name, body) - render :file => method_name, :body => body - ensure - @current_template_content_type = nil - end - - def render(opts) - file = opts[:file] - opts[:locals] ||= {} - - @template = initialize_template_class(opts.delete(:body)) - - if file - prefix = mailer_name unless file =~ /\// - template = view_paths.find(file, {:formats => formats}, prefix) + def render(*args) + # TODO Fix me. Deprecate assigns to be given as a :body hash + options = args.last.is_a?(Hash) ? args.last : {} + if options[:body] + options.delete(:body).each do |k, v| + instance_variable_set(:"@#{k}", v) + end end - layout = opts.key?(:layout) ? opts.delete(:layout) : :default - layout = _layout_for_option(layout, :formats => formats) - - if template - @template._render_template(template, layout, opts) - elsif inline = opts[:inline] - @template._render_inline(inline, layout, opts) - end - end - - def default_template_format - if @current_template_content_type - Mime::Type.lookup(@current_template_content_type).to_sym - else - :html - end + super end def template_root @@ -624,12 +602,6 @@ module ActionMailer #:nodoc: "#{mailer_name}" end - def initialize_template_class(assigns) - template = ActionView::Base.new(self.class.view_paths, assigns, self) - template.formats = [default_template_format] - template - end - def sort_parts(parts, order = []) order = order.collect { |s| s.downcase } -- cgit v1.2.3 From 03960048616593c249745d1e321dbcc7f0483c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 21 Oct 2009 17:47:10 -0200 Subject: Add some basic render_test to AbstractController. --- actionmailer/lib/action_mailer/base.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 3009d8cdfc..e3690577e1 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -386,11 +386,6 @@ module ActionMailer #:nodoc: [:"*/*"] end - # Refactor out all mailer_name - def _prefix - mailer_name - end - class << self attr_writer :mailer_name -- cgit v1.2.3 From 418c3f801cd436f011b37fe69059073e69e05084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 21 Oct 2009 21:05:55 -0200 Subject: Another refactoring on AM. body is deprecated, use render instead. --- actionmailer/lib/action_mailer/base.rb | 152 ++++++---------------- actionmailer/lib/action_mailer/deprecated_body.rb | 44 +++++++ 2 files changed, 87 insertions(+), 109 deletions(-) create mode 100644 actionmailer/lib/action_mailer/deprecated_body.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index e3690577e1..3d71ba5a14 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -263,6 +263,8 @@ module ActionMailer #:nodoc: include ActionController::UrlWriter end + include ActionMailer::DeprecatedBody + private_class_method :new #:nodoc: class_inheritable_accessor :view_paths @@ -304,16 +306,11 @@ module ActionMailer #:nodoc: cattr_accessor :default_implicit_parts_order cattr_reader :protected_instance_variables - @@protected_instance_variables = %w(@body) + @@protected_instance_variables = [] # Specify the BCC addresses for the message adv_attr_accessor :bcc - # 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 - # Specify the CC addresses for the message. adv_attr_accessor :cc @@ -358,33 +355,27 @@ module ActionMailer #:nodoc: # have multiple mailer methods share the same template. adv_attr_accessor :template + # The mail and action_name instances referenced by this mailer. + attr_reader :mail, :action_name + + # Where the response body is stored. + attr_internal :response_body + # 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. + attr_writer :mailer_name + def mailer_name(value = nil) if value - self.mailer_name = value + @mailer_name = value else - self.class.mailer_name + @mailer_name || self.class.mailer_name end end - def mailer_name=(value) - self.class.mailer_name = value - end - - # The mail object instance referenced by this mailer. - attr_reader :mail - attr_reader :template_name, :default_template_name, :action_name - attr_internal :response_body - - def controller_path - self.class.controller_path - end - - def formats - [:"*/*"] - end + # Alias controller_path to mailer_name so render :partial in views work. + alias :controller_path :mailer_name class << self attr_writer :mailer_name @@ -393,10 +384,6 @@ module ActionMailer #:nodoc: @mailer_name ||= name.underscore end - # for ActionView compatibility - alias_method :controller_name, :mailer_name - alias_method :controller_path, :mailer_name - def respond_to?(method_symbol, include_private = false) #:nodoc: matches_dynamic_method?(method_symbol) || super end @@ -472,58 +459,8 @@ module ActionMailer #:nodoc: initialize_defaults(method_name) __send__(method_name, *parameters) - # Check if render was called. - @body = self.response_body if @body.is_a?(Hash) && @body.empty? - - # If an explicit, textual body has not been set, we check assumptions. - unless String === @body - # TODO Fix me. Deprecate assigns to be given as a :body hash - if @body.is_a?(Hash) - @body.each do |k, v| - instance_variable_set(:"@#{k}", v) - end - end - - # First, we look to see if there are any likely templates that match, - # which include the content-type in their file name (i.e., - # "the_template_file.text.html.erb", etc.). Only do this if parts - # have not already been specified manually. - # if @parts.empty? - template_root.find_all(@template, {}, template_path).each do |template| - @parts << Part.new( - :content_type => template.mime_type ? template.mime_type.to_s : "text/plain", - :disposition => "inline", - :charset => charset, - :body => render_to_body(:_template => template) - ) - end - - if @parts.size > 1 - @content_type = "multipart/alternative" if @content_type !~ /^multipart/ - @parts = sort_parts(@parts, @implicit_parts_order) - end - # end - - # Then, if there were such templates, we check to see if we ought to - # also render a "normal" template (without the content type). If a - # normal template exists (or if there were no implicit parts) we render - # it. - # ==== - # TODO: Revisit this - # template_exists = @parts.empty? - # template_exists ||= template_root.find("#{mailer_name}/#{@template}") - # @body = render_message(@template, @body) if template_exists - - # Finally, if there are other message parts and a textual body exists, - # we shift it onto the front of the parts and set the body to nil (so - # that create_mail doesn't try to render it in addition to the parts). - # ==== - # TODO: Revisit this - # if !@parts.empty? && String === @body - # @parts.unshift Part.new(:charset => charset, :body => @body) - # @body = nil - # end - end + # Create e-mail parts + create_parts # If this is a multipart e-mail add the mime_version if it is not # already set. @@ -556,6 +493,7 @@ module ActionMailer #:nodoc: end private + # Set up the default values for the various instance variables of this # mailer. Subclasses may override this method to provide different # defaults. @@ -568,38 +506,42 @@ module ActionMailer #:nodoc: @mailer_name ||= self.class.name.underscore @parts ||= [] @headers ||= {} - @body ||= {} @mime_version = @@default_mime_version.dup if @@default_mime_version @sent_on ||= Time.now - end - def render(*args) - # TODO Fix me. Deprecate assigns to be given as a :body hash - options = args.last.is_a?(Hash) ? args.last : {} - if options[:body] - options.delete(:body).each do |k, v| - instance_variable_set(:"@#{k}", v) - end - end - - super + super # Run deprecation hooks end - def template_root - self.class.template_root - end + def create_parts + super # Run deprecation hooks - def template_root=(root) - self.class.template_root = root - end + if String === response_body + @parts.unshift Part.new( + :content_type => "text/plain", + :disposition => "inline", + :charset => charset, + :body => response_body + ) + else + self.class.template_root.find_all(@template, {}, mailer_name).each do |template| + @parts << Part.new( + :content_type => template.mime_type ? template.mime_type.to_s : "text/plain", + :disposition => "inline", + :charset => charset, + :body => render_to_body(:_template => template) + ) + end - def template_path - "#{mailer_name}" + if @parts.size > 1 + @content_type = "multipart/alternative" if @content_type !~ /^multipart/ + @parts = sort_parts(@parts, @implicit_parts_order) + end + end end def sort_parts(parts, order = []) order = order.collect { |s| s.downcase } - + parts = parts.sort do |a, b| a_ct = a.content_type.downcase b_ct = b.content_type.downcase @@ -648,14 +590,6 @@ module ActionMailer #:nodoc: m.set_content_type(real_content_type, nil, ctype_attrs) m.body = normalize_new_lines(@parts.first.body) else - if String === body - part = TMail::Mail.new - part.body = normalize_new_lines(body) - part.set_content_type(real_content_type, nil, ctype_attrs) - part.set_content_disposition "inline" - m.parts << part - end - @parts.each do |p| part = (TMail::Mail === p ? p : p.to_mail(self)) m.parts << part diff --git a/actionmailer/lib/action_mailer/deprecated_body.rb b/actionmailer/lib/action_mailer/deprecated_body.rb new file mode 100644 index 0000000000..982f098bc5 --- /dev/null +++ b/actionmailer/lib/action_mailer/deprecated_body.rb @@ -0,0 +1,44 @@ +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 + def self.included(base) + base.class_eval do + # 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 + end + end + + def initialize_defaults(method_name) + @body ||= {} + end + + def create_parts + if String === @body + ActiveSupport::Deprecation.warn('body is deprecated. To set the body with a text ' << + 'call render(:text => "body").', caller[7,1]) + self.response_body = @body + elsif @body.is_a?(Hash) && !@body.empty? + ActiveSupport::Deprecation.warn('body is deprecated. To set assigns simply ' << + 'use instance variables', caller[7,1]) + @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 is deprecated. To set assigns simply ' << + 'use instance variables', caller[0,1]) + + options.delete(:body).each do |k, v| + instance_variable_set(:"@#{k}", v) + end + end + + super + end + end +end -- cgit v1.2.3 From e9667ad1f0e74694d9df16fa2819a1679ee21e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 21 Oct 2009 21:26:10 -0200 Subject: Make tests run without deprecation warning (just one left). --- actionmailer/lib/action_mailer/deprecated_body.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/deprecated_body.rb b/actionmailer/lib/action_mailer/deprecated_body.rb index 982f098bc5..50ff262432 100644 --- a/actionmailer/lib/action_mailer/deprecated_body.rb +++ b/actionmailer/lib/action_mailer/deprecated_body.rb @@ -18,11 +18,11 @@ module ActionMailer def create_parts if String === @body ActiveSupport::Deprecation.warn('body is deprecated. To set the body with a text ' << - 'call render(:text => "body").', caller[7,1]) + 'call render(:text => "body").', caller[0,10]) self.response_body = @body elsif @body.is_a?(Hash) && !@body.empty? ActiveSupport::Deprecation.warn('body is deprecated. To set assigns simply ' << - 'use instance variables', caller[7,1]) + 'use instance variables', caller[0,10]) @body.each { |k, v| instance_variable_set(:"@#{k}", v) } end end -- cgit v1.2.3 From 81c416b72e44a75ea74ba342cd223a33f3b80caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 28 Oct 2009 09:08:55 -0200 Subject: More refactoring on ActionMailer::Base. --- actionmailer/lib/action_mailer/base.rb | 43 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 3d71ba5a14..49551dca51 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -272,18 +272,6 @@ module ActionMailer #:nodoc: cattr_accessor :logger - class << self - delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::File, :prefix => :file - delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Sendmail, :prefix => :sendmail - delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Smtp, :prefix => :smtp - - def delivery_method=(method_name) - @delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name) - end - end - self.delivery_method = :smtp - superclass_delegating_reader :delivery_method - @@raise_delivery_errors = true cattr_accessor :raise_delivery_errors @@ -305,8 +293,8 @@ module ActionMailer #:nodoc: @@default_implicit_parts_order = [ "text/html", "text/enriched", "text/plain" ] cattr_accessor :default_implicit_parts_order - cattr_reader :protected_instance_variables @@protected_instance_variables = [] + cattr_reader :protected_instance_variables # Specify the BCC addresses for the message adv_attr_accessor :bcc @@ -380,10 +368,18 @@ module ActionMailer #:nodoc: class << self attr_writer :mailer_name + delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::File, :prefix => :file + delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Sendmail, :prefix => :sendmail + delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Smtp, :prefix => :smtp + def mailer_name @mailer_name ||= name.underscore end + def delivery_method=(method_name) + @delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name) + end + def respond_to?(method_symbol, include_private = false) #:nodoc: matches_dynamic_method?(method_symbol) || super end @@ -445,6 +441,11 @@ module ActionMailer #:nodoc: end end + # Configure delivery method. Check ActionMailer::DeliveryMethod for more + # instructions. + superclass_delegating_reader :delivery_method + self.delivery_method = :smtp + # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer # will be initialized according to the named method. If not, the mailer will # remain uninitialized (useful when you only need to invoke the "receive" @@ -498,15 +499,17 @@ module ActionMailer #:nodoc: # mailer. Subclasses may override this method to provide different # defaults. def initialize_defaults(method_name) - @charset ||= @@default_charset.dup - @content_type ||= @@default_content_type.dup + @charset ||= @@default_charset.dup + @content_type ||= @@default_content_type.dup @implicit_parts_order ||= @@default_implicit_parts_order.dup - @template ||= method_name - @default_template_name = @action_name = @template - @mailer_name ||= self.class.name.underscore - @parts ||= [] + @mime_version ||= @@default_mime_version.dup if @@default_mime_version + + @mailer_name ||= self.class.mailer_name + @template ||= method_name + @action_name = @template + + @parts ||= [] @headers ||= {} - @mime_version = @@default_mime_version.dup if @@default_mime_version @sent_on ||= Time.now super # Run deprecation hooks -- cgit v1.2.3 From 2aafdc839600240a55cea06c960d0a2a7f63016d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 28 Oct 2009 09:22:07 -0200 Subject: Use I18n on ActionMailer subjects by default. --- actionmailer/lib/action_mailer/base.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 49551dca51..3855ce91c8 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -463,9 +463,9 @@ module ActionMailer #:nodoc: # Create e-mail parts create_parts - # If this is a multipart e-mail add the mime_version if it is not - # already set. - @mime_version ||= "1.0" if !@parts.empty? + # Set the subject if not set yet + @subject ||= I18n.t(method_name, :scope => [:actionmailer, :subjects, mailer_name], + :default => method_name.humanize) # build the mail object itself @mail = create_mail @@ -539,6 +539,10 @@ module ActionMailer #:nodoc: @content_type = "multipart/alternative" if @content_type !~ /^multipart/ @parts = sort_parts(@parts, @implicit_parts_order) end + + # If this is a multipart e-mail add the mime_version if it is not + # already set. + @mime_version ||= "1.0" if !@parts.empty? end end -- cgit v1.2.3 From 976c2647240fd40a2b706ab5e41856cd47e7b212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 28 Oct 2009 10:33:05 -0200 Subject: Extracted localized_cache.rb from ActionController, added it to AbstractController and made ActionMailer use it. --- actionmailer/lib/action_mailer/base.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 3855ce91c8..293af01bbf 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -254,6 +254,7 @@ module ActionMailer #:nodoc: include AdvAttrAccessor, PartContainer, Quoting, Utils include AbstractController::RenderingController + include AbstractController::LocalizedCache include AbstractController::Layouts include AbstractController::Helpers -- cgit v1.2.3 From 73a36b599ae21d444d3b1f4114306a80b37ec8d4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 5 Nov 2009 16:04:05 -0800 Subject: Shush --- actionmailer/lib/action_mailer/vendor/tmail-1.2.3/tmail/parser.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/vendor/tmail-1.2.3/tmail/parser.rb b/actionmailer/lib/action_mailer/vendor/tmail-1.2.3/tmail/parser.rb index ab1a828471..0ddc525213 100644 --- a/actionmailer/lib/action_mailer/vendor/tmail-1.2.3/tmail/parser.rb +++ b/actionmailer/lib/action_mailer/vendor/tmail-1.2.3/tmail/parser.rb @@ -43,6 +43,7 @@ module Racc class Parser + old_verbose, $VERBOSE = $VERBOSE, nil Racc_Runtime_Version = '1.4.5' Racc_Runtime_Revision = '$Revision: 1.7 $'.split[1] @@ -71,6 +72,7 @@ module Racc Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_R Racc_Runtime_Type = 'ruby' end + $VERBOSE = old_verbose def Parser.racc_runtime_type Racc_Runtime_Type -- cgit v1.2.3 From 4a1f43878105d701fd2dd769ec3cf7e81d133c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 9 Nov 2009 16:35:31 -0200 Subject: Change mailer subjects lookup. --- actionmailer/lib/action_mailer/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 293af01bbf..6a5a598f94 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -465,8 +465,8 @@ module ActionMailer #:nodoc: create_parts # Set the subject if not set yet - @subject ||= I18n.t(method_name, :scope => [:actionmailer, :subjects, mailer_name], - :default => method_name.humanize) + @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], + :default => method_name.humanize) # build the mail object itself @mail = create_mail -- cgit v1.2.3 From d887e46c2946eb1619f551420c5d076e31f9f919 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Nov 2009 14:14:17 -0800 Subject: Be sure to invoke the super chain! And initialize response body. --- actionmailer/lib/action_mailer/base.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 6a5a598f94..bef9bb8e79 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -452,6 +452,8 @@ module ActionMailer #:nodoc: # remain uninitialized (useful when you only need to invoke the "receive" # method, for instance). def initialize(method_name=nil, *parameters) #:nodoc: + @_response_body = nil + super() create!(method_name, *parameters) if method_name end -- cgit v1.2.3