mongoid-history 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +22 -18
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +4 -0
  5. data/CONTRIBUTING.md +4 -4
  6. data/Gemfile +8 -6
  7. data/README.md +2 -12
  8. data/UPGRADING.md +20 -1
  9. data/lib/mongoid/history.rb +8 -4
  10. data/lib/mongoid/history/attributes/base.rb +2 -2
  11. data/lib/mongoid/history/attributes/create.rb +2 -2
  12. data/lib/mongoid/history/attributes/update.rb +2 -2
  13. data/lib/mongoid/history/options.rb +11 -20
  14. data/lib/mongoid/history/trackable.rb +71 -56
  15. data/lib/mongoid/history/tracker.rb +8 -5
  16. data/lib/mongoid/history/version.rb +1 -1
  17. data/spec/integration/embedded_in_polymorphic_spec.rb +26 -49
  18. data/spec/integration/integration_spec.rb +132 -120
  19. data/spec/integration/multi_relation_spec.rb +14 -20
  20. data/spec/integration/multiple_trackers_spec.rb +35 -38
  21. data/spec/integration/nested_embedded_documents_spec.rb +31 -51
  22. data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +64 -76
  23. data/spec/integration/subclasses_spec.rb +17 -5
  24. data/spec/integration/track_history_order_spec.rb +59 -27
  25. data/spec/integration/validation_failure_spec.rb +21 -8
  26. data/spec/spec_helper.rb +6 -1
  27. data/spec/unit/attributes/base_spec.rb +17 -26
  28. data/spec/unit/attributes/create_spec.rb +152 -125
  29. data/spec/unit/attributes/destroy_spec.rb +68 -58
  30. data/spec/unit/attributes/update_spec.rb +71 -50
  31. data/spec/unit/callback_options_spec.rb +36 -30
  32. data/spec/unit/embedded_methods_spec.rb +42 -24
  33. data/spec/unit/history_spec.rb +12 -10
  34. data/spec/unit/my_instance_methods_spec.rb +191 -121
  35. data/spec/unit/options_spec.rb +49 -26
  36. data/spec/unit/singleton_methods_spec.rb +156 -88
  37. data/spec/unit/trackable_spec.rb +254 -156
  38. data/spec/unit/tracker_spec.rb +81 -54
  39. metadata +2 -2
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History::Tracker do
4
- before :all do
4
+ before :each do
5
5
  class Element
6
6
  include Mongoid::Document
7
7
  include Mongoid::Timestamps
@@ -9,20 +9,32 @@ describe Mongoid::History::Tracker do
9
9
 
10
10
  field :body
11
11
 
12
- track_history on: [:body], track_create: true, track_update: true, track_destroy: true
12
+ track_history on: [:body]
13
13
  end
14
14
 
15
15
  class Prompt < Element
16
16
  end
17
+
18
+ class User
19
+ include Mongoid::Document
20
+ end
17
21
  end
18
22
 
23
+ after :each do
24
+ Object.send(:remove_const, :Element)
25
+ Object.send(:remove_const, :Prompt)
26
+ Object.send(:remove_const, :User)
27
+ end
28
+
29
+ let(:user) { User.create! }
30
+
19
31
  it 'tracks subclass create and update' do
20
- prompt = Prompt.new
32
+ prompt = Prompt.new(modifier: user)
21
33
  expect { prompt.save! }.to change(Tracker, :count).by(1)
22
34
  expect { prompt.update_attributes!(body: 'one') }.to change(Tracker, :count).by(1)
23
- prompt.undo!
35
+ prompt.undo! user
24
36
  expect(prompt.body).to be_blank
25
- prompt.redo! nil, 2
37
+ prompt.redo! user, 2
26
38
  expect(prompt.body).to eq('one')
27
39
  expect { prompt.destroy }.to change(Tracker, :count).by(1)
28
40
  end
@@ -1,52 +1,84 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History::Tracker do
4
- it 'should not track fields when track_history not called' do
5
- class NotModel
6
- include Mongoid::Document
7
- include Mongoid::History::Trackable
4
+ context 'when track_history not called' do
5
+ before :each do
6
+ class NotModel
7
+ include Mongoid::Document
8
+ include Mongoid::History::Trackable
8
9
 
