Skip to content

Commit

Permalink
Add support to multipart e-mails (just put them in your mailers folde…
Browse files Browse the repository at this point in the history
…r) and headers customization by simply defining headers_for in your model.
  • Loading branch information
josevalim committed Mar 26, 2010
1 parent 4d3a3ce commit 12b64c6
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 15 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.rdoc
Expand Up @@ -7,7 +7,14 @@
* Devise.orm is deprecated. This reduces the required API to hook your ORM with devise.
* Use metal for failure app.
* HTML e-mails now have proper formatting.
* Do not remove options from Datamapper and MongoMapper in find
* Do not remove options from Datamapper and MongoMapper in find.
* Allow to give :skip and :controllers in routes.
* Move trackable logic to the model.
* E-mails now use any template available in the filesystem. Easy to create multipart e-mails.
* E-mails asks headers_for in the model to set the proper headers.

* bug fix
* Do not use lock! on lockable since it's part of ActiveRecord API.

* deprecations
* Rails 3 compatible only.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -2,7 +2,7 @@ source "http://gemcutter.org"

# Need to install Rails from source
gem "rails", :git => "git://github.com/rails/rails.git"
gem "warden", "0.9.7"
gem "warden", "0.10.2"
gem "sqlite3-ruby", :require => "sqlite3"
gem "webrat", "0.7"
gem "mocha", :require => false
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -45,7 +45,7 @@ begin
s.authors = ['José Valim', 'Carlos Antônio']
s.files = FileList["[A-Z]*", "{app,config,lib}/**/*"]
s.extra_rdoc_files = FileList["[A-Z]*"] - %w(Gemfile Rakefile)
s.add_dependency("warden", "~> 0.9.7")
s.add_dependency("warden", "~> 0.10.2")
end

Jeweler::GemcutterTasks.new
Expand Down
19 changes: 13 additions & 6 deletions app/models/devise/mailer.rb
Expand Up @@ -24,16 +24,23 @@ def setup_mail(record, action)
raise "Invalid devise resource #{record}" unless @devise_mapping
@resource = instance_variable_set("@#{@devise_mapping.name}", record)

mail(:subject => translate(@devise_mapping, action),
:from => mailer_sender(@devise_mapping), :to => record.email) do |format|
format.html { render_with_scope(action, :controller => "mailer") }
end
template_path = ["devise/mailer"]
template_path.unshift "#{@devise_mapping.as}/mailer" if self.class.scoped_views?

headers = {
:subject => translate(@devise_mapping, action),
:from => mailer_sender(@devise_mapping),
:to => record.email,
:template_path => template_path
}

headers.merge!(record.headers_for(action)) if record.respond_to?(:headers_for)
mail(headers)
end

def mailer_sender(mapping)
if Devise.mailer_sender.is_a?(Proc)
block_args = mapping.name if Devise.mailer_sender.arity > 0
Devise.mailer_sender.call(block_args)
Devise.mailer_sender.call(mapping.name)
else
Devise.mailer_sender
end
Expand Down
4 changes: 2 additions & 2 deletions lib/devise/controllers/scoped_views.rb
Expand Up @@ -4,7 +4,7 @@ module ScopedViews
extend ActiveSupport::Concern

module ClassMethods
def scoped_views
def scoped_views?
defined?(@scoped_views) ? @scoped_views : Devise.scoped_views
end

Expand All @@ -20,7 +20,7 @@ def scoped_views=(value)
def render_with_scope(action, options={})
controller_name = options.delete(:controller) || self.controller_name

if self.class.scoped_views
if self.class.scoped_views?
begin
render :template => "#{devise_mapping.as}/#{controller_name}/#{action}"
rescue ActionView::MissingTemplate
Expand Down
5 changes: 5 additions & 0 deletions lib/devise/models/authenticatable.rb
Expand Up @@ -87,6 +87,11 @@ def update_with_password(params={})
result
end

# Allows you to overwrite mail messages headers for a given mail.
def headers_for(action)
{}
end

protected

# Digests the password using the configured encryptor.
Expand Down
2 changes: 1 addition & 1 deletion test/integration/authenticatable_test.rb
Expand Up @@ -226,7 +226,7 @@ class AuthenticationTest < ActionController::IntegrationTest
end

assert_match /Special user view/, response.body
assert !Devise::PasswordsController.scoped_views
assert !Devise::PasswordsController.scoped_views?
ensure
Devise::SessionsController.send :remove_instance_variable, :@scoped_views
end
Expand Down
6 changes: 3 additions & 3 deletions test/models_test.rb
Expand Up @@ -23,12 +23,12 @@ def assert_include_modules(klass, *modules)
end

test 'add modules cherry pick' do
assert_include_modules Admin, :authenticatable, :registerable, :timeoutable
assert_include_modules Admin, :authenticatable, :registerable, :timeoutable, :recoverable
end

test 'order of module inclusion' do
correct_module_order = [:authenticatable, :registerable, :timeoutable]
incorrect_module_order = [:authenticatable, :timeoutable, :registerable]
correct_module_order = [:authenticatable, :recoverable, :registerable, :timeoutable]
incorrect_module_order = [:authenticatable, :timeoutable, :registerable, :recoverable]

assert_include_modules Admin, *incorrect_module_order

Expand Down

0 comments on commit 12b64c6

Please sign in to comment.