Module: MongoTrail::AuditTrail

Included in:
MongoTrail
Defined in:
lib/mongo_trail.rb

Overview

Document structure:

> id Primary key (can’t use _id due to limitations in upsert)

> keys Array of Strings

> data Array of Document

Instance Method Summary collapse

Instance Method Details

#export_audit_trail(id, &block) ⇒ Object

If a block is given, writes an array to the block line by line Otherwise, returns an array.



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

def export_audit_trail( id, &block )
  if ! block then
    return [].tap { |result| export_audit_trail( id ) { |row| result << row } }
  end

  bkey = { id: id }
  doc  = audit_trail_configuration.find( bkey ).to_a.first
  if doc then
    keys = doc["keys"].sort
    data = doc["data"]
    data.each { |row| block.call( row ) }
  end
end

#record_audit_trail(id, data = {}) ⇒ Object



21
22
23
24
25
# File 'lib/mongo_trail.rb', line 21

def record_audit_trail( id, data = {} )
  bkey = { id: id }
  keys = data.keys.sort
  audit_trail_configuration.update( bkey, { "$set" => bkey, "$addToSet" => { keys: { "$each" => keys } }, "$push" => { data: data } }, upsert: true )
end