9
- field :foo
10
+ field :foo
11
+ end
10
12
  end
11
13
 
12
- expect(NotModel.respond_to?(:tracked?)).to be false
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
13
21
  end
14
22
 
15
- it 'should track fields when track_history called inside class and before fields' do
16
- class InsideBeforeModel
17
- include Mongoid::Document
18
- include Mongoid::History::Trackable
23
+ context 'boefore field' do
24
+ before :each do
25
+ class InsideBeforeModel
26
+ include Mongoid::Document
27
+ include Mongoid::History::Trackable
19
28
 
20
- track_history on: :fields
29
+ track_history on: :fields
21
30
 
22
- field :foo
31
+ field :foo
32
+ end
23
33
  end
24
34
 
25
- expect(InsideBeforeModel.tracked?(:foo)).to be true
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
26
42
  end
27
43
 
28
- it 'should track fields when track_history called inside class and after fields' do
29
- class InsideAfterModel
30
- include Mongoid::Document
31
- include Mongoid::History::Trackable
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
32
49
 
33
- field :foo
50
+ field :foo
34
51
 
35
- track_history on: :fields
52
+ track_history on: :fields
53
+ end
36
54
  end
37
55
 
38
- expect(InsideAfterModel.tracked?(:foo)).to be true
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
39
63
  end
40
64
 
41
- it 'should track fields when track_history called outside class' do
42
- class OutsideModel
43
- include Mongoid::Document
44
- include Mongoid::History::Trackable
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
45
70
 
46
- field :foo
71
+ field :foo
72
+ end
47
73
  end
48
74
 
49
- OutsideModel.track_history on: :fields
50
- expect(OutsideModel.tracked?(:foo)).to be true
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
51
83
  end
52
84
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History::Tracker do
4
- before :all do
4
+ before :each do
5
5
  class Element
6
6
  include Mongoid::Document
7
7
  include Mongoid::Timestamps
@@ -12,9 +12,13 @@ describe Mongoid::History::Tracker do
12
12
 
13
13
  validates :title, presence: true
14
14
 
15
- has_many :items, dependent: :restrict
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
16
20
 
17
- track_history on: [:body], track_create: true, track_update: true, track_destroy: true
21
+ track_history on: [:body]
18
22
  end
19
23
 
20
24
  class Item
@@ -27,13 +31,22 @@ describe Mongoid::History::Tracker do
27
31
  class Prompt < Element
28
32
  end
29
33
 
30
- @persisted_history_options = Mongoid::History.trackable_class_options
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)
31
44
  end
32
45
 
33
- before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
46
+ let(:user) { User.create! }
34
47
 
35
48
  it 'does not track delete when parent class validation fails' do
36
- prompt = Prompt.new(title: 'first')
49
+ prompt = Prompt.new(title: 'first', modifier: user)
37
50
  expect { prompt.save! }.to change(Tracker, :count).by(1)
38
51
  expect do
39
52
  expect { prompt.update_attributes!(title: nil, body: 'one') }
@@ -42,7 +55,7 @@ describe Mongoid::History::Tracker do
42
55
  end
43
56
 
44
57
  it 'does not track delete when parent class restrict dependency fails' do
45
- prompt = Prompt.new(title: 'first')
58
+ prompt = Prompt.new(title: 'first', modifier: user)
46
59
  prompt.items << Item.new
47
60
  expect { prompt.save! }.to change(Tracker, :count).by(1)
48
61
  expect(prompt.version).to eq(1)
@@ -52,7 +65,7 @@ describe Mongoid::History::Tracker do
52
65
  end
53
66
 
54
67
  it 'does not track delete when restrict dependency fails' do
55
- elem = Element.new(title: 'first')
68
+ elem = Element.new(title: 'first', modifier: user)
56
69
  elem.items << Item.new
57
70
  expect { elem.save! }.to change(Tracker, :count).by(1)
58
71
  expect(elem.version).to eq(1)
@@ -17,9 +17,14 @@ require 'mongoid/history'
17
17
 
18
18
  RSpec.configure do |config|
19
19
  config.raise_errors_for_deprecations!
20
+
20
21
  config.before :all do
21
22
  Mongoid.logger.level = Logger::INFO
