mongoid-history 0.8.3 → 0.8.5
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 +4 -4
- data/.coveralls.yml +1 -1
- data/.document +5 -5
- data/.github/workflows/test.yml +72 -0
- data/.gitignore +46 -46
- data/.rspec +2 -2
- data/.rubocop.yml +6 -6
- data/.rubocop_todo.yml +99 -99
- data/CHANGELOG.md +173 -163
- data/CONTRIBUTING.md +117 -118
- data/Dangerfile +1 -1
- data/Gemfile +49 -40
- data/LICENSE.txt +20 -20
- data/README.md +609 -608
- data/RELEASING.md +66 -67
- data/Rakefile +24 -24
- data/UPGRADING.md +53 -53
- data/lib/mongoid/history/attributes/base.rb +72 -72
- data/lib/mongoid/history/attributes/create.rb +45 -45
- data/lib/mongoid/history/attributes/destroy.rb +34 -34
- data/lib/mongoid/history/attributes/update.rb +104 -104
- data/lib/mongoid/history/options.rb +177 -177
- data/lib/mongoid/history/trackable.rb +588 -583
- data/lib/mongoid/history/tracker.rb +247 -247
- data/lib/mongoid/history/version.rb +5 -5
- data/lib/mongoid/history.rb +77 -77
- data/lib/mongoid-history.rb +1 -1
- data/mongoid-history.gemspec +25 -25
- data/perf/benchmark_modified_attributes_for_create.rb +65 -65
- data/perf/gc_suite.rb +21 -21
- data/spec/integration/embedded_in_polymorphic_spec.rb +112 -112
- data/spec/integration/integration_spec.rb +976 -976
- data/spec/integration/multi_relation_spec.rb +47 -47
- data/spec/integration/multiple_trackers_spec.rb +68 -68
- data/spec/integration/nested_embedded_documents_spec.rb +64 -64
- data/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb +124 -124
- data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +115 -115
- data/spec/integration/subclasses_spec.rb +47 -47
- data/spec/integration/track_history_order_spec.rb +84 -84
- data/spec/integration/validation_failure_spec.rb +76 -76
- data/spec/spec_helper.rb +32 -30
- data/spec/support/error_helpers.rb +7 -0
- data/spec/support/mongoid.rb +11 -11
- data/spec/support/mongoid_history.rb +12 -12
- data/spec/unit/attributes/base_spec.rb +141 -141
- data/spec/unit/attributes/create_spec.rb +342 -342
- data/spec/unit/attributes/destroy_spec.rb +228 -228
- data/spec/unit/attributes/update_spec.rb +342 -342
- data/spec/unit/callback_options_spec.rb +165 -165
- data/spec/unit/embedded_methods_spec.rb +87 -87
- data/spec/unit/history_spec.rb +58 -58
- data/spec/unit/my_instance_methods_spec.rb +555 -555
- data/spec/unit/options_spec.rb +365 -365
- data/spec/unit/singleton_methods_spec.rb +406 -406
- data/spec/unit/store/default_store_spec.rb +11 -11
- data/spec/unit/store/request_store_spec.rb +13 -13
- data/spec/unit/trackable_spec.rb +1057 -987
- data/spec/unit/tracker_spec.rb +190 -190
- metadata +9 -7
- data/.travis.yml +0 -36
@@ -1,115 +1,115 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Tracker do
|
4
|
-
before :each do
|
5
|
-
class ModelOne
|
6
|
-
include Mongoid::Document
|
7
|
-
include Mongoid::History::Trackable
|
8
|
-
|
9
|
-
field :name, type: String
|
10
|
-
belongs_to :user
|
11
|
-
embeds_one :one_embedded, as: :embedable
|
12
|
-
|
13
|
-
track_history
|
14
|
-
end
|
15
|
-
|
16
|
-
class ModelTwo
|
17
|
-
include Mongoid::Document
|
18
|
-
include Mongoid::History::Trackable
|
19
|
-
|
20
|
-
field :name, type: String
|
21
|
-
belongs_to :user
|
22
|
-
embeds_one :one_embedded, as: :embedable
|
23
|
-
|
24
|
-
track_history
|
25
|
-
end
|
26
|
-
|
27
|
-
class OneEmbedded
|
28
|
-
include Mongoid::Document
|
29
|
-
include Mongoid::History::Trackable
|
30
|
-
|
31
|
-
field :name
|
32
|
-
embeds_many :embedded_twos, store_as: :ems
|
33
|
-
embedded_in :embedable, polymorphic: true
|
34
|
-
|
35
|
-
track_history scope: %i[model_one model_two]
|
36
|
-
end
|
37
|
-
|
38
|
-
class EmbeddedTwo
|
39
|
-
include Mongoid::Document
|
40
|
-
include Mongoid::History::Trackable
|
41
|
-
|
42
|
-
field :name
|
43
|
-
embedded_in :one_embedded
|
44
|
-
|
45
|
-
track_history scope: %i[model_one model_two]
|
46
|
-
end
|
47
|
-
|
48
|
-
class User
|
49
|
-
include Mongoid::Document
|
50
|
-
|
51
|
-
has_many :model_ones
|
52
|
-
has_many :model_twos
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
after :each do
|
57
|
-
Object.send(:remove_const, :ModelOne)
|
58
|
-
Object.send(:remove_const, :ModelTwo)
|
59
|
-
Object.send(:remove_const, :OneEmbedded)
|
60
|
-
Object.send(:remove_const, :EmbeddedTwo)
|
61
|
-
Object.send(:remove_const, :User)
|
62
|
-
end
|
63
|
-
|
64
|
-
let (:user) { User.create! }
|
65
|
-
|
66
|
-
it 'tracks history for nested embedded documents with polymorphic relations' do
|
67
|
-
user = User.create!
|
68
|
-
|
69
|
-
model_one = user.model_ones.build(name: 'model_one', modifier: user)
|
70
|
-
model_one.save!
|
71
|
-
model_one.build_one_embedded(name: 'model_one_one_embedded', modifier: user).save!
|
72
|
-
expect(model_one.history_tracks.count).to eq(2)
|
73
|
-
expect(model_one.one_embedded.history_tracks.count).to eq(1)
|
74
|
-
|
75
|
-
model_one.reload
|
76
|
-
model_one.one_embedded.update_attributes!(name: 'model_one_embedded_one!')
|
77
|
-
expect(model_one.history_tracks.count).to eq(3)
|
78
|
-
expect(model_one.one_embedded.history_tracks.count).to eq(2)
|
79
|
-
expect(model_one.history_tracks.last.action).to eq('update')
|
80
|
-
|
81
|
-
model_one.build_one_embedded(name: 'Lorem ipsum', modifier: user).save!
|
82
|
-
expect(model_one.history_tracks.count).to eq(4)
|
83
|
-
expect(model_one.one_embedded.history_tracks.count).to eq(1)
|
84
|
-
expect(model_one.one_embedded.history_tracks.last.action).to eq('create')
|
85
|
-
expect(model_one.one_embedded.history_tracks.last.association_chain.last['name']).to eq('one_embedded')
|
86
|
-
|
87
|
-
embedded_one1 = model_one.one_embedded.embedded_twos.create!(name: 'model_one_one_embedded_1', modifier: user)
|
88
|
-
expect(model_one.history_tracks.count).to eq(5)
|
89
|
-
expect(model_one.one_embedded.history_tracks.count).to eq(2)
|
90
|
-
expect(embedded_one1.history_tracks.count).to eq(1)
|
91
|
-
|
92
|
-
model_two = user.model_twos.build(name: 'model_two', modifier: user)
|
93
|
-
model_two.save!
|
94
|
-
model_two.build_one_embedded(name: 'model_two_one_embedded', modifier: user).save!
|
95
|
-
expect(model_two.history_tracks.count).to eq(2)
|
96
|
-
expect(model_two.one_embedded.history_tracks.count).to eq(1)
|
97
|
-
|
98
|
-
model_two.reload
|
99
|
-
model_two.one_embedded.update_attributes!(name: 'model_two_one_embedded!')
|
100
|
-
expect(model_two.history_tracks.count).to eq(3)
|
101
|
-
expect(model_two.one_embedded.history_tracks.count).to eq(2)
|
102
|
-
expect(model_two.history_tracks.last.action).to eq('update')
|
103
|
-
|
104
|
-
model_two.build_one_embedded(name: 'Lorem ipsum', modifier: user).save!
|
105
|
-
expect(model_two.history_tracks.count).to eq(4)
|
106
|
-
expect(model_two.one_embedded.history_tracks.count).to eq(1)
|
107
|
-
expect(model_two.one_embedded.history_tracks.last.action).to eq('create')
|
108
|
-
expect(model_two.one_embedded.history_tracks.last.association_chain.last['name']).to eq('one_embedded')
|
109
|
-
|
110
|
-
embedded_one2 = model_two.one_embedded.embedded_twos.create!(name: 'model_two_one_embedded_1', modifier: user)
|
111
|
-
expect(model_two.history_tracks.count).to eq(5)
|
112
|
-
expect(model_two.one_embedded.history_tracks.count).to eq(2)
|
113
|
-
expect(embedded_one2.history_tracks.count).to eq(1)
|
114
|
-
end
|
115
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
before :each do
|
5
|
+
class ModelOne
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::History::Trackable
|
8
|
+
|
9
|
+
field :name, type: String
|
10
|
+
belongs_to :user
|
11
|
+
embeds_one :one_embedded, as: :embedable
|
12
|
+
|
13
|
+
track_history
|
14
|
+
end
|
15
|
+
|
16
|
+
class ModelTwo
|
17
|
+
include Mongoid::Document
|
18
|
+
include Mongoid::History::Trackable
|
19
|
+
|
20
|
+
field :name, type: String
|
21
|
+
belongs_to :user
|
22
|
+
embeds_one :one_embedded, as: :embedable
|
23
|
+
|
24
|
+
track_history
|
25
|
+
end
|
26
|
+
|
27
|
+
class OneEmbedded
|
28
|
+
include Mongoid::Document
|
29
|
+
include Mongoid::History::Trackable
|
30
|
+
|
31
|
+
field :name
|
32
|
+
embeds_many :embedded_twos, store_as: :ems
|
33
|
+
embedded_in :embedable, polymorphic: true
|
34
|
+
|
35
|
+
track_history scope: %i[model_one model_two]
|
36
|
+
end
|
37
|
+
|
38
|
+
class EmbeddedTwo
|
39
|
+
include Mongoid::Document
|
40
|
+
include Mongoid::History::Trackable
|
41
|
+
|
42
|
+
field :name
|
43
|
+
embedded_in :one_embedded
|
44
|
+
|
45
|
+
track_history scope: %i[model_one model_two]
|
46
|
+
end
|
47
|
+
|
48
|
+
class User
|
49
|
+
include Mongoid::Document
|
50
|
+
|
51
|
+
has_many :model_ones
|
52
|
+
has_many :model_twos
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
after :each do
|
57
|
+
Object.send(:remove_const, :ModelOne)
|
58
|
+
Object.send(:remove_const, :ModelTwo)
|
59
|
+
Object.send(:remove_const, :OneEmbedded)
|
60
|
+
Object.send(:remove_const, :EmbeddedTwo)
|
61
|
+
Object.send(:remove_const, :User)
|
62
|
+
end
|
63
|
+
|
64
|
+
let (:user) { User.create! }
|
65
|
+
|
66
|
+
it 'tracks history for nested embedded documents with polymorphic relations' do
|
67
|
+
user = User.create!
|
68
|
+
|
69
|
+
model_one = user.model_ones.build(name: 'model_one', modifier: user)
|
70
|
+
model_one.save!
|
71
|
+
model_one.build_one_embedded(name: 'model_one_one_embedded', modifier: user).save!
|
72
|
+
expect(model_one.history_tracks.count).to eq(2)
|
73
|
+
expect(model_one.one_embedded.history_tracks.count).to eq(1)
|
74
|
+
|
75
|
+
model_one.reload
|
76
|
+
model_one.one_embedded.update_attributes!(name: 'model_one_embedded_one!')
|
77
|
+
expect(model_one.history_tracks.count).to eq(3)
|
78
|
+
expect(model_one.one_embedded.history_tracks.count).to eq(2)
|
79
|
+
expect(model_one.history_tracks.last.action).to eq('update')
|
80
|
+
|
81
|
+
model_one.build_one_embedded(name: 'Lorem ipsum', modifier: user).save!
|
82
|
+
expect(model_one.history_tracks.count).to eq(4)
|
83
|
+
expect(model_one.one_embedded.history_tracks.count).to eq(1)
|
84
|
+
expect(model_one.one_embedded.history_tracks.last.action).to eq('create')
|
85
|
+
expect(model_one.one_embedded.history_tracks.last.association_chain.last['name']).to eq('one_embedded')
|
86
|
+
|
87
|
+
embedded_one1 = model_one.one_embedded.embedded_twos.create!(name: 'model_one_one_embedded_1', modifier: user)
|
88
|
+
expect(model_one.history_tracks.count).to eq(5)
|
89
|
+
expect(model_one.one_embedded.history_tracks.count).to eq(2)
|
90
|
+
expect(embedded_one1.history_tracks.count).to eq(1)
|
91
|
+
|
92
|
+
model_two = user.model_twos.build(name: 'model_two', modifier: user)
|
93
|
+
model_two.save!
|
94
|
+
model_two.build_one_embedded(name: 'model_two_one_embedded', modifier: user).save!
|
95
|
+
expect(model_two.history_tracks.count).to eq(2)
|
96
|
+
expect(model_two.one_embedded.history_tracks.count).to eq(1)
|
97
|
+
|
98
|
+
model_two.reload
|
99
|
+
model_two.one_embedded.update_attributes!(name: 'model_two_one_embedded!')
|
100
|
+
expect(model_two.history_tracks.count).to eq(3)
|
101
|
+
expect(model_two.one_embedded.history_tracks.count).to eq(2)
|
102
|
+
expect(model_two.history_tracks.last.action).to eq('update')
|
103
|
+
|
104
|
+
model_two.build_one_embedded(name: 'Lorem ipsum', modifier: user).save!
|
105
|
+
expect(model_two.history_tracks.count).to eq(4)
|
106
|
+
expect(model_two.one_embedded.history_tracks.count).to eq(1)
|
107
|
+
expect(model_two.one_embedded.history_tracks.last.action).to eq('create')
|
108
|
+
expect(model_two.one_embedded.history_tracks.last.association_chain.last['name']).to eq('one_embedded')
|
109
|
+
|
110
|
+
embedded_one2 = model_two.one_embedded.embedded_twos.create!(name: 'model_two_one_embedded_1', modifier: user)
|
111
|
+
expect(model_two.history_tracks.count).to eq(5)
|
112
|
+
expect(model_two.one_embedded.history_tracks.count).to eq(2)
|
113
|
+
expect(embedded_one2.history_tracks.count).to eq(1)
|
114
|
+
end
|
115
|
+
end
|
@@ -1,47 +1,47 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Tracker do
|
4
|
-
before :each do
|
5
|
-
class Element
|
6
|
-
include Mongoid::Document
|
7
|
-
include Mongoid::Timestamps
|
8
|
-
include Mongoid::History::Trackable
|
9
|
-
|
10
|
-
track_history
|
11
|
-
|
12
|
-
field :body
|
13
|
-
|
14
|
-
# force preparation of options
|
15
|
-
history_trackable_options
|
16
|
-
end
|
17
|
-
|
18
|
-
class Prompt < Element
|
19
|
-
field :head
|
20
|
-
end
|
21
|
-
|
22
|
-
class User
|
23
|
-
include Mongoid::Document
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
after :each do
|
28
|
-
Object.send(:remove_const, :Element)
|
29
|
-
Object.send(:remove_const, :Prompt)
|
30
|
-
Object.send(:remove_const, :User)
|
31
|
-
end
|
32
|
-
|
33
|
-
let(:user) { User.create! }
|
34
|
-
|
35
|
-
it 'tracks subclass create and update' do
|
36
|
-
prompt = Prompt.new(modifier: user)
|
37
|
-
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
38
|
-
expect { prompt.update_attributes!(body: 'one', head: 'two') }.to change(Tracker, :count).by(1)
|
39
|
-
prompt.undo! user
|
40
|
-
expect(prompt.body).to be_blank
|
41
|
-
expect(prompt.head).to be_blank
|
42
|
-
prompt.redo! user, 2
|
43
|
-
expect(prompt.body).to eq('one')
|
44
|
-
expect(prompt.head).to eq('two')
|
45
|
-
expect { prompt.destroy }.to change(Tracker, :count).by(1)
|
46
|
-
end
|
47
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
before :each do
|
5
|
+
class Element
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::Timestamps
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
|
10
|
+
track_history
|
11
|
+
|
12
|
+
field :body
|
13
|
+
|
14
|
+
# force preparation of options
|
15
|
+
history_trackable_options
|
16
|
+
end
|
17
|
+
|
18
|
+
class Prompt < Element
|
19
|
+
field :head
|
20
|
+
end
|
21
|
+
|
22
|
+
class User
|
23
|
+
include Mongoid::Document
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
after :each do
|
28
|
+
Object.send(:remove_const, :Element)
|
29
|
+
Object.send(:remove_const, :Prompt)
|
30
|
+
Object.send(:remove_const, :User)
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:user) { User.create! }
|
34
|
+
|
35
|
+
it 'tracks subclass create and update' do
|
36
|
+
prompt = Prompt.new(modifier: user)
|
37
|
+
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
38
|
+
expect { prompt.update_attributes!(body: 'one', head: 'two') }.to change(Tracker, :count).by(1)
|
39
|
+
prompt.undo! user
|
40
|
+
expect(prompt.body).to be_blank
|
41
|
+
expect(prompt.head).to be_blank
|
42
|
+
prompt.redo! user, 2
|
43
|
+
expect(prompt.body).to eq('one')
|
44
|
+
expect(prompt.head).to eq('two')
|
45
|
+
expect { prompt.destroy }.to change(Tracker, :count).by(1)
|
46
|
+
end
|
47
|
+
end
|
@@ -1,84 +1,84 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Tracker do
|
4
|
-
context 'when track_history not called' do
|
5
|
-
before :each do
|
6
|
-
class NotModel
|
7
|
-
include Mongoid::Document
|
8
|
-
include Mongoid::History::Trackable
|
9
|
-
|
10
|
-
field :foo
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
after :each do
|
15
|
-
Object.send(:remove_const, :NotModel)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should not track fields' do
|
19
|
-
expect(NotModel.respond_to?(:tracked?)).to be false
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'boefore field' do
|
24
|
-
before :each do
|
25
|
-
class InsideBeforeModel
|
26
|
-
include Mongoid::Document
|
27
|
-
include Mongoid::History::Trackable
|
28
|
-
|
29
|
-
track_history on: :fields
|
30
|
-
|
31
|
-
field :foo
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
after :each do
|
36
|
-
Object.send(:remove_const, :InsideBeforeModel)
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should track fields' do
|
40
|
-
expect(InsideBeforeModel.tracked?(:foo)).to be true
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'when track_history called inside class and after fields' do
|
45
|
-
before :each do
|
46
|
-
class InsideAfterModel
|
47
|
-
include Mongoid::Document
|
48
|
-
include Mongoid::History::Trackable
|
49
|
-
|
50
|
-
field :foo
|
51
|
-
|
52
|
-
track_history on: :fields
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
after :each do
|
57
|
-
Object.send(:remove_const, :InsideAfterModel)
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'should track fields' do
|
61
|
-
expect(InsideAfterModel.tracked?(:foo)).to be true
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'when track_history called outside class' do
|
66
|
-
before :each do
|
67
|
-
class OutsideModel
|
68
|
-
include Mongoid::Document
|
69
|
-
include Mongoid::History::Trackable
|
70
|
-
|
71
|
-
field :foo
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
after :each do
|
76
|
-
Object.send(:remove_const, :OutsideModel)
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'should track fields' do
|
80
|
-
OutsideModel.track_history on: :fields
|
81
|
-
expect(OutsideModel.tracked?(:foo)).to be true
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
context 'when track_history not called' do
|
5
|
+
before :each do
|
6
|
+
class NotModel
|
7
|
+
include Mongoid::Document
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
|
10
|
+
field :foo
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
after :each do
|
15
|
+
Object.send(:remove_const, :NotModel)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should not track fields' do
|
19
|
+
expect(NotModel.respond_to?(:tracked?)).to be false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'boefore field' do
|
24
|
+
before :each do
|
25
|
+
class InsideBeforeModel
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::History::Trackable
|
28
|
+
|
29
|
+
track_history on: :fields
|
30
|
+
|
31
|
+
field :foo
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
after :each do
|
36
|
+
Object.send(:remove_const, :InsideBeforeModel)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should track fields' do
|
40
|
+
expect(InsideBeforeModel.tracked?(:foo)).to be true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when track_history called inside class and after fields' do
|
45
|
+
before :each do
|
46
|
+
class InsideAfterModel
|
47
|
+
include Mongoid::Document
|
48
|
+
include Mongoid::History::Trackable
|
49
|
+
|
50
|
+
field :foo
|
51
|
+
|
52
|
+
track_history on: :fields
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
after :each do
|
57
|
+
Object.send(:remove_const, :InsideAfterModel)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should track fields' do
|
61
|
+
expect(InsideAfterModel.tracked?(:foo)).to be true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when track_history called outside class' do
|
66
|
+
before :each do
|
67
|
+
class OutsideModel
|
68
|
+
include Mongoid::Document
|
69
|
+
include Mongoid::History::Trackable
|
70
|
+
|
71
|
+
field :foo
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
after :each do
|
76
|
+
Object.send(:remove_const, :OutsideModel)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should track fields' do
|
80
|
+
OutsideModel.track_history on: :fields
|
81
|
+
expect(OutsideModel.tracked?(:foo)).to be true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -1,76 +1,76 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Mongoid::History::Tracker do
|
4
|
-
before :each do
|
5
|
-
class Element
|
6
|
-
include Mongoid::Document
|
7
|
-
include Mongoid::Timestamps
|
8
|
-
include Mongoid::History::Trackable
|
9
|
-
|
10
|
-
field :title
|
11
|
-
field :body
|
12
|
-
|
13
|
-
validates :title, presence: true
|
14
|
-
|
15
|
-
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
16
|
-
has_many :items, dependent: :restrict_with_exception
|
17
|
-
else
|
18
|
-
has_many :items, dependent: :restrict
|
19
|
-
end
|
20
|
-
|
21
|
-
track_history on: [:body]
|
22
|
-
end
|
23
|
-
|
24
|
-
class Item
|
25
|
-
include Mongoid::Document
|
26
|
-
include Mongoid::Timestamps
|
27
|
-
|
28
|
-
belongs_to :element
|
29
|
-
end
|
30
|
-
|
31
|
-
class Prompt < Element
|
32
|
-
end
|
33
|
-
|
34
|
-
class User
|
35
|
-
include Mongoid::Document
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
after :each do
|
40
|
-
Object.send(:remove_const, :Element)
|
41
|
-
Object.send(:remove_const, :Item)
|
42
|
-
Object.send(:remove_const, :Prompt)
|
43
|
-
Object.send(:remove_const, :User)
|
44
|
-
end
|
45
|
-
|
46
|
-
let(:user) { User.create! }
|
47
|
-
|
48
|
-
it 'does not track delete when parent class validation fails' do
|
49
|
-
prompt = Prompt.new(title: 'first', modifier: user)
|
50
|
-
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
51
|
-
expect do
|
52
|
-
expect { prompt.update_attributes!(title: nil, body: 'one') }
|
53
|
-
.to raise_error(Mongoid::Errors::Validations)
|
54
|
-
end.to change(Tracker, :count).by(0)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'does not track delete when parent class restrict dependency fails' do
|
58
|
-
prompt = Prompt.new(title: 'first', modifier: user)
|
59
|
-
prompt.items << Item.new
|
60
|
-
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
61
|
-
expect(prompt.version).to eq(1)
|
62
|
-
expect do
|
63
|
-
expect { prompt.destroy }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
64
|
-
end.to change(Tracker, :count).by(0)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'does not track delete when restrict dependency fails' do
|
68
|
-
elem = Element.new(title: 'first', modifier: user)
|
69
|
-
elem.items << Item.new
|
70
|
-
expect { elem.save! }.to change(Tracker, :count).by(1)
|
71
|
-
expect(elem.version).to eq(1)
|
72
|
-
expect do
|
73
|
-
expect { elem.destroy }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
74
|
-
end.to change(Tracker, :count).by(0)
|
75
|
-
end
|
76
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Tracker do
|
4
|
+
before :each do
|
5
|
+
class Element
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::Timestamps
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
|
10
|
+
field :title
|
11
|
+
field :body
|
12
|
+
|
13
|
+
validates :title, presence: true
|
14
|
+
|
15
|
+
if Mongoid::Compatibility::Version.mongoid7_or_newer?
|
16
|
+
has_many :items, dependent: :restrict_with_exception
|
17
|
+
else
|
18
|
+
has_many :items, dependent: :restrict
|
19
|
+
end
|
20
|
+
|
21
|
+
track_history on: [:body]
|
22
|
+
end
|
23
|
+
|
24
|
+
class Item
|
25
|
+
include Mongoid::Document
|
26
|
+
include Mongoid::Timestamps
|
27
|
+
|
28
|
+
belongs_to :element
|
29
|
+
end
|
30
|
+
|
31
|
+
class Prompt < Element
|
32
|
+
end
|
33
|
+
|
34
|
+
class User
|
35
|
+
include Mongoid::Document
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
after :each do
|
40
|
+
Object.send(:remove_const, :Element)
|
41
|
+
Object.send(:remove_const, :Item)
|
42
|
+
Object.send(:remove_const, :Prompt)
|
43
|
+
Object.send(:remove_const, :User)
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:user) { User.create! }
|
47
|
+
|
48
|
+
it 'does not track delete when parent class validation fails' do
|
49
|
+
prompt = Prompt.new(title: 'first', modifier: user)
|
50
|
+
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
51
|
+
expect do
|
52
|
+
expect { prompt.update_attributes!(title: nil, body: 'one') }
|
53
|
+
.to raise_error(Mongoid::Errors::Validations)
|
54
|
+
end.to change(Tracker, :count).by(0)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'does not track delete when parent class restrict dependency fails' do
|
58
|
+
prompt = Prompt.new(title: 'first', modifier: user)
|
59
|
+
prompt.items << Item.new
|
60
|
+
expect { prompt.save! }.to change(Tracker, :count).by(1)
|
61
|
+
expect(prompt.version).to eq(1)
|
62
|
+
expect do
|
63
|
+
expect { prompt.destroy }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
64
|
+
end.to change(Tracker, :count).by(0)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'does not track delete when restrict dependency fails' do
|
68
|
+
elem = Element.new(title: 'first', modifier: user)
|
69
|
+
elem.items << Item.new
|
70
|
+
expect { elem.save! }.to change(Tracker, :count).by(1)
|
71
|
+
expect(elem.version).to eq(1)
|
72
|
+
expect do
|
73
|
+
expect { elem.destroy }.to raise_error(Mongoid::Errors::DeleteRestriction)
|
74
|
+
end.to change(Tracker, :count).by(0)
|
75
|
+
end
|
76
|
+
end
|