mongoid_simple_audit 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +24 -30
- data/app/assets/javascripts/mongoid_simple_audit.css +28 -0
- data/gemfiles/rails_3.gemfile.lock +1 -1
- data/gemfiles/rails_4.gemfile.lock +1 -1
- data/lib/mongoid/simple_audit/engine.rb +7 -0
- data/lib/mongoid/simple_audit/simple_audit.rb +7 -2
- data/lib/mongoid/simple_audit/version.rb +1 -1
- data/lib/mongoid_simple_audit.rb +1 -0
- data/spec/factories.rb +11 -0
- data/spec/models/simple_mongo_audit_spec.rb +36 -9
- data/spec/spec_helper.rb +25 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OWZhOTA1ZWNkM2U5YjYwMDUwOWIyMDFlNzhjNGQ3MWRiMGZhOTQ2NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTIyMWFiNDcyNTQ4N2Y3YWQxMjZjMzk5MDgwZmYyNjA2YzI5NTU2Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzM0Nzc0OTYzYjkxMWJkZmQ3Nzg2ODk5MjIzZjQ2NGJjNDA0MDk5ZDNkN2Vm
|
10
|
+
NjMyZmQyMzI3Yzg1ZDdiZTE5ZDMwOTg3NWJhYjBmNjI0OTRjYzc0MDBkMjhj
|
11
|
+
NzIyN2QxYmYwMjRiZDY4ZTQ3ZGRmYWY0YmIyNWZmM2JmNDIzYzI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZmU5Mzg1YTRkNWJlYTVkNDMyZmUwMzhkN2Q0ZDFjMjhkZWM5NzMzMzZkMjZh
|
14
|
+
NTE5NjFiYWJmNWRjZTBjMmZmYjM1MWI3MTkzYjZmNjIyYmU1ZmU3NzZiNjA4
|
15
|
+
ZDIwZTA5YmI5OWI5NmRlYTkyM2UzNTBkNjVlY2ViM2I2MTVkYTI=
|
data/README.md
CHANGED
@@ -2,17 +2,21 @@
|
|
2
2
|
|
3
3
|
# MongoidSimpleAudit
|
4
4
|
|
5
|
-
This is modified version of the Gem simple_audit from Gabriel Tarnovan (https://github.com/gtarnovan/simple_audit) wich uses MongoDB with Mongoid to store audit logs instead of ActiveRecord.
|
5
|
+
This is a modified version of the Gem simple_audit from Gabriel Tarnovan (https://github.com/gtarnovan/simple_audit) wich uses MongoDB with Mongoid to store audit logs instead of ActiveRecord. Furthermore it is capable of auditing both ActiveRecord and Mongoid models, which is especialy usefull for applications which use both backends.
|
6
6
|
|
7
7
|
# What is this Gem about?
|
8
8
|
|
9
|
-
It's a simple auditing solution for ActiveRecord and
|
9
|
+
It's a simple auditing solution for ActiveRecord and Mongoid models. Provides an easy way of creating audit logs for complex model associations.
|
10
10
|
Instead of storing audits for all data aggregated by the audited model, you can specify a serializable representation of the model.
|
11
11
|
mongoid_simple_audit is intended as a straightforward, performant and simple auditing solution.
|
12
12
|
|
13
13
|
* a helper method is provided to easily display the audit log
|
14
14
|
* the Audit object provides a #delta method which computes the differences between two audits
|
15
15
|
|
16
|
+
## Requirements
|
17
|
+
|
18
|
+
The gem requires Mongoid version 3.x with Ruby on Rails 3.x or Mongoid 4.x with Rails 4.x
|
19
|
+
|
16
20
|
## Installation
|
17
21
|
|
18
22
|
Add this line to your application's Gemfile:
|
@@ -47,7 +51,7 @@ Don't forget to create the MongoDB indeces
|
|
47
51
|
# in your view
|
48
52
|
<%= render_audits(@booking) %>
|
49
53
|
|
50
|
-
Audit ActiveRecord models. Somewhere in your (backend) views show the audit logs.
|
54
|
+
Audit ActiveRecord or Mongoid models. Somewhere in your (backend) views show the audit logs.
|
51
55
|
|
52
56
|
# in your model
|
53
57
|
# app/models/booking.rb
|
@@ -57,6 +61,14 @@ Audit ActiveRecord models. Somewhere in your (backend) views show the audit logs
|
|
57
61
|
...
|
58
62
|
end
|
59
63
|
|
64
|
+
# or for Mongoid
|
65
|
+
|
66
|
+
class Booking
|
67
|
+
include Mongoid::Document
|
68
|
+
simple_audit
|
69
|
+
...
|
70
|
+
end
|
71
|
+
|
60
72
|
# in your view
|
61
73
|
# app/views/bookings/booking.html.erb
|
62
74
|
|
@@ -64,34 +76,10 @@ Audit ActiveRecord models. Somewhere in your (backend) views show the audit logs
|
|
64
76
|
<%= render_audits(@booking) %>
|
65
77
|
...
|
66
78
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
}
|
71
|
-
.audit {
|
72
|
-
-moz-border-radius:8px;
|
73
|
-
-webkit-border-radius: 8px;
|
74
|
-
background-color: #dfdfdf;
|
75
|
-
padding: 6px;
|
76
|
-
margin-bottom: 8px;
|
77
|
-
font-size: 12px;
|
78
|
-
}
|
79
|
-
.audit .action, .audit .user, .audit .timestamp{
|
80
|
-
float: left;
|
81
|
-
margin-right: 6px;
|
82
|
-
}
|
83
|
-
.audit .changes {
|
84
|
-
clear: both;
|
85
|
-
white-space: pre;
|
86
|
-
}
|
79
|
+
# in order to have the default styles, include the stylesheet
|
80
|
+
|
81
|
+
<%= stylesheet_link_tag 'mongoid_simple_audit' %>
|
87
82
|
|
88
|
-
.audit .current {
|
89
|
-
margin-left: 6px;
|
90
|
-
}
|
91
|
-
.audit .previous {
|
92
|
-
margin-left: 6px;
|
93
|
-
text-decoration: line-through;
|
94
|
-
}
|
95
83
|
|
96
84
|
# Assumptions and limitations
|
97
85
|
|
@@ -142,6 +130,12 @@ only the differences between revisions will be shown, thus making the audit info
|
|
142
130
|
|
143
131
|
Negative aspect though, its not working nicely hand in hand with not MongoDB backends
|
144
132
|
|
133
|
+
## How to migrate simple_audit record stored with ActiveRecord
|
134
|
+
|
135
|
+
If you want to migrate from the ActiveRecord version of simple_audit to a document based datamodel (maybe because your database got super fat), mongoid_simple_audit has a rake task for that.
|
136
|
+
|
137
|
+
rake mongoid_simple_audit:migrates_from_mysql_to_mongoid
|
138
|
+
|
145
139
|
## Contributing
|
146
140
|
|
147
141
|
1. Fork it ( https://github.com/felixabele/mongoid_simple_audit/fork )
|
@@ -0,0 +1,28 @@
|
|
1
|
+
/* MongoidSimpleAudit Styles */
|
2
|
+
.audits {
|
3
|
+
clear: both;
|
4
|
+
}
|
5
|
+
.audit {
|
6
|
+
-moz-border-radius:8px;
|
7
|
+
-webkit-border-radius: 8px;
|
8
|
+
background-color: #dfdfdf;
|
9
|
+
padding: 6px;
|
10
|
+
margin-bottom: 8px;
|
11
|
+
font-size: 12px;
|
12
|
+
}
|
13
|
+
.audit .action, .audit .user, .audit .timestamp{
|
14
|
+
float: left;
|
15
|
+
margin-right: 6px;
|
16
|
+
}
|
17
|
+
.audit .changes {
|
18
|
+
clear: both;
|
19
|
+
white-space: pre;
|
20
|
+
}
|
21
|
+
|
22
|
+
.audit .current {
|
23
|
+
margin-left: 6px;
|
24
|
+
}
|
25
|
+
.audit .previous {
|
26
|
+
margin-left: 6px;
|
27
|
+
text-decoration: line-through;
|
28
|
+
}
|
@@ -59,8 +59,13 @@ module Mongoid
|
|
59
59
|
|
60
60
|
attributes_and_associations = proc do |record|
|
61
61
|
changes = record.attributes
|
62
|
-
|
63
|
-
|
62
|
+
as_types = [:belongs_to]
|
63
|
+
as_types << :embeds_one if record.singleton_class.included_modules.include? Mongoid::Document
|
64
|
+
|
65
|
+
as_types.each do |as_type|
|
66
|
+
record.class.reflect_on_all_associations( as_type ).each do |assoc|
|
67
|
+
changes[assoc.name] = record.send(assoc.name).to_s
|
68
|
+
end
|
64
69
|
end
|
65
70
|
changes
|
66
71
|
end
|
data/lib/mongoid_simple_audit.rb
CHANGED
@@ -2,6 +2,7 @@ require 'mongoid/simple_audit/simple_audit'
|
|
2
2
|
require 'mongoid/simple_audit/audit'
|
3
3
|
require 'mongoid/simple_audit/modification'
|
4
4
|
require 'mongoid/simple_audit/helper'
|
5
|
+
require 'mongoid/simple_audit/engine'
|
5
6
|
require File.dirname(__FILE__) + '/mongoid/simple_audit/railtie' if defined?(Rails::Railtie)
|
6
7
|
|
7
8
|
if defined?(ActiveRecord::Base)
|
data/spec/factories.rb
CHANGED
@@ -11,6 +11,17 @@ FactoryGirl.define do
|
|
11
11
|
address
|
12
12
|
end
|
13
13
|
|
14
|
+
factory :mongoid_address, class: "Mongodoc::Address" do
|
15
|
+
line_1 "John"
|
16
|
+
zip "12047"
|
17
|
+
end
|
18
|
+
|
19
|
+
factory :mongoid_person, class: "Mongodoc::Person" do
|
20
|
+
name "Felix"
|
21
|
+
email "felix.abele@gmail.com"
|
22
|
+
address {FactoryGirl.build( :mongoid_address )}
|
23
|
+
end
|
24
|
+
|
14
25
|
factory :user do
|
15
26
|
name "Some user"
|
16
27
|
end
|
@@ -36,11 +36,6 @@ module Mongoid
|
|
36
36
|
expect( person.audit.modifications.last.change_log ).to have_key( 'name' )
|
37
37
|
end
|
38
38
|
|
39
|
-
it "should audit associated entity changes" do
|
40
|
-
person = create :person
|
41
|
-
expect( person.audit.modifications.last.change_log['address']['line_1'] ).to be_present
|
42
|
-
end
|
43
|
-
|
44
39
|
it "should not audit if no changes where made" do
|
45
40
|
person = create :person
|
46
41
|
person.update_attributes name: person.name
|
@@ -55,10 +50,42 @@ module Mongoid
|
|
55
50
|
expect( User.new.full_name ).to eql address.audit.modifications.last.username
|
56
51
|
end
|
57
52
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
53
|
+
# ----------------------------
|
54
|
+
# --- ActiveRecord specific Tests
|
55
|
+
# ----------------------------
|
56
|
+
context "ActiveRecord specific Tests" do
|
57
|
+
|
58
|
+
it "should audit associated entity changes" do
|
59
|
+
person = create :person, address: create(:address, line_1: 'Munich'), name: 'doe1'
|
60
|
+
create_audit = person.audit.modifications.last
|
61
|
+
person.address = create(:address, line_1: 'Berlin')
|
62
|
+
person.name = 'doe2'
|
63
|
+
person.save
|
64
|
+
update_audit = person.audit.modifications.last
|
65
|
+
expect( update_audit.delta(create_audit) ).to eql( {"name"=>["doe1", "doe2"], "address"=>[{"line_1"=>"Munich", "zip"=>"12047"}, {"line_1"=>"Berlin", "zip"=>"12047"}]} )
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# ----------------------------
|
70
|
+
# --- Mongoid specific Tests
|
71
|
+
# ----------------------------
|
72
|
+
context "Mongoid specific Tests" do
|
73
|
+
|
74
|
+
it 'should audit Mongoid models' do
|
75
|
+
address = Mongodoc::Address.create(line_1: 'Hans', zip: '10928')
|
76
|
+
expect( address.audit.modifications ).not_to be_empty
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should audit associated entity changes" do
|
80
|
+
person = create :mongoid_person, address: create(:mongoid_address, line_1: 'Munich'), name: 'doe1'
|
81
|
+
create_audit = person.audit.modifications.last
|
82
|
+
person.address = create(:mongoid_address, line_1: 'Berlin')
|
83
|
+
person.name = 'doe2'
|
84
|
+
person.save
|
85
|
+
update_audit = person.audit.modifications.last
|
86
|
+
expect( update_audit.delta(create_audit) ).to eql( {"name"=>["doe1", "doe2"], "address"=>[{"line_1"=>"Munich", "zip"=>"12047"}, {"line_1"=>"Berlin", "zip"=>"12047"}]} )
|
87
|
+
end
|
88
|
+
end
|
62
89
|
end
|
63
90
|
|
64
91
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -77,12 +77,6 @@ class Address < ActiveRecord::Base
|
|
77
77
|
simple_audit( username_method: :full_name )
|
78
78
|
end
|
79
79
|
|
80
|
-
class MongoidAddress
|
81
|
-
include Mongoid::Document
|
82
|
-
include Mongoid::Attributes::Dynamic if Mongoid::VERSION.to_i > 3
|
83
|
-
simple_audit
|
84
|
-
end
|
85
|
-
|
86
80
|
class Person < ActiveRecord::Base
|
87
81
|
has_one :address
|
88
82
|
simple_audit( audit_changes_only: true ) do |record|
|
@@ -93,6 +87,31 @@ class Person < ActiveRecord::Base
|
|
93
87
|
end
|
94
88
|
end
|
95
89
|
|
90
|
+
module Mongodoc
|
91
|
+
class Address
|
92
|
+
include Mongoid::Document
|
93
|
+
field :line_1
|
94
|
+
field :zip
|
95
|
+
field :type
|
96
|
+
|
97
|
+
embeds_one :person, class_name: 'Mongodoc::Person'
|
98
|
+
simple_audit
|
99
|
+
end
|
100
|
+
|
101
|
+
class Person
|
102
|
+
include Mongoid::Document
|
103
|
+
field :name
|
104
|
+
field :email
|
105
|
+
embedded_in :address, class_name: 'Mongodoc::Address'
|
106
|
+
simple_audit( audit_changes_only: true ) do |record|
|
107
|
+
{
|
108
|
+
name: record.name,
|
109
|
+
address: { line_1: record.address.line_1, zip: record.address.zip }
|
110
|
+
}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
96
115
|
class HomeAddress < Address
|
97
116
|
simple_audit :username_method => :short_name
|
98
117
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_simple_audit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felix Abele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -167,11 +167,13 @@ files:
|
|
167
167
|
- LICENSE.txt
|
168
168
|
- README.md
|
169
169
|
- Rakefile
|
170
|
+
- app/assets/javascripts/mongoid_simple_audit.css
|
170
171
|
- gemfiles/rails_3.gemfile
|
171
172
|
- gemfiles/rails_3.gemfile.lock
|
172
173
|
- gemfiles/rails_4.gemfile
|
173
174
|
- gemfiles/rails_4.gemfile.lock
|
174
175
|
- lib/mongoid/simple_audit/audit.rb
|
176
|
+
- lib/mongoid/simple_audit/engine.rb
|
175
177
|
- lib/mongoid/simple_audit/helper.rb
|
176
178
|
- lib/mongoid/simple_audit/modification.rb
|
177
179
|
- lib/mongoid/simple_audit/railtie.rb
|