aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-01-20 05:04:19 -0800
committerGodfrey Chan <godfreykfc@gmail.com>2014-01-24 19:49:30 -0800
commit8ca59237dd4951efcc9861142222254a134911ca (patch)
treef075e6e2152d980572f0256f5912055851e17af0 /activemodel
parent20490adcbf00cd382e8e310415955a427b93e398 (diff)
downloadrails-8ca59237dd4951efcc9861142222254a134911ca.tar.gz
rails-8ca59237dd4951efcc9861142222254a134911ca.tar.bz2
rails-8ca59237dd4951efcc9861142222254a134911ca.zip
Got all the new tests passing
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/secure_password.rb18
1 files changed, 8 insertions, 10 deletions
diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb
index e4af1efa65..c0e60fbb8a 100644
--- a/activemodel/lib/active_model/secure_password.rb
+++ b/activemodel/lib/active_model/secure_password.rb
@@ -57,11 +57,15 @@ module ActiveModel
include InstanceMethodsOnActivation
if options.fetch(:validations, true)
- validates_confirmation_of :password, if: :password_confirmation_required?
- validates_presence_of :password, on: :create
- validates_presence_of :password_confirmation, if: :password_confirmation_required?
+ # This ensures the model has a password by checking whether the password_digest
+ # is present, so that this works with both new and existing records. However,
+ # when there is an error, the message is added to the password attribute instead
+ # so that the error message will makes sense to the end-user.
+ validate do |record|
+ record.errors.add(:password, :blank) unless record.password_digest.present?
+ end
- before_create { raise "Password digest missing on new record" if password_digest.blank? }
+ validates_confirmation_of :password, if: ->{ self.password.present? }
end
if respond_to?(:attributes_protected_by_default)
@@ -112,12 +116,6 @@ module ActiveModel
def password_confirmation=(unencrypted_password)
@password_confirmation = unencrypted_password
end
-
- private
-
- def password_confirmation_required?
- password_confirmation && password.present?
- end
end
end
end