mongoid-audit_log 0.0.8 → 0.1.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 +24 -5
- data/lib/mongoid/audit_log.rb +6 -7
- data/lib/mongoid/audit_log/config.rb +0 -3
- data/lib/mongoid/audit_log/entry.rb +4 -4
- data/lib/mongoid/audit_log/version.rb +1 -1
- data/spec/mongoid/audit_log/entry_spec.rb +8 -4
- data/spec/mongoid/audit_log_spec.rb +18 -8
- metadata +3 -5
- data/lib/mongoid/audit_log/caches.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 155cd76ed2ee5e3a9a6d9941f4c8d4b47cef005c
|
4
|
+
data.tar.gz: e16bc334bc6531efb3ffb1e6f0a7af5a6b301904
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a8d97bb2479bf9f6a53cefd8f1c1a754c9dd9d1c00633c6e1aa76a3dd0a023996d08db19c2f6ad027bca8605056bf1b2e3a04f180aa8898f301cf106645521a
|
7
|
+
data.tar.gz: 7fdeec856542dd3a3dd2e6ad95a96b8e945647942feecb8107f65d715c3b0220209538301db6edfc751213bc778fcbf8f17e7e0fe6829f5c46015ee8fd315a0a
|
data/README.md
CHANGED
@@ -77,7 +77,7 @@ Mongoid::AuditLog.record do
|
|
77
77
|
module.update_attributes(:name => 'model')
|
78
78
|
end
|
79
79
|
|
80
|
-
model.audit_log_entries.length == 2
|
80
|
+
model.audit_log_entries.length == 2 # => true
|
81
81
|
|
82
82
|
model.audit_log_entries.first.create? # => true
|
83
83
|
model.audit_log_entries.first.update? # => false
|
@@ -91,12 +91,12 @@ model.audit_log_entries.second.destroy? # => false
|
|
91
91
|
model.audit_log_entries.second.tracked_changes.should == { 'name' => [nil, 'model'] }
|
92
92
|
```
|
93
93
|
|
94
|
-
There are also some built-in scopes (from the tests):
|
94
|
+
There are also some built-in scopes (examples from the tests):
|
95
95
|
|
96
96
|
```ruby
|
97
|
-
create = Entry.create!(:action => :create, :created_at => 10.minutes.ago)
|
98
|
-
update = Entry.create!(:action => :update, :created_at => 5.minutes.ago)
|
99
|
-
destroy = Entry.create!(:action => :destroy, :created_at => 1.minutes.ago)
|
97
|
+
create = Entry.create!(:action => :create, :created_at => 10.minutes.ago)
|
98
|
+
update = Entry.create!(:action => :update, :created_at => 5.minutes.ago)
|
99
|
+
destroy = Entry.create!(:action => :destroy, :created_at => 1.minutes.ago)
|
100
100
|
|
101
101
|
Entry.creates.to_a.should == [create]
|
102
102
|
Entry.updates.to_a.should == [update]
|
@@ -105,6 +105,25 @@ There are also some built-in scopes (from the tests):
|
|
105
105
|
end
|
106
106
|
```
|
107
107
|
|
108
|
+
### Additional saved data
|
109
|
+
|
110
|
+
You can access the attributes of the model saved on the `Mongoid::AuditLog::Entry`.
|
111
|
+
They are saved on the document in the `#model_attributes`, and include any changes in
|
112
|
+
the `#tracked_changes` hash.
|
113
|
+
|
114
|
+
Examples:
|
115
|
+
```ruby
|
116
|
+
Mongoid::AuditLog.record do
|
117
|
+
model = Model.create!(:name => 'foo bar')
|
118
|
+
end
|
119
|
+
|
120
|
+
model.audit_log_entries.length == 1 # => true
|
121
|
+
|
122
|
+
model.audit_log_entries.first.create? # => true
|
123
|
+
model.audit_log_entries.first.model_attributes # => {"name"=>"foo bar"}
|
124
|
+
```
|
125
|
+
|
126
|
+
|
108
127
|
## Contributing
|
109
128
|
|
110
129
|
1. Fork it
|
data/lib/mongoid/audit_log.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "mongoid/audit_log/version"
|
2
2
|
require "mongoid/audit_log/config"
|
3
3
|
require "mongoid/audit_log/entry"
|
4
|
-
require "mongoid/audit_log/caches"
|
5
4
|
require "mongoid/audit_log/changes"
|
6
5
|
require "mongoid/audit_log/embedded_changes"
|
7
6
|
|
@@ -13,13 +12,13 @@ module Mongoid
|
|
13
12
|
has_many :audit_log_entries, :as => :audited,
|
14
13
|
:class_name => 'Mongoid::AuditLog::Entry', :validate => false
|
15
14
|
|
16
|
-
|
15
|
+
AuditLog.actions.each do |action|
|
17
16
|
send("before_#{action}") do
|
18
|
-
set_audit_log_changes if
|
17
|
+
set_audit_log_changes if AuditLog.recording?
|
19
18
|
end
|
20
19
|
|
21
20
|
send("after_#{action}") do
|
22
|
-
save_audit_log_entry(action) if
|
21
|
+
save_audit_log_entry(action) if AuditLog.recording?
|
23
22
|
end
|
24
23
|
end
|
25
24
|
end
|
@@ -50,17 +49,17 @@ module Mongoid
|
|
50
49
|
private
|
51
50
|
|
52
51
|
def set_audit_log_changes
|
53
|
-
@_audit_log_changes =
|
52
|
+
@_audit_log_changes = Changes.new(self).tap(&:read)
|
54
53
|
end
|
55
54
|
|
56
55
|
def save_audit_log_entry(action)
|
57
56
|
unless action == :update && @_audit_log_changes.all.blank?
|
58
|
-
|
57
|
+
Entry.create!(
|
59
58
|
:action => action,
|
60
59
|
:audited_type => self.class,
|
61
60
|
:audited_id => id,
|
62
61
|
:tracked_changes => @_audit_log_changes.all,
|
63
|
-
:
|
62
|
+
:model_attributes => attributes.dup
|
64
63
|
)
|
65
64
|
end
|
66
65
|
end
|
@@ -7,7 +7,7 @@ module Mongoid
|
|
7
7
|
field :action, :type => Symbol
|
8
8
|
field :tracked_changes, :type => Hash, :default => {}
|
9
9
|
field :modifier_id, :type => String
|
10
|
-
field :
|
10
|
+
field :model_attributes, :type => Hash
|
11
11
|
|
12
12
|
belongs_to :audited, :polymorphic => true
|
13
13
|
|
@@ -52,14 +52,14 @@ module Mongoid
|
|
52
52
|
|
53
53
|
def respond_to?(sym, *args)
|
54
54
|
key = sym.to_s
|
55
|
-
(
|
55
|
+
(model_attributes.present? && model_attributes.has_key?(key)) || super
|
56
56
|
end
|
57
57
|
|
58
58
|
def method_missing(sym, *args, &block)
|
59
59
|
key = sym.to_s
|
60
60
|
|
61
|
-
if
|
62
|
-
|
61
|
+
if model_attributes.present? && model_attributes.has_key?(key)
|
62
|
+
model_attributes[key]
|
63
63
|
else
|
64
64
|
super
|
65
65
|
end
|
@@ -72,18 +72,22 @@ module Mongoid
|
|
72
72
|
end
|
73
73
|
|
74
74
|
describe '#respond_to?' do
|
75
|
-
let(:entry)
|
75
|
+
let(:entry) do
|
76
|
+
Entry.new(:model_attributes => { 'name' => 'foo', 'other' => nil })
|
77
|
+
end
|
76
78
|
|
77
|
-
it 'returns true for
|
79
|
+
it 'returns true for methods from the model attributes' do
|
78
80
|
entry.respond_to?(:name).should be_true
|
79
81
|
entry.respond_to?(:other).should be_true
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
83
85
|
describe '#method_missing' do
|
84
|
-
let(:entry)
|
86
|
+
let(:entry) do
|
87
|
+
Entry.new(:model_attributes => { 'name' => 'foo', 'other' => nil })
|
88
|
+
end
|
85
89
|
|
86
|
-
it 'responds to methods for which it has a
|
90
|
+
it 'responds to methods for which it has a model attribute' do
|
87
91
|
entry.name.should == 'foo'
|
88
92
|
entry.other.should == nil
|
89
93
|
end
|
@@ -51,10 +51,7 @@ module Mongoid
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
it 'saves
|
55
|
-
tmp = Mongoid::AuditLog.cache_fields
|
56
|
-
Mongoid::AuditLog.cache_fields << :name
|
57
|
-
|
54
|
+
it 'saves model attributes' do
|
58
55
|
AuditLog.record do
|
59
56
|
product = Product.create!(:name => 'Foo bar')
|
60
57
|
product.update_attributes(:name => 'Bar baz')
|
@@ -62,11 +59,9 @@ module Mongoid
|
|
62
59
|
|
63
60
|
product.audit_log_entries.count.should == 3
|
64
61
|
product.audit_log_entries.each do |entry|
|
65
|
-
entry.name.should be_present
|
62
|
+
entry.model_attributes['name'].should be_present
|
66
63
|
end
|
67
64
|
end
|
68
|
-
|
69
|
-
Mongoid::AuditLog.cache_fields = tmp
|
70
65
|
end
|
71
66
|
|
72
67
|
it 'saves the modifier if passed' do
|
@@ -108,7 +103,15 @@ module Mongoid
|
|
108
103
|
entry = product.audit_log_entries.first
|
109
104
|
|
110
105
|
entry.create?.should be_true
|
111
|
-
|
106
|
+
|
107
|
+
entry.tracked_changes.should == {
|
108
|
+
'name' => [nil, 'Foo bar']
|
109
|
+
}
|
110
|
+
|
111
|
+
entry.model_attributes.should == {
|
112
|
+
'_id' => product.id,
|
113
|
+
'name' => 'Foo bar'
|
114
|
+
}
|
112
115
|
end
|
113
116
|
|
114
117
|
it 'saves embedded creations' do
|
@@ -119,6 +122,13 @@ module Mongoid
|
|
119
122
|
entry = product.audit_log_entries.first
|
120
123
|
|
121
124
|
entry.create?.should be_true
|
125
|
+
|
126
|
+
entry.model_attributes.should == {
|
127
|
+
'_id' => product.id,
|
128
|
+
'name' => 'Foo bar',
|
129
|
+
'variants' => [{ '_id' => product.variants.first.id, 'sku'=>'sku' }]
|
130
|
+
}
|
131
|
+
|
122
132
|
entry.tracked_changes.should == {
|
123
133
|
'name' => [nil, 'Foo bar'],
|
124
134
|
'variants' => [{ 'sku' => [nil, 'sku'] }]
|
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.0
|
4
|
+
version: 0.1.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: 2015-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -66,7 +66,6 @@ files:
|
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
68
|
- lib/mongoid/audit_log.rb
|
69
|
-
- lib/mongoid/audit_log/caches.rb
|
70
69
|
- lib/mongoid/audit_log/changes.rb
|
71
70
|
- lib/mongoid/audit_log/config.rb
|
72
71
|
- lib/mongoid/audit_log/embedded_changes.rb
|
@@ -98,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
97
|
version: 1.3.6
|
99
98
|
requirements: []
|
100
99
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.4.1
|
102
101
|
signing_key:
|
103
102
|
specification_version: 4
|
104
103
|
summary: No fancy versioning, undo, redo, etc. Just saves changes to Mongoid models
|
@@ -109,4 +108,3 @@ test_files:
|
|
109
108
|
- spec/mongoid/audit_log_spec.rb
|
110
109
|
- spec/spec_helper.rb
|
111
110
|
- spec/support/models.rb
|
112
|
-
has_rdoc:
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module Mongoid
|
2
|
-
module AuditLog
|
3
|
-
class Caches
|
4
|
-
attr_reader :model
|
5
|
-
|
6
|
-
def initialize(model)
|
7
|
-
@model = model
|
8
|
-
end
|
9
|
-
|
10
|
-
def all
|
11
|
-
return nil unless Mongoid::AuditLog.cache_fields.present?
|
12
|
-
|
13
|
-
Mongoid::AuditLog.cache_fields.inject({}) do |memo, field|
|
14
|
-
memo[field] = model.send(field) if model.respond_to?(field)
|
15
|
-
memo
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|