123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::Encryption::EncryptableRecord

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
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.

Constant Summary

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

Instance Method Summary

DSL Calls

included

[ GitHub ]


10
11
12
13
14
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 10

included do
  class_attribute :encrypted_attributes

  validate :cant_modify_encrypted_attributes_when_frozen, if: -> { has_encrypted_attributes? && ActiveRecord::Encryption.context.frozen_encryption? }
end

Instance Method Details

#ciphertext_for(attribute_name)

Returns the ciphertext for attribute_name.

[ GitHub ]

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

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

#decrypt

Decrypts all the encryptable attributes and saves the changes.

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 171

def decrypt
  decrypt_attributes if has_encrypted_attributes?
end

#encrypt

Encrypts all the encryptable attributes and saves the changes.

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 166

def encrypt
  encrypt_attributes if has_encrypted_attributes?
end

#encrypted_attribute?(attribute_name) ⇒ Boolean

Returns whether a given attribute is encrypted or not.

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/encryptable_record.rb', line 146

def encrypted_attribute?(attribute_name)
  name = attribute_name.to_s
  name = self.class.attribute_aliases[name] || name

  return false unless self.class.encrypted_attributes&.include? name.to_sym

  type = type_for_attribute(name)
  type.encrypted? read_attribute_before_type_cast(name)
end