module ActiveRecord::Encryption::Contexts

def context

Returns the current context. By default it will return the current context.
def context
  self.current_custom_context || self.default_context
end

def current_custom_context

def current_custom_context
  self.custom_contexts&.last
end

def protecting_encrypted_data(&block)

* Writing encrypted content will fail.
* Reading encrypted content will return its ciphertext.

Runs the provided block in an encryption context where:
def protecting_encrypted_data(&block)
  with_encryption_context encryptor: ActiveRecord::Encryption::EncryptingOnlyEncryptor.new, frozen_encryption: true, &block
end

def with_encryption_context(properties)

Encryption contexts can be nested.

end
...
ActiveRecord::Encryption.with_encryption_context(encryptor: ActiveRecord::Encryption::NullEncryptor.new) do

Example:

It supports overriding all the properties defined in +Context+.

Configures a custom encryption context to use when running the provided block of code.
def with_encryption_context(properties)
  self.custom_contexts ||= []
  self.custom_contexts << default_context.dup
  properties.each do |key, value|
    self.current_custom_context.send("#{key}=", value)
  end
  yield
ensure
  self.custom_contexts.pop
end

def without_encryption(&block)

* Writing encrypted content will write its clear text.
* Reading encrypted content will return its ciphertexts.

Runs the provided block in an encryption context where encryption is disabled:
def without_encryption(&block)
  with_encryption_context encryptor: ActiveRecord::Encryption::NullEncryptor.new, &block
end