tangledwires-audited 6.0.1 → 6.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ecb53136ee29db88a0b1baeb8278767eecc1bd05c72846fa43c07c0395adf89
4
- data.tar.gz: 8a893fe19e2468d48ddd85d3a32f15a751f21cc2ed9502cfdc229c7267b89b18
3
+ metadata.gz: 8595ede793eed6955e5140df3030c279680e85a65d9497f924e96a6f801169a1
4
+ data.tar.gz: 1fef5c05d2e1e9d2e7e0342a550b587fca3b77060ce2aa716de2cfd82c6f56a0
5
5
  SHA512:
6
- metadata.gz: 0dfbacc7c5280a43bccb9f116b972a1e3f9189de9eb0841365980e436ac521e6cc31844e385f4d08864c510057caa4dec769e3c3629f70df52d9151d789edbe8
7
- data.tar.gz: b14b86bb8069096f0ec360c16c14a9167b767105ba2921563ff5776d01fac31cc8f8e05b47ffc64cdf46d9231faf1a7cf53fd1d6590d0b7beb9f046c2a216d5d
6
+ metadata.gz: cb0a4d73b7a68a1891c00204f2d84fa611d451f6e332e378d9dfa042a80e5f75746e4560a099fd944dc2ecc8cb3d3dbb27e2762448b1dff983c506524bfc50be
7
+ data.tar.gz: 0c636c2739c5be06a4eb055893cfeda5b3ef1be0d0b31914f5b6e8d1ab0dbd8589ce992913a9e588db3df22d8c7f072a66c04c770584603fc371c87de1311c26
data/README.md CHANGED
@@ -6,8 +6,13 @@ TangledWires Audited
6
6
 
7
7
  **Audited** (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited can also record who made those changes, save comments and associate models related to the changes.
8
8
 
9
+ Audited currently (6.2) works with Rails 8.0, 7.2, 7.1, 7.0.
9
10
 
10
- Audited currently (5.6) works with Rails 8.0, 7.2, 7.1, 7.0.
11
+ ## Changes from `collectiveidea/audited`
12
+ * Remove support for Rails versions below 7.0
13
+ * Add support for auditing HABTM associations
14
+ * Allow multiple associated audits ([collectiveidea/audited#406](https://github.com/collectiveidea/audited/pull/406))
15
+ * Add an option to redact a record's audits when the record is deleted
11
16
 
12
17
  ## Supported Rubies
13
18
 
@@ -58,6 +58,13 @@ module Audited
58
58
  # end
59
59
  # end
60
60
  #
61
+ # * +redact_after_destroy+ - If an array is passed, the columns in the array
62
+ # will be redacted from audits when the record is destroyed.
63
+ #
64
+ # class User < ActiveRecord::Base
65
+ # audited redact_after_destroy: [:name, :email, :address]
66
+ # end
67
+ #
61
68
  def audited(options = {})
62
69
  audited? ? update_audited_options(options) : set_audit(options)
63
70
  end
@@ -90,6 +97,53 @@ module Audited
90
97
  before_update :audit_update if audited_options[:on].include?(:update)
91
98
  after_touch :audit_touch if audited_options[:on].include?(:touch) && ::ActiveRecord::VERSION::MAJOR >= 6
92
99
  before_destroy :audit_destroy if audited_options[:on].include?(:destroy)
100
+ after_destroy :redact_after_destroy unless audited_options[:redact_after_destroy].nil?
101
+
102
+ reflect_on_all_associations(:has_and_belongs_to_many).each do |reflection|
103
+ associated_model = reflection.name
104
+
105
+ # Attach callbacks to HABTM associations to create an audit when something is added or removed.
106
+ # after_add and after_remove are called only when the associated objects are added or removed through the association collection
107
+ add_callback_to_reflection reflection, :after_add, ->(owner, record) { owner.send("_pending_#{association}_audit")[:add] << record.id }
108
+ add_callback_to_reflection reflection, :after_remove, ->(owner, record) { owner.send("_pending_#{association}_audit")[:remove] << record.id }
109
+
110
+ # This is used so that only one audit is created even if multiple items are added/removed from the association.
111
+ # Since after_add and after_remove are fired *before* saving, we can build a list of things added and removed, then combine them into one audit *after* everything is saved.
112
+ define_method "_pending_#{associated_model}_audit" do
113
+ instance_variable_get("@_pending_#{associated_model}_audit") || instance_variable_set("@_pending_#{associated_model}_audit", { add: [], remove: [] })
114
+ end
115
+
116
+ # The following section creates audits when HABTM associations are modified in a way other than the association collection.
117
+ ids_method = "#{associated_model.to_s.singularize}_ids"
118
+ mod = Module.new do
119
+ define_method "#{ids_method}=" do |new_ids|
120
+ old_ids = send ids_method
121
+ super new_ids # Call original Rails setter
122
+
123
+ added = new_ids - old_ids
124
+ removed = old_ids - new_ids
125
+
126
+ send("_pending_#{associated_model}_audit")[:add].concat(added)
127
+ send("_pending_#{associated_model}_audit")[:remove].concat(removed)
128
+ end
129
+ end
130
+ prepend mod
131
+
132
+ after_save do
133
+ pending = send("_pending_#{associated_model}_audit")
134
+ next if pending[:add].empty? && pending[:remove].empty?
135
+
136
+ write_audit(
137
+ action: "update",
138
+ audited_changes: {
139
+ associated_model.to_s => pending.transform_values(&:uniq)
140
+ }
141
+ )
142
+
143
+ # Clear pending changes
144
+ instance_variable_set "@_pending_#{associated_model}_audit", { add: [], remove: [] }
145
+ end
146
+ end
93
147
 
94
148
  # Define and set after_audit and around_audit callbacks. This might be useful if you want
95
149
  # to notify a party after the audit has been created or if you want to access the newly-created
@@ -117,6 +171,15 @@ module Audited
117
171
  normalize_audited_options
118
172
  self.audit_associated_with = audited_options[:associated_with]
119
173
  end
174
+
175
+ private
176
+ def add_callback_to_reflection(reflection, type, callback)
177
+ reflection.options[type] ||= []
178
+
179
+ unless reflection.options[type].include?(callback)
180
+ reflection.options[type] << callback
181
+ end
182
+ end
120
183
  end
121
184
 
122
185
  module AuditedInstanceMethods
@@ -375,6 +438,16 @@ module Audited
375
438
  end
376
439
  end
377
440
 
441
+ def redact_after_destroy
442
+ audits.each do |audit|
443
+ filter_attr_values(
444
+ audited_changes: audit.audited_changes,
445
+ attrs: Array(audited_options[:redact_after_destroy]).map(&:to_s),
446
+ placeholder: audited_options[:redaction_value] || REDACTED
447
+ )
448
+ end
449
+ end
450
+
378
451
  def write_audit(attrs)
379
452
  self.audit_comment = nil
380
453
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Audited
4
- VERSION = "6.0.1"
4
+ VERSION = "6.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tangledwires-audited
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1
4
+ version: 6.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TangledWires Ltd
@@ -236,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
236
  - !ruby/object:Gem::Version
237
237
  version: '0'
238
238
  requirements: []
239
- rubygems_version: 4.0.3
239
+ rubygems_version: 4.0.6
240
240
  specification_version: 4
241
241
  summary: Log all changes to your models
242
242
  test_files: []