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

Refactor code related with authentication keys on password recovery and account unlocking, closes #396.

This commit is contained in:
José Valim 2010-09-21 12:05:17 +02:00
parent 850afec96e
commit 5429f940e7
3 changed files with 9 additions and 28 deletions

View file

@ -102,24 +102,10 @@ module Devise
# Find an initialize a record setting an error if it can't be found.
def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
if value.present?
conditions = { attribute => value }
record = find(:first, :conditions => conditions)
end
unless record
record = new
if value.present?
record.send(:"#{attribute}=", value)
else
error = :blank
end
record.errors.add(attribute, error)
end
record
find_or_initialize_with_errors([attribute], { attribute => value }, error)
end
# Find an initialize a group of attributes based on a list of required attributes.
def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
attributes = attributes.slice(*required_attributes)
attributes.delete_if { |key, value| value.blank? }
@ -132,12 +118,8 @@ module Devise
record = new
record.send(:attributes=, attributes, false)
if attributes.size == required_attributes.size
record.errors.add(:base, error)
else
required_attributes.reject { |k| attributes[k].present? }.each do |attribute|
record.errors.add(attribute, :blank)
end
required_attributes.each do |key|
record.errors.add(key, attributes[key].present? ? error : :blank)
end
end

View file

@ -175,7 +175,7 @@ class LockableTest < ActiveSupport::TestCase
test 'should add error to new user email if no email was found' do
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
assert_equal 'not found', unlock_user.errors[:base].join
assert_equal 'not found', unlock_user.errors[:email].join
end
test 'should find a user to send unlock instructions by authentication_keys' do
@ -201,5 +201,4 @@ class LockableTest < ActiveSupport::TestCase
assert_not user.access_locked?
assert_equal 'was not locked', user.errors[:email].join
end
end

View file

@ -83,7 +83,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_not reset_password_user.persisted?
assert_equal "not found", reset_password_user.errors[:base].join
assert_equal "not found", reset_password_user.errors[:email].join
end
test 'should find a user to send instructions by authentication_keys' do