Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow scoped views to be customized per controller/mailer class.

  • Loading branch information...
commit 9798ad74559d118ad18ccc74918ccd2f090dacda 1 parent 54cd2cc
@josevalim josevalim authored
View
2  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
View
3  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
@@ -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
View
4 lib/devise.rb
@@ -140,7 +140,7 @@ 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
@@ -148,7 +148,7 @@ module Orm
# 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
View
14 lib/devise/controllers/internal_helpers.rb
@@ -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?
@@ -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}")
@@ -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
View
14 test/integration/authenticatable_test.rb
@@ -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
View
9 test/mailers/confirmation_instructions_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.