Change behavior to skip reconfirmation after creating a record with #save called in callback (#4125)

https://github.com/plataformatec/devise/pull/4125
This commit is contained in:
Arne Zeising 2016-06-13 22:57:24 +02:00 committed by Lucas Mazza
parent 4015488b90
commit ac702843dd
2 changed files with 21 additions and 2 deletions

View File

@ -43,7 +43,7 @@ module Devise
included do
before_create :generate_confirmation_token, if: :confirmation_required?
after_create :skip_reconfirmation!, if: :send_confirmation_notification?
after_create :skip_reconfirmation_in_callback!, if: :send_confirmation_notification?
if respond_to?(:after_commit) # ActiveRecord
after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification?
after_commit :send_reconfirmation_instructions, on: :update, if: :reconfirmation_required?
@ -56,6 +56,7 @@ module Devise
def initialize(*args, &block)
@bypass_confirmation_postpone = false
@skip_reconfirmation_in_callback = false
@reconfirmation_required = false
@skip_confirmation_notification = false
@raw_confirmation_token = nil
@ -165,6 +166,12 @@ module Devise
protected
# To not require reconfirmation after creating with #save called in a
# callback call skip_create_confirmation!
def skip_reconfirmation_in_callback!
@skip_reconfirmation_in_callback = true
end
# A callback method used to deliver confirmation
# instructions on creation. This can be overridden
# in models to map to a nice sign up e-mail.
@ -253,7 +260,11 @@ module Devise
end
def postpone_email_change?
postpone = self.class.reconfirmable && email_changed? && !@bypass_confirmation_postpone && self.email.present?
postpone = self.class.reconfirmable &&
email_changed? &&
!@bypass_confirmation_postpone &&
self.email.present? &&
(!@skip_reconfirmation_in_callback || !self.email_was.nil?)
@bypass_confirmation_postpone = false
postpone
end

View File

@ -508,4 +508,12 @@ class ReconfirmableTest < ActiveSupport::TestCase
admin = Admin::WithSaveInCallback.create(valid_attributes.except(:username))
assert !admin.pending_reconfirmation?
end
test 'should require reconfirmation after creating a record and updating the email' do
admin = create_admin
assert !admin.instance_variable_get(:@bypass_confirmation_postpone)
admin.email = "new_test@email.com"
admin.save
assert admin.pending_reconfirmation?
end
end