Created
December 26, 2011 16:12
-
-
Save jbrowning/1521498 to your computer and use it in GitHub Desktop.
A Devise user#update_with_password which only requires the current password if changing the password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_with_password(params={}) | |
result = nil | |
current_password = params.delete(:current_password) | |
if params[:password].blank? | |
params.delete(:password) | |
params.delete(:password_confirmation) if params[:password_confirmation].blank? | |
result = self.update_without_password(params) | |
else | |
result = if valid_password?(current_password) | |
update_attributes(params) | |
else | |
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid) | |
self.attributes = params | |
false | |
end | |
clean_up_passwords | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment