Skip to content

Commit

Permalink
Allow scoped views to be customized per controller/mailer class.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 8, 2010
1 parent 54cd2cc commit 9798ad7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,7 @@
* enhancements
* Added Registerable
* Added Http Basic Authentication support
* Allow scoped_views to be customized per controller/mailer class

== 0.9.2

Expand Down
3 changes: 2 additions & 1 deletion app/models/devise_mailer.rb
@@ -1,4 +1,5 @@
class DeviseMailer < ::ActionMailer::Base
extend Devise::Controllers::InternalHelpers::ScopedViews

# Deliver confirmation instructions when the user is created or its email is
# updated, and also when confirmation is manually requested
Expand Down Expand Up @@ -31,7 +32,7 @@ def setup_mail(record, key)
end

def render_with_scope(key, mapping, assigns)
if Devise.scoped_views
if self.class.scoped_views
begin
render :file => "devise_mailer/#{mapping.as}/#{key}", :body => assigns
rescue ActionView::MissingTemplate
Expand Down
4 changes: 2 additions & 2 deletions lib/devise.rb
Expand Up @@ -140,15 +140,15 @@ module Orm

# Tell when to use the default scope, if one cannot be found from routes.
mattr_accessor :use_default_scope
@@use_default_scope
@@use_default_scope = false

# The default scope which is used by warden.
mattr_accessor :default_scope
@@default_scope = nil

# Address which sends Devise e-mails.
mattr_accessor :mailer_sender
@@mailer_sender
@@mailer_sender = nil

# Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
mattr_accessor :token_authentication_key
Expand Down
14 changes: 13 additions & 1 deletion lib/devise/controllers/internal_helpers.rb
Expand Up @@ -7,6 +7,7 @@ module InternalHelpers #:nodoc:

def self.included(base)
base.class_eval do
extend ScopedViews
unloadable

helper_method :resource, :scope_name, :resource_name, :resource_class, :devise_mapping, :devise_controller?
Expand All @@ -17,6 +18,16 @@ def self.included(base)
end
end

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

def scoped_views=(value)
@scoped_views = value
end
end

# Gets the actual resource stored in the instance variable
def resource
instance_variable_get(:"@#{resource_name}")
Expand Down Expand Up @@ -104,7 +115,8 @@ def set_now_flash_message(key, kind)
# Accepts just :controller as option.
def render_with_scope(action, options={})
controller_name = options.delete(:controller) || self.controller_name
if Devise.scoped_views

if self.class.scoped_views
begin
render :template => "#{controller_name}/#{devise_mapping.as}/#{action}"
rescue ActionView::MissingTemplate
Expand Down
14 changes: 14 additions & 0 deletions test/integration/authenticatable_test.rb
Expand Up @@ -219,6 +219,20 @@ class AuthenticationTest < ActionController::IntegrationTest
end
end

test 'renders the scoped view if turned on in an specific controller' do
begin
SessionsController.scoped_views = true
assert_raise Webrat::NotFoundError do
sign_in_as_user
end

assert_match /Special user view/, response.body
assert !PasswordsController.scoped_views
ensure
SessionsController.send :remove_instance_variable, :@scoped_views
end
end

test 'does not render the scoped view if turned off' do
swap Devise, :scoped_views => false do
assert_nothing_raised do
Expand Down
9 changes: 9 additions & 0 deletions test/mailers/confirmation_instructions_test.rb
Expand Up @@ -63,6 +63,15 @@ def mail
end
end

test 'renders a scoped if scoped_views is set in the mailer class' do
begin
DeviseMailer.scoped_views = true
assert_equal user.email, mail.body
ensure
DeviseMailer.send :remove_instance_variable, :@scoped_views
end
end

test 'mailer sender accepts a proc' do
swap Devise, :mailer_sender => lambda { "another@example.com" } do
assert_equal ['another@example.com'], mail.from
Expand Down

0 comments on commit 9798ad7

Please sign in to comment.