From b842a72e771338f7357d4a9b20ddf70384ab0b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 15 Dec 2009 01:20:59 +0100 Subject: [PATCH 1/3] Do not care blank passwords on update --- CHANGELOG.rdoc | 3 +++ lib/devise/models/authenticatable.rb | 10 +++++++++- test/models/authenticatable_test.rb | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index d0ba6b4a..09345f1f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,6 @@ +* enhancements + * Do not care about blank passwords on update + == 0.7.2 * deprecation diff --git a/lib/devise/models/authenticatable.rb b/lib/devise/models/authenticatable.rb index aa86448e..e9a1f843 100644 --- a/lib/devise/models/authenticatable.rb +++ b/lib/devise/models/authenticatable.rb @@ -62,11 +62,19 @@ module Devise end end - # Verifies whether an incoming_password (ie from login) is the user password. + # Verifies whether an incoming_password (ie from sign in) is the user password. def valid_password?(incoming_password) password_digest(incoming_password) == encrypted_password end + # Overwrite update_attributes to not care for blank passwords. + def update_attributes(attributes) + [:password, :password_confirmation].each do |k| + attributes.delete(k) unless attributes[k].present? + end + super + end + protected # Digests the password using the configured encryptor. diff --git a/test/models/authenticatable_test.rb b/test/models/authenticatable_test.rb index c61e61e7..627c04f8 100644 --- a/test/models/authenticatable_test.rb +++ b/test/models/authenticatable_test.rb @@ -27,6 +27,13 @@ class AuthenticatableTest < ActiveSupport::TestCase assert_equal salt, user.password_salt end + test 'should not care about empty password on update' do + user = create_user + user.update_attributes(:email => "jose.valim+updated@gmail.com", :password => "") + user.reload + assert_equal user.email, "jose.valim+updated@gmail.com" + end + test 'should generate a base64 hash using SecureRandom for password salt' do ActiveSupport::SecureRandom.expects(:base64).with(15).returns('friendly_token') assert_equal 'friendly_token', new_user.password_salt From 0a5ba20931fc1904dc54a09262132d2212d81f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 15 Dec 2009 01:32:40 +0100 Subject: [PATCH 2/3] Mail views a scoped as well. --- CHANGELOG.rdoc | 1 + app/models/devise_mailer.rb | 16 ++++++++++++++-- test/mailers/confirmation_instructions_test.rb | 6 ++++++ .../users/confirmation_instructions.erb | 1 + 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test/rails_app/app/views/devise_mailer/users/confirmation_instructions.erb diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 09345f1f..27e52164 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,5 +1,6 @@ * enhancements * Do not care about blank passwords on update + * Mail views are scoped as well == 0.7.2 diff --git a/app/models/devise_mailer.rb b/app/models/devise_mailer.rb index 79d9bdd3..7bc91a9f 100644 --- a/app/models/devise_mailer.rb +++ b/app/models/devise_mailer.rb @@ -26,7 +26,7 @@ class DeviseMailer < ::ActionMailer::Base # Configure default email options def setup_mail(record, key) - mapping = Devise.mappings.values.find { |m| m.to == record.class } + mapping = Devise::Mapping.find_by_class(record.class) raise "Invalid devise resource #{record}" unless mapping subject translate(mapping, key) @@ -34,7 +34,19 @@ class DeviseMailer < ::ActionMailer::Base recipients record.email sent_on Time.now content_type 'text/html' - body mapping.name => record, :resource => record + body render_with_scope(key, mapping, mapping.name => record, :resource => record) + end + + def render_with_scope(key, mapping, assigns) + if Devise.scoped_views + begin + render :file => "devise_mailer/#{mapping.as}/#{key}", :body => assigns + rescue ActionView::MissingTemplate + render :file => "devise_mailer/#{key}", :body => assigns + end + else + render :file => "devise_mailer/#{key}", :body => assigns + end end # Setup subject namespaced by model. It means you're able to setup your diff --git a/test/mailers/confirmation_instructions_test.rb b/test/mailers/confirmation_instructions_test.rb index ecf0a0fd..4eb3cb0d 100644 --- a/test/mailers/confirmation_instructions_test.rb +++ b/test/mailers/confirmation_instructions_test.rb @@ -56,4 +56,10 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase confirmation_url_regexp = %r{} assert_match confirmation_url_regexp, mail.body end + + test 'renders a scoped if scoped_views is set to true' do + swap Devise, :scoped_views => true do + assert_equal user.email, mail.body + end + end end diff --git a/test/rails_app/app/views/devise_mailer/users/confirmation_instructions.erb b/test/rails_app/app/views/devise_mailer/users/confirmation_instructions.erb new file mode 100644 index 00000000..f6e30c3e --- /dev/null +++ b/test/rails_app/app/views/devise_mailer/users/confirmation_instructions.erb @@ -0,0 +1 @@ +<%= @resource.email %> \ No newline at end of file From 508fb2c7eddbcbbf913f76ebe1d98c7b5f2ca0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 15 Dec 2009 01:41:04 +0100 Subject: [PATCH 3/3] Tell about explicit require for ORM. --- generators/devise_install/templates/devise.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/generators/devise_install/templates/devise.rb b/generators/devise_install/templates/devise.rb index 625fedee..a7422b40 100644 --- a/generators/devise_install/templates/devise.rb +++ b/generators/devise_install/templates/devise.rb @@ -41,8 +41,9 @@ Devise.setup do |config| # Configure the e-mail address which will be shown in DeviseMailer. # config.mailer_sender = "foo.bar@yourapp.com" - # Configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper. - # config.orm = :active_record + # Load and configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper. + # require 'devise/orm/mongo_mapper' + # config.orm = :mongo_mapper # Turn scoped views on. Before rendering "sessions/new", it will first check for # "sessions/users/new". It's turned off by default because it's slower if you