Class: MassEncryption::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/mass_encryption/batch.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass:, from_id:, size: DEFAULT_BATCH_SIZE, track: 0, tracks_count: 1) ⇒ Batch

Returns a new instance of Batch.



8
9
10
11
12
13
14
# File 'lib/mass_encryption/batch.rb', line 8

def initialize(klass:, from_id:, size: DEFAULT_BATCH_SIZE, track: 0, tracks_count: 1)
  @class_name = klass.name # not storing class as instance variable as it causes stack overflow error with json serialization
  @from_id = from_id
  @size = size
  @track = track
  @tracks_count = tracks_count
end

Instance Attribute Details

#from_idObject (readonly)

Returns the value of attribute from_id.



2
3
4
# File 'lib/mass_encryption/batch.rb', line 2

def from_id
  @from_id
end

#sizeObject (readonly)

Returns the value of attribute size.



2
3
4
# File 'lib/mass_encryption/batch.rb', line 2

def size
  @size
end

#trackObject (readonly)

Returns the value of attribute track.



2
3
4
# File 'lib/mass_encryption/batch.rb', line 2

def track
  @track
end

#tracks_countObject (readonly)

Returns the value of attribute tracks_count.



2
3
4
# File 'lib/mass_encryption/batch.rb', line 2

def tracks_count
  @tracks_count
end

Instance Method Details

#encrypt_later(auto_enqueue_next: false) ⇒ Object



31
32
33
# File 'lib/mass_encryption/batch.rb', line 31

def encrypt_later(auto_enqueue_next: false)
  MassEncryption::BatchEncryptionJob.perform_later(self, auto_enqueue_next: auto_enqueue_next)
end

#encrypt_nowObject



20
21
22
23
24
25
# File 'lib/mass_encryption/batch.rb', line 20

def encrypt_now
  if klass.encrypted_attributes.present? && present?
    validate_encrypting_is_allowed
    encrypt_records
  end
end

#klassObject



16
17
18
# File 'lib/mass_encryption/batch.rb', line 16

def klass
  @class_name.constantize
end

#nextObject



41
42
43
# File 'lib/mass_encryption/batch.rb', line 41

def next
  self.class.new(klass: klass, from_id: next_track_records.last.id + 1, size: size, track: track, tracks_count: tracks_count)
end

#present?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/mass_encryption/batch.rb', line 35

def present?
  # we deliberately load the association to avoid 2 queries when checking if there are records
  # before encrypting in +MassEncryption::BatchEncryptionJob+
  records.present?
end

#recordsObject



45
46
47
# File 'lib/mass_encryption/batch.rb', line 45

def records
  @records ||= klass.where("id >= ?", determine_from_id).order(id: :asc).limit(size)
end

#to_sObject



49
50
51
# File 'lib/mass_encryption/batch.rb', line 49

def to_s
  "<#{klass}> from: #{from_id} size: #{size} (track=#{track}, tracks_count=#{tracks_count}) | #{records.first.id} - #{records.last.id}"
end

#validate_encrypting_is_allowedObject

Raises:

  • (ActiveRecord::Encryption::Errors::Configuration)


27
28
29
# File 'lib/mass_encryption/batch.rb', line 27

def validate_encrypting_is_allowed
  raise ActiveRecord::Encryption::Errors::Configuration, "can't mass encrypt while in protected mode" if ActiveRecord::Encryption.context.frozen_encryption?
end