Skip to content

Instantly share code, notes, and snippets.

@joshk
Created February 22, 2010 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshk/311119 to your computer and use it in GitHub Desktop.
Save joshk/311119 to your computer and use it in GitHub Desktop.
def update_with_password(params={})
current_password = params.delete(:current_password)
params.delete(:password) if params[:password].blank?
params.delete(:password_confirmation) if params[:password_confirmation].blank?
skip_password_check = !(current_password.present? || params[:password] || params[:password_confirmation])
result = if skip_password_check || 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 unless result
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment