mongoid-history 0.8.3 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -1
  3. data/.document +5 -5
  4. data/.github/workflows/test.yml +72 -0
  5. data/.gitignore +46 -46
  6. data/.rspec +2 -2
  7. data/.rubocop.yml +6 -6
  8. data/.rubocop_todo.yml +99 -99
  9. data/CHANGELOG.md +173 -163
  10. data/CONTRIBUTING.md +117 -118
  11. data/Dangerfile +1 -1
  12. data/Gemfile +49 -40
  13. data/LICENSE.txt +20 -20
  14. data/README.md +609 -608
  15. data/RELEASING.md +66 -67
  16. data/Rakefile +24 -24
  17. data/UPGRADING.md +53 -53
  18. data/lib/mongoid/history/attributes/base.rb +72 -72
  19. data/lib/mongoid/history/attributes/create.rb +45 -45
  20. data/lib/mongoid/history/attributes/destroy.rb +34 -34
  21. data/lib/mongoid/history/attributes/update.rb +104 -104
  22. data/lib/mongoid/history/options.rb +177 -177
  23. data/lib/mongoid/history/trackable.rb +588 -583
  24. data/lib/mongoid/history/tracker.rb +247 -247
  25. data/lib/mongoid/history/version.rb +5 -5
  26. data/lib/mongoid/history.rb +77 -77
  27. data/lib/mongoid-history.rb +1 -1
  28. data/mongoid-history.gemspec +25 -25
  29. data/perf/benchmark_modified_attributes_for_create.rb +65 -65
  30. data/perf/gc_suite.rb +21 -21
  31. data/spec/integration/embedded_in_polymorphic_spec.rb +112 -112
  32. data/spec/integration/integration_spec.rb +976 -976
  33. data/spec/integration/multi_relation_spec.rb +47 -47
  34. data/spec/integration/multiple_trackers_spec.rb +68 -68
  35. data/spec/integration/nested_embedded_documents_spec.rb +64 -64
  36. data/spec/integration/nested_embedded_documents_tracked_in_parent_spec.rb +124 -124
  37. data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +115 -115
  38. data/spec/integration/subclasses_spec.rb +47 -47
  39. data/spec/integration/track_history_order_spec.rb +84 -84
  40. data/spec/integration/validation_failure_spec.rb +76 -76
  41. data/spec/spec_helper.rb +32 -30
  42. data/spec/support/error_helpers.rb +7 -0
  43. data/spec/support/mongoid.rb +11 -11
  44. data/spec/support/mongoid_history.rb +12 -12
  45. data/spec/unit/attributes/base_spec.rb +141 -141
  46. data/spec/unit/attributes/create_spec.rb +342 -342
  47. data/spec/unit/attributes/destroy_spec.rb +228 -228
  48. data/spec/unit/attributes/update_spec.rb +342 -342
  49. data/spec/unit/callback_options_spec.rb +165 -165
  50. data/spec/unit/embedded_methods_spec.rb +87 -87
  51. data/spec/unit/history_spec.rb +58 -58
  52. data/spec/unit/my_instance_methods_spec.rb +555 -555
  53. data/spec/unit/options_spec.rb +365 -365
  54. data/spec/unit/singleton_methods_spec.rb +406 -406
  55. data/spec/unit/store/default_store_spec.rb +11 -11
  56. data/spec/unit/store/request_store_spec.rb +13 -13
  57. data/spec/unit/trackable_spec.rb +1057 -987
  58. data/spec/unit/tracker_spec.rb +190 -190
  59. metadata +9 -7
  60. data/.travis.yml +0 -36
