diff --git a/lib/devise/models/authenticatable.rb b/lib/devise/models/authenticatable.rb index 9221a4a9..a0c676a0 100644 --- a/lib/devise/models/authenticatable.rb +++ b/lib/devise/models/authenticatable.rb @@ -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 diff --git a/test/models/lockable_test.rb b/test/models/lockable_test.rb index 6f09c41e..ff34817f 100644 --- a/test/models/lockable_test.rb +++ b/test/models/lockable_test.rb @@ -175,9 +175,9 @@ 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 swap Devise, :authentication_keys => [:username, :email] do user = create_user @@ -185,7 +185,7 @@ class LockableTest < ActiveSupport::TestCase assert_equal unlock_user, user end end - + test 'should require all authentication_keys' do swap Devise, :authentication_keys => [:username, :email] do user = create_user @@ -201,5 +201,4 @@ class LockableTest < ActiveSupport::TestCase assert_not user.access_locked? assert_equal 'was not locked', user.errors[:email].join end - end diff --git a/test/models/recoverable_test.rb b/test/models/recoverable_test.rb index 69c29eb3..564e392d 100644 --- a/test/models/recoverable_test.rb +++ b/test/models/recoverable_test.rb @@ -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