Last active
May 2, 2021 09:32
-
-
Save nowk/5028376 to your computer and use it in GitHub Desktop.
Devise #update_with_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
class User < ActiveRecord::Base | |
def update_with_password(params, *options) | |
_current_password = params[:current_password] | |
_password = params[:password] | |
_password_confirmation = params[:password_confirmation] | |
unless _current_password.blank? | |
if _password.blank? | |
errors.add(:password, "can't be blank") | |
false | |
else | |
super | |
end | |
else | |
super | |
end | |
end | |
end | |
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
describe User, "updating password" do | |
it "requires the current password" do | |
user = FactoryGirl.create(:user) | |
user.update_with_password(:password => 'newpassword', | |
:password_confirmation => 'newpassword').should be_false | |
user.errors[:current_password].should include("can't be blank") | |
end | |
it "requires current_password to match the current password" do | |
user = FactoryGirl.create(:user) | |
user.update_with_password(:current_password => 'differentpassword', :password => 'foo', | |
:password_confirmation => 'foo').should be_false | |
user.errors[:current_password].should include("is invalid") | |
end | |
it "requires password and password_confirmation to be present" do | |
user = FactoryGirl.create(:user) | |
user.error_on_blank_password = true | |
user.update_with_password(:current_password => 'astrongpassword').should be_false | |
user.errors[:password].should include("can't be blank") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment