Skip to content

Instantly share code, notes, and snippets.

@nowk
Last active May 2, 2021 09:32
Show Gist options
  • Save nowk/5028376 to your computer and use it in GitHub Desktop.
Save nowk/5028376 to your computer and use it in GitHub Desktop.
Devise #update_with_password
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
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