Module: ColumnCryptor::ClassMethods

Defined in:
lib/column_cryptor.rb

Instance Method Summary collapse

Instance Method Details

#encrypts(*args) ⇒ Object

Define which columns to mark as being encrypted. For example, if your ActiveRecord model has two columns you’d like to encrypt named “address” and “phone_number”, you would call this as:

encrypts :address, :phone_number

This will define two methods for each attribute: a getter (i.e. “address”) and a setter (i.e. “address=”). The getter will automatically attempt to decrypt the data, while the setter will attempt to encrypt the data. When ActiveRecord writes your record to the database, the values of these columns will be the ciphertext.

Note that you must set ColumnCryptor.private_key prior to using these setters/getters.



55
56
57
58
59
60
61
62
63
# File 'lib/column_cryptor.rb', line 55

def encrypts(*args)
  if args.respond_to?(:each)
    args.each do |attribute|
      define_encryption_methods attribute
    end
  else
    define_encryption_methods args
  end
end