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 +4 -4
- data/README.md +23 -0
- data/lib/mongoid/audit_log.rb +20 -2
- data/lib/mongoid/audit_log/entry.rb +1 -1
- data/lib/mongoid/audit_log/version.rb +1 -1
- data/spec/mongoid/audit_log/entry_spec.rb +5 -0
- data/spec/mongoid/audit_log_spec.rb +64 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61de8364fc9a0e8554f5c421560717892f0031b3034fc6978df71de6f106d87d
|
4
|
+
data.tar.gz: '053874e715158f7623cf52121bce0aad06696517c9abaffe1c9891ecc67011c0'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/mongoid/audit_log.rb
CHANGED
@@ -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
|
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
|
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
|
@@ -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.
|
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:
|
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.
|
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
|