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

Use persisted? instead of new_record?

In order to be more ActiveModel compliant, lets use persisted? whereever we can. Particularly for datamapper, new_record? causes api warnings. Better to stick to the ActiveModel api I think.
This commit is contained in:
Jacques Crocker 2010-03-28 13:26:07 -07:00
parent 63deb0e80a
commit 6d31e368bf
7 changed files with 17 additions and 17 deletions

View file

@ -49,7 +49,7 @@ module Devise
# Verifies whether a user is confirmed or not
def confirmed?
!new_record? && !confirmed_at.nil?
persisted? && !confirmed_at.nil?
end
# Send confirmation instructions by email
@ -138,7 +138,7 @@ module Devise
# Options must contain the user email
def send_confirmation_instructions(attributes={})
confirmable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
confirmable.resend_confirmation_token unless confirmable.new_record?
confirmable.resend_confirmation_token if confirmable.persisted?
confirmable
end
@ -148,7 +148,7 @@ module Devise
# Options must have the confirmation_token
def confirm_by_token(confirmation_token)
confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
confirmable.confirm! unless confirmable.new_record?
confirmable.confirm! if confirmable.persisted?
confirmable
end

View file

@ -119,7 +119,7 @@ module Devise
# Options must contain the user email
def send_unlock_instructions(attributes={})
lockable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
lockable.resend_unlock_token unless lockable.new_record?
lockable.resend_unlock_token if lockable.persisted?
lockable
end
@ -129,7 +129,7 @@ module Devise
# Options must have the unlock_token
def unlock_access_by_token(unlock_token)
lockable = find_or_initialize_with_error_by(:unlock_token, unlock_token)
lockable.unlock_access! unless lockable.new_record?
lockable.unlock_access! if lockable.persisted?
lockable
end

View file

@ -56,7 +56,7 @@ module Devise
# Attributes must contain the user email
def send_reset_password_instructions(attributes={})
recoverable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
recoverable.send_reset_password_instructions unless recoverable.new_record?
recoverable.send_reset_password_instructions if recoverable.persisted?
recoverable
end
@ -67,7 +67,7 @@ module Devise
# Attributes must contain reset_password_token, password and confirmation
def reset_password_by_token(attributes={})
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) unless recoverable.new_record?
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) if recoverable.persisted?
recoverable
end
end

View file

@ -42,7 +42,7 @@ module Devise
# Passwords are always required if it's a new record, or if the password
# or confirmation are being set somewhere.
def password_required?
new_record? || !password.nil? || !password_confirmation.nil?
!persisted? || !password.nil? || !password_confirmation.nil?
end
module ClassMethods

View file

@ -60,13 +60,13 @@ class ConfirmableTest < ActiveSupport::TestCase
test 'should return a new record with errors when a invalid token is given' do
confirmed_user = User.confirm_by_token('invalid_confirmation_token')
assert confirmed_user.new_record?
assert_not confirmed_user.persisted?
assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
end
test 'should return a new record with errors when a blank token is given' do
confirmed_user = User.confirm_by_token('')
assert confirmed_user.new_record?
assert_not confirmed_user.persisted?
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
end
@ -119,7 +119,7 @@ class ConfirmableTest < ActiveSupport::TestCase
test 'should return a new user if no email was found' do
confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
assert confirmation_user.new_record?
assert_not confirmation_user.persisted?
end
test 'should add error to new user email if no email was found' do

View file

@ -156,13 +156,13 @@ class LockableTest < ActiveSupport::TestCase
test 'should return a new record with errors when a invalid token is given' do
locked_user = User.unlock_access_by_token('invalid_token')
assert locked_user.new_record?
assert_not locked_user.persisted?
assert_equal "is invalid", locked_user.errors[:unlock_token].join
end
test 'should return a new record with errors when a blank token is given' do
locked_user = User.unlock_access_by_token('')
assert locked_user.new_record?
assert_not locked_user.persisted?
assert_equal "can't be blank", locked_user.errors[:unlock_token].join
end
@ -183,7 +183,7 @@ class LockableTest < ActiveSupport::TestCase
test 'should return a new user if no email was found' do
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
assert unlock_user.new_record?
assert_not unlock_user.persisted?
end
test 'should add error to new user email if no email was found' do

View file

@ -82,7 +82,7 @@ class RecoverableTest < ActiveSupport::TestCase
test 'should return a new record with errors if user was not found by e-mail' do
reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
assert reset_password_user.new_record?
assert_not reset_password_user.persisted?
assert_equal "not found", reset_password_user.errors[:email].join
end
@ -110,13 +110,13 @@ class RecoverableTest < ActiveSupport::TestCase
test 'should a new record with errors if no reset_password_token is found' do
reset_password_user = User.reset_password_by_token(:reset_password_token => 'invalid_token')
assert reset_password_user.new_record?
assert_not reset_password_user.persisted?
assert_equal "is invalid", reset_password_user.errors[:reset_password_token].join
end
test 'should a new record with errors if reset_password_token is blank' do
reset_password_user = User.reset_password_by_token(:reset_password_token => '')
assert reset_password_user.new_record?
assert_not reset_password_user.persisted?
assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
end