shuber/attr_encrypted — pull request #36 by saepia (2012-04-26 12:26:59+00:00)
Title: Fix compatibility with ruby 1.9 string encoding issues

In some cases, under ruby 1.9 decryptor engine return value encoded as US-ASCII even if they contain UTF-8 data. It causes problems because string would be interpret as "\xC5\x81ukasz" not "Łukasz". Rails would then prevent displaying that value and will throw an error in the view.

This patch adds new parameter :charset that allows the user to force output encoding on selected attributes. It accepts anything accepted by String#force_encoding and special value of :default that just maps to the existing default encoding used by ruby. In Rails it is set in environment.rb as Encoding.default_internal but it is much cleaner and portable to use value like :default.

Sample usage:

attr_encrypted :myattr, (...), :charset => :default
attr_encrypted :myattr, (...), :charset => "utf-8"