Skip to content

Instantly share code, notes, and snippets.

@kaspergrubbe
Created July 24, 2015 10:06
Show Gist options
  • Save kaspergrubbe/43bf53850df44db41717 to your computer and use it in GitHub Desktop.
Save kaspergrubbe/43bf53850df44db41717 to your computer and use it in GitHub Desktop.
class PasswordChange
include ActiveModel::Model
attr_accessor(
:user,
:current_password,
:new_password,
:new_password_confirmation,
)
validates_presence_of :current_password, :new_password, :new_password_confirmation
validate :current_password_should_be_correct
validate :new_password_and_confirmation_should_match
def save
if valid?
user.password = new_password
user.save!
end
end
private
def current_password_should_be_correct
errors.add(:current_password, "is invalid") unless user.authenticate?(current_password)
end
def new_password_and_confirmation_should_match
errors.add(:base, "New password and new password confirmation does not match") if new_password != new_password_confirmation
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment