mongoid-audit_log 0.6.0 → 0.6.1

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: 718afad5f797fe8acf640b0541086f99033e4457707a5fb5daa3b4505adf8d8d
4
- data.tar.gz: 92ba729bc96163933fa532d5d66b35ceeef4b3b85f43a76cd47c0e83518af6b1
3
+ metadata.gz: 61de8364fc9a0e8554f5c421560717892f0031b3034fc6978df71de6f106d87d
4
+ data.tar.gz: '053874e715158f7623cf52121bce0aad06696517c9abaffe1c9891ecc67011c0'
5
5
  SHA512:
6
- metadata.gz: 346b36e40c17a2bb6836c304f868eaadf92db5574cc13fd3aaaa2dd9243a4f689e582e6ed4941346ddb35406ca28351afd2e4fee928764a7d224cb180fbd7fc0
7
- data.tar.gz: 0c88dbb79f64014e8f62439d48df1fadb16e1742f01573fc132b989012b14185c32b6f8f2a9302c2e4f5a5b2b7ca05d20258bc934ee25858427c4e5aeff34d6d
6
+ metadata.gz: 32c4792b0a76084ad7c3c6f493be3caf796000facb5b82fc0fe4b7948fc85a10c062b85f7dfc5f503fd051d890b50bb6b4d008ec44adb651e0e43431671b6b7c
7
+ data.tar.gz: 04ed8d08325d4a606764a39e5ac9134381883324ced95b70f3cd4915a8cba28dce0a517694f59110a33837f8a8e680011de25d2d5b610b287210551c6d9c3c86
data/README.md CHANGED
@@ -146,6 +146,29 @@ model == Model.find_by(name: 'foo bar') # => true
146
146
 
147
147
  It's possible to end up in a situation where a destroy entry cannot be restored, e.g. an entry deleting an embedded document for a root document that's already been deleted. In these scenarios, `Mongoid::AuditLog::Restore::InvalidRestore` will be raised.
148
148
 
149
+ ### Disabling
150
+
151
+ The `AuditLog` module provides methods to included classes to allow explicit disabling or enabling of logging. This can be useful if a model includes the mixin indirectly through another mixin or inheritance.
152
+
153
+ ```ruby
154
+ class Parent
155
+ include Mongoid::Document
156
+ include Mongoid::AuditLog
157
+ end
158
+
159
+ class Child < Parent
160
+ disable_audit_log
161
+ end
162
+
163
+ class Grandchild < Child
164
+ enable_audit_log
165
+ end
166
+
167
+ Parent.audit_log_enabled? # => true
168
+ Child.audit_log_enabled? # => false
169
+ Grandchild.audit_log_enabled? # => true
170
+ ```
171
+
149
172
  ## Contributing
150
173
 
151
174
  1. Fork it
@@ -14,15 +14,29 @@ module Mongoid
14
14
 
15
15
  AuditLog.actions.each do |action|
16
16
  send("before_#{action}") do
17
- set_audit_log_changes if AuditLog.recording?
17
+ set_audit_log_changes if record_audit_log?
18
18
  end
19
19
 
20
20
  send("after_#{action}") do
21
- save_audit_log_entry(action) if AuditLog.recording?
21
+ save_audit_log_entry(action) if record_audit_log?
22
22
  end
23
23
  end
24
24
  end
25
25
 
26
+ class_methods do
27
+ def enable_audit_log
28
+ @audit_log_enabled = true
29
+ end
30
+
31
+ def disable_audit_log
32
+ @audit_log_enabled = false
33
+ end
34
+
35
+ def audit_log_enabled?
36
+ !defined?(@audit_log_enabled) || @audit_log_enabled
37
+ end
38
+ end
39
+
26
40
  def self.record(modifier = nil)
27
41
  already_recording = recording?
28
42
  enable unless already_recording
@@ -62,6 +76,10 @@ module Mongoid
62
76
  Thread.current[:mongoid_audit_log_modifier] = modifier
63
77
  end
64
78
 
79
+ def record_audit_log?
80
+ AuditLog.recording? && self.class.audit_log_enabled?
81
+ end
82
+
65
83
  private
