aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib/action_mailer/delivery_methods.rb
diff options
context:
space:
mode:
authorJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-24 18:11:57 +0100
committerJosé Valim and Mikel Lindsaar <pair@programming.com>2010-01-24 18:11:57 +0100
commit0d931fecbb1132abb71a83bb91435812f2012d0e (patch)
treefd96e7ab8b9061a93f78042d5208d5ada2aac2d1 /actionmailer/lib/action_mailer/delivery_methods.rb
parent5dead5bb88cf04b039df8a1a51ffdb18d0443efe (diff)
downloadrails-0d931fecbb1132abb71a83bb91435812f2012d0e.tar.gz
rails-0d931fecbb1132abb71a83bb91435812f2012d0e.tar.bz2
rails-0d931fecbb1132abb71a83bb91435812f2012d0e.zip
Finish cleaning up delivery methods implementation.
Diffstat (limited to 'actionmailer/lib/action_mailer/delivery_methods.rb')
-rw-r--r--actionmailer/lib/action_mailer/delivery_methods.rb96
1 files changed, 54 insertions, 42 deletions
diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb
index 16b84d4118..38325e512f 100644
--- a/actionmailer/lib/action_mailer/delivery_methods.rb
+++ b/actionmailer/lib/action_mailer/delivery_methods.rb
@@ -1,63 +1,75 @@
+require 'tmpdir'
+
module ActionMailer
- # This modules makes a DSL for adding delivery methods to ActionMailer
+ # Provides a DSL for adding delivery methods to ActionMailer.
module DeliveryMethods
- # TODO Make me class inheritable
- def delivery_settings
- @@delivery_settings ||= Hash.new { |h,k| h[k] = {} }
- end
+ extend ActiveSupport::Concern
- def delivery_methods
- @@delivery_methods ||= {}
- end
+ included do
+ extlib_inheritable_accessor :delivery_methods, :delivery_method,
+ :instance_writer => false
- def delivery_method=(method)
- raise ArgumentError, "Unknown delivery method #{method.inspect}" unless delivery_methods[method]
- @delivery_method = method
- end
+ self.delivery_methods = {}
+ self.delivery_method = :smtp
- def add_delivery_method(symbol, klass, default_options={})
- self.delivery_methods[symbol] = klass
- self.delivery_settings[symbol] = default_options
- end
+ add_delivery_method :smtp, Mail::SMTP,
+ :address => "localhost",
+ :port => 25,
+ :domain => 'localhost.localdomain',
+ :user_name => nil,
+ :password => nil,
+ :authentication => nil,
+ :enable_starttls_auto => true
- def wrap_delivery_behavior(mail, method=nil)
- method ||= delivery_method
+ add_delivery_method :file, Mail::FileDelivery,
+ :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
- mail.register_for_delivery_notification(self)
+ add_delivery_method :sendmail, Mail::Sendmail,
+ :location => '/usr/sbin/sendmail',
+ :arguments => '-i -t'
- if method.is_a?(Symbol)
- mail.delivery_method(delivery_methods[method],
- delivery_settings[method])
- else
- mail.delivery_method(method)
- end
-
- mail.perform_deliveries = perform_deliveries
- mail.raise_delivery_errors = raise_delivery_errors
+ add_delivery_method :test, Mail::TestMailer
end
+ module ClassMethods
+ # Adds a new delivery method through the given class using the given symbol
+ # as alias and the default options supplied:
+ #
+ # Example:
+ #
+ # add_delivery_method :sendmail, Mail::Sendmail,
+ # :location => '/usr/sbin/sendmail',
+ # :arguments => '-i -t'
+ #
+ def add_delivery_method(symbol, klass, default_options={})
+ unless respond_to?(:"#{symbol}_settings")
+ extlib_inheritable_accessor(:"#{symbol}_settings", :instance_writer => false)
+ end
- def respond_to?(method_symbol, include_private = false) #:nodoc:
- matches_settings_method?(method_symbol) || super
- end
+ send(:"#{symbol}_settings=", default_options)
+ self.delivery_methods[symbol.to_sym] = klass
+ end
- protected
+ def wrap_delivery_behavior(mail, method=delivery_method) #:nodoc:
+ mail.register_for_delivery_notification(self)
- # TODO Get rid of this method missing magic
- def method_missing(method_symbol, *parameters) #:nodoc:
- if match = matches_settings_method?(method_symbol)
- if match[2]
- delivery_settings[match[1].to_sym] = parameters[0]
+ if method.is_a?(Symbol)
+ if klass = delivery_methods[method.to_sym]
+ mail.delivery_method(klass, send(:"#{method}_settings"))
+ else
+ raise "Invalid delivery method #{method.inspect}"
+ end
else
- delivery_settings[match[1].to_sym]
+ mail.delivery_method(method)
end
- else
- super
+
+ mail.perform_deliveries = perform_deliveries
+ mail.raise_delivery_errors = raise_delivery_errors
end
end
- def matches_settings_method?(method_name) #:nodoc:
- /(#{delivery_methods.keys.join('|')})_settings(=)?$/.match(method_name.to_s)
+ def wrap_delivery_behavior!(*args) #:nodoc:
+ self.class.wrap_delivery_behavior(message, *args)
end
end
end \ No newline at end of file