Module: ActiveCapture

Defined in:
lib/active_capture.rb

Class Method Summary collapse

Class Method Details

.flush(directory) ⇒ Object

Flushes all captured data of a specified directory



43
44
45
# File 'lib/active_capture.rb', line 43

def self.flush(directory)
  Support::CaptureStorage.flush(directory)
end

.restore(record, capture_file, merge: false) ⇒ Object

Restores a record and its associations from a capture file



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_capture.rb', line 27

def self.restore(record, capture_file, merge: false)
  captured_records = Support::CaptureStorage.load_capture(capture_file)
  validate_capture(record, captured_records)

  ActiveRecord::Base.transaction do
    begin
      # Update the record's attributes and restore its associations
      record.update!(captured_records['attributes'])
      restore_associations(record, captured_records['associations'], merge: merge)
    rescue StandardError => e
      raise "Failed to restore record: #{e.message}"
    end
  end
end

.take(record, associations: [], name: nil) ⇒ Object

Captures the state of a record and its associations



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_capture.rb', line 8

def self.take(record, associations: [], name: nil)
  validate_record(record)

  capture_data = {
    model: record.class.name,
    record_id: record.id,
    attributes: record.attributes,
    associations: capture_associations(record, associations)
  }

  begin
    # Save the captured data in json file
    Support::CaptureStorage.save_capture(record, capture_data, name)
  rescue StandardError => e
    raise "Failed to save capture: #{e.message}"
  end
end