From 6d31e368bfb0eb00f40180968ae9f4e2ec4e71c7 Mon Sep 17 00:00:00 2001 From: Jacques Crocker Date: Sun, 28 Mar 2010 13:26:07 -0700 Subject: [PATCH] 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. --- lib/devise/models/confirmable.rb | 6 +++--- lib/devise/models/lockable.rb | 4 ++-- lib/devise/models/recoverable.rb | 4 ++-- lib/devise/models/validatable.rb | 2 +- test/models/confirmable_test.rb | 6 +++--- test/models/lockable_test.rb | 6 +++--- test/models/recoverable_test.rb | 6 +++--- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index ffa2e0c5..51a02704 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -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 diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index 6d000716..f51eb8c7 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -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 diff --git a/lib/devise/models/recoverable.rb b/lib/devise/models/recoverable.rb index fd80c665..bc66971a 100644 --- a/lib/devise/models/recoverable.rb +++ b/lib/devise/models/recoverable.rb @@ -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 diff --git a/lib/devise/models/validatable.rb b/lib/devise/models/validatable.rb index c92aa903..01020128 100644 --- a/lib/devise/models/validatable.rb +++ b/lib/devise/models/validatable.rb @@ -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 diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index 5466e64f..d0409cef 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -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 diff --git a/test/models/lockable_test.rb b/test/models/lockable_test.rb index a86d5fab..bc219ff4 100644 --- a/test/models/lockable_test.rb +++ b/test/models/lockable_test.rb @@ -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 diff --git a/test/models/recoverable_test.rb b/test/models/recoverable_test.rb index ec0c5d69..4cfd7b9c 100644 --- a/test/models/recoverable_test.rb +++ b/test/models/recoverable_test.rb @@ -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