22
- Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6?
23
+ Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?
23
24
  Mongoid.belongs_to_required_by_default = false if Mongoid::Compatibility::Version.mongoid6?
24
25
  end
26
+
27
+ config.before :each do
28
+ Mongoid::History.reset!
29
+ end
25
30
  end
@@ -1,31 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History::Attributes::Base do
4
- let(:model_one) do
5
- Class.new do
4
+ before :each do
5
+ class ModelOne
6
6
  include Mongoid::Document
7
7
  include Mongoid::History::Trackable
8
+
8
9
  field :foo
9
10
  field :b, as: :bar
10
- def self.name
11
- 'ModelOne'
12
- end
13
11
  end
14
- end
15
12
 
16
- before :all do
17
13
  class ModelTwo
18
14
  include Mongoid::Document
15
+
19
16
  field :foo
20
17
  field :goo
21
18
  end
22
19
  end
23
20
 
24
- after :all do
21
+ after :each do
22
+ Object.send(:remove_const, :ModelOne)
25
23
  Object.send(:remove_const, :ModelTwo)
26
24
  end
27
25
 
28
- let(:obj_one) { model_one.new }
26
+ let(:obj_one) { ModelOne.new }
29
27
  let(:base) { described_class.new(obj_one) }
30
28
  subject { base }
31
29
 
@@ -37,7 +35,7 @@ describe Mongoid::History::Attributes::Base do
37
35
 
38
36
  describe '#trackable_class' do
39
37
  subject { base.send(:trackable_class) }
40
- it { is_expected.to eq model_one }
38
+ it { is_expected.to eq ModelOne }
41
39
  end
42
40
 
43
41
  describe '#aliased_fields' do
@@ -47,8 +45,7 @@ describe Mongoid::History::Attributes::Base do
47
45
 
48
46
  describe '#changes_method' do
49
47
  before(:each) do
50
- model_one.instance_variable_set(:@history_trackable_options, nil)
51
- model_one.track_history changes_method: :my_changes
48
+ ModelOne.track_history changes_method: :my_changes
52
49
  end
53
50
  subject { base.send(:changes_method) }
54
51
  it { is_expected.to eq :my_changes }
@@ -56,8 +53,7 @@ describe Mongoid::History::Attributes::Base do
56
53
 
57
54
  describe '#changes' do
58
55
  before(:each) do
59
- model_one.instance_variable_set(:@history_trackable_options, nil)
60
- model_one.track_history
56
+ ModelOne.track_history
61
57
  allow(obj_one).to receive(:changes) { { 'foo' => ['Foo', 'Foo-new'] } }
62
58
  end
63
59
  subject { base.send(:changes) }
@@ -65,15 +61,11 @@ describe Mongoid::History::Attributes::Base do
65
61
  end
66
62
 
67
63
  describe '#format_field' do
68
- before(:each) do
69
- model_one.instance_variable_set(:@history_trackable_options, nil)
70
- end
71
-
72
64
  subject { base.send(:format_field, :bar, 'foo') }
73
65
 
74
66
  context 'when formatted via string' do
75
67
  before do
76
- model_one.track_history format: { bar: '*%s*' }
68
+ ModelOne.track_history format: { bar: '*%s*' }
77
69
  end
78
70
 
79
71
  it { is_expected.to eq '*foo*' }
@@ -81,7 +73,7 @@ describe Mongoid::History::Attributes::Base do
81
73
 
82
74
  context 'when formatted via proc' do
83
75
  before do
84
- model_one.track_history format: { bar: ->(v) { v * 2 } }
76
+ ModelOne.track_history format: { bar: ->(v) { v * 2 } }
85
77
  end
86
78
 
87
79
  it { is_expected.to eq 'foofoo' }
@@ -89,7 +81,7 @@ describe Mongoid::History::Attributes::Base do
89
81
 
90
82
  context 'when not formatted' do
91
83
  before do
92
- model_one.track_history
84
+ ModelOne.track_history
93
85
  end
94
86
 
95
87
  it { is_expected.to eq 'foo' }
@@ -100,15 +92,14 @@ describe Mongoid::History::Attributes::Base do
100
92
  let(:model_two) { ModelTwo.new(foo: :bar, goo: :baz) }
101
93
 
102
94
  before :each do
103
- model_one.instance_variable_set(:@history_trackable_options, nil)
104
- model_one.send(relation_type, :model_two)
95
+ ModelOne.send(relation_type, :model_two)
105
96
  end
106
97
 
107
98
  subject { base.send("format_#{relation_type}_relation", :model_two, model_two.attributes) }
108
99
 
109
100
  context 'with permitted attributes' do
110
101
  before do
111
- model_one.track_history on: { model_two: %i[foo] }
102
+ ModelOne.track_history on: { model_two: %i[foo] }
112
103
  end
113
104
 
114
105
  it 'should select only permitted attributes' do
@@ -119,7 +110,7 @@ describe Mongoid::History::Attributes::Base do
119
110
 
120
111
  context 'with attributes formatted via string' do
121
112
  before do
122
- model_one.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: '&%s&' } }
113
+ ModelOne.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: '&%s&' } }
123
114
  end
124
115
 
125
116
  it 'should select obfuscate permitted attributes' do
@@ -130,7 +121,7 @@ describe Mongoid::History::Attributes::Base do
130
121
 
131
122
  context 'with attributes formatted via proc' do
132
123
  before do
133
- model_one.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: ->(v) { v.to_s * 2 } } }
124
+ ModelOne.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: ->(v) { v.to_s * 2 } } }
134
125
  end
135
126
 
136
127
  it 'should select obfuscate permitted attributes' do
@@ -1,20 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::History::Attributes::Create do
4
- let(:model_one) do
5
- Class.new do
4
+ before :each do
5
+ class ModelOne
6
6
  include Mongoid::Document
7
7
  include Mongoid::History::Trackable
8
+
8
9
  store_in collection: :model_ones
9
10
  field :foo
10
11
  field :b, as: :bar
11
- def self.name
12
- 'ModelOne'
13
- end
12
+
13
+ track_history on: :foo
14
14
  end
15
15
  end
16
16
 
17
- let(:obj_one) { model_one.new }
17
+ after :each do
18
+ Object.send(:remove_const, :ModelOne)
19
+ end
20
+
18
21
  let(:base) { described_class.new(obj_one) }
19
22
  subject { base }
20
23
 
@@ -22,294 +25,318 @@ describe Mongoid::History::Attributes::Create do
22
25
  subject { base.attributes }
23
26
 
24
27
  describe 'fields' do
25
- before(:each) do
26
- model_one.instance_variable_set(:@history_trackable_options, nil)
27
- model_one.track_history on: :foo
28
- end
29
- let(:obj_one) { model_one.new(foo: 'Foo', bar: 'Bar') }
28
+ let(:obj_one) { ModelOne.new }
29
+ let(:obj_one) { ModelOne.new(foo: 'Foo', bar: 'Bar') }
30
30
  it { is_expected.to eq('foo' => [nil, 'Foo']) }
31
31
  end
32
32
 
33
33
  describe '#insert_embeds_one_changes' do
34
34
  context 'when untracked relation' do
35
- before(:all) do
36
- # Need class name constant
35
+ before :each do
37
36
  class ModelTwo
38
37
  include Mongoid::Document
39
38
  include Mongoid::History::Trackable
39
+
40
40
  store_in collection: :model_twos
41
- embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
41
+
42
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
43
+ embeds_one :emb_one_one
44
+ else
45
+ embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
46
+ end
47
+
48
+ track_history on: :fields
42
49
  end
43
50
 
44
51
  class EmbOneOne
45
52
  include Mongoid::Document
53
+
46
54
  field :em_bar
47
55
  embedded_in :model_one
48
56
  end
49
57
  end
50
58
 
51
- before(:each) do
52
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
53
- ModelTwo.track_history on: :fields
59
+ after :each do
60
+ Object.send(:remove_const, :ModelTwo)
61
+ Object.send(:remove_const, :EmbOneOne)
54
62
  end
55
63
 
56
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
57
- let(:emb_obj_one) { EmbOneOne.new(em_bar: 'Em-Bar') }
64
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
65
+ let(:emb_obj) { EmbOneOne.new(em_bar: 'Em-Bar') }
58
66
 
59
67
  it { is_expected.to eq({}) }
