Added skip_confirmation! It skips confirmation token generation, e-mail sending and automatically sets confirmed_at.
This commit is contained in:
parent
358a2389ce
commit
9a658d5e74
|
@ -34,8 +34,8 @@ module Devise
|
||||||
base.class_eval do
|
base.class_eval do
|
||||||
extend ClassMethods
|
extend ClassMethods
|
||||||
|
|
||||||
before_create :generate_confirmation_token
|
before_create :generate_confirmation_token, :if => :confirmation_required?
|
||||||
after_create :send_confirmation_instructions
|
after_create :send_confirmation_instructions, :if => :confirmation_required?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,8 +78,20 @@ module Devise
|
||||||
confirmed? || confirmation_period_valid?
|
confirmed? || confirmation_period_valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# If you don't want confirmation to be sent on create, neither a code
|
||||||
|
# to be generated, call skip_confirmation!
|
||||||
|
def skip_confirmation!
|
||||||
|
self.confirmed_at = Time.now
|
||||||
|
@skip_confirmation = true
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
# Callback to overwrite if confirmation is required or not.
|
||||||
|
def confirmation_required?
|
||||||
|
!@skip_confirmation
|
||||||
|
end
|
||||||
|
|
||||||
# Checks if the confirmation for the user is within the limit time.
|
# Checks if the confirmation for the user is within the limit time.
|
||||||
# We do this by calculating if the difference between today and the
|
# We do this by calculating if the difference between today and the
|
||||||
# confirmation sent date does not exceed the confirm in time configured.
|
# confirmation sent date does not exceed the confirm in time configured.
|
||||||
|
|
|
@ -20,16 +20,11 @@ module Devise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update password
|
|
||||||
def reset_password(new_password, new_password_confirmation)
|
|
||||||
self.password = new_password
|
|
||||||
self.password_confirmation = new_password_confirmation
|
|
||||||
end
|
|
||||||
|
|
||||||
# Update password saving the record and clearing token. Returns true if
|
# Update password saving the record and clearing token. Returns true if
|
||||||
# the passwords are valid and the record was saved, false otherwise.
|
# the passwords are valid and the record was saved, false otherwise.
|
||||||
def reset_password!(new_password, new_password_confirmation)
|
def reset_password!(new_password, new_password_confirmation)
|
||||||
reset_password(new_password, new_password_confirmation)
|
self.password = new_password
|
||||||
|
self.password_confirmation = new_password_confirmation
|
||||||
clear_reset_password_token if valid?
|
clear_reset_password_token if valid?
|
||||||
save
|
save
|
||||||
end
|
end
|
||||||
|
|
|
@ -108,6 +108,17 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
|
||||||
|
user = new_user
|
||||||
|
user.skip_confirmation!
|
||||||
|
|
||||||
|
assert_email_not_sent do
|
||||||
|
user.save!
|
||||||
|
assert_nil user.confirmation_token
|
||||||
|
assert_not_nil user.confirmed_at
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test 'should find a user to send confirmation instructions' do
|
test 'should find a user to send confirmation instructions' do
|
||||||
user = create_user
|
user = create_user
|
||||||
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
||||||
|
@ -125,20 +136,13 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||||
assert_equal 'not found', confirmation_user.errors[:email]
|
assert_equal 'not found', confirmation_user.errors[:email]
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'should reset confirmation token before send the confirmation instructions email' do
|
test 'should generate a confirmation token before send the confirmation instructions email' do
|
||||||
user = create_user
|
user = create_user
|
||||||
token = user.confirmation_token
|
token = user.confirmation_token
|
||||||
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
||||||
assert_not_equal token, user.reload.confirmation_token
|
assert_not_equal token, user.reload.confirmation_token
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'should reset confirmation status when sending the confirmation instructions' do
|
|
||||||
user = create_user
|
|
||||||
assert_not user.confirmed?
|
|
||||||
confirmation_user = User.send_confirmation_instructions(:email => user.email)
|
|
||||||
assert_not user.reload.confirmed?
|
|
||||||
end
|
|
||||||
|
|
||||||
test 'should send email instructions for the user confirm it\'s email' do
|
test 'should send email instructions for the user confirm it\'s email' do
|
||||||
user = create_user
|
user = create_user
|
||||||
assert_email_sent do
|
assert_email_sent do
|
||||||
|
|
|
@ -32,7 +32,7 @@ class RecoverableTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
test 'should reset password and password confirmation from params' do
|
test 'should reset password and password confirmation from params' do
|
||||||
user = create_user
|
user = create_user
|
||||||
user.reset_password('123456789', '987654321')
|
user.reset_password!('123456789', '987654321')
|
||||||
assert_equal '123456789', user.password
|
assert_equal '123456789', user.password
|
||||||
assert_equal '987654321', user.password_confirmation
|
assert_equal '987654321', user.password_confirmation
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue