omg-audit-group 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/audit_group.rb +95 -14
  3. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20e5522972e49c63dc37ea573d6f17b9a9f96c86c9ee754c688dc64d304273a7
4
- data.tar.gz: 0f6d2979feaf9721716ed88a318707e279bfbadc32bf35c450ec69497d248f53
3
+ metadata.gz: c2103693c9d639bfce07ecae254b8df32a5571ed8084a141aa3b7d072ea62632
4
+ data.tar.gz: 8b49dfc610cacdf906fa910414064ad4cec032a14809512111e22c3f290c5e38
5
5
  SHA512:
6
- metadata.gz: ca13f29190d4020aafb6765a6f9294c9aa6f712bb6cfd0e9e86e9a902b8a3ba2767e5bb36c71a1d6026fc434df4ca6d11e5280be2b52ac5dfd3be859acdc6dcb
7
- data.tar.gz: 75bb4c7909885993f31fe383b7c74598b4fc0ebf239e2c6b3a34eb8757ecc88242e23d58dd08e4d10a215f0341a7b7493d0f4740dfe3048c0df7b4812aa79369
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.3'
6
+ VERSION = '0.1.4'
8
7
 
9
- attr_reader :block, :request_uuid
10
-
11
- delegate :current, :unset_request_uuid, to: :class
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
- delegate :request_uuid, :audits, to: :current
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
- def request(&block)
32
- raise ArgumentError, 'No block given and no active group' unless block_given?
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
- new.request(&block)
50
+ def audits
51
+ current.audits
35
52
  end
36
53
  end
37
54
 
38
- def initialize(request_uuid = SecureRandom.uuid)
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
- block.call if block.present?
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
- def set_request_uuid
54
- self.class.set_request_uuid(request_uuid)
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.3
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-05-18 00:00:00.000000000 Z
11
+ date: 2024-08-10 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
  - - ">="
@@ -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/omg-util/tree/main/audit_group
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