Class: AuditGroup
- Inherits:
-
Object
- Object
- AuditGroup
- Defined in:
- lib/omg-audit-group.rb
Defined Under Namespace
Classes: LockError
Class Attribute Summary collapse
-
.current ⇒ Object
Returns the value of attribute current.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#dry_run ⇒ Object
readonly
Returns the value of attribute dry_run.
-
#locked ⇒ Object
readonly
Returns the value of attribute locked.
-
#request_uuid ⇒ Object
readonly
Returns the value of attribute request_uuid.
Class Method Summary collapse
- .audits ⇒ Object
-
.request(dry_run: false) { ... } ⇒ Object
Creates a new AuditGroup and runs operations to be all given the same ‘request_uuid`.
- .request_uuid ⇒ Object
-
.reset ⇒ Object
Clears out any current ‘request_uuid` or AuditGroup request.
-
.set_request_uuid(request_uuid = SecureRandom.uuid) ⇒ Object
Updates ‘audited` gem to make every operation use the same `request_uuid`.
-
.unset_request_uuid ⇒ Object
Resets ‘audited` gem to generate a new `request_uuid` for each operation.
Instance Method Summary collapse
-
#audits ⇒ ActiveRecord::Relation
Returns all associated audits.
- #current ⇒ Object
-
#dry_run? ⇒ Boolean
Whether this request is a dry run and will therefore not persist changes.
-
#initialize(request_uuid = SecureRandom.uuid, dry_run: false) { ... } ⇒ AuditGroup
constructor
Creates a new AuditGroup instance and updates ‘AuditGroup.current` to it.
- #lock! ⇒ Object
-
#locked? ⇒ Boolean
Whether this request has already been run and is therefore locked.
-
#request { ... } ⇒ AuditGroup
Sets the ‘request_uuid` used by the `audited` store, runs the passed in block, then clears the `request_uuid`.
- #set_request_uuid ⇒ Object
- #unset_request_uuid ⇒ Object
Constructor Details
#initialize(request_uuid = SecureRandom.uuid, dry_run: false) { ... } ⇒ AuditGroup
Creates a new AuditGroup instance and updates ‘AuditGroup.current` to it.
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
.current ⇒ Object
Returns the value of attribute current.
13 14 15 |
# File 'lib/omg-audit-group.rb', line 13 def current @current end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
53 54 55 |
# File 'lib/omg-audit-group.rb', line 53 def block @block end |
#dry_run ⇒ Object (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 |
#locked ⇒ Object (readonly)
Returns the value of attribute locked.
53 54 55 |
# File 'lib/omg-audit-group.rb', line 53 def locked @locked end |
#request_uuid ⇒ Object (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
.audits ⇒ Object
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`
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_uuid ⇒ Object
44 45 46 |
# File 'lib/omg-audit-group.rb', line 44 def request_uuid current.request_uuid end |
.reset ⇒ Object
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`.
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_uuid ⇒ Object
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
#audits ⇒ ActiveRecord::Relation
Returns all associated audits. If ‘dry_run` is true, these will not be persistent in DB.
124 125 126 |
# File 'lib/omg-audit-group.rb', line 124 def audits Audited::Audit.where(request_uuid: request_uuid) end |
#current ⇒ Object
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.
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.
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`.
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_uuid ⇒ Object
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_uuid ⇒ Object
132 133 134 |
# File 'lib/omg-audit-group.rb', line 132 def unset_request_uuid self.class.unset_request_uuid end |