Module: Nygma::Encryptable::ClassMethods

Defined in:
lib/nygma/encryptable.rb

Instance Method Summary collapse

Instance Method Details

#encrypt(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nygma/encryptable.rb', line 25

def encrypt(*args)
  args.each do |attribute|
    unless has_column?(attribute)
      Rails.logger.tagged('Nygma::Encryptable::encrypt') {
        Rails.logger.info "Unable to encrypt missing attribute #{attribute} in #{self.name}, check your migrations!"
      }
    else
      attr = "#{attribute}"

      define_method("#{attr}") do
        encryptor.decrypt(self[attribute]) if self[attribute]
      end

      define_method("#{attr}=") do |val|
        encrypted_payload = encryptor.encrypt(val)
        self[attribute] = encrypted_payload if encrypted_payload
      end
    end
  end
end