Module: ActiveRecord::Encryption::EncryptableRecord

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
activerecord/lib/active_record/encryption/encryptable_record.rb

Overview

This is the concern mixed in Active Record models to make them encryptable. It adds the encrypts attribute declaration, as well as the API to encrypt and decrypt records.

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included, prepend_features, prepended

Instance Method Details

#ciphertext_for(attribute_name) ⇒ Object

Returns the ciphertext for attribute_name.



147
148
149
150
151
152
153
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 147

def ciphertext_for(attribute_name)
  if encrypted_attribute?(attribute_name)
    read_attribute_before_type_cast(attribute_name)
  else
    read_attribute_for_database(attribute_name)
  end
end

#decryptObject

Decrypts all the encryptable attributes and saves the changes.



161
162
163
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 161

def decrypt
  decrypt_attributes if has_encrypted_attributes?
end

#encryptObject

Encrypts all the encryptable attributes and saves the changes.



156
157
158
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 156

def encrypt
  encrypt_attributes if has_encrypted_attributes?
end

#encrypted_attribute?(attribute_name) ⇒ Boolean

Returns whether a given attribute is encrypted or not.

Returns:

  • (Boolean)


142
143
144
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 142

def encrypted_attribute?(attribute_name)
  ActiveRecord::Encryption.encryptor.encrypted? read_attribute_before_type_cast(attribute_name)
end