mongoid-history 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.document +5 -5
- data/.rspec +1 -1
- data/.rubocop.yml +75 -0
- data/.travis.yml +7 -5
- data/CHANGELOG.md +94 -76
- data/Gemfile +16 -12
- data/LICENSE.txt +20 -20
- data/README.md +309 -307
- data/Rakefile +37 -37
- data/VERSION +1 -1
- data/lib/mongoid/history/trackable.rb +337 -338
- data/lib/mongoid/history/tracker.rb +215 -220
- data/lib/mongoid/history.rb +25 -27
- data/lib/mongoid-history.rb +9 -10
- data/mongoid-history.gemspec +80 -68
- data/spec/integration/integration_spec.rb +788 -754
- data/spec/integration/multi_relation_spec.rb +53 -50
- data/spec/integration/nested_embedded_documents_spec.rb +77 -77
- data/spec/integration/subclasses_spec.rb +29 -0
- data/spec/spec_helper.rb +10 -11
- data/spec/support/mongoid.rb +11 -15
- data/spec/support/mongoid_history.rb +13 -15
- data/spec/trackable_spec.rb +240 -240
- data/spec/tracker_spec.rb +10 -10
- metadata +65 -30
- data/lib/mongoid/history/sweeper.rb +0 -44
@@ -1,50 +1,53 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Tracker do
|
4
|
-
before :all do
|
5
|
-
class Model
|
6
|
-
include Mongoid::Document
|
7
|
-
include Mongoid::History::Trackable
|
8
|
-
|
9
|
-
field :name, type: String
|
10
|
-
belongs_to :user, inverse_of: :models
|
11
|
-
has_and_belongs_to_many :external_users, class_name: "User", inverse_of: :external_models
|
12
|
-
|
13
|
-
track_history :
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
class User
|
23
|
-
include Mongoid::Document
|
24
|
-
has_many :models, :
|
25
|
-
has_and_belongs_to_many :external_model, class_name: "Model", inverse_of: :external_users
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should be possible to undo when having multiple relations to modifier class" do
|
30
|
-
user = User.new
|
31
|
-
user.save
|
32
|
-
|
33
|
-
model = Model.new
|
34
|
-
model.name = "Foo"
|
35
|
-
model.user = user
|
36
|
-
model.save!
|
37
|
-
|
38
|
-
model.name = "Bar"
|
39
|
-
model.save!
|
40
|
-
|
41
|
-
model.undo! user
|
42
|
-
model.name.should == "Foo"
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
before :all do
|
5
|
+
class Model
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::History::Trackable
|
8
|
+
|
9
|
+
field :name, type: String
|
10
|
+
belongs_to :user, inverse_of: :models
|
11
|
+
has_and_belongs_to_many :external_users, class_name: "User", inverse_of: :external_models
|
12
|
+
|
13
|
+
track_history on: [:name, :user, :external_user_ids], # track title and body fields only, default is :all
|
14
|
+
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
|
15
|
+
modifier_field_inverse_of: nil, # no inverse modifier relationship
|
16
|
+
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
|
17
|
+
track_create: false, # track document creation, default is false
|
18
|
+
track_update: true, # track document updates, default is true
|
19
|
+
track_destroy: false # track document destruction, default is false
|
20
|
+
end
|
21
|
+
|
22
|
+
class User
|
23
|
+
include Mongoid::Document
|
24
|
+
has_many :models, dependent: :destroy, inverse_of: :user
|
25
|
+
has_and_belongs_to_many :external_model, class_name: "Model", inverse_of: :external_users
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be possible to undo when having multiple relations to modifier class" do
|
30
|
+
user = User.new
|
31
|
+
user.save
|
32
|
+
|
33
|
+
model = Model.new
|
34
|
+
model.name = "Foo"
|
35
|
+
model.user = user
|
36
|
+
model.save!
|
37
|
+
|
38
|
+
model.name = "Bar"
|
39
|
+
model.save!
|
40
|
+
|
41
|
+
model.undo! user
|
42
|
+
model.name.should == "Foo"
|
43
|
+
|
44
|
+
model.redo! user, 1
|
45
|
+
model.name.should == "Bar"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should track foreign key relations" do
|
49
|
+
Model.tracked_field?(:external_user_ids).should be_true
|
50
|
+
Model.tracked_field?(:user).should be_true
|
51
|
+
Model.tracked_field?(:user_id).should be_true
|
52
|
+
end
|
53
|
+
end
|
@@ -1,77 +1,77 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Tracker do
|
4
|
-
before :all do
|
5
|
-
class Model
|
6
|
-
include Mongoid::Document
|
7
|
-
include Mongoid::History::Trackable
|
8
|
-
|
9
|
-
field :name, type: String
|
10
|
-
belongs_to :user, inverse_of: :models
|
11
|
-
embeds_many :embones
|
12
|
-
|
13
|
-
track_history :
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
class Embone
|
22
|
-
include Mongoid::Document
|
23
|
-
include Mongoid::History::Trackable
|
24
|
-
|
25
|
-
field :name
|
26
|
-
embeds_many :embtwos, store_as: :ems
|
27
|
-
embedded_in :model
|
28
|
-
|
29
|
-
track_history :
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
class Embtwo
|
39
|
-
include Mongoid::Document
|
40
|
-
include Mongoid::History::Trackable
|
41
|
-
|
42
|
-
field :name
|
43
|
-
embedded_in :embone
|
44
|
-
|
45
|
-
track_history :
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
class User
|
55
|
-
include Mongoid::Document
|
56
|
-
has_many :models, :
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should be able to track history for nested embedded documents" do
|
61
|
-
user = User.new
|
62
|
-
user.save!
|
63
|
-
|
64
|
-
model = Model.new(
|
65
|
-
model.user = user
|
66
|
-
model.save!
|
67
|
-
embedded1 = model.embones.create(
|
68
|
-
embedded2 = embedded1.embtwos.create(
|
69
|
-
|
70
|
-
embedded2.name = "a new name"
|
71
|
-
embedded2.save!
|
72
|
-
|
73
|
-
model.history_tracks.first.undo! user
|
74
|
-
embedded1.reload.name.should == "e1name"
|
75
|
-
embedded2.reload.name.should == "e2name"
|
76
|
-
end
|
77
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
before :all do
|
5
|
+
class Model
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::History::Trackable
|
8
|
+
|
9
|
+
field :name, type: String
|
10
|
+
belongs_to :user, inverse_of: :models
|
11
|
+
embeds_many :embones
|
12
|
+
|
13
|
+
track_history on: :all, # track title and body fields only, default is :all
|
14
|
+
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
|
15
|
+
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
|
16
|
+
track_create: false, # track document creation, default is false
|
17
|
+
track_update: true, # track document updates, default is true
|
18
|
+
track_destroy: false # track document destruction, default is false
|
19
|
+
end
|
20
|
+
|
21
|
+
class Embone
|
22
|
+
include Mongoid::Document
|
23
|
+
include Mongoid::History::Trackable
|
24
|
+
|
25
|
+
field :name
|
26
|
+
embeds_many :embtwos, store_as: :ems
|
27
|
+
embedded_in :model
|
28
|
+
|
29
|
+
track_history on: :all, # track title and body fields only, default is :all
|
30
|
+
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
|
31
|
+
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
|
32
|
+
track_create: false, # track document creation, default is false
|
33
|
+
track_update: true, # track document updates, default is true
|
34
|
+
track_destroy: false, # track document destruction, default is false
|
35
|
+
scope: :model
|
36
|
+
end
|
37
|
+
|
38
|
+
class Embtwo
|
39
|
+
include Mongoid::Document
|
40
|
+
include Mongoid::History::Trackable
|
41
|
+
|
42
|
+
field :name
|
43
|
+
embedded_in :embone
|
44
|
+
|
45
|
+
track_history on: :all, # track title and body fields only, default is :all
|
46
|
+
modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
|
47
|
+
version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
|
48
|
+
track_create: false, # track document creation, default is false
|
49
|
+
track_update: true, # track document updates, default is true
|
50
|
+
track_destroy: false, # track document destruction, default is false
|
51
|
+
scope: :model
|
52
|
+
end
|
53
|
+
|
54
|
+
class User
|
55
|
+
include Mongoid::Document
|
56
|
+
has_many :models, dependent: :destroy, inverse_of: :user
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should be able to track history for nested embedded documents" do
|
61
|
+
user = User.new
|
62
|
+
user.save!
|
63
|
+
|
64
|
+
model = Model.new(name: "m1name")
|
65
|
+
model.user = user
|
66
|
+
model.save!
|
67
|
+
embedded1 = model.embones.create(name: "e1name")
|
68
|
+
embedded2 = embedded1.embtwos.create(name: "e2name")
|
69
|
+
|
70
|
+
embedded2.name = "a new name"
|
71
|
+
embedded2.save!
|
72
|
+
|
73
|
+
model.history_tracks.first.undo! user
|
74
|
+
embedded1.reload.name.should == "e1name"
|
75
|
+
embedded2.reload.name.should == "e2name"
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
before :all do
|
5
|
+
class Element
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::Timestamps
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
|
10
|
+
field :body
|
11
|
+
|
12
|
+
track_history on: [:body], track_create: true, track_update: true, track_destroy: true
|
13
|
+
end
|
14
|
+
|
15
|
+
class Prompt < Element
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "tracks subclass create and update" do
|
20
|
+
prompt = Prompt.new
|
21
|
+
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
22
|
+
expect { prompt.update_attributes!(body: "one") }.to change(Tracker, :count).by(1)
|
23
|
+
prompt.undo!
|
24
|
+
prompt.body.should be_blank
|
25
|
+
prompt.redo! nil, 2
|
26
|
+
prompt.body.should == "one"
|
27
|
+
expect { prompt.destroy }.to change(Tracker, :count).by(1)
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'bundler/setup'
|
5
|
-
|
6
|
-
Bundler.require :default, :test
|
7
|
-
|
8
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
-
|
10
|
-
require 'mongoid-history'
|
11
|
-
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
Bundler.require :default, :test
|
7
|
+
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
9
|
+
|
10
|
+
require 'mongoid-history'
|
data/spec/support/mongoid.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
|
-
Mongoid.configure do |config|
|
2
|
-
config.connect_to('mongoid_history_test')
|
3
|
-
end
|
4
|
-
|
5
|
-
RSpec.configure do |config|
|
6
|
-
config.
|
7
|
-
Mongoid.
|
8
|
-
end
|
9
|
-
|
10
|
-
config.
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
config.backtrace_clean_patterns = [ /lib\/rspec\/(core|expectations|matchers|mocks)/ ]
|
15
|
-
end
|
1
|
+
Mongoid.configure do |config|
|
2
|
+
config.connect_to('mongoid_history_test')
|
3
|
+
end
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.after(:each) do
|
7
|
+
Mongoid.purge!
|
8
|
+
end
|
9
|
+
|
10
|
+
config.backtrace_exclusion_patterns = [%r{lib\/rspec\/(core|expectations|matchers|mocks)}]
|
11
|
+
end
|
@@ -1,15 +1,13 @@
|
|
1
|
-
RSpec.configure do |config|
|
2
|
-
config.before :each do
|
3
|
-
class Tracker
|
4
|
-
include Mongoid::History::Tracker
|
5
|
-
end
|
6
|
-
Mongoid::History.tracker_class_name = "Tracker"
|
7
|
-
Mongoid::History.modifier_class_name = "User"
|
8
|
-
end
|
9
|
-
config.after :each do
|
10
|
-
Mongoid::History.tracker_class_name = nil
|
11
|
-
Mongoid::History.trackable_class_options = nil
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.before :each do
|
3
|
+
class Tracker
|
4
|
+
include Mongoid::History::Tracker
|
5
|
+
end
|
6
|
+
Mongoid::History.tracker_class_name = "Tracker"
|
7
|
+
Mongoid::History.modifier_class_name = "User"
|
8
|
+
end
|
9
|
+
config.after :each do
|
10
|
+
Mongoid::History.tracker_class_name = nil
|
11
|
+
Mongoid::History.trackable_class_options = nil
|
12
|
+
end
|
13
|
+
end
|