Class: AuditGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/omg-audit-group.rb

Defined Under Namespace

Classes: LockError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_uuid = SecureRandom.uuid, dry_run: false) { ... } ⇒ AuditGroup

Creates a new AuditGroup instance and updates ‘AuditGroup.current` to it.

Parameters:

  • request_uuid (String) (defaults to: SecureRandom.uuid)

    What all audits within the group will have assigned as their request_uuid. Useful if you want to group additional operations under a ‘request_uuid` that already exists in the DB.

  • dry_run (Boolean) (defaults to: false)

    If true, the transaction will be rolled back after the block is executed.

Yields:

  • operations whose audits should be associated with the same request_uuid.



62
63
64
65
66
67
68
69
70
# File 'lib/omg-audit-group.rb', line 62

def initialize(request_uuid = SecureRandom.uuid, dry_run: false, &block)
  @request_uuid = request_uuid
  @dry_run = dry_run
  @locked = false

  self.class.current = self

  request(&block) if block_given?
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



13
14
15
# File 'lib/omg-audit-group.rb', line 13

def current
  @current
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



53
54
55
# File 'lib/omg-audit-group.rb', line 53

def block
  @block
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



53
54
55
# File 'lib/omg-audit-group.rb', line 53

def dry_run
  @dry_run
end

#lockedObject (readonly)

Returns the value of attribute locked.



53
54
55
# File 'lib/omg-audit-group.rb', line 53

def locked
  @locked
end

#request_uuidObject (readonly)

Returns the value of attribute request_uuid.



53
54
55
# File 'lib/omg-audit-group.rb', line 53

def request_uuid
  @request_uuid
end

Class Method Details

.auditsObject



48
49
50
# File 'lib/omg-audit-group.rb', line 48

def audits
  current.audits
end

.request(dry_run: false) { ... } ⇒ Object

Creates a new AuditGroup and runs operations to be all given the same ‘request_uuid`

Yields:

  • operations whose audits should be associated with the same request_uuid.



40
41
42
# File 'lib/omg-audit-group.rb', line 40

def request(dry_run: false, &block)
  new(dry_run: dry_run).request(&block)
end

.request_uuidObject



44
45
46
# File 'lib/omg-audit-group.rb', line 44

def request_uuid
  current.request_uuid
end

.resetObject

Clears out any current ‘request_uuid` or AuditGroup request.



31
32
33
34
# File 'lib/omg-audit-group.rb', line 31

def reset
  unset_request_uuid
  @current = nil
end

.set_request_uuid(request_uuid = SecureRandom.uuid) ⇒ Object

Updates ‘audited` gem to make every operation use the same `request_uuid`.

Parameters:

  • request_uuid (String) (defaults to: SecureRandom.uuid)

    The ‘request_uuid` to use for all operations within the block.



19
20
21
# File 'lib/omg-audit-group.rb', line 19

def set_request_uuid(request_uuid = SecureRandom.uuid)
  Audited.store[:current_request_uuid] = request_uuid
end

.unset_request_uuidObject

Resets ‘audited` gem to generate a new `request_uuid` for each operation.



25
26
27
# File 'lib/omg-audit-group.rb', line 25

def unset_request_uuid
  Audited.store.delete(:current_request_uuid)
end

Instance Method Details

#auditsActiveRecord::Relation

Returns all associated audits. If ‘dry_run` is true, these will not be persistent in DB.

Returns:

  • (ActiveRecord::Relation)

    all audits associated with the request.



124
125
126
# File 'lib/omg-audit-group.rb', line 124

def audits
  Audited::Audit.where(request_uuid: request_uuid)
end

#currentObject



136
137
138
# File 'lib/omg-audit-group.rb', line 136

def current
  self.class.current
end

#dry_run?Boolean

Returns whether this request is a dry run and will therefore not persist changes.

Returns:

  • (Boolean)

    whether this request is a dry run and will therefore not persist changes.



106
107
108
# File 'lib/omg-audit-group.rb', line 106

def dry_run?
  dry_run
end

#lock!Object



116
117
118
# File 'lib/omg-audit-group.rb', line 116

def lock!
  @locked = true
end

#locked?Boolean

Returns whether this request has already been run and is therefore locked.

Returns:

  • (Boolean)

    whether this request has already been run and is therefore locked.



112
113
114
# File 'lib/omg-audit-group.rb', line 112

def locked?
  locked
end

#request { ... } ⇒ AuditGroup

Sets the ‘request_uuid` used by the `audited` store, runs the passed in block, then clears the `request_uuid`.

Subsequent `request` calls will also be given the same `request_uuid`.

Yields:

  • operations whose audits should be associated with the same request_uuid.

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/omg-audit-group.rb', line 78

def request(&block)
  raise ArgumentError, 'No block given' unless block_given?
  raise LockError if locked?

  set_request_uuid

  if dry_run?
    ActiveRecord::Base.transaction do
      block.call

      # Calls .to_a to keep records in memory after rollback
      @audits = audits.to_a

      raise ActiveRecord::Rollback
    end
  else
    block.call
  end

  lock!

  self
ensure
  unset_request_uuid
end

#set_request_uuidObject



128
129
130
# File 'lib/omg-audit-group.rb', line 128

def set_request_uuid
  self.class.set_request_uuid(request_uuid)
end

#unset_request_uuidObject



132
133
134
# File 'lib/omg-audit-group.rb', line 132

def unset_request_uuid
  self.class.unset_request_uuid
end