1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Fix up implementation of reconfirmable.

This commit is contained in:
Brian Rose 2011-08-12 10:27:57 -06:00
parent 10ac4dbc35
commit a7c5a2e65d
4 changed files with 24 additions and 19 deletions

View file

@ -42,23 +42,17 @@ module Devise
end
def email_exists_in_unconfirmed_emails?(record)
query = record.class
unless record.new_record?
if record.respond_to?(:_id)
query = query.where(:_id => {'$ne' => record._id})
else
query = query.where('id <> ?', record.id)
end
end
query = query.where(:unconfirmed_email => record.email)
query.exists?
count = record.class.where(:unconfirmed_email => record.email).count
expected_count = record.new_record? ? 0 : 1
count > expected_count
end
end
included do
before_create :generate_confirmation_token, :if => :confirmation_required?
after_create :send_confirmation_instructions, :if => :confirmation_required?
before_update :prevent_email_change, :if => :prevent_email_change?
before_update :postpone_email_change_until_confirmation, :if => :postpone_email_change?
after_update :send_confirmation_instructions, :if => :email_change_confirmation_required?
end
@ -111,6 +105,14 @@ module Devise
self.confirmed_at = Time.now
end
def headers_for(action)
if action == :confirmation_instructions && respond_to?(:unconfirmed_email)
{ :to => unconfirmed_email.present? ? unconfirmed_email : email }
else
{}
end
end
protected
# Callback to overwrite if confirmation is required or not.
@ -168,13 +170,13 @@ module Devise
confirm! unless confirmed?
end
def prevent_email_change
def postpone_email_change_until_confirmation
@email_change_confirmation_required = true
self.unconfirmed_email = self.email
self.email = self.email_was
end
def prevent_email_change?
def postpone_email_change?
self.class.reconfirmable && email_changed? && email != unconfirmed_email_was
end

View file

@ -80,8 +80,8 @@ class ConfirmableTest < ActiveSupport::TestCase
end
test 'should send confirmation instructions by email' do
assert_email_sent do
create_user
assert_email_sent "mynewuser@example.com" do
create_user :email => "mynewuser@example.com"
end
end
@ -123,7 +123,7 @@ class ConfirmableTest < ActiveSupport::TestCase
test 'should send email instructions for the user confirm its email' do
user = create_user
assert_email_sent do
assert_email_sent user.email do
User.send_confirmation_instructions(:email => user.email)
end
end
@ -265,7 +265,7 @@ class ConfirmableOnChangeTest < ConfirmableTest
test 'should send confirmation instructions by email after changing email' do
user = create_user
assert user.confirm!
assert_email_sent do
assert_email_sent "new_test@example.com" do
assert user.update_attributes(:email => 'new_test@example.com')
end
end

View file

@ -16,7 +16,7 @@ class SerializableTest < ActiveSupport::TestCase
end
test 'should include unsafe keys on XML if a force_except is provided' do
assert_no_match /email/, @user.to_xml(:force_except => :email)
assert_no_match /<email/, @user.to_xml(:force_except => :email)
assert_match /confirmation-token/, @user.to_xml(:force_except => :email)
end

View file

@ -14,8 +14,11 @@ class ActiveSupport::TestCase
end
alias :assert_present :assert_not_blank
def assert_email_sent(&block)
def assert_email_sent(address = nil, &block)
assert_difference('ActionMailer::Base.deliveries.size') { yield }
if address.present?
assert_equal address, ActionMailer::Base.deliveries.last['to'].to_s
end
end
def assert_email_not_sent(&block)