60
-
61
- after(:all) do
62
- Object.send(:remove_const, :ModelTwo)
63
- Object.send(:remove_const, :EmbOneOne)
64
- end
65
68
  end
66
69
 
67
70
  context 'when tracked relation' do
68
- before(:all) do
69
- # Need class name constant
71
+ before :each do
70
72
  class ModelTwo
71
73
  include Mongoid::Document
72
74
  include Mongoid::History::Trackable
75
+
73
76
  store_in collection: :model_twos
74
- embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
77
+
78
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
79
+ embeds_one :emb_one_one
80
+ else
81
+ embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
82
+ end
83
+
84
+ track_history on: :emb_one_one
75
85
  end
76
86
 
77
87
  class EmbOneOne
78
88
  include Mongoid::Document
89
+
79
90
  field :em_bar
80
91
  embedded_in :model_one
81
92
  end
82
93
  end
83
94
 
84
- before(:each) do
85
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
86
- ModelTwo.track_history on: :emb_one_one
87
- end
88
-
89
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
90
- let(:emb_obj_one) { EmbOneOne.new(em_bar: 'Em-Bar') }
91
-
92
- it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj_one._id, 'em_bar' => 'Em-Bar' }]) }
93
-
94
- after(:all) do
95
+ after :each do
95
96
  Object.send(:remove_const, :ModelTwo)
96
97
  Object.send(:remove_const, :EmbOneOne)
97
98
  end
99
+
100
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
101
+ let(:emb_obj) { EmbOneOne.new(em_bar: 'Em-Bar') }
102
+
103
+ it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj._id, 'em_bar' => 'Em-Bar' }]) }
98
104
  end
99
105
 
100
106
  context 'when paranoia_field without alias' do
101
- before(:all) do
102
- # Need class name constant
107
+ before :each do
103
108
  class ModelTwo
104
109
  include Mongoid::Document
105
110
  include Mongoid::History::Trackable
111
+
106
112
  store_in collection: :model_twos
107
- embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
113
+
114
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
115
+ embeds_one :emb_one_one
116
+ else
117
+ embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
118
+ end
119
+
120
+ track_history on: :emb_one_one
108
121
  end
109
122
 
110
123
  class EmbOneOne
111
124
  include Mongoid::Document
112
125
  include Mongoid::History::Trackable
126
+
113
127
  field :em_bar
114
128
  field :removed_at
129
+
115
130
  embedded_in :model_one
131
+
132
+ history_settings paranoia_field: :removed_at
116
133
  end
117
134
  end
118
135
 
119
- before(:each) do
120
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
121
- EmbOneOne.instance_variable_set(:@rackable_settings, nil)
122
- ModelTwo.track_history on: :emb_one_one
123
- EmbOneOne.history_settings paranoia_field: :removed_at
136
+ after :each do
137
+ Object.send(:remove_const, :ModelTwo)
138
+ Object.send(:remove_const, :EmbOneOne)
124
139
  end
125
140
 
126
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
127
- let(:emb_obj_one) { EmbOneOne.new(em_bar: 'Em-Bar', removed_at: Time.now) }
141
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
142
+ let(:emb_obj) { EmbOneOne.new(em_bar: 'Em-Bar', removed_at: Time.now) }
128
143
 
129
144
  it { is_expected.to eq({}) }
130
-
131
- after(:all) do
132
- Object.send(:remove_const, :ModelTwo)
133
- Object.send(:remove_const, :EmbOneOne)
134
- end
135
145
  end
136
146
 
137
147
  context 'when paranoia_field with alias' do
138
- before(:all) do
139
- # Need class name constant
148
+ before :each do
140
149
  class ModelTwo
141
150
  include Mongoid::Document
142
151
  include Mongoid::History::Trackable
152
+
143
153
  store_in collection: :model_twos
144
- embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
154
+
155
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
156
+ embeds_one :emb_one_one
157
+ else
158
+ embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
159
+ end
160
+
161
+ track_history on: :emb_one_one
145
162
  end
146
163
 
147
164
  class EmbOneOne
148
165
  include Mongoid::Document
149
166
  include Mongoid::History::Trackable
167
+
150
168
  field :em_bar
151
169
  field :rmvt, as: :removed_at
170
+
152
171
  embedded_in :model_one
