From a2f84852af504df8c93c0b0f1afff7f76601b400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 12 Jun 2010 20:56:55 +0200 Subject: [PATCH] Allow the mailer class to be configured. --- CHANGELOG.rdoc | 1 + lib/devise.rb | 12 ++++++++++++ lib/devise/models/confirmable.rb | 2 +- lib/devise/models/lockable.rb | 2 +- lib/devise/models/recoverable.rb | 2 +- lib/generators/devise_install/templates/devise.rb | 4 ++++ 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 79d2ad76..52690b42 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -6,6 +6,7 @@ * Added authenticate(scope) support in routes (by github.com/wildchild) * Added after_update_path_for to registrations controller (by github.com/thedelchop) * Added anybody_signed_in? helper (by github.com/SSDany) + * Allow the mailer object to be replaced through config.mailer = "MyOwnMailer" * bug fix * confirmation_required? is properly honored on active? calls. (by github.com/paulrosania) diff --git a/lib/devise.rb b/lib/devise.rb index ebf56a81..c22752b4 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/numeric/time' +require 'active_support/dependencies' module Devise autoload :FailureApp, 'devise/failure_app' @@ -161,6 +162,17 @@ module Devise yield self end + # Get the mailer class from the mailer reference object. + def self.mailer + @@mailer_ref.get + end + + # Set the mailer reference object to access the mailer. + def self.mailer=(class_name) + @@mailer_ref = ActiveSupport::Dependencies.ref(class_name) + end + self.mailer = "Devise::Mailer" + # Register a model in Devise. You can call this manually if you don't want # to use devise routes. Check devise_for in routes to know which options # are available. diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 657cc162..1e2c6d60 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -51,7 +51,7 @@ module Devise # Send confirmation instructions by email def send_confirmation_instructions generate_confirmation_token if self.confirmation_token.nil? - ::Devise::Mailer.confirmation_instructions(self).deliver + ::Devise.mailer.confirmation_instructions(self).deliver end # Resend confirmation token. This method does not need to generate a new token. diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index eac1a5a8..a5132026 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -49,7 +49,7 @@ module Devise # Send unlock instructions by email def send_unlock_instructions - ::Devise::Mailer.unlock_instructions(self).deliver + ::Devise.mailer.unlock_instructions(self).deliver end # Resend the unlock instructions if the user is locked. diff --git a/lib/devise/models/recoverable.rb b/lib/devise/models/recoverable.rb index bc66971a..b8210368 100644 --- a/lib/devise/models/recoverable.rb +++ b/lib/devise/models/recoverable.rb @@ -28,7 +28,7 @@ module Devise # Resets reset password token and send reset password instructions by email def send_reset_password_instructions generate_reset_password_token! - ::Devise::Mailer.reset_password_instructions(self).deliver + ::Devise.mailer.reset_password_instructions(self).deliver end protected diff --git a/lib/generators/devise_install/templates/devise.rb b/lib/generators/devise_install/templates/devise.rb index b4f97696..4b59b114 100644 --- a/lib/generators/devise_install/templates/devise.rb +++ b/lib/generators/devise_install/templates/devise.rb @@ -1,9 +1,13 @@ # Use this hook to configure devise mailer, warden hooks and so forth. The first # four configuration values can also be set straight in your models. Devise.setup do |config| + # ==> Mailer Configuration # Configure the e-mail address which will be shown in DeviseMailer. config.mailer_sender = "please-change-me@config-initializers-devise.com" + # Configure the class responsible to send e-mails. + # config.mailer = "Devise::Mailer" + # ==> ORM configuration # Load and configure the ORM. Supports :active_record (default), :mongoid # (bson_ext recommended) and :data_mapper (experimental).