I have a few lines in my User model to strip leading/tailing white-space from the User's email and password. The original code looked like this:
before_save {self.email = email.strip}
before_save {self.password = password.strip}
before_save {self.password_confirmation = password_confirmation.strip}
This passed my test:
test "password entry should ignore leading/tailing whitespace" do
@user = User.create(name: "M", email: " [email protected]",
password: " password", password_confirmation: " password")
assert @user.authenticate("password")
assert_not @user.authenticate(" password")
end
Now I tried to re-factor it:
before_save {email.strip!}
before_save {password.strip!}
before_save {password_confirmation.strip!}
This works fine for my email test, but it's broken the password test above. So, the question is, how is the original version actually different from the re-factored code?
You cannot use password.strip!
because in fact there's no such field as password
- it is a setter password=
that generates and stores password hash.