mongoid_simple_audit 0.1.4

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDNhMjFmOTBkZTJmMWZiZDI1ZDNhZmJiZWJlMzk0MzdmODNjN2MxOQ==
5
+ data.tar.gz: !binary |-
6
+ YzY1MWFkZjhlZGUzYmIyNDEzOTIyYzMxNGZlZTU5NTViY2EzYmJlYw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZTU4MTNmOGVhMTIzNTAxZGEwM2E2ZjQ2ZDYwYjVlMjE2NGIxYWZhOThiMTdk
10
+ ZGUwNTEzNDQ0YzM0ZTMwZGQ1NjA0NWY0Yjg1Mzg2YWI4Yjg3ZDY5M2U4OTU2
11
+ NDEwMmNlNTlmZTEzNjNlZjIxZmRiZTk0MGYxN2QwMjE1NjcxNWI=
12
+ data.tar.gz: !binary |-
13
+ YjkxNGQyOGYzYTZkN2Q3NzMwODVlODM4YzA3NmU1YzY3NmQxNTI1YTkxYTVi
14
+ YTVkOGQ3M2I4ZTYwMzM0ZGQyZTU1MmU2M2IzMGM5NTQyYjk0NDc1NDRjZWY4
15
+ MTNiZmM3OWNjNTcwMGU0NzI2M2I1NWM5ZTU4MGFjZDg5M2U4MmU=
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+
8
+ gemfile:
9
+ - gemfiles/rails_3.gemfile
10
+ - gemfiles/rails_4.gemfile
11
+
12
+ script: 'bundle exec rake'
13
+
14
+ services:
15
+ - mongodb
16
+ before_script:
17
+ - sleep 15
18
+
19
+ notifications:
20
+ email:
21
+ recipients:
22
+ - felix.abele@gmail.com
23
+ on_failure: change
24
+ on_success: never
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "rails-3" do
2
+ gem "mongoid", '>= 3.0', '< 4.0.0'
3
+ end
4
+
5
+ appraise "rails-4" do
6
+ gem "mongoid", '>= 4.0.0'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_simple_audit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Felix Abele
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,151 @@
1
+ [![Build Status](https://travis-ci.org/felixabele/mongoid_simple_audit.svg?branch=master)](https://travis-ci.org/felixabele/mongoid_simple_audit) [![Coverage Status](https://img.shields.io/coveralls/felixabele/mongoid_simple_audit.svg)](https://coveralls.io/r/felixabele/mongoid_simple_audit?branch=master)
2
+
3
+ # MongoidSimpleAudit
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.
6
+
7
+ # What is this Gem about?
8
+
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
+ Instead of storing audits for all data aggregated by the audited model, you can specify a serializable representation of the model.
11
+ mongoid_simple_audit is intended as a straightforward, performant and simple auditing solution.
12
+
13
+ * a helper method is provided to easily display the audit log
14
+ * the Audit object provides a #delta method which computes the differences between two audits
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'mongoid_simple_audit'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install mongoid_simple_audit
29
+
30
+ Don't forget to create the MongoDB indeces
31
+
32
+ rake db:mongoid:create_indexes
33
+
34
+ ## Usage
35
+
36
+ class Booking < ActiveRecord::Base
37
+ simple_audit do |record|
38
+ # data to be audited
39
+ {
40
+ :price => record.price,
41
+ :period => record.period,
42
+ ...
43
+ }
44
+ end
45
+ end
46
+
47
+ # in your view
48
+ <%= render_audits(@booking) %>
49
+
50
+ Audit ActiveRecord models. Somewhere in your (backend) views show the audit logs.
51
+
52
+ # in your model
53
+ # app/models/booking.rb
54
+
55
+ class Booking < ActiveRecord::Base
56
+ simple_audit
57
+ ...
58
+ end
59
+
60
+ # in your view
61
+ # app/views/bookings/booking.html.erb
62
+
63
+ ...
64
+ <%= render_audits(@booking) %>
65
+ ...
66
+
67
+ ## Sample styling
68
+ .audits {
69
+ clear: both;
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
+ }
87
+
88
+ .audit .current {
89
+ margin-left: 6px;
90
+ }
91
+ .audit .previous {
92
+ margin-left: 6px;
93
+ text-decoration: line-through;
94
+ }
95
+
96
+ # Assumptions and limitations
97
+
98
+ * Your user model is called User (or Cms::User) and the current user User.current
99
+ See [sentient_user](http://github.com/bokmann/sentient_user) for more information.
100
+
101
+
102
+ ## Customize auditing
103
+
104
+ By default after each save, all model's attributes and `belongs_to` associations (their `id` and `to_s` on these) are saved in the audits table.
105
+ You can customize the data which is saved by supplying a block which will return all relevant data for the audited model.
106
+
107
+ # app/models/booking.rb
108
+
109
+ class Booking < ActiveRecord::Base
110
+ simple_audit do |record|
111
+ {
112
+ :state => record.state,
113
+ :price => record.price.format,
114
+ :period => record.period.to_s,
115
+ :housing_units => record.housing_units.collect(&:name).join('; '),
116
+ ...
117
+ }
118
+ end
119
+ ...
120
+ end
121
+
122
+ You can also customize the attribute of the User model which will be stored in the audit.
123
+
124
+ # default is :name
125
+ simple_audit :username_method => :email
126
+
127
+ As a default it will audit all save and update calls even though no changes where made, unless you specify :audit_changes_only => true
128
+
129
+ simple_audit :audit_changes_only => true
130
+
131
+ ## Rendering audit trail
132
+
133
+ A helper method for displaying a list of audits is provided. It will render a decorated list of the provided audits;
134
+ only the differences between revisions will be shown, thus making the audit information easily readable.
135
+
136
+ ## Why I consider the MongoDB Version more suitable to the original with ActiveRecord
137
+
138
+ 1. Changed attributes are natively searchable by the database
139
+ 2. Better perfomance
140
+ 3. High scalability
141
+ 4. It supports the Mongoid wrapper
142
+
143
+ Negative aspect though, its not working nicely hand in hand with not MongoDB backends
144
+
145
+ ## Contributing
146
+
147
+ 1. Fork it ( https://github.com/felixabele/mongoid_simple_audit/fork )
148
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
149
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
150
+ 4. Push to the branch (`git push origin my-new-feature`)
151
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'progress']
8
+ end
9
+
10
+ task :default => :spec
11
+
12
+
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", ">= 3.0", "< 4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,137 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ mongoid_simple_audit (0.1.4)
5
+ mongoid
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (3.2.18)
11
+ activemodel (= 3.2.18)
12
+ activesupport (= 3.2.18)
13
+ builder (~> 3.0.0)
14
+ erubis (~> 2.7.0)
15
+ journey (~> 1.0.4)
16
+ rack (~> 1.4.5)
17
+ rack-cache (~> 1.2)
18
+ rack-test (~> 0.6.1)
19
+ sprockets (~> 2.2.1)
20
+ activemodel (3.2.18)
21
+ activesupport (= 3.2.18)
22
+ builder (~> 3.0.0)
23
+ activerecord (3.2.18)
24
+ activemodel (= 3.2.18)
25
+ activesupport (= 3.2.18)
26
+ arel (~> 3.0.2)
27
+ tzinfo (~> 0.3.29)
28
+ activesupport (3.2.18)
29
+ i18n (~> 0.6, >= 0.6.4)
30
+ multi_json (~> 1.0)
31
+ appraisal (1.0.2)
32
+ bundler
33
+ rake
34
+ thor (>= 0.14.0)
35
+ arel (3.0.3)
36
+ builder (3.0.4)
37
+ coveralls (0.7.1)
38
+ multi_json (~> 1.3)
39
+ rest-client
40
+ simplecov (>= 0.7)
41
+ term-ansicolor
42
+ thor
43
+ diff-lcs (1.2.5)
44
+ docile (1.1.5)
45
+ erubis (2.7.0)
46
+ factory_girl (4.4.0)
47
+ activesupport (>= 3.0.0)
48
+ factory_girl_rails (4.4.1)
49
+ factory_girl (~> 4.4.0)
50
+ railties (>= 3.0.0)
51
+ hike (1.2.3)
52
+ i18n (0.6.11)
53
+ journey (1.0.4)
54
+ json (1.8.1)
55
+ mime-types (1.25.1)
56
+ mongoid (3.1.6)
57
+ activemodel (~> 3.2)
58
+ moped (~> 1.4)
59
+ origin (~> 1.0)
60
+ tzinfo (~> 0.3.29)
61
+ moped (1.5.2)
62
+ multi_json (1.10.1)
63
+ netrc (0.7.7)
64
+ origin (1.1.0)
65
+ rack (1.4.5)
66
+ rack-cache (1.2)
67
+ rack (>= 0.4)
68
+ rack-ssl (1.3.4)
69
+ rack
70
+ rack-test (0.6.2)
71
+ rack (>= 1.0)
72
+ railties (3.2.18)
73
+ actionpack (= 3.2.18)
74
+ activesupport (= 3.2.18)
75
+ rack-ssl (~> 1.3.2)
76
+ rake (>= 0.8.7)
77
+ rdoc (~> 3.4)
78
+ thor (>= 0.14.6, < 2.0)
79
+ rake (10.3.2)
80
+ rdoc (3.12.2)
81
+ json (~> 1.4)
82
+ rest-client (1.7.2)
83
+ mime-types (>= 1.16, < 3.0)
84
+ netrc (~> 0.7)
85
+ rspec (3.1.0)
86
+ rspec-core (~> 3.1.0)
87
+ rspec-expectations (~> 3.1.0)
88
+ rspec-mocks (~> 3.1.0)
89
+ rspec-core (3.1.2)
90
+ rspec-support (~> 3.1.0)
91
+ rspec-expectations (3.1.0)
92
+ diff-lcs (>= 1.2.0, < 2.0)
93
+ rspec-support (~> 3.1.0)
94
+ rspec-mocks (3.1.0)
95
+ rspec-support (~> 3.1.0)
96
+ rspec-rails (3.1.0)
97
+ actionpack (>= 3.0)
98
+ activesupport (>= 3.0)
99
+ railties (>= 3.0)
100
+ rspec-core (~> 3.1.0)
101
+ rspec-expectations (~> 3.1.0)
102
+ rspec-mocks (~> 3.1.0)
103
+ rspec-support (~> 3.1.0)
104
+ rspec-support (3.1.0)
105
+ simplecov (0.9.0)
106
+ docile (~> 1.1.0)
107
+ multi_json
108
+ simplecov-html (~> 0.8.0)
109
+ simplecov-html (0.8.0)
110
+ sprockets (2.2.2)
111
+ hike (~> 1.2)
112
+ multi_json (~> 1.0)
113
+ rack (~> 1.0)
114
+ tilt (~> 1.1, != 1.3.0)
115
+ sqlite3 (1.3.9)
116
+ term-ansicolor (1.3.0)
117
+ tins (~> 1.0)
118
+ thor (0.19.1)
119
+ tilt (1.4.1)
120
+ tins (1.3.2)
121
+ tzinfo (0.3.41)
122
+
123
+ PLATFORMS
124
+ ruby
125
+
126
+ DEPENDENCIES
127
+ activerecord
128
+ appraisal
129
+ bundler (~> 1.6)
130
+ coveralls (~> 0.7.1)
131
+ factory_girl_rails
132
+ mongoid (>= 3.0, < 4.0.0)
133
+ mongoid_simple_audit!
134
+ rake
135
+ rspec
136
+ rspec-rails
137
+ sqlite3
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", ">= 4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,128 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ mongoid_simple_audit (0.1.4)
5
+ mongoid
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (4.0.0)
11
+ activesupport (= 4.0.0)
12
+ builder (~> 3.1.0)
13
+ erubis (~> 2.7.0)
14
+ rack (~> 1.5.2)
15
+ rack-test (~> 0.6.2)
16
+ activemodel (4.0.0)
17
+ activesupport (= 4.0.0)
18
+ builder (~> 3.1.0)
19
+ activerecord (4.0.0)
20
+ activemodel (= 4.0.0)
21
+ activerecord-deprecated_finders (~> 1.0.2)
22
+ activesupport (= 4.0.0)
23
+ arel (~> 4.0.0)
24
+ activerecord-deprecated_finders (1.0.3)
25
+ activesupport (4.0.0)
26
+ i18n (~> 0.6, >= 0.6.4)
27
+ minitest (~> 4.2)
28
+ multi_json (~> 1.3)
29
+ thread_safe (~> 0.1)
30
+ tzinfo (~> 0.3.37)
31
+ appraisal (1.0.2)
32
+ bundler
33
+ rake
34
+ thor (>= 0.14.0)
35
+ arel (4.0.2)
36
+ bson (2.3.0)
37
+ builder (3.1.4)
38
+ connection_pool (2.0.0)
39
+ coveralls (0.7.1)
40
+ multi_json (~> 1.3)
41
+ rest-client
42
+ simplecov (>= 0.7)
43
+ term-ansicolor
44
+ thor
45
+ diff-lcs (1.2.5)
46
+ docile (1.1.5)
47
+ erubis (2.7.0)
48
+ factory_girl (4.4.0)
49
+ activesupport (>= 3.0.0)
50
+ factory_girl_rails (4.4.1)
51
+ factory_girl (~> 4.4.0)
52
+ railties (>= 3.0.0)
53
+ i18n (0.6.11)
54
+ mime-types (1.25.1)
55
+ minitest (4.7.5)
56
+ mongoid (4.0.0)
57
+ activemodel (~> 4.0)
58
+ moped (~> 2.0.0)
59
+ origin (~> 2.1)
60
+ tzinfo (>= 0.3.37)
61
+ moped (2.0.0)
62
+ bson (~> 2.2)
63
+ connection_pool (~> 2.0)
64
+ optionable (~> 0.2.0)
65
+ multi_json (1.10.1)
66
+ netrc (0.7.7)
67
+ optionable (0.2.0)
68
+ origin (2.1.1)
69
+ rack (1.5.2)
70
+ rack-test (0.6.2)
71
+ rack (>= 1.0)
72
+ railties (4.0.0)
73
+ actionpack (= 4.0.0)
74
+ activesupport (= 4.0.0)
75
+ rake (>= 0.8.7)
76
+ thor (>= 0.18.1, < 2.0)
77
+ rake (10.3.2)
78
+ rest-client (1.7.2)
79
+ mime-types (>= 1.16, < 3.0)
80
+ netrc (~> 0.7)
81
+ rspec (3.1.0)
82
+ rspec-core (~> 3.1.0)
83
+ rspec-expectations (~> 3.1.0)
84
+ rspec-mocks (~> 3.1.0)
85
+ rspec-core (3.1.2)
86
+ rspec-support (~> 3.1.0)
87
+ rspec-expectations (3.1.0)
88
+ diff-lcs (>= 1.2.0, < 2.0)
89
+ rspec-support (~> 3.1.0)
90
+ rspec-mocks (3.1.0)
91
+ rspec-support (~> 3.1.0)
92
+ rspec-rails (3.1.0)
93
+ actionpack (>= 3.0)
94
+ activesupport (>= 3.0)
95
+ railties (>= 3.0)
96
+ rspec-core (~> 3.1.0)
97
+ rspec-expectations (~> 3.1.0)
98
+ rspec-mocks (~> 3.1.0)
99
+ rspec-support (~> 3.1.0)
100
+ rspec-support (3.1.0)
101
+ simplecov (0.9.0)
102
+ docile (~> 1.1.0)
103
+ multi_json
104
+ simplecov-html (~> 0.8.0)
105
+ simplecov-html (0.8.0)
106
+ sqlite3 (1.3.9)
107
+ term-ansicolor (1.3.0)
108
+ tins (~> 1.0)
109
+ thor (0.19.1)
110
+ thread_safe (0.3.4)
111
+ tins (1.3.2)
112
+ tzinfo (0.3.41)
113
+
114
+ PLATFORMS
115
+ ruby
116
+
117
+ DEPENDENCIES
118
+ activerecord
119
+ appraisal
120
+ bundler (~> 1.6)
121
+ coveralls (~> 0.7.1)
122
+ factory_girl_rails
123
+ mongoid (>= 4.0.0)
124
+ mongoid_simple_audit!
125
+ rake
126
+ rspec
127
+ rspec-rails
128
+ sqlite3
@@ -0,0 +1,27 @@
1
+ module Mongoid
2
+ module SimpleAudit
3
+ class Audit
4
+ include Mongoid::Document
5
+
6
+ field :auditable_type, type: String
7
+ field :auditable_id, type: String
8
+
9
+ index({ auditable_id: 1, auditable_type: 1 })
10
+
11
+ embeds_many :modifications
12
+
13
+ scope :by_auditable, (lambda do |auditable|
14
+ where( auditable_type: auditable.class.name, auditable_id: auditable.id )
15
+ end)
16
+
17
+ def self.find_or_create_by_auditable auditable
18
+ self.find_or_create_by( auditable_type: auditable.class.name, auditable_id: auditable.id )
19
+ end
20
+
21
+ def self.find_by_auditable auditable
22
+ self.by_auditable( auditable ).first
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ module Mongoid
4
+ module SimpleAudit
5
+ module Helper
6
+
7
+ # Render the change log for the given audited model
8
+ def render_audits(audited_model)
9
+ return '' if !audited_model.respond_to?(:audit) || audited_model.audit.nil?
10
+ audits = (audited_model.audit.modifications).dup.sort{|a,b| b.created_at <=> a.created_at}
11
+ res = ''
12
+ audits.each_with_index do |audit, index|
13
+ older_audit = audits[index + 1]
14
+ res += content_tag(:div, :class => 'audit') do
15
+ content_tag(:div, audit.action, :class => "action #{audit.action}") +
16
+ content_tag(:div, audit.username, :class => "user") +
17
+ content_tag(:div, l(audit.created_at), :class => "timestamp") +
18
+ content_tag(:div, :class => 'changes') do
19
+ changes = if older_audit.present?
20
+ audit.delta(older_audit).sort{|x,y| audited_model.class.human_attribute_name(x.first) <=> audited_model.class.human_attribute_name(y.first)}.collect do |k, v|
21
+ next if k.to_s == 'created_at' || k.to_s == 'updated_at'
22
+ "\n" +
23
+ audited_model.class.human_attribute_name(k) +
24
+ ":" +
25
+ content_tag(:span, (v.last ? v.last.to_s.force_encoding("UTF-8") : '' ), :class => 'current') +
26
+ content_tag(:span, (v.first ? v.first.to_s.force_encoding("UTF-8") : '' ), :class => 'previous')
27
+ end
28
+ else
29
+ audit.change_log.sort{|x,y| audited_model.class.human_attribute_name(x.first) <=> audited_model.class.human_attribute_name(y.first)}.reject{|k, v| v.blank?}.collect {|k, v| "\n#{audited_model.class.human_attribute_name(k)}: #{v}"}
30
+ end
31
+ raw changes.join
32
+ end
33
+ end
34
+ end
35
+ raw res
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,57 @@
1
+ module Mongoid
2
+ module SimpleAudit
3
+ class Modification
4
+ include Mongoid::Document
5
+ include Mongoid::Timestamps::Created
6
+ include Mongoid::Attributes::Dynamic if Mongoid::VERSION.to_i > 3
7
+
8
+ field :action, type: String
9
+ field :username, type: String
10
+ field :change_log, type: Hash
11
+
12
+ embedded_in :audit
13
+
14
+ # Computes the differences of the change logs between two audits.
15
+ #
16
+ # Returns a hash containing arrays of the form
17
+ # {
18
+ # :key_1 => [<value_in_other_change_log>, <value_in_this_audit>],
19
+ # :key_2 => [<value_in_other_change_log>, <value_in_this_audit>],
20
+ # }
21
+ def delta(other_change_log)
22
+ return self.change_log if other_change_log.nil?
23
+
24
+ {}.tap do |d|
25
+
26
+ # first for keys present only in this audit
27
+ (self.change_log.keys - other_change_log.change_log.keys).each do |k|
28
+ d[k] = [nil, self.change_log[k]]
29
+ end
30
+
31
+ # .. then for keys present only in other audit
32
+ (other_change_log.change_log.keys - self.change_log.keys).each do |k|
33
+ d[k] = [other_change_log.change_log[k], nil]
34
+ end
35
+
36
+ # .. finally for keys present in both, but with different values
37
+ self.change_log.keys.each do |k|
38
+ if self.change_log[k] != other_change_log.change_log[k]
39
+ d[k] = [other_change_log.change_log[k], self.change_log[k]]
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ # as mongoid stores hashes keys as strings, here us a way to get the change log symbolized
48
+ # TODO: refactor
49
+ def symbolized_change_log
50
+ sym_log = self.change_log.dup
51
+ sym_log = sym_log.symbolize_keys
52
+ sym_log.each{ |k,v| sym_log[k] = v.symbolize_keys if v.is_a? Hash }
53
+ sym_log
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,11 @@
1
+ module Mongoid
2
+ module SimpleAudit
3
+ class Railtie < Rails::Railtie
4
+ railtie_name :mongoid_simple_audit
5
+
6
+ rake_tasks do
7
+ require 'mongoid/simple_audit/tasks'
8
+ end
9
+ end
10
+ end
11
+ end