172
+
173
+ history_settings paranoia_field: :removed_at
153
174
  end
154
175
  end
155
176
 
156
- before(:each) do
157
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
158
- EmbOneOne.instance_variable_set(:@rackable_settings, nil)
159
- ModelTwo.track_history on: :emb_one_one
160
- EmbOneOne.history_settings paranoia_field: :removed_at
177
+ after :each do
178
+ Object.send(:remove_const, :ModelTwo)
179
+ Object.send(:remove_const, :EmbOneOne)
161
180
  end
162
181
 
163
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
164
- let(:emb_obj_one) { EmbOneOne.new(em_bar: 'Em-Bar', removed_at: Time.now) }
182
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
183
+ let(:emb_obj) { EmbOneOne.new(em_bar: 'Em-Bar', removed_at: Time.now) }
165
184
 
166
185
  it { is_expected.to eq({}) }
167
-
168
- after(:all) do
169
- Object.send(:remove_const, :ModelTwo)
170
- Object.send(:remove_const, :EmbOneOne)
171
- end
172
186
  end
173
187
 
174
188
  context 'with permitted attributes' do
175
- before(:all) do
176
- # Need class name constant
189
+ before :each do
177
190
  class ModelTwo
178
191
  include Mongoid::Document
179
192
  include Mongoid::History::Trackable
193
+
180
194
  store_in collection: :model_twos
181
- embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
195
+
196
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
197
+ embeds_one :emb_one_one
198
+ else
199
+ embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne'
200
+ end
201
+
202
+ track_history on: [{ emb_one_one: :em_bar }]
182
203
  end
183
204
 
184
205
  class EmbOneOne
185
206
  include Mongoid::Document
186
207
  include Mongoid::History::Trackable
208
+
187
209
  field :em_foo
188
210
  field :em_bar
211
+
189
212
  embedded_in :model_one
190
213
  end
191
214
  end
192
215
 
193
- before(:each) do
194
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
195
- ModelTwo.track_history on: [{ emb_one_one: :em_bar }]
196
- end
197
-
198
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
199
- let(:emb_obj_one) { EmbOneOne.new(em_foo: 'Em-Foo', em_bar: 'Em-Bar') }
200
-
201
- it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj_one._id, 'em_bar' => 'Em-Bar' }]) }
202
-
203
- after(:all) do
216
+ after :each do
204
217
  Object.send(:remove_const, :ModelTwo)
205
218
  Object.send(:remove_const, :EmbOneOne)
206
219
  end
220
+
221
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
222
+ let(:emb_obj) { EmbOneOne.new(em_foo: 'Em-Foo', em_bar: 'Em-Bar') }
223
+
224
+ it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj._id, 'em_bar' => 'Em-Bar' }]) }
207
225
  end
208
226
 
209
227
  context 'when relation with alias' do
210
- before(:all) do
211
- # Need class name constant
228
+ before :each do
212
229
  class ModelTwo
213
230
  include Mongoid::Document
214
231
  include Mongoid::History::Trackable
232
+
215
233
  store_in collection: :model_twos
216
- embeds_one :emb_one_one, store_as: :eoo, inverse_class_name: 'EmbOneOne'
234
+
235
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
236
+ embeds_one :emb_one_one, store_as: :eoo
237
+ else
238
+ embeds_one :emb_one_one, inverse_class_name: 'EmbOneOne', store_as: :eoo
239
+ end
240
+
241
+ track_history on: :emb_one_one
217
242
  end
218
243
 
219
244
  class EmbOneOne
220
245
  include Mongoid::Document
246
+
221
247
  field :em_bar
222
248
  embedded_in :model_one
223
249
  end
224
250
  end
225
251
 
226
- before(:each) do
227
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
228
- ModelTwo.track_history on: :emb_one_one
229
- end
230
-
231
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
232
- let(:emb_obj_one) { EmbOneOne.new(em_bar: 'Em-Bar') }
233
-
234
- it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj_one._id, 'em_bar' => 'Em-Bar' }]) }
235
-
236
- after(:all) do
252
+ after :each do
237
253
  Object.send(:remove_const, :ModelTwo)
238
254
  Object.send(:remove_const, :EmbOneOne)
239
255
  end
