Skip to content

Instantly share code, notes, and snippets.

@jbrowning
Created December 26, 2011 16:12
Show Gist options
  • Save jbrowning/1521498 to your computer and use it in GitHub Desktop.
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
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