mongoid-audit_log 0.2.0 → 0.3.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 +4 -4
- data/lib/mongoid/audit_log.rb +9 -1
- data/lib/mongoid/audit_log/entry.rb +50 -0
- data/lib/mongoid/audit_log/version.rb +1 -1
- data/mongoid-audit_log.gemspec +1 -1
- data/spec/mongoid/audit_log/entry_spec.rb +29 -0
- data/spec/mongoid/audit_log_spec.rb +55 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c64fb48b393df395b00f96fd8f804df26a8c361
|
4
|
+
data.tar.gz: 63e4405ba7cd83a09bb306fd569ec1beec6d6e10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a3d0d35183456d3d385340d9343be542293dd280f4c7e8e7fea8c3e0b738df4799144a7bb032edc7afd78b01dd1a2528a2df4b2bde6f9594b24d379b1a57f62
|
7
|
+
data.tar.gz: ba9e4830088d3e14add3950bd29ea7f4f182136752db49235599aa76862d69ec5b7d075529e63b4e499ccbeebffc5a20b9ea92f642b253d35375bb054649b17a
|
data/lib/mongoid/audit_log.rb
CHANGED
@@ -61,9 +61,17 @@ module Mongoid
|
|
61
61
|
:audited_type => self.class,
|
62
62
|
:audited_id => id,
|
63
63
|
:tracked_changes => @_audit_log_changes.all,
|
64
|
-
:model_attributes => attributes.dup
|
64
|
+
:model_attributes => attributes.dup,
|
65
|
+
:document_path => traverse_association_chain
|
65
66
|
)
|
66
67
|
end
|
67
68
|
end
|
69
|
+
|
70
|
+
def traverse_association_chain(node = self, current_relation = nil)
|
71
|
+
relation = node.embedded? ? node.metadata_name.to_s : nil
|
72
|
+
list = node._parent ? traverse_association_chain(node._parent, relation) : []
|
73
|
+
list << { class_name: node.class.name, id: node.id, relation: current_relation }
|
74
|
+
list
|
75
|
+
end
|
68
76
|
end
|
69
77
|
end
|
@@ -8,6 +8,7 @@ module Mongoid
|
|
8
8
|
field :tracked_changes, :type => Hash, :default => {}
|
9
9
|
field :modifier_id, :type => String
|
10
10
|
field :model_attributes, :type => Hash
|
11
|
+
field :document_path, :type => Array
|
11
12
|
|
12
13
|
belongs_to :audited, :polymorphic => true
|
13
14
|
|
@@ -50,6 +51,29 @@ module Mongoid
|
|
50
51
|
@modifier = modifier
|
51
52
|
end
|
52
53
|
|
54
|
+
def for_embedded_doc?
|
55
|
+
document_path.try(:length).to_i > 1
|
56
|
+
end
|
57
|
+
|
58
|
+
def audited
|
59
|
+
if for_embedded_doc?
|
60
|
+
lookup_from_document_path
|
61
|
+
else
|
62
|
+
audited_type.constantize.where(id: audited_id).first
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def root
|
67
|
+
root = document_path.first
|
68
|
+
return audited if root.blank?
|
69
|
+
|
70
|
+
if for_embedded_doc?
|
71
|
+
root['class_name'].constantize.find(root['id'])
|
72
|
+
else
|
73
|
+
audited
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
53
77
|
def respond_to?(sym, *args)
|
54
78
|
key = sym.to_s
|
55
79
|
(model_attributes.present? && model_attributes.has_key?(key)) || super
|
@@ -64,6 +88,32 @@ module Mongoid
|
|
64
88
|
super
|
65
89
|
end
|
66
90
|
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def lookup_from_document_path
|
95
|
+
return nil if document_path.blank?
|
96
|
+
|
97
|
+
document_path.reduce(root) do |current, path|
|
98
|
+
relation_match = if document_path_matches?(path, current)
|
99
|
+
current
|
100
|
+
elsif current.respond_to?(:detect)
|
101
|
+
current.detect do |model|
|
102
|
+
document_path_matches?(path, model)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
if path['relation'].blank?
|
107
|
+
return relation_match
|
108
|
+
else
|
109
|
+
relation_match.send(path['relation'])
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def document_path_matches?(path, object)
|
115
|
+
object.class.name == path['class_name'] && object.id == path['id']
|
116
|
+
end
|
67
117
|
end
|
68
118
|
end
|
69
119
|
end
|
data/mongoid-audit_log.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["bencrouse@gmail.com"]
|
11
11
|
spec.description = %q{Stupid simple audit logging for Mongoid}
|
12
12
|
spec.summary = %q{No fancy versioning, undo, redo, etc. Just saves changes to Mongoid models in a separate collection.}
|
13
|
-
spec.homepage = "https://github.com/bencrouse/mongoid-
|
13
|
+
spec.homepage = "https://github.com/bencrouse/mongoid-audit_log"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -10,12 +10,30 @@ module Mongoid
|
|
10
10
|
class ::Product
|
11
11
|
include Mongoid::Document
|
12
12
|
include Mongoid::AuditLog
|
13
|
+
field :name, :type => String
|
14
|
+
embeds_many :variants
|
15
|
+
end
|
16
|
+
|
17
|
+
class ::Variant
|
18
|
+
include Mongoid::Document
|
19
|
+
include Mongoid::AuditLog
|
20
|
+
field :sku, :type => String
|
21
|
+
embedded_in :product
|
22
|
+
embeds_many :options
|
23
|
+
end
|
24
|
+
|
25
|
+
class ::Option
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::AuditLog
|
28
|
+
field :name, :type => String
|
29
|
+
embedded_in :variant
|
13
30
|
end
|
14
31
|
end
|
15
32
|
|
16
33
|
after(:all) do
|
17
34
|
AuditLog.modifier_class_name = @remember_modifier_class_name
|
18
35
|
Object.send(:remove_const, :Product)
|
36
|
+
Object.send(:remove_const, :Variant)
|
19
37
|
end
|
20
38
|
|
21
39
|
let(:user) { User.create! }
|
@@ -57,6 +75,17 @@ module Mongoid
|
|
57
75
|
end
|
58
76
|
end
|
59
77
|
|
78
|
+
describe '#audited' do
|
79
|
+
it 'uses the document path to find embedded documents' do
|
80
|
+
product = Product.create!(:name => 'Foo bar')
|
81
|
+
variant = product.variants.create!
|
82
|
+
option = AuditLog.record { variant.options.create! }
|
83
|
+
|
84
|
+
entry = Entry.desc(:created_at).first
|
85
|
+
entry.audited.should == option
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
60
89
|
describe '#modifier_id=' do
|
61
90
|
let(:entry) { Entry.new }
|
62
91
|
|
@@ -12,6 +12,7 @@ module Mongoid
|
|
12
12
|
|
13
13
|
class ::Variant
|
14
14
|
include Mongoid::Document
|
15
|
+
include Mongoid::AuditLog
|
15
16
|
field :sku, :type => String
|
16
17
|
embedded_in :product
|
17
18
|
end
|
@@ -124,6 +125,7 @@ module Mongoid
|
|
124
125
|
entry = product.audit_log_entries.first
|
125
126
|
|
126
127
|
entry.create?.should be_true
|
128
|
+
entry.root.should == product
|
127
129
|
|
128
130
|
entry.tracked_changes.should == {
|
129
131
|
'name' => [nil, 'Foo bar']
|
@@ -141,13 +143,14 @@ module Mongoid
|
|
141
143
|
product.save!
|
142
144
|
|
143
145
|
entry = product.audit_log_entries.first
|
146
|
+
entry.root.should == product
|
144
147
|
|
145
148
|
entry.create?.should be_true
|
146
149
|
|
147
150
|
entry.model_attributes.should == {
|
148
151
|
'_id' => product.id,
|
149
152
|
'name' => 'Foo bar',
|
150
|
-
'variants' => [{ '_id' => product.variants.first.id, 'sku'=>'sku' }]
|
153
|
+
'variants' => [{ '_id' => product.variants.first.id, 'sku' => 'sku' }]
|
151
154
|
}
|
152
155
|
|
153
156
|
entry.tracked_changes.should == {
|
@@ -155,6 +158,21 @@ module Mongoid
|
|
155
158
|
'variants' => [{ 'sku' => [nil, 'sku'] }]
|
156
159
|
}
|
157
160
|
end
|
161
|
+
|
162
|
+
it 'tracks parents on embedded creations' do
|
163
|
+
product = Product.create!(:name => 'Foo bar')
|
164
|
+
variant = product.variants.create!(sku: 'sku')
|
165
|
+
|
166
|
+
entry = Mongoid::AuditLog::Entry.desc(:created_at).first
|
167
|
+
entry.root.should == product
|
168
|
+
entry.document_path.length.should == 2
|
169
|
+
entry.document_path.first['class_name'].should == product.class.name
|
170
|
+
entry.document_path.first['id'].should == product.id
|
171
|
+
entry.document_path.first['relation'].should == 'variants'
|
172
|
+
entry.document_path.second['class_name'].should == variant.class.name
|
173
|
+
entry.document_path.second['id'].should == variant.id
|
174
|
+
entry.document_path.second['relation'].should == nil
|
175
|
+
end
|
158
176
|
end
|
159
177
|
|
160
178
|
context 'update' do
|
@@ -164,6 +182,7 @@ module Mongoid
|
|
164
182
|
entry = product.audit_log_entries.desc(:created_at).first
|
165
183
|
|
166
184
|
entry.update?.should be_true
|
185
|
+
entry.root.should == product
|
167
186
|
entry.tracked_changes.should == { 'name' => ['Foo bar', 'Bar baz'] }
|
168
187
|
end
|
169
188
|
|
@@ -179,6 +198,7 @@ module Mongoid
|
|
179
198
|
entry = product.audit_log_entries.desc(:created_at).first
|
180
199
|
|
181
200
|
entry.update?.should be_true
|
201
|
+
entry.root.should == product
|
182
202
|
entry.tracked_changes.should == {
|
183
203
|
'name' => ['Foo bar', 'Bar baz'],
|
184
204
|
'variants' => [{ 'sku' => ['sku', 'newsku'] }]
|
@@ -190,6 +210,23 @@ module Mongoid
|
|
190
210
|
product.update_attributes(:name => 'Foo bar')
|
191
211
|
product.audit_log_entries.length.should == 1
|
192
212
|
end
|
213
|
+
|
214
|
+
it 'tracks parents on embedded updates' do
|
215
|
+
product = Product.create!(:name => 'Foo bar')
|
216
|
+
variant = product.variants.create!(sku: 'sku')
|
217
|
+
variant.sku = 'newsku'
|
218
|
+
variant.save!
|
219
|
+
|
220
|
+
entry = Mongoid::AuditLog::Entry.desc(:created_at).first
|
221
|
+
entry.root.should == product
|
222
|
+
entry.document_path.length.should == 2
|
223
|
+
entry.document_path.first['class_name'].should == product.class.name
|
224
|
+
entry.document_path.first['id'].should == product.id
|
225
|
+
entry.document_path.first['relation'].should == 'variants'
|
226
|
+
entry.document_path.second['class_name'].should == variant.class.name
|
227
|
+
entry.document_path.second['id'].should == variant.id
|
228
|
+
entry.document_path.second['relation'].should == nil
|
229
|
+
end
|
193
230
|
end
|
194
231
|
|
195
232
|
context 'destroy' do
|
@@ -199,6 +236,23 @@ module Mongoid
|
|
199
236
|
entry = product.audit_log_entries.desc(:created_at).first
|
200
237
|
|
201
238
|
entry.destroy?.should be_true
|
239
|
+
entry.root.should == nil
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'tracks parents on embedded destroys' do
|
243
|
+
product = Product.create!(:name => 'Foo bar')
|
244
|
+
variant = product.variants.create!(sku: 'sku')
|
245
|
+
variant.destroy!
|
246
|
+
|
247
|
+
entry = Mongoid::AuditLog::Entry.desc(:created_at).first
|
248
|
+
entry.root.should == product
|
249
|
+
entry.document_path.length.should == 2
|
250
|
+
entry.document_path.first['class_name'].should == product.class.name
|
251
|
+
entry.document_path.first['id'].should == product.id
|
252
|
+
entry.document_path.first['relation'].should == 'variants'
|
253
|
+
entry.document_path.second['class_name'].should == variant.class.name
|
254
|
+
entry.document_path.second['id'].should == variant.id
|
255
|
+
entry.document_path.second['relation'].should == nil
|
202
256
|
end
|
203
257
|
end
|
204
258
|
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.
|
4
|
+
version: 0.3.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: 2016-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -77,7 +77,7 @@ files:
|
|
77
77
|
- spec/mongoid/audit_log_spec.rb
|
78
78
|
- spec/spec_helper.rb
|
79
79
|
- spec/support/models.rb
|
80
|
-
homepage: https://github.com/bencrouse/mongoid-
|
80
|
+
homepage: https://github.com/bencrouse/mongoid-audit_log
|
81
81
|
licenses:
|
82
82
|
- MIT
|
83
83
|
metadata: {}
|
@@ -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.
|
100
|
+
rubygems_version: 2.5.1
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: No fancy versioning, undo, redo, etc. Just saves changes to Mongoid models
|
@@ -108,3 +108,4 @@ test_files:
|
|
108
108
|
- spec/mongoid/audit_log_spec.rb
|
109
109
|
- spec/spec_helper.rb
|
110
110
|
- spec/support/models.rb
|
111
|
+
has_rdoc:
|