mongoid-audit_log 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/mongoid/audit_log.rb +23 -9
- data/lib/mongoid/audit_log/entry.rb +10 -2
- data/lib/mongoid/audit_log/version.rb +1 -1
- data/spec/mongoid/audit_log/entry_spec.rb +12 -0
- data/spec/mongoid/audit_log_spec.rb +26 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd2a9b75ea8803b8c62a52e358adf073ca20cc92
|
4
|
+
data.tar.gz: '08ee1ca6dd2855610ac0f9e41da2a71f9072b5d5'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca3fb108c8e8c6fcc11bc0cf2c9c3580ace95971c010b58fdba043035b08a754f583882562b720a89193c5f2cc7d4b93f37df578e913cfc569a7e56bc74f236b
|
7
|
+
data.tar.gz: 4fac09d329077517f1d6277d69929d3feb35410e712c9a119b9d42ff612755b0717ca1ef6a02c624937c3ebad5cfc474a6e91f62925f1bfde47e22473f698b31
|
data/README.md
CHANGED
@@ -44,6 +44,11 @@ If you want to log the user who made the change, pass that user to the record me
|
|
44
44
|
Mongoid::AuditLog.record(current_user) do
|
45
45
|
Model.create!
|
46
46
|
end
|
47
|
+
|
48
|
+
# or
|
49
|
+
|
50
|
+
Mongoid::AuditLog.current_modifier = current_user
|
51
|
+
Mongoid::AuditLog.enable
|
47
52
|
```
|
48
53
|
|
49
54
|
A basic implementation in a Rails app might look something like:
|
data/lib/mongoid/audit_log.rb
CHANGED
@@ -24,20 +24,30 @@ module Mongoid
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.record(modifier = nil)
|
27
|
-
|
28
|
-
|
27
|
+
already_recording = recording?
|
28
|
+
enable unless already_recording
|
29
|
+
self.current_modifier = modifier
|
29
30
|
yield
|
30
31
|
ensure
|
31
|
-
|
32
|
-
|
32
|
+
disable unless already_recording
|
33
|
+
self.current_modifier = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.enable
|
37
|
+
Thread.current[:mongoid_audit_log_recording] = true
|
33
38
|
end
|
34
39
|
|
35
40
|
def self.disable
|
36
|
-
|
41
|
+
already_recording = recording?
|
37
42
|
Thread.current[:mongoid_audit_log_recording] = false
|
38
|
-
|
39
|
-
|
40
|
-
|
43
|
+
|
44
|
+
if block_given?
|
45
|
+
begin
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
Thread.current[:mongoid_audit_log_recording] = already_recording
|
49
|
+
end
|
50
|
+
end
|
41
51
|
end
|
42
52
|
|
43
53
|
def self.recording?
|
@@ -47,6 +57,10 @@ module Mongoid
|
|
47
57
|
def self.current_modifier
|
48
58
|
Thread.current[:mongoid_audit_log_modifier]
|
49
59
|
end
|
60
|
+
|
61
|
+
def self.current_modifier=(modifier)
|
62
|
+
Thread.current[:mongoid_audit_log_modifier] = modifier
|
63
|
+
end
|
50
64
|
|
51
65
|
private
|
52
66
|
|
@@ -61,7 +75,7 @@ module Mongoid
|
|
61
75
|
:audited_type => self.class,
|
62
76
|
:audited_id => id,
|
63
77
|
:tracked_changes => @_audit_log_changes.all,
|
64
|
-
:model_attributes => attributes.
|
78
|
+
:model_attributes => attributes.deep_dup,
|
65
79
|
:document_path => traverse_association_chain
|
66
80
|
)
|
67
81
|
end
|
@@ -10,7 +10,11 @@ module Mongoid
|
|
10
10
|
field :model_attributes, :type => Hash
|
11
11
|
field :document_path, :type => Array
|
12
12
|
|
13
|
-
|
13
|
+
if Gem::Version.new(Mongoid::VERSION) < Gem::Version.new('6.0.0.beta')
|
14
|
+
belongs_to :audited, :polymorphic => true
|
15
|
+
else
|
16
|
+
belongs_to :audited, :polymorphic => true, :optional => true
|
17
|
+
end
|
14
18
|
|
15
19
|
index({ :audited_id => 1, :audited_type => 1 })
|
16
20
|
index({ :modifier_id => 1 })
|
@@ -28,7 +32,9 @@ module Mongoid
|
|
28
32
|
|
29
33
|
def valid?(*)
|
30
34
|
result = super
|
31
|
-
|
35
|
+
if result && modifier.blank?
|
36
|
+
self.modifier = Mongoid::AuditLog.current_modifier
|
37
|
+
end
|
32
38
|
result
|
33
39
|
end
|
34
40
|
|
@@ -56,6 +62,8 @@ module Mongoid
|
|
56
62
|
end
|
57
63
|
|
58
64
|
def audited
|
65
|
+
return nil if audited_type.blank? || audited_id.blank?
|
66
|
+
|
59
67
|
if for_embedded_doc?
|
60
68
|
lookup_from_document_path
|
61
69
|
else
|
@@ -68,6 +68,14 @@ module Mongoid
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
describe '#valid?' do
|
72
|
+
it 'does not override a manually set modifier' do
|
73
|
+
entry = Entry.new(:modifier_id => user.id)
|
74
|
+
entry.valid?
|
75
|
+
entry.modifier.should == user
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
71
79
|
describe '#modifier' do
|
72
80
|
it 'finds the modifier based on the configured class' do
|
73
81
|
entry = Entry.new(:modifier_id => user.id)
|
@@ -84,6 +92,10 @@ module Mongoid
|
|
84
92
|
entry = Entry.desc(:created_at).first
|
85
93
|
entry.audited.should == option
|
86
94
|
end
|
95
|
+
|
96
|
+
it 'returns nil if the audited info is blank' do
|
97
|
+
Entry.new.audited.should be_nil
|
98
|
+
end
|
87
99
|
end
|
88
100
|
|
89
101
|
describe '#modifier_id=' do
|
@@ -16,6 +16,8 @@ module Mongoid
|
|
16
16
|
field :sku, :type => String
|
17
17
|
embedded_in :product
|
18
18
|
end
|
19
|
+
|
20
|
+
AuditLog.disable
|
19
21
|
end
|
20
22
|
|
21
23
|
after(:all) do
|
@@ -95,8 +97,31 @@ module Mongoid
|
|
95
97
|
end
|
96
98
|
end
|
97
99
|
|
100
|
+
describe '.enable' do
|
101
|
+
after(:each) do
|
102
|
+
AuditLog.disable
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'starts recording' do
|
106
|
+
AuditLog.enable
|
107
|
+
|
108
|
+
product = Product.create!(:name => 'Foo bar')
|
109
|
+
product.audit_log_entries.count.should == 1
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
98
113
|
describe '.disable' do
|
99
|
-
it '
|
114
|
+
it 'stops recording' do
|
115
|
+
AuditLog.enable
|
116
|
+
AuditLog.disable
|
117
|
+
|
118
|
+
product = Product.create!(:name => 'Foo bar')
|
119
|
+
product.audit_log_entries.should be_empty
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'can disable recording for a block' do
|
123
|
+
AuditLog.disable
|
124
|
+
|
100
125
|
AuditLog.record do
|
101
126
|
product = Product.create!(:name => 'Foo bar')
|
102
127
|
|
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.
|
4
|
+
version: 0.4.0
|
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: 2017-03-11 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
|
version: 1.3.6
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.5.
|
100
|
+
rubygems_version: 2.5.2
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: No fancy versioning, undo, redo, etc. Just saves changes to Mongoid models
|