omg-audit-group 0.1.3 → 0.1.5

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