omg-audit-group 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/audit_group.rb +95 -14
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2103693c9d639bfce07ecae254b8df32a5571ed8084a141aa3b7d072ea62632
|
4
|
+
data.tar.gz: 8b49dfc610cacdf906fa910414064ad4cec032a14809512111e22c3f290c5e38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11d2bd4799f4680ca1b20487e64cd90292a2993c3741bceaa4301fc2adc273de765a346360fbc35268f078b18a334256571f59e7898b227918c4ef743a2aff57
|
7
|
+
data.tar.gz: 910a307b97833b1542ec839bb9b20c5cbc9732bbeb316accd78140a8e7d102894240ddf2e3227cbc2ee8e79172514eecf07048863dbd579cec120ee02deeb22c
|
data/lib/audit_group.rb
CHANGED
@@ -1,60 +1,141 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'active_support/all'
|
4
3
|
require 'audited'
|
5
4
|
|
6
5
|
class AuditGroup
|
7
|
-
VERSION = '0.1.
|
6
|
+
VERSION = '0.1.4'
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
class LockError < StandardError
|
9
|
+
def initialize
|
10
|
+
super('Request is locked and cannot be run again. If you want to add operations to an existing request_uuid, create a new instance')
|
11
|
+
end
|
12
|
+
end
|
12
13
|
|
13
14
|
class << self
|
14
15
|
attr_accessor :current
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
##
|
18
|
+
# Updates `audited` gem to make every operation use the same `request_uuid`.
|
19
|
+
#
|
20
|
+
# @param request_uuid [String] The `request_uuid` to use for all operations within the block.
|
18
21
|
def set_request_uuid(request_uuid = SecureRandom.uuid)
|
19
22
|
Audited.store[:current_request_uuid] = request_uuid
|
20
23
|
end
|
21
24
|
|
25
|
+
##
|
26
|
+
# Resets `audited` gem to generate a new `request_uuid` for each operation.
|
22
27
|
def unset_request_uuid
|
23
28
|
Audited.store.delete(:current_request_uuid)
|
24
29
|
end
|
25
30
|
|
31
|
+
##
|
32
|
+
# Clears out any current `request_uuid` or AuditGroup request.
|
26
33
|
def reset
|
27
34
|
unset_request_uuid
|
28
35
|
@current = nil
|
29
36
|
end
|
30
37
|
|
31
|
-
|
32
|
-
|
38
|
+
##
|
39
|
+
# Creates a new AuditGroup and runs operations to be all given the same `request_uuid`
|
40
|
+
#
|
41
|
+
# @yield operations whose audits should be associated with the same request_uuid.
|
42
|
+
def request(dry_run: false, &block)
|
43
|
+
new(dry_run: dry_run).request(&block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_uuid
|
47
|
+
current.request_uuid
|
48
|
+
end
|
33
49
|
|
34
|
-
|
50
|
+
def audits
|
51
|
+
current.audits
|
35
52
|
end
|
36
53
|
end
|
37
54
|
|
38
|
-
|
55
|
+
attr_reader :block, :request_uuid, :dry_run, :locked
|
56
|
+
|
57
|
+
##
|
58
|
+
# Creates a new AuditGroup instance and updates `AuditGroup.current` to it.
|
59
|
+
#
|
60
|
+
# @param request_uuid [String] What all audits within the group will have assigned as their request_uuid.
|
61
|
+
# Useful if you want to group additional operations under a `request_uuid` that already exists in the DB.
|
62
|
+
# @param dry_run [Boolean] If true, the transaction will be rolled back after the block is executed.
|
63
|
+
# @yield operations whose audits should be associated with the same request_uuid.
|
64
|
+
def initialize(request_uuid = SecureRandom.uuid, dry_run: false, &block)
|
39
65
|
@request_uuid = request_uuid
|
66
|
+
@dry_run = dry_run
|
67
|
+
@locked = false
|
68
|
+
|
40
69
|
self.class.current = self
|
70
|
+
|
71
|
+
request(&block) if block_given?
|
41
72
|
end
|
42
73
|
|
74
|
+
##
|
75
|
+
# Sets the `request_uuid` used by the `audited` store, runs the passed in block, then clears the `request_uuid`.
|
76
|
+
# Subsequent `request` calls will also be given the same `request_uuid`.
|
77
|
+
#
|
78
|
+
# @yield operations whose audits should be associated with the same request_uuid.
|
79
|
+
# @return [AuditGroup] itself
|
43
80
|
def request(&block)
|
81
|
+
raise ArgumentError, 'No block given' unless block_given?
|
82
|
+
raise LockError if locked?
|
83
|
+
|
44
84
|
set_request_uuid
|
45
85
|
|
46
|
-
|
86
|
+
if dry_run?
|
87
|
+
ActiveRecord::Base.transaction do
|
88
|
+
block.call
|
89
|
+
|
90
|
+
# Calls .to_a to keep records in memory after rollback
|
91
|
+
@audits = audits.to_a
|
92
|
+
|
93
|
+
raise ActiveRecord::Rollback
|
94
|
+
end
|
95
|
+
else
|
96
|
+
block.call
|
97
|
+
end
|
98
|
+
|
99
|
+
lock!
|
47
100
|
|
48
101
|
self
|
49
102
|
ensure
|
50
103
|
unset_request_uuid
|
51
104
|
end
|
52
105
|
|
53
|
-
|
54
|
-
|
106
|
+
##
|
107
|
+
# @return [Boolean] whether this request is a dry run and will therefore not persist changes.
|
108
|
+
def dry_run?
|
109
|
+
dry_run
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# @return [Boolean] whether this request has already been run and is therefore locked.
|
114
|
+
def locked?
|
115
|
+
locked
|
55
116
|
end
|
56
117
|
|
118
|
+
def lock!
|
119
|
+
@locked = true
|
120
|
+
end
|
121
|
+
|
122
|
+
##
|
123
|
+
# Returns all associated audits. If `dry_run` is true, these will not be persistent in DB.
|
124
|
+
#
|
125
|
+
# @return [ActiveRecord::Relation] all audits associated with the request.
|
57
126
|
def audits
|
58
127
|
Audited::Audit.where(request_uuid: request_uuid)
|
59
128
|
end
|
129
|
+
|
130
|
+
def set_request_uuid
|
131
|
+
self.class.set_request_uuid(request_uuid)
|
132
|
+
end
|
133
|
+
|
134
|
+
def unset_request_uuid
|
135
|
+
self.class.unset_request_uuid
|
136
|
+
end
|
137
|
+
|
138
|
+
def current
|
139
|
+
self.class.current
|
140
|
+
end
|
60
141
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omg-audit-group
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Greenfield
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -59,11 +59,11 @@ extensions: []
|
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
61
|
- lib/audit_group.rb
|
62
|
-
homepage: https://github.com/omgreenfield/
|
62
|
+
homepage: https://github.com/omgreenfield/audit-group
|
63
63
|
licenses:
|
64
64
|
- MIT
|
65
65
|
metadata:
|
66
|
-
homepage_uri: https://github.com/omgreenfield/
|
66
|
+
homepage_uri: https://github.com/omgreenfield/audit-group
|
67
67
|
rubygems_mfa_required: 'true'
|
68
68
|
post_install_message:
|
69
69
|
rdoc_options: []
|
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
|
-
rubygems_version: 3.5.
|
83
|
+
rubygems_version: 3.5.17
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
86
|
summary: Groups transactions from the `audited` gem into a single request_uuid
|