Created
July 24, 2015 10:06
-
-
Save kaspergrubbe/43bf53850df44db41717 to your computer and use it in GitHub Desktop.
This file contains 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
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