Do not run validatins unless on reconfirmable branch, closes #1515.

This commit is contained in:
José Valim 2011-12-20 20:54:49 +01:00
parent 94e5a589b6
commit 89db0dd44d
4 changed files with 18 additions and 10 deletions

View File

@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
devise (1.5.2) devise (2.0.0.rc)
bcrypt-ruby (~> 3.0) bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3) orm_adapter (~> 0.0.3)
warden (~> 1.1) warden (~> 1.1)

View File

@ -40,15 +40,17 @@ module Devise
# is already confirmed, add an error to email field. If the user is invalid # is already confirmed, add an error to email field. If the user is invalid
# add errors # add errors
def confirm! def confirm!
unless_confirmed do pending_any_confirmation do
self.confirmation_token = nil self.confirmation_token = nil
self.confirmed_at = Time.now.utc self.confirmed_at = Time.now.utc
if self.class.reconfirmable if self.class.reconfirmable && unconfirmed_email.present?
@bypass_postpone = true @bypass_postpone = true
self.email = unconfirmed_email if unconfirmed_email.present? self.email = unconfirmed_email
self.unconfirmed_email = nil self.unconfirmed_email = nil
save
# We need to validate in such cases to enforce e-mail uniqueness
save(:validate => true)
else else
save(:validate => false) save(:validate => false)
end end
@ -73,7 +75,7 @@ module Devise
# Resend confirmation token. This method does not need to generate a new token. # Resend confirmation token. This method does not need to generate a new token.
def resend_confirmation_token def resend_confirmation_token
unless_confirmed { send_confirmation_instructions } pending_any_confirmation { send_confirmation_instructions }
end end
# Overwrites active_for_authentication? for confirmation # Overwrites active_for_authentication? for confirmation
@ -133,10 +135,9 @@ module Devise
confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago
end end
# Checks whether the record is confirmed or not or a new email has been added, yielding to the block # Checks whether the record requires any confirmation.
# if it's already confirmed, otherwise adds an error to email. def pending_any_confirmation
def unless_confirmed if !confirmed? || pending_reconfirmation?
unless confirmed? && !pending_reconfirmation?
yield yield
else else
self.errors.add(:email, :already_confirmed) self.errors.add(:email, :already_confirmed)

View File

@ -238,6 +238,12 @@ class ConfirmableTest < ActiveSupport::TestCase
end end
class ReconfirmableTest < ActiveSupport::TestCase class ReconfirmableTest < ActiveSupport::TestCase
test 'should not worry about validations on confirm even with reconfirmable' do
admin = create_admin
admin.reset_password_token = "a"
assert admin.confirm!
end
test 'should generate confirmation token after changing email' do test 'should generate confirmation token after changing email' do
admin = create_admin admin = create_admin
assert admin.confirm! assert admin.confirm!

View File

@ -7,6 +7,7 @@ module SharedAdmin
:unlock_strategy => :time, :lock_strategy => :none, :unlock_strategy => :time, :lock_strategy => :none,
:allow_unconfirmed_access_for => 2.weeks, :reconfirmable => true :allow_unconfirmed_access_for => 2.weeks, :reconfirmable => true
validates_length_of :reset_password_token, :minimum => 3, :allow_blank => true
validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed? validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed?
end end