66
84
 
67
85
  def set_audit_log_changes
@@ -44,7 +44,7 @@ module Mongoid
44
44
  nil
45
45
  else
46
46
  klass = Mongoid::AuditLog.modifier_class_name.constantize
47
- klass.find(modifier_id)
47
+ klass.find(modifier_id) rescue nil
48
48
  end
49
49
  end
50
50
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module AuditLog
3
- VERSION = "0.6.0"
3
+ VERSION = "0.6.1"
4
4
  end
5
5
  end
@@ -81,6 +81,11 @@ module Mongoid
81
81
  entry = Entry.new(:modifier_id => user.id)
82
82
  entry.modifier.should == user
83
83
  end
84
+
85
+ it 'can handle a missing modifier' do
86
+ entry = Entry.new(:modifier_id => 'foo')
87
+ entry.modifier.should be_nil
88
+ end
84
89
  end
85
90
 
86
91
  describe '#audited' do
@@ -25,6 +25,10 @@ module Mongoid
25
25
  Object.send(:remove_const, :Variant)
26
26
  end
27
27
 
28
+ after(:each) do
29
+ Product.enable_audit_log
30
+ end
31
+
28
32
  describe '.record' do
29
33
  it 'does not save an entry if not recording' do
30
34
  product = Product.create!
@@ -198,6 +202,15 @@ module Mongoid
198
202
  entry.document_path.second['id'].should == variant.id
199
203
  entry.document_path.second['relation'].should == nil
200
204
  end
205
+
206
+ it 'does not record if model disables logging' do
207
+ product = Product.create!(:name => 'Foo bar')
208
+ product.audit_log_entries.count.should eq(1)
209
+
210
+ Product.disable_audit_log
211
+ product.update!(name: 'Foo bar baz')
212
+ product.audit_log_entries.count.should eq(1)
213
+ end
201
214
  end
202
215
 
203
216
  context 'update' do
@@ -281,5 +294,56 @@ module Mongoid
281
294
  end
282
295
  end
283
296
  end
297
+
298
+ describe '.disable_audit_log' do
299
+ it 'disables recording for model' do
300
+ AuditLog.record do
301
+ product = Product.create!(name: 'Foo bar')
302
+ product.audit_log_entries.count.should eq(1)
303
+
304
+ Product.disable_audit_log
305
+ product.record_audit_log?.should be_false
306
+
307
+ product.update!(name: 'Bar foo')
308
+ product.audit_log_entries.count.should eq(1)
309
+ end
310
+ end
311
+ end
312
+
313
+ describe '.enable_audit_log' do
314
+ it 'enables recording for model' do
315
+ AuditLog.record do
316
+ Product.disable_audit_log
317
+
318
+ product = Product.create!(name: 'Foo bar')
319
+ product.audit_log_entries.count.should eq(0)
320
+
321
+ Product.enable_audit_log
322
+ product.record_audit_log?.should be_true
323
+
324
+ product.update!(name: 'Bar foo')
325
+ product.audit_log_entries.count.should eq(1)
326
+ end
327
+ end
328
+ end
329
+
330
+ describe '#record_audit_log?' do
331
+ it 'returns true while recording' do
332
+ Product.new.record_audit_log?.should be_false
333
+
334
+ AuditLog.record do
335
+ Product.new.record_audit_log?.should be_true
336
+ end
337
+ end
338
+
339
+ it 'returns false when audit log is disabled' do
340
+ Product.disable_audit_log
341
+ Product.new.record_audit_log?.should be_false
342
+
343
+ AuditLog.record do
344
+ Product.new.record_audit_log?.should be_false
345
+ end
346
+ end
347
+ end
284
348
  end
285
349
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-audit_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Crouse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-09 00:00:00.000000000 Z
11
+ date: 2020-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  - !ruby/object:Gem::Version
98
98
  version: 1.3.6
99
99
  requirements: []
100
- rubygems_version: 3.0.6
100
+ rubygems_version: 3.0.3
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: No fancy versioning, undo, redo, etc. Just saves changes to Mongoid models