data/spec/spec_helper.rb CHANGED
@@ -1,30 +1,32 @@
1
- require 'coveralls'
2
- Coveralls.wear!
3
-
4
- $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
5
-
6
- require 'active_support/all'
7
- require 'mongoid'
8
- require 'request_store'
9
-
10
- # Undefine RequestStore so that it may be stubbed in specific tests
11
- RequestStoreTemp = RequestStore
12
- Object.send(:remove_const, :RequestStore)
13
-
14
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
-
16
- require 'mongoid/history'
17
-
18
- RSpec.configure do |config|
19
- config.raise_errors_for_deprecations!
20
-
21
- config.before :all do
22
- Mongoid.logger.level = Logger::INFO
23
- Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?
24
- Mongoid.belongs_to_required_by_default = false if Mongoid::Compatibility::Version.mongoid6?
25
- end
26
-
27
- config.before :each do
28
- Mongoid::History.reset!
29
- end
30
- end
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
5
+
6
+ require 'active_support/all'
7
+ require 'mongoid'
8
+ require 'request_store'
9
+
10
+ # Undefine RequestStore so that it may be stubbed in specific tests
11
+ RequestStoreTemp = RequestStore
12
+ Object.send(:remove_const, :RequestStore)
13
+
14
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
+
16
+ require 'mongoid/history'
17
+
18
+ RSpec.configure do |config|
19
+ config.raise_errors_for_deprecations!
20
+
21
+ config.before :all do
22
+ Mongoid.logger.level = Logger::INFO
23
+ Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?
24
+ Mongoid.belongs_to_required_by_default = false if Mongoid::Compatibility::Version.mongoid6?
25
+ end
26
+
27
+ config.before :each do
28
+ Mongoid::History.reset!
29
+ end
30
+
31
+ config.include ErrorHelpers
32
+ end
@@ -0,0 +1,7 @@
1
+ module ErrorHelpers
2
+ def ignore_errors
3
+ yield
4
+ rescue StandardError => e
5
+ Mongoid.logger.debug "ignored error #{e}"
6
+ end
7
+ end
@@ -1,11 +1,11 @@
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
+ 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,12 +1,12 @@
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
- end
12
- end
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
+ end
12
+ end
@@ -1,141 +1,141 @@
1
- require 'spec_helper'
2
-
3
- describe Mongoid::History::Attributes::Base do
4
- before :each do
5
- class ModelOne
6
- include Mongoid::Document
7
- include Mongoid::History::Trackable
8
-
9
- field :foo
10
- field :b, as: :bar
11
- end
12
-
13
- class ModelTwo
14
- include Mongoid::Document
15
-
16
- field :foo
17
- field :goo
18
- end
19
- end
20
-
21
- after :each do
22
- Object.send(:remove_const, :ModelOne)
23
- Object.send(:remove_const, :ModelTwo)
24
- end
25
-
26
- let(:obj_one) { ModelOne.new }
27
- let(:base) { described_class.new(obj_one) }
28
- subject { base }
29
-
30
- it { is_expected.to respond_to(:trackable) }
31
-
32
- describe '#initialize' do
33
- it { expect(base.instance_variable_get(:@trackable)).to eq obj_one }
34
- end
35
-
36
- describe '#trackable_class' do
37
- subject { base.send(:trackable_class) }
38
- it { is_expected.to eq ModelOne }
39
- end
40
-
41
- describe '#aliased_fields' do
42
- subject { base.send(:aliased_fields) }
43
- it { is_expected.to eq('id' => '_id', 'bar' => 'b') }
44
- end
45
-
46
- describe '#changes_method' do
47
- before(:each) do
48
- ModelOne.track_history changes_method: :my_changes
49
- end
50
- subject { base.send(:changes_method) }
51
- it { is_expected.to eq :my_changes }
52
- end
53
-
54
- describe '#changes' do
55
- before(:each) do
56
- ModelOne.track_history
57
- allow(obj_one).to receive(:changes) { { 'foo' => ['Foo', 'Foo-new'] } }
58
- end
59
- subject { base.send(:changes) }
60
- it { is_expected.to eq('foo' => ['Foo', 'Foo-new']) }
61
- end
62
-
63
- describe '#format_field' do
64
- subject { base.send(:format_field, :bar, 'foo') }
65
-
66
- context 'when formatted via string' do
67
- before do
68
- ModelOne.track_history format: { bar: '*%s*' }
69
- end
70
-
71
- it { is_expected.to eq '*foo*' }
72
- end
73
-
74
- context 'when formatted via proc' do
75
- before do
76
- ModelOne.track_history format: { bar: ->(v) { v * 2 } }
77
- end
78
-
79
- it { is_expected.to eq 'foofoo' }
80
- end
81
-
82
- context 'when not formatted' do
83
- before do
84
- ModelOne.track_history
85
- end
86
-
87
- it { is_expected.to eq 'foo' }
88
- end
89
- end
90
-
91
- shared_examples 'formats embedded relation' do |relation_type|
92
- let(:model_two) { ModelTwo.new(foo: :bar, goo: :baz) }
93
-
94
- before :each do
95
- ModelOne.send(relation_type, :model_two)
96
- end
97
-
98
- subject { base.send("format_#{relation_type}_relation", :model_two, model_two.attributes) }
99
-
100
- context 'with permitted attributes' do
101
- before do
102
- ModelOne.track_history on: { model_two: %i[foo] }
103
- end
104
-
105
- it 'should select only permitted attributes' do
106
- is_expected.to include('foo' => :bar)
107
- is_expected.to_not include('goo')
108
- end
109
- end
110
-
111
- context 'with attributes formatted via string' do
112
- before do
113
- ModelOne.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: '&%s&' } }
114
- end
115
-
116
- it 'should select obfuscate permitted attributes' do
117
- is_expected.to include('foo' => '&bar&')
118
- is_expected.to_not include('goo')
119
- end
120
- end
121
-
122
- context 'with attributes formatted via proc' do
123
- before do
124
- ModelOne.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: ->(v) { v.to_s * 2 } } }
125
- end
126
-
127
- it 'should select obfuscate permitted attributes' do
128
- is_expected.to include('foo' => 'barbar')
129
- is_expected.to_not include('goo')
130
- end
131
- end
132
- end
133
-
134
- describe '#format_embeds_one_relation' do
135
- include_examples 'formats embedded relation', :embeds_one
136
- end
137
-
138
- describe '#format_embeds_many_relation' do
139
- include_examples 'formats embedded relation', :embeds_many
140
- end
141
- end
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::History::Attributes::Base do
4
+ before :each do
5
+ class ModelOne
6
+ include Mongoid::Document
7
+ include Mongoid::History::Trackable
8
+
9
+ field :foo
10
+ field :b, as: :bar
11
+ end
12
+
13
+ class ModelTwo
14
+ include Mongoid::Document
15
+
16
+ field :foo
17
+ field :goo
18
+ end
19
+ end
20
+
21
+ after :each do
22
+ Object.send(:remove_const, :ModelOne)
23
+ Object.send(:remove_const, :ModelTwo)
24
+ end
25
+
26
+ let(:obj_one) { ModelOne.new }
27
+ let(:base) { described_class.new(obj_one) }
28
+ subject { base }
29
+
30
+ it { is_expected.to respond_to(:trackable) }
31
+
32
+ describe '#initialize' do
33
+ it { expect(base.instance_variable_get(:@trackable)).to eq obj_one }
34
+ end
35
+
36
+ describe '#trackable_class' do
37
+ subject { base.send(:trackable_class) }
38
+ it { is_expected.to eq ModelOne }
39
+ end
40
+
41
+ describe '#aliased_fields' do
42
+ subject { base.send(:aliased_fields) }
43
+ it { is_expected.to eq('id' => '_id', 'bar' => 'b') }
44
+ end
45
+
46
+ describe '#changes_method' do
47
+ before(:each) do
48
+ ModelOne.track_history changes_method: :my_changes
49
+ end
50
+ subject { base.send(:changes_method) }
51
+ it { is_expected.to eq :my_changes }
52
+ end
53
+
54
+ describe '#changes' do
55
+ before(:each) do
56
+ ModelOne.track_history
57
+ allow(obj_one).to receive(:changes) { { 'foo' => ['Foo', 'Foo-new'] } }
58
+ end
59
+ subject { base.send(:changes) }
60
+ it { is_expected.to eq('foo' => ['Foo', 'Foo-new']) }
61
+ end
62
+
63
+ describe '#format_field' do
64
+ subject { base.send(:format_field, :bar, 'foo') }
65
+
66
+ context 'when formatted via string' do
67
+ before do
68
+ ModelOne.track_history format: { bar: '*%s*' }
69
+ end
70
+
71
+ it { is_expected.to eq '*foo*' }
72
+ end
73
+
74
+ context 'when formatted via proc' do
75
+ before do
76
+ ModelOne.track_history format: { bar: ->(v) { v * 2 } }
77
+ end
78
+
79
+ it { is_expected.to eq 'foofoo' }
80
+ end
81
+
82
+ context 'when not formatted' do
83
+ before do
84
+ ModelOne.track_history
85
+ end
86
+
87
+ it { is_expected.to eq 'foo' }
88
+ end
89
+ end
90
+
91
+ shared_examples 'formats embedded relation' do |relation_type|
92
+ let(:model_two) { ModelTwo.new(foo: :bar, goo: :baz) }
93
+
94
+ before :each do
95
+ ModelOne.send(relation_type, :model_two)
96
+ end
97
+
98
+ subject { base.send("format_#{relation_type}_relation", :model_two, model_two.attributes) }
99
+
100
+ context 'with permitted attributes' do
101
+ before do
102
+ ModelOne.track_history on: { model_two: %i[foo] }
103
+ end
104
+
105
+ it 'should select only permitted attributes' do
106
+ is_expected.to include('foo' => :bar)
107
+ is_expected.to_not include('goo')
108
+ end
109
+ end
110
+
111
+ context 'with attributes formatted via string' do
112
+ before do
113
+ ModelOne.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: '&%s&' } }
114
+ end
115
+
116
+ it 'should select obfuscate permitted attributes' do
117
+ is_expected.to include('foo' => '&bar&')
118
+ is_expected.to_not include('goo')
119
+ end
120
+ end
121
+
122
+ context 'with attributes formatted via proc' do
123
+ before do
124
+ ModelOne.track_history on: { model_two: %i[foo] }, format: { model_two: { foo: ->(v) { v.to_s * 2 } } }
125
+ end
126
+
127
+ it 'should select obfuscate permitted attributes' do
128
+ is_expected.to include('foo' => 'barbar')
129
+ is_expected.to_not include('goo')
130
+ end
131
+ end
132
+ end
133
+
134
+ describe '#format_embeds_one_relation' do
135
+ include_examples 'formats embedded relation', :embeds_one
136
+ end
137
+
138
+ describe '#format_embeds_many_relation' do
139
+ include_examples 'formats embedded relation', :embeds_many
140
+ end
141
+ end