256
+
257
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
258
+ let(:emb_obj) { EmbOneOne.new(em_bar: 'Em-Bar') }
259
+
260
+ it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj._id, 'em_bar' => 'Em-Bar' }]) }
240
261
  end
241
262
 
242
263
  context 'when no object' do
243
- before(:all) do
244
- # Need class name constant
264
+ before :each do
245
265
  class ModelTwo
246
266
  include Mongoid::Document
247
267
  include Mongoid::History::Trackable
268
+
248
269
  store_in collection: :model_twos
249
- embeds_one :emb_one_one, store_as: :eoo, inverse_class_name: 'EmbOneOne'
270
+
271
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
272
+ embeds_one :emb_one_one, store_as: :eoo
273
+ else
274
+ embeds_one :emb_one_one, store_as: :eoo, inverse_class_name: 'EmbOneOne'
275
+ end
276
+
277
+ track_history on: :emb_one_one
250
278
  end
251
279
 
252
280
  class EmbOneOne
253
281
  include Mongoid::Document
282
+
254
283
  field :em_bar
255
284
  embedded_in :model_one
256
285
  end
257
286
  end
258
287
 
259
- before(:each) do
260
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
261
- ModelTwo.track_history on: :emb_one_one
288
+ after :each do
289
+ Object.send(:remove_const, :ModelTwo)
290
+ Object.send(:remove_const, :EmbOneOne)
262
291
  end
263
292
 
264
293
  let(:obj_one) { ModelTwo.new }
265
294
 
266
295
  it { is_expected.to eq({}) }
267
-
268
- after(:all) do
269
- Object.send(:remove_const, :ModelTwo)
270
- Object.send(:remove_const, :EmbOneOne)
271
- end
272
296
  end
273
297
 
274
298
  context 'when object not paranoid' do
275
- before(:all) do
276
- # Need class name constant
299
+ before :each do
277
300
  class ModelTwo
278
301
  include Mongoid::Document
279
302
  include Mongoid::History::Trackable
303
+
280
304
  store_in collection: :model_twos
281
- embeds_one :emb_one_one, store_as: :eoo, inverse_class_name: 'EmbOneOne'
305
+
306
+ if Mongoid::Compatibility::Version.mongoid7_or_newer?
307
+ embeds_one :emb_one_one, store_as: :eoo
308
+ else
309
+ embeds_one :emb_one_one, store_as: :eoo, inverse_class_name: 'EmbOneOne'
310
+ end
311
+
312
+ track_history on: :emb_one_one
282
313
  end
283
314
 
284
315
  class EmbOneOne
285
316
  include Mongoid::Document
286
317
  include Mongoid::History::Trackable
318
+
287
319
  field :em_bar
288
320
  field :cancelled_at
321
+
289
322
  embedded_in :model_one
290
- end
291
- end
292
323
 
293
- before(:each) do
294
- ModelTwo.instance_variable_set(:@history_trackable_options, nil)
295
- EmbOneOne.instance_variable_set(:@trackable_settings, nil)
296
- ModelTwo.track_history on: :emb_one_one
297
- EmbOneOne.history_settings paranoia_field: :cancelled_at
324
+ history_settings paranoia_field: :cancelled_at
325
+ end
298
326
  end
299
327
 
300
- let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj_one) }
301
- let(:emb_obj_one) { EmbOneOne.new(em_bar: 'Em-Bar') }
302
-
303
- it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj_one._id, 'em_bar' => 'Em-Bar' }]) }
304
-
305
- after(:all) do
328
+ after :each do
306
329
  Object.send(:remove_const, :ModelTwo)
307
330
  Object.send(:remove_const, :EmbOneOne)
308
331
  end
332
+
333
+ let(:obj_one) { ModelTwo.new(emb_one_one: emb_obj) }
334
+ let(:emb_obj) { EmbOneOne.new(em_bar: 'Em-Bar') }
335
+
336
+ it { is_expected.to eq('emb_one_one' => [nil, { '_id' => emb_obj._id, 'em_bar' => 'Em-Bar' }]) }
309
337
  end
310
338
  end
311
339
 
312
- describe '#insert_embeds_many_changes' do
313
- end
340
+ pending '#insert_embeds_many_changes'
314
341
  end
315
342
  end