Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
type cast values as strings before encrypting, closes attr-encrypted#16
Browse files Browse the repository at this point in the history
  • Loading branch information
shuber committed Mar 12, 2011
1 parent 34b56c2 commit d325268
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/attr_encrypted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def decrypt(attribute, encrypted_value, options = {})
def encrypt(attribute, value, options = {})
options = encrypted_attributes[attribute.to_sym].merge(options)
if options[:if] && !options[:unless] && !value.nil? && !(value.is_a?(String) && value.empty?)
value = options[:marshaler].send(options[:dump_method], value) if options[:marshal]
value = options[:marshal] ? options[:marshaler].send(options[:dump_method], value) : value.to_s
encrypted_value = options[:encryptor].send(options[:encrypt_method], options.merge!(:value => value))
encrypted_value = [encrypted_value].pack(options[:encode]) if options[:encode]
encrypted_value
Expand Down
6 changes: 6 additions & 0 deletions test/attr_encrypted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,10 @@ def test_should_always_reset_options
assert_equal "not_encrypted_stuff", @user.encrypted_with_if_changed
end

def test_should_cast_values_as_strings_before_encrypting
string_encrypted_email = User.encrypt_email('3')
assert_equal string_encrypted_email, User.encrypt_email(3)
assert_equal '3', User.decrypt_email(string_encrypted_email)
end

end

0 comments on commit d325268

Please sign in to comment.