mongoid-audit 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.ruby-version +1 -1
- data/.travis.yml +11 -1
- data/Gemfile +2 -1
- data/README.md +12 -1
- data/gemfiles/mongoid-4.0.gemfile +5 -0
- data/lib/mongoid-audit.rb +3 -25
- data/lib/mongoid-audit/rails_admin.rb +4 -4
- data/lib/mongoid-audit/railtie.rb +18 -0
- data/lib/mongoid-audit/trackable.rb +4 -2
- data/lib/mongoid-audit/tracker.rb +18 -2
- data/lib/mongoid-audit/version.rb +1 -1
- data/mongoid-audit.gemspec +9 -8
- data/spec/integration/integration_spec.rb +119 -6
- data/spec/spec_helper.rb +13 -1
- data/spec/support/database_cleaner.rb +6 -4
- data/spec/support/mongoid.rb +7 -8
- data/spec/trackable_spec.rb +20 -7
- data/spec/tracker_spec.rb +1 -1
- metadata +46 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9c16d338784fdacfa88a98c10f63d1512a813c5
|
4
|
+
data.tar.gz: 008ae2f160e31bb3851558f8acb60d60d083543c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c118e113a776ab1e4e80671eb563be01ebc665606723ca3708ce3510835a518f87ac6e034a750f7d0ebef1689c6b347346a31af75c167f00b52bf00e82722fd1
|
7
|
+
data.tar.gz: d1fde99a8443fae7373fba853e35fb4f4922bd9b29aeee85d6385110531b5d790add3f2381be0c88f6dbc39ef7114ca018cb4c7608f3d3ad33a77528bc679d8f
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.0.0-
|
1
|
+
ruby-2.0.0-p247
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
Mongoid::Audit
|
2
2
|
==============
|
3
3
|
|
4
|
+
Mongoid Audit is a fork of mongoid-history https://github.com/aq1018/mongoid-history
|
5
|
+
|
6
|
+
Fork differences:
|
7
|
+
* Rails 4 support
|
8
|
+
* Built in rails_admin auditing support
|
9
|
+
* Properly setting user (modifier) all the time
|
10
|
+
|
11
|
+
In case of problems, you can try mongoid-history, which didn't work properly for us.
|
12
|
+
|
4
13
|
[![Build Status](https://secure.travis-ci.org/rs-pro/mongoid-audit.png?branch=master)](http://travis-ci.org/rs-pro/mongoid-audit)
|
5
14
|
[![Dependency Status](https://gemnasium.com/rs-pro/mongoid-audit.png)](https://gemnasium.com/rs-pro/mongoid-audit)
|
15
|
+
[![Coverage Status](https://coveralls.io/repos/rs-pro/mongoid-audit/badge.png?branch=master)](https://coveralls.io/r/rs-pro/mongoid-audit?branch=master)
|
16
|
+
[![Gem Version](https://badge.fury.io/rb/mongoid-audit.png)](http://badge.fury.io/rb/mongoid-audit)
|
6
17
|
|
7
18
|
Mongoid-audit tracks historical changes for any document, including embedded ones. It achieves this by storing all history tracks in a single collection that you define. Embedded documents are referenced by storing an association path, which is an array of `document_name` and `document_id` fields starting from the top most parent document and down to the embedded document that should track history.
|
8
19
|
|
@@ -110,7 +121,7 @@ class Post
|
|
110
121
|
:version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
|
111
122
|
:track_create => false, # track document creation, default is false
|
112
123
|
:track_update => true, # track document updates, default is true
|
113
|
-
:track_destroy => false
|
124
|
+
:track_destroy => false # track document destruction, default is false
|
114
125
|
end
|
115
126
|
|
116
127
|
class Comment
|
data/lib/mongoid-audit.rb
CHANGED
@@ -4,8 +4,6 @@ require 'mongoid'
|
|
4
4
|
require 'rails-observers'
|
5
5
|
require 'rails/observers/active_model/active_model'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
7
|
module Mongoid
|
10
8
|
module Audit
|
11
9
|
mattr_accessor :tracker_class_name
|
@@ -19,28 +17,8 @@ module Mongoid
|
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
|
-
require
|
24
|
-
module Rails
|
25
|
-
module Mongoid
|
26
|
-
class Railtie < Rails::Railtie
|
27
|
-
initializer "instantiate observers" do
|
28
|
-
config.after_initialize do
|
29
|
-
::Mongoid::Audit.tracker_class.add_observer ::Mongoid::Audit::Sweeper.instance
|
30
|
-
|
31
|
-
# install model observer and action controller filter
|
32
|
-
# Mongoid::Audit::Sweeper.send(:observe, Mongoid::Audit.tracker_class_name)
|
33
|
-
if defined?(ActionController) and defined?(ActionController::Base)
|
34
|
-
ActionController::Base.class_eval do
|
35
|
-
before_filter { |controller| ::Mongoid::Audit::Sweeper.instance.before(controller) }
|
36
|
-
after_filter { |controller| ::Mongoid::Audit::Sweeper.instance.after(controller) }
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
20
|
+
if defined? Rails::Railtie
|
21
|
+
require 'mongoid-audit/railtie'
|
44
22
|
end
|
45
23
|
|
46
24
|
if Object.const_defined?("RailsAdmin")
|
@@ -53,4 +31,4 @@ require 'mongoid-audit/sweeper'
|
|
53
31
|
|
54
32
|
Mongoid::Audit.modifier_class_name = "User"
|
55
33
|
Mongoid::Audit.trackable_class_options = {}
|
56
|
-
Mongoid::Audit.current_user_method ||= :current_user
|
34
|
+
Mongoid::Audit.current_user_method ||= :current_user
|
@@ -9,7 +9,7 @@ module RailsAdmin
|
|
9
9
|
def message
|
10
10
|
@message = @version.action
|
11
11
|
mods = @version.modified.to_a.map do |c|
|
12
|
-
if c[1].class.name == "Moped::BSON::Binary"
|
12
|
+
if c[1].class.name == "Moped::BSON::Binary" || c[1].class.name == "BSON::Binary"
|
13
13
|
c[0] + " = {binary data}"
|
14
14
|
elsif c[1].to_s.length > 220
|
15
15
|
c[0] + " = " + c[1].to_s[0..200]
|
@@ -70,7 +70,7 @@ module RailsAdmin
|
|
70
70
|
|
71
71
|
def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
|
72
72
|
history = @version_class.where('association_chain.name' => model.model_name)
|
73
|
-
history = history.any_of(action: /.*#{query}
|
73
|
+
history = history.any_of({action: /.*#{query}.*/}, {modifier_id: /.*#{query}.*/}) if query.present?
|
74
74
|
if sort
|
75
75
|
order = sort_reverse == "true" ? :desc : :asc
|
76
76
|
history = history.order_by(sort.to_sym => order)
|
@@ -85,7 +85,7 @@ module RailsAdmin
|
|
85
85
|
|
86
86
|
def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
|
87
87
|
history = @version_class.where('association_chain.name' => model.model_name, 'association_chain.id' => object.id)
|
88
|
-
history = history.any_of(
|
88
|
+
history = history.any_of({action: /.*#{query}.*/}, {username: /.*#{query}.*/}) if query.present?
|
89
89
|
if sort
|
90
90
|
order = sort_reverse == "true" ? :desc : :asc
|
91
91
|
history = history.order_by(sort.to_sym => order)
|
@@ -105,4 +105,4 @@ end
|
|
105
105
|
|
106
106
|
RailsAdmin.add_extension(:mongoid_audit, RailsAdmin::Extensions::MongoidAudit, {
|
107
107
|
:auditing => true
|
108
|
-
})
|
108
|
+
})
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Audit
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
initializer "instantiate observers" do
|
5
|
+
config.after_initialize do
|
6
|
+
::Mongoid::Audit.tracker_class.add_observer( ::Mongoid::Audit::Sweeper.instance )
|
7
|
+
# install model observer and action controller filter
|
8
|
+
if defined?(ActionController) and defined?(ActionController::Base)
|
9
|
+
ActionController::Base.class_eval do
|
10
|
+
before_filter { |controller| ::Mongoid::Audit::Sweeper.instance.before(controller) }
|
11
|
+
after_filter { |controller| ::Mongoid::Audit::Sweeper.instance.after(controller) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -7,7 +7,7 @@ module Mongoid::Audit
|
|
7
7
|
scope_name = self.collection_name.to_s.singularize.to_sym
|
8
8
|
default_options = {
|
9
9
|
:on => :all,
|
10
|
-
:except => [
|
10
|
+
:except => [],
|
11
11
|
:modifier_field => :modifier,
|
12
12
|
:version_field => :version,
|
13
13
|
:scope => scope_name,
|
@@ -22,6 +22,7 @@ module Mongoid::Audit
|
|
22
22
|
# manually ensure _id, id, version will not be tracked in history
|
23
23
|
options[:except] = [options[:except]] unless options[:except].is_a? Array
|
24
24
|
options[:except] << options[:version_field]
|
25
|
+
options[:except] += [:created_at, :updated_at, :deleted_at, :c_at, :u_at]
|
25
26
|
options[:except] << "#{options[:modifier_field]}_id".to_sym
|
26
27
|
options[:except] += [:_id, :id]
|
27
28
|
options[:except] = options[:except].map(&:to_s).flatten.compact.uniq
|
@@ -34,7 +35,7 @@ module Mongoid::Audit
|
|
34
35
|
end
|
35
36
|
|
36
37
|
field options[:version_field].to_sym, :type => Integer
|
37
|
-
belongs_to options[:modifier_field].to_sym, :class_name => Mongoid::Audit.modifier_class_name
|
38
|
+
belongs_to options[:modifier_field].to_sym, :class_name => Mongoid::Audit.modifier_class_name, inverse_of: nil
|
38
39
|
|
39
40
|
include MyInstanceMethods
|
40
41
|
extend SingletonMethods
|
@@ -206,6 +207,7 @@ module Mongoid::Audit
|
|
206
207
|
self.send("#{history_trackable_options[:version_field]}=", current_version)
|
207
208
|
|
208
209
|
track = Mongoid::Audit.tracker_class.create!(history_tracker_attributes(:update).merge(:version => current_version, :action => "update", :trackable => self))
|
210
|
+
|
209
211
|
self.send("#{history_trackable_options[:modifier_field]}=", track.modifier)
|
210
212
|
|
211
213
|
clear_memoization
|
@@ -20,6 +20,24 @@ module Mongoid::Audit
|
|
20
20
|
Mongoid::Audit.tracker_class_name = self.name.tableize.singularize.to_sym
|
21
21
|
|
22
22
|
index({'association_chain.name' => 1, 'association_chain.id' => 1})
|
23
|
+
|
24
|
+
Mongoid::Interceptable::CALLBACKS.each do |callback|
|
25
|
+
callback_method = :"_notify_#{Mongoid::Audit.tracker_class_name}_#{callback}"
|
26
|
+
module_eval <<-RUBY, __FILE__, __LINE__+1
|
27
|
+
#{callback} #{callback_method.inspect}
|
28
|
+
def #{callback_method}(&block)
|
29
|
+
if "#{callback}".start_with?( 'around_' )
|
30
|
+
notify_observers(#{callback.inspect}, &block)
|
31
|
+
yield
|
32
|
+
else
|
33
|
+
notify_observers(#{callback.inspect}, &block)
|
34
|
+
true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
private #{callback_method.inspect}
|
38
|
+
RUBY
|
39
|
+
end
|
40
|
+
|
23
41
|
end
|
24
42
|
|
25
43
|
def undo!(modifier)
|
@@ -74,7 +92,6 @@ module Mongoid::Audit
|
|
74
92
|
@trackable_parent ||= trackable_parents_and_trackable[-2]
|
75
93
|
end
|
76
94
|
|
77
|
-
|
78
95
|
def affected
|
79
96
|
@affected ||= (modified.keys | original.keys).inject({}){ |h,k| h[k] =
|
80
97
|
trackable ? trackable.attributes[k] : modified[k]; h}
|
@@ -149,6 +166,5 @@ private
|
|
149
166
|
end while( !chain.empty? )
|
150
167
|
documents
|
151
168
|
end
|
152
|
-
|
153
169
|
end
|
154
170
|
end
|
data/mongoid-audit.gemspec
CHANGED
@@ -17,13 +17,14 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.
|
21
|
-
gem.
|
22
|
-
gem.
|
20
|
+
gem.add_dependency 'easy_diff', '~> 0.0.2'
|
21
|
+
gem.add_dependency 'mongoid', ['>= 4.0', '< 5.0']
|
22
|
+
gem.add_dependency 'rails-observers', '~> 0.1.2'
|
23
23
|
|
24
|
-
gem.add_development_dependency
|
25
|
-
gem.add_development_dependency
|
26
|
-
gem.add_development_dependency
|
27
|
-
gem.add_development_dependency
|
28
|
-
gem.add_development_dependency
|
24
|
+
gem.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
gem.add_development_dependency "rake"
|
26
|
+
gem.add_development_dependency "rspec"
|
27
|
+
gem.add_development_dependency "database_cleaner"
|
28
|
+
gem.add_development_dependency "coveralls"
|
29
|
+
gem.add_development_dependency "activesupport", '~> 4.0.0'
|
29
30
|
end
|
@@ -2,10 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
2
2
|
|
3
3
|
describe Mongoid::Audit do
|
4
4
|
before :all do
|
5
|
-
class HistoryTracker
|
6
|
-
include Mongoid::Audit::Tracker
|
7
|
-
end
|
8
|
-
|
9
5
|
class Post
|
10
6
|
include Mongoid::Document
|
11
7
|
include Mongoid::Timestamps
|
@@ -14,6 +10,7 @@ describe Mongoid::Audit do
|
|
14
10
|
field :title
|
15
11
|
field :body
|
16
12
|
field :rating
|
13
|
+
field :views
|
17
14
|
|
18
15
|
embeds_many :comments
|
19
16
|
embeds_one :section
|
@@ -50,6 +47,8 @@ describe Mongoid::Audit do
|
|
50
47
|
include Mongoid::Timestamps
|
51
48
|
include Mongoid::Audit::Trackable
|
52
49
|
|
50
|
+
has_and_belongs_to_many :own_restaurants, class_name: 'Restaurant', inverse_of: :owners
|
51
|
+
|
53
52
|
field :email
|
54
53
|
field :name
|
55
54
|
track_history :except => [:email]
|
@@ -65,6 +64,17 @@ describe Mongoid::Audit do
|
|
65
64
|
field :title
|
66
65
|
track_history :on => [:title], :scope => :post, :track_create => true, :track_destroy => true, :modifier_field => :updated_by
|
67
66
|
end
|
67
|
+
|
68
|
+
class Restaurant
|
69
|
+
include Mongoid::Document
|
70
|
+
include Mongoid::Audit::Trackable
|
71
|
+
|
72
|
+
has_and_belongs_to_many :owners, class_name: 'User', inverse_of: nil
|
73
|
+
|
74
|
+
field :title
|
75
|
+
|
76
|
+
track_history :on => [:title], :track_create => true, :track_destroy => true
|
77
|
+
end
|
68
78
|
end
|
69
79
|
|
70
80
|
before :each do
|
@@ -322,6 +332,15 @@ describe Mongoid::Audit do
|
|
322
332
|
|
323
333
|
it "should assign modifier" do
|
324
334
|
@section.update_attributes(:title => "Business", :modifier => @another_user)
|
335
|
+
@post.reload
|
336
|
+
# just workaround strange mongoid #last bug for now
|
337
|
+
@post.history_tracks.to_a[-1].modifier.should == @another_user
|
338
|
+
end
|
339
|
+
|
340
|
+
it "should update modifier" do
|
341
|
+
@section.update_attributes(:title => 'Technology 2', :modifier => @user)
|
342
|
+
@section.update_attributes(:title => "Business", :modifier => @another_user)
|
343
|
+
@post.reload
|
325
344
|
@post.history_tracks.last.modifier.should == @another_user
|
326
345
|
end
|
327
346
|
end
|
@@ -361,9 +380,9 @@ describe Mongoid::Audit do
|
|
361
380
|
|
362
381
|
describe "embedded with cascading callbacks" do
|
363
382
|
before(:each) do
|
364
|
-
Mongoid.instantiate_observers
|
365
383
|
Thread.current[:mongoid_history_sweeper_controller] = self
|
366
|
-
self.stub!(:current_user).and_return @user
|
384
|
+
# self.stub!(:current_user).and_return @user
|
385
|
+
allow(self).to receive(:current_user).and_return(@user)
|
367
386
|
@tag_foo = @post.tags.create(:title => "foo", :updated_by => @user)
|
368
387
|
@tag_bar = @post.tags.create(:title => "bar")
|
369
388
|
end
|
@@ -546,7 +565,101 @@ describe Mongoid::Audit do
|
|
546
565
|
@comment.redo! @user, :last => 1
|
547
566
|
@comment.title.should == "Test5"
|
548
567
|
end
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
describe "duplicate relations" do
|
572
|
+
it "should save correct relation" do
|
573
|
+
lambda{ Restaurant.create!( title: 'test', modifier_id: @user.id ) }.should_not raise_error
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
|
578
|
+
describe "rails admin history" do
|
579
|
+
before :each do
|
580
|
+
@restaurant = Restaurant.create!( title: 'test', modifier_id: @user.id )
|
581
|
+
@adapter = ::RailsAdmin::Extensions::MongoidAudit::AuditingAdapter.new( nil )
|
582
|
+
@model = Struct.new( :model_name ).new( 'Restaurant' )
|
583
|
+
end
|
584
|
+
|
585
|
+
it "should list all records from history table" do
|
586
|
+
query = ''
|
587
|
+
|
588
|
+
items = @adapter.listing_for_model(@model, query, false, 'false', true, 1, 10)
|
589
|
+
|
590
|
+
expect( items ).not_to be_empty
|
591
|
+
|
592
|
+
item = items.first
|
593
|
+
|
594
|
+
expect( item.message ).to eq 'create Restaurant [title = test]'
|
595
|
+
expect( item.table ).to eq 'Restaurant'
|
596
|
+
expect( item.username ).to eq @user.email
|
597
|
+
expect( item.item ).to eq @restaurant.id
|
598
|
+
|
599
|
+
query = nil
|
600
|
+
|
601
|
+
items = @adapter.listing_for_model(@model, query, false, 'false', true, 1, 10)
|
602
|
+
|
603
|
+
expect( items ).not_to be_empty
|
604
|
+
|
605
|
+
item = items.first
|
606
|
+
|
607
|
+
expect( item.message ).to eq 'create Restaurant [title = test]'
|
608
|
+
end
|
609
|
+
|
610
|
+
it "should list records from history table specified by query" do
|
611
|
+
query = 'create'
|
612
|
+
|
613
|
+
items = @adapter.listing_for_model(@model, query, false, 'false', true, 1, 10)
|
614
|
+
|
615
|
+
expect( items ).not_to be_empty
|
616
|
+
|
617
|
+
item = items.first
|
618
|
+
|
619
|
+
expect( item.message ).to eq 'create Restaurant [title = test]'
|
620
|
+
expect( item.table ).to eq 'Restaurant'
|
621
|
+
expect( item.username ).to eq @user.email
|
622
|
+
expect( item.item ).to eq @restaurant.id
|
623
|
+
end
|
624
|
+
|
625
|
+
it "should list records from history table specified by item" do
|
626
|
+
query = ''
|
627
|
+
|
628
|
+
items = @adapter.listing_for_object(@model, @restaurant, query, false, 'false', true, 1, 10)
|
629
|
+
|
630
|
+
expect( items ).not_to be_empty
|
631
|
+
|
632
|
+
item = items.first
|
633
|
+
|
634
|
+
expect( item.message ).to eq 'create Restaurant [title = test]'
|
635
|
+
expect( item.table ).to eq 'Restaurant'
|
636
|
+
expect( item.username ).to eq @user.email
|
637
|
+
expect( item.item ).to eq @restaurant.id
|
638
|
+
|
639
|
+
query = nil
|
640
|
+
|
641
|
+
items = @adapter.listing_for_object(@model, @restaurant, query, false, 'false', true, 1, 10)
|
642
|
+
|
643
|
+
expect( items ).not_to be_empty
|
644
|
+
|
645
|
+
item = items.first
|
646
|
+
|
647
|
+
expect( item.message ).to eq 'create Restaurant [title = test]'
|
648
|
+
end
|
649
|
+
|
650
|
+
it "should list records from history table specified by item and query" do
|
651
|
+
query = 'create'
|
652
|
+
|
653
|
+
items = @adapter.listing_for_object(@model, @restaurant, query, false, 'false', true, 1, 10)
|
654
|
+
|
655
|
+
expect( items ).not_to be_empty
|
656
|
+
|
657
|
+
item = items.first
|
549
658
|
|
659
|
+
expect( item.message ).to eq 'create Restaurant [title = test]'
|
660
|
+
expect( item.table ).to eq 'Restaurant'
|
661
|
+
expect( item.username ).to eq @user.email
|
662
|
+
expect( item.item ).to eq @restaurant.id
|
550
663
|
end
|
551
664
|
end
|
552
665
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
3
|
+
|
4
|
+
ENV["RAILS_ENV"] ||= 'test'
|
5
|
+
|
1
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
7
|
|
3
8
|
require 'rubygems'
|
@@ -6,6 +11,11 @@ require 'active_support/core_ext'
|
|
6
11
|
require 'mongoid'
|
7
12
|
require 'database_cleaner'
|
8
13
|
|
14
|
+
module RailsAdmin
|
15
|
+
def self.add_extension(*args); end
|
16
|
+
end
|
17
|
+
require "mongoid-audit/rails_admin"
|
18
|
+
|
9
19
|
Bundler.require
|
10
20
|
|
11
21
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
|
@@ -13,4 +23,6 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
|
|
13
23
|
end
|
14
24
|
|
15
25
|
require 'mongoid-audit'
|
16
|
-
|
26
|
+
class HistoryTracker
|
27
|
+
include Mongoid::Audit::Tracker
|
28
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
DatabaseCleaner[:mongoid].strategy = :truncation
|
2
|
+
|
1
3
|
RSpec.configure do |config|
|
2
|
-
config.before :
|
3
|
-
DatabaseCleaner.
|
4
|
+
config.before :each do
|
5
|
+
DatabaseCleaner.start
|
4
6
|
end
|
7
|
+
|
5
8
|
config.after :each do
|
6
9
|
DatabaseCleaner.clean
|
7
10
|
end
|
8
|
-
end
|
9
|
-
|
11
|
+
end
|
data/spec/support/mongoid.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Mongoid.configure do |config|
|
2
|
+
config.connect_to "mongoid-audit-test"
|
3
|
+
end
|
3
4
|
|
4
5
|
RSpec.configure do |config|
|
5
6
|
config.before :each do
|
6
|
-
|
7
|
+
HistoryTracker.add_observer(::Mongoid::Audit::Sweeper.instance)
|
7
8
|
end
|
8
|
-
config.
|
9
|
+
config.backtrace_exclusion_patterns = [
|
9
10
|
# /\/lib\d*\/ruby\//,
|
10
11
|
# /bin\//,
|
11
12
|
# /gems/,
|
12
13
|
# /spec\/spec_helper\.rb/,
|
13
14
|
/lib\/rspec\/(core|expectations|matchers|mocks)/
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
|
15
|
+
]
|
16
|
+
end
|
data/spec/trackable_spec.rb
CHANGED
@@ -12,6 +12,11 @@ describe Mongoid::Audit::Trackable do
|
|
12
12
|
Mongoid::Audit.trackable_class_options = nil
|
13
13
|
end
|
14
14
|
|
15
|
+
def compare( option )
|
16
|
+
option[:except].sort! unless option[:except].nil?
|
17
|
+
option.should == @expected_option
|
18
|
+
end
|
19
|
+
|
15
20
|
it "should have #track_history" do
|
16
21
|
MyModel.should respond_to :track_history
|
17
22
|
end
|
@@ -24,6 +29,8 @@ describe Mongoid::Audit::Trackable do
|
|
24
29
|
|
25
30
|
describe "#track_history" do
|
26
31
|
before :each do
|
32
|
+
Object.send(:remove_const, :MyModel) if Object.constants.include?(:MyModel)
|
33
|
+
|
27
34
|
class MyModel
|
28
35
|
include Mongoid::Document
|
29
36
|
include Mongoid::Audit::Trackable
|
@@ -35,7 +42,7 @@ describe Mongoid::Audit::Trackable do
|
|
35
42
|
:modifier_field => :modifier,
|
36
43
|
:version_field => :version,
|
37
44
|
:scope => :my_model,
|
38
|
-
:except => ["created_at", "updated_at", "deleted_at", "c_at", "u_at", "
|
45
|
+
:except => ["version", "created_at", "updated_at", "deleted_at", "c_at", "u_at", "modifier_id", "_id", "id"].sort,
|
39
46
|
:track_create => false,
|
40
47
|
:track_update => true,
|
41
48
|
:track_destroy => false,
|
@@ -47,7 +54,7 @@ describe Mongoid::Audit::Trackable do
|
|
47
54
|
end
|
48
55
|
|
49
56
|
it "should have default options" do
|
50
|
-
Mongoid::Audit.trackable_class_options[:my_model]
|
57
|
+
compare( Mongoid::Audit.trackable_class_options[:my_model] )
|
51
58
|
end
|
52
59
|
|
53
60
|
it "should define callback function #track_update" do
|
@@ -63,21 +70,29 @@ describe Mongoid::Audit::Trackable do
|
|
63
70
|
end
|
64
71
|
|
65
72
|
it "should define #history_trackable_options" do
|
66
|
-
MyModel.history_trackable_options
|
73
|
+
compare( MyModel.history_trackable_options )
|
67
74
|
end
|
68
75
|
|
69
76
|
context "sub-model" do
|
70
77
|
before :each do
|
78
|
+
Object.send(:remove_const, :MyModel) if Object.constants.include?(:MyModel)
|
79
|
+
Object.send(:remove_const, :MySubModel) if Object.constants.include?(:MySubModel)
|
80
|
+
|
81
|
+
class MyModel
|
82
|
+
include Mongoid::Document
|
83
|
+
include Mongoid::Audit::Trackable
|
84
|
+
track_history
|
85
|
+
end
|
71
86
|
class MySubModel < MyModel
|
72
87
|
end
|
73
88
|
end
|
74
89
|
|
75
90
|
it "should have default options" do
|
76
|
-
Mongoid::Audit.trackable_class_options[:my_model]
|
91
|
+
compare( Mongoid::Audit.trackable_class_options[:my_model] )
|
77
92
|
end
|
78
93
|
|
79
94
|
it "should define #history_trackable_options" do
|
80
|
-
MySubModel.history_trackable_options
|
95
|
+
compare( MySubModel.history_trackable_options )
|
81
96
|
end
|
82
97
|
end
|
83
98
|
|
@@ -114,8 +129,6 @@ describe Mongoid::Audit::Trackable do
|
|
114
129
|
MyModel2.new.track_history?.should == true
|
115
130
|
end
|
116
131
|
end
|
117
|
-
|
118
132
|
end
|
119
|
-
|
120
133
|
end
|
121
134
|
end
|
data/spec/tracker_spec.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,77 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-audit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gleb Tv
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: easy_diff
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.0.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.0.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: mongoid
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '4.0'
|
34
|
+
- - <
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '5.0'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
41
|
- - '>='
|
39
42
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
43
|
+
version: '4.0'
|
44
|
+
- - <
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '5.0'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: rails-observers
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
51
|
- - ~>
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.1.
|
53
|
+
version: 0.1.2
|
48
54
|
type: :runtime
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
58
|
- - ~>
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.1.
|
60
|
+
version: 0.1.2
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: bundler
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.3'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.3'
|
55
75
|
- !ruby/object:Gem::Dependency
|
56
76
|
name: rake
|
57
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,44 +90,44 @@ dependencies:
|
|
70
90
|
name: rspec
|
71
91
|
requirement: !ruby/object:Gem::Requirement
|
72
92
|
requirements:
|
73
|
-
- -
|
93
|
+
- - '>='
|
74
94
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
95
|
+
version: '0'
|
76
96
|
type: :development
|
77
97
|
prerelease: false
|
78
98
|
version_requirements: !ruby/object:Gem::Requirement
|
79
99
|
requirements:
|
80
|
-
- -
|
100
|
+
- - '>='
|
81
101
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
102
|
+
version: '0'
|
83
103
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
104
|
+
name: database_cleaner
|
85
105
|
requirement: !ruby/object:Gem::Requirement
|
86
106
|
requirements:
|
87
107
|
- - '>='
|
88
108
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
109
|
+
version: '0'
|
90
110
|
type: :development
|
91
111
|
prerelease: false
|
92
112
|
version_requirements: !ruby/object:Gem::Requirement
|
93
113
|
requirements:
|
94
114
|
- - '>='
|
95
115
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
116
|
+
version: '0'
|
97
117
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
118
|
+
name: coveralls
|
99
119
|
requirement: !ruby/object:Gem::Requirement
|
100
120
|
requirements:
|
101
|
-
- -
|
121
|
+
- - '>='
|
102
122
|
- !ruby/object:Gem::Version
|
103
|
-
version: 0
|
123
|
+
version: '0'
|
104
124
|
type: :development
|
105
125
|
prerelease: false
|
106
126
|
version_requirements: !ruby/object:Gem::Requirement
|
107
127
|
requirements:
|
108
|
-
- -
|
128
|
+
- - '>='
|
109
129
|
- !ruby/object:Gem::Version
|
110
|
-
version: 0
|
130
|
+
version: '0'
|
111
131
|
- !ruby/object:Gem::Dependency
|
112
132
|
name: activesupport
|
113
133
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,6 +149,7 @@ executables: []
|
|
129
149
|
extensions: []
|
130
150
|
extra_rdoc_files: []
|
131
151
|
files:
|
152
|
+
- .coveralls.yml
|
132
153
|
- .gitignore
|
133
154
|
- .rspec
|
134
155
|
- .ruby-gemset
|
@@ -139,9 +160,11 @@ files:
|
|
139
160
|
- README.md
|
140
161
|
- Rakefile
|
141
162
|
- config/mongoid.yml
|
163
|
+
- gemfiles/mongoid-4.0.gemfile
|
142
164
|
- lib/mongoid-audit.rb
|
143
165
|
- lib/mongoid-audit/mongoid_observer.rb
|
144
166
|
- lib/mongoid-audit/rails_admin.rb
|
167
|
+
- lib/mongoid-audit/railtie.rb
|
145
168
|
- lib/mongoid-audit/sweeper.rb
|
146
169
|
- lib/mongoid-audit/trackable.rb
|
147
170
|
- lib/mongoid-audit/tracker.rb
|
@@ -172,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
195
|
version: '0'
|
173
196
|
requirements: []
|
174
197
|
rubyforge_project:
|
175
|
-
rubygems_version: 2.
|
198
|
+
rubygems_version: 2.1.10
|
176
199
|
signing_key:
|
177
200
|
specification_version: 4
|
178
201
|
summary: Easily track model change history and mantain audit log with mongoid
|