Class: ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/application_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descendants_using_encryptionObject



15
16
17
18
19
20
21
22
23
# File 'app/models/application_record.rb', line 15

def self.descendants_using_encryption
  Rails.application.eager_load!
  ApplicationRecord.descendants.select do |model|
    model = model.name.constantize
    model.descendants.empty? &&
      model.respond_to?(:lockbox_attributes) &&
      model.lockbox_attributes.any?
  end
end

.lockbox_optionsObject



6
7
8
9
10
11
12
13
# File 'app/models/application_record.rb', line 6

def self.lockbox_options
  {
    previous_versions: [
      { padding: false },
      { padding: false, master_key: Settings.lockbox.master_key }
    ]
  }
end

Instance Method Details

#timestamp_attributes_for_update_in_modelObject



25
26
27
28
29
30
31
# File 'app/models/application_record.rb', line 25

def timestamp_attributes_for_update_in_model
  kms_key_changed = changed? && changed.include?('encrypted_kms_key')
  called_from_kms_encrypted = caller_locations(1, 1)[0].label == 'encrypt_kms_keys'

  # If update is due to kms key, don't update updated_at
  kms_key_changed || called_from_kms_encrypted ? [] : super
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'app/models/application_record.rb', line 33

def valid?(context = nil)
  kms_key_changed = changed.include?('encrypted_kms_key')
  only_kms_changes = changed.all? { |f| f == 'encrypted_kms_key' || f.include?('_ciphertext') }

  # Skip validation ONLY during the case where only KMS fields are being updated
  # AND the encrypted_kms_key is present
  kms_key_changed && only_kms_changes ? true : super
end