Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## my validation code
- def validate_on_update
- puts("#{self.password} : #{User.obscure(old_password)}")
- if new_password and not new_password.empty? and User.obscure(old_password) != self.password
- puts 'adding error'
- errors.add(:password, "does not match old password")
- end
- end
- def after_validation_on_update
- self.password = User.obscure(self.new_password) if self.new_password and not self.new_password.empty?
- puts 'in after validation'
- end
- ## my test
- it 'should not fail if the given old password is correct' do
- puts "starting update"
- User.update(1, {:new_password => 'frank',
- :new_password_confirmation => 'frank',
- :old_password => 'pass'}).should have(0).errors_on(:password)
- end
- ## output from the test
- "starting update"
- "9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684 : 9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684"
- "in after validation"
- "86a8c2da8527a1c6978bdca6d7986fe14ae147fe : 9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684"
- "adding an error"
- "in after validation"
- ## in console
- >> User.update(2, {:new_password => 'frank', :new_password_confirmation => 'frank', :old_password => 'test'})
- a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 : a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
- in after validation
- => #<User:0xb72da854 @new_password_confirmation="frank", @new_record_before_save=nil, @errors=#<ActiveRecord::Errors:0xb72d9d00 @errors={}, @base=#<Rider:0xb72da854 ...>>, @old_password="test", @new_password="frank", @attributes={"name"=>"evan", "id"=>"2", "password"=>"86a8c2da8527a1c6978bdca6d7986fe14ae147fe", "email"=>"evan"}>
Add Comment
Please, Sign In to add comment