Module: Provenance::Trackers::BulkOperations

Defined in:
lib/provenance/trackers/bulk_operations.rb

Overview

Intercepts bulk operations (‘update_all`/`delete_all`) which bypass ActiveRecord callbacks and therefore never reach the regular Trackable path. Captures the ids of the affected rows before execution and records the change in the journal.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.track(relation, action, updates) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/provenance/trackers/bulk_operations.rb', line 21

def track(relation, action, updates)
  journal = Provenance::Context.journal
  klass = relation.klass

  return yield unless trackable?(journal, klass)

  ids, truncated = safe_collect_ids(relation, klass)
  transaction_id = safe_transaction_id(klass)

  result = yield

  begin
    journal.add_bulk_change(klass, action, ids, updates,
      truncated: truncated, transaction_id: transaction_id)
  rescue StandardError
    # Auditing must never break the underlying operation.
  end

  result
end

Instance Method Details

#delete_all(*args) ⇒ Object



16
17
18
# File 'lib/provenance/trackers/bulk_operations.rb', line 16

def delete_all(*args)
  Provenance::Trackers::BulkOperations.track(self, :bulk_delete, nil) { super }
end

#update_all(*args) ⇒ Object



12
13
14
# File 'lib/provenance/trackers/bulk_operations.rb', line 12

def update_all(*args)
  Provenance::Trackers::BulkOperations.track(self, :bulk_update, args.first) { super }
end