mongoid-history 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +9 -9
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -0
- data/README.md +17 -4
- data/RELEASING.md +1 -2
- data/UPGRADING.md +25 -0
- data/lib/mongoid/history/attributes/update.rb +11 -9
- data/lib/mongoid/history/options.rb +11 -16
- data/lib/mongoid/history/trackable.rb +23 -14
- data/lib/mongoid/history/tracker.rb +7 -3
- data/lib/mongoid/history/version.rb +1 -1
- data/mongoid-history.gemspec +1 -1
- data/spec/integration/integration_spec.rb +27 -21
- data/spec/integration/track_history_order_spec.rb +52 -0
- data/spec/unit/attributes/destroy_spec.rb +1 -1
- data/spec/unit/attributes/update_spec.rb +113 -2
- data/spec/unit/callback_options_spec.rb +159 -0
- data/spec/unit/options_spec.rb +10 -9
- data/spec/unit/trackable_spec.rb +50 -6
- metadata +9 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d77abcedc5d3a8dd4dc76cc8d6320fbce3aaa1da
|
4
|
+
data.tar.gz: 7ea0951e39d1ce44841a92dacd55d576c6542612
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 332314784de0fa89013bab41d9586eeae0e72ee743ce964561d384df270a255970fe23abbc6081f7b0a592085bba8d7cf4f056a3bf24e4deff0099a23628d2b2
|
7
|
+
data.tar.gz: '0228bacf2516b4da7bd57db5171d3e92bc4b418c0df3fcc49fc366d783ed3bf789b2487abb9c32036996fb8ad975dd4adcfad1edeecfff72671bb3da5384b200'
|
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2017-
|
3
|
+
# on 2017-12-08 18:06:04 +0530 using RuboCop version 0.48.1.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
@@ -27,27 +27,27 @@ Lint/ParenthesesAsGroupedExpression:
|
|
27
27
|
Metrics/AbcSize:
|
28
28
|
Max: 50
|
29
29
|
|
30
|
-
# Offense count:
|
30
|
+
# Offense count: 111
|
31
31
|
# Configuration parameters: CountComments, ExcludedMethods.
|
32
32
|
Metrics/BlockLength:
|
33
|
-
Max:
|
33
|
+
Max: 807
|
34
34
|
|
35
35
|
# Offense count: 1
|
36
36
|
# Configuration parameters: CountComments.
|
37
37
|
Metrics/ClassLength:
|
38
|
-
Max:
|
38
|
+
Max: 126
|
39
39
|
|
40
|
-
# Offense count:
|
40
|
+
# Offense count: 5
|
41
41
|
Metrics/CyclomaticComplexity:
|
42
42
|
Max: 10
|
43
43
|
|
44
|
-
# Offense count:
|
44
|
+
# Offense count: 462
|
45
45
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
46
46
|
# URISchemes: http, https
|
47
47
|
Metrics/LineLength:
|
48
48
|
Max: 688
|
49
49
|
|
50
|
-
# Offense count:
|
50
|
+
# Offense count: 15
|
51
51
|
# Configuration parameters: CountComments.
|
52
52
|
Metrics/MethodLength:
|
53
53
|
Max: 23
|
@@ -55,9 +55,9 @@ Metrics/MethodLength:
|
|
55
55
|
# Offense count: 2
|
56
56
|
# Configuration parameters: CountComments.
|
57
57
|
Metrics/ModuleLength:
|
58
|
-
Max:
|
58
|
+
Max: 180
|
59
59
|
|
60
|
-
# Offense count:
|
60
|
+
# Offense count: 5
|
61
61
|
Metrics/PerceivedComplexity:
|
62
62
|
Max: 12
|
63
63
|
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
### 0.8.0 (2018/01/16)
|
2
|
+
|
3
|
+
* [#180](https://github.com/mongoid/mongoid-history/pull/180): Removed deprecation notice - [@sivagollapalli](https://github.com/sivagollapalli).
|
4
|
+
* [#208](https://github.com/mongoid/mongoid-history/pull/208): Fix: history tracks fields declared after `track_history` - [@mikwat](https://github.com/mikwat).
|
5
|
+
* [#210](https://github.com/mongoid/mongoid-history/pull/210): Do not track unmodified embedded relations - [@jagdeepsingh](https://github.com/jagdeepsingh).
|
6
|
+
* [#205](https://github.com/mongoid/mongoid-history/pull/205): Allow modifier field to be optional - [@yads](https://github.com/yads).
|
7
|
+
* [#211](https://github.com/mongoid/mongoid-history/pull/211): Enable tracking create/destroy by default - [@jagdeepsingh](https://github.com/jagdeepsingh).
|
8
|
+
* [#212](https://github.com/mongoid/mongoid-history/pull/212): `track_history` method support for `:if` and `:unless` options - [@jagdeepsingh](https://github.com/jagdeepsingh).
|
9
|
+
|
1
10
|
### 0.7.0 (2017/11/14)
|
2
11
|
|
3
12
|
* [#202](https://github.com/mongoid/mongoid-history/pull/202): Do not create tracker on persistence error - [@mikwat](https://github.com/mikwat).
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -66,12 +66,13 @@ class Post
|
|
66
66
|
# telling Mongoid::History how you want to track changes
|
67
67
|
# dynamic fields will be tracked automatically (for MongoId 4.0+ you should include Mongoid::Attributes::Dynamic to your model)
|
68
68
|
track_history :on => [:title, :body], # track title and body fields only, default is :all
|
69
|
-
:modifier_field => :modifier, # adds "belongs_to :modifier" to track who made the change, default is :modifier
|
69
|
+
:modifier_field => :modifier, # adds "belongs_to :modifier" to track who made the change, default is :modifier, set to nil to not create modifier_field
|
70
70
|
:modifier_field_inverse_of => :nil, # adds an ":inverse_of" option to the "belongs_to :modifier" relation, default is not set
|
71
|
+
:modifier_field_optional => true, # marks the modifier relationship as optional (requires Mongoid 6 or higher)
|
71
72
|
:version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
|
72
|
-
:track_create
|
73
|
-
:track_update
|
74
|
-
:track_destroy
|
73
|
+
:track_create => true, # track document creation, default is true
|
74
|
+
:track_update => true, # track document updates, default is true
|
75
|
+
:track_destroy => true # track document destruction, default is true
|
75
76
|
end
|
76
77
|
|
77
78
|
class Comment
|
@@ -429,6 +430,18 @@ Or perhaps you are namespacing to a module:
|
|
429
430
|
Mongoid::History.modifier_class_name = 'CMS::Author'
|
430
431
|
```
|
431
432
|
|
433
|
+
**Conditional :if and :unless options**
|
434
|
+
|
435
|
+
The `track_history` method supports `:if` and `:unless` options which will skip generating
|
436
|
+
the history tracker unless they are satisfied. These options can take either a method
|
437
|
+
`Symbol` or a `Proc`. They behave identical to how `:if` and `:unless` behave in Rails model callbacks.
|
438
|
+
|
439
|
+
```ruby
|
440
|
+
track_history on: [:ip],
|
441
|
+
if: :should_i_track_history?,
|
442
|
+
unless: ->(obj){ obj.method_to_skip_history }
|
443
|
+
```
|
444
|
+
|
432
445
|
**Using an alternate changes method**
|
433
446
|
|
434
447
|
Sometimes you may wish to provide an alternate method for determining which changes should be tracked. For example, if you are using embedded documents
|
data/RELEASING.md
CHANGED
data/UPGRADING.md
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
## Upgrading Mongoid History
|
2
|
+
|
3
|
+
### Upgrading to 0.8.0
|
4
|
+
|
5
|
+
#### History is now tracked on create and destroy by default
|
6
|
+
|
7
|
+
By default, Mongoid History will now track all actions (create, update, and destroy.)
|
8
|
+
Previously, only update actions were tracked by default.
|
9
|
+
|
10
|
+
To preserve the old behavior, please modify your call to `track_history` as follows:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
track_history ...
|
14
|
+
track_create: false,
|
15
|
+
track_destroy: false
|
16
|
+
```
|
17
|
+
|
18
|
+
See [#207](https://github.com/mongoid/mongoid-history/pull/207) for more information.
|
19
|
+
|
1
20
|
### Upgrading to 0.7.0
|
2
21
|
|
3
22
|
#### Remove history track when create, update or destroy raises an error
|
@@ -7,3 +26,9 @@ created before the call will now be deleted. In the past this was a problem for
|
|
7
26
|
`dependent: :restrict`.
|
8
27
|
|
9
28
|
See [#202](https://github.com/mongoid/mongoid-history/pull/202) for more information.
|
29
|
+
|
30
|
+
# Main Changes / Upgrading Notes
|
31
|
+
|
32
|
+
See [#189](https://github.com/mongoid/mongoid-history/pull/189) for more information.
|
33
|
+
|
34
|
+
* Currently, the `:all` option behaves identically to `:fields`. Future versions will track all fields and relations of trackable class when using `:all`.
|
@@ -10,7 +10,7 @@ module Mongoid
|
|
10
10
|
elsif trackable_class.tracked_embeds_many?(k)
|
11
11
|
insert_embeds_many_changes(k, v)
|
12
12
|
elsif trackable_class.tracked?(k, :update)
|
13
|
-
@attributes[k] = format_field(k, v)
|
13
|
+
@attributes[k] = format_field(k, v) unless v.all?(&:blank?)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
@attributes
|
@@ -22,20 +22,22 @@ module Mongoid
|
|
22
22
|
relation = trackable_class.database_field_name(relation)
|
23
23
|
relation_class = trackable_class.embeds_one_class(relation)
|
24
24
|
paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
original_value = value[0][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[0])
|
26
|
+
modified_value = value[1][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[1])
|
27
|
+
return if original_value == modified_value
|
28
|
+
@attributes[relation] = [original_value, modified_value]
|
28
29
|
end
|
29
30
|
|
30
31
|
def insert_embeds_many_changes(relation, value)
|
31
32
|
relation = trackable_class.database_field_name(relation)
|
32
33
|
relation_class = trackable_class.embeds_many_class(relation)
|
33
34
|
paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
original_value = value[0].reject { |rel| rel[paranoia_field].present? }
|
36
|
+
.map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
|
37
|
+
modified_value = value[1].reject { |rel| rel[paranoia_field].present? }
|
38
|
+
.map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
|
39
|
+
return if original_value == modified_value
|
40
|
+
@attributes[relation] = [original_value, modified_value]
|
39
41
|
end
|
40
42
|
end
|
41
43
|
end
|
@@ -3,20 +3,22 @@ module Mongoid
|
|
3
3
|
class Options
|
4
4
|
attr_reader :trackable, :options
|
5
5
|
|
6
|
-
def initialize(trackable)
|
6
|
+
def initialize(trackable, opts = {})
|
7
7
|
@trackable = trackable
|
8
|
+
@options = default_options.merge(opts)
|
8
9
|
end
|
9
10
|
|
10
11
|
def scope
|
11
12
|
trackable.collection_name.to_s.singularize.to_sym
|
12
13
|
end
|
13
14
|
|
14
|
-
def
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def prepared
|
16
|
+
@prepared ||= begin
|
17
|
+
prepare_skipped_fields
|
18
|
+
prepare_formatted_fields
|
19
|
+
parse_tracked_fields_and_relations
|
20
|
+
options
|
21
|
+
end
|
20
22
|
end
|
21
23
|
|
22
24
|
private
|
@@ -30,9 +32,9 @@ module Mongoid
|
|
30
32
|
version_field: :version,
|
31
33
|
changes_method: :changes,
|
32
34
|
scope: scope,
|
33
|
-
track_create:
|
35
|
+
track_create: true,
|
34
36
|
track_update: true,
|
35
|
-
track_destroy:
|
37
|
+
track_destroy: true,
|
36
38
|
format: nil }
|
37
39
|
end
|
38
40
|
|
@@ -76,13 +78,6 @@ module Mongoid
|
|
76
78
|
# when `[:foo, { posts: [:id, :title] }]`, then return as is
|
77
79
|
@options[:on] = Array(options[:on])
|
78
80
|
|
79
|
-
# # :all is just an alias to :fields for now, to support existing users of `mongoid-history`
|
80
|
-
# # In future, :all will track all the fields and relations of trackable class
|
81
|
-
if options[:on].include?(:all)
|
82
|
-
warn "[DEPRECATION] Use :fields instead of :all to track all fields in class #{trackable}.\n\
|
83
|
-
Going forward, :all will track all the fields and relations for the class"
|
84
|
-
end
|
85
|
-
|
86
81
|
@options[:on] = options[:on].map { |opt| opt == :all ? :fields : opt }
|
87
82
|
|
88
83
|
if options[:on].include?(:fields)
|
@@ -7,14 +7,16 @@ module Mongoid
|
|
7
7
|
def track_history(options = {})
|
8
8
|
extend EmbeddedMethods
|
9
9
|
|
10
|
-
|
11
|
-
options = options_parser.parse(options)
|
10
|
+
history_options = Mongoid::History::Options.new(self, options)
|
12
11
|
|
13
|
-
field options[:version_field].to_sym, type: Integer
|
12
|
+
field history_options.options[:version_field].to_sym, type: Integer
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
unless history_options.options[:modifier_field].nil?
|
15
|
+
belongs_to_modifier_options = { class_name: Mongoid::History.modifier_class_name }
|
16
|
+
belongs_to_modifier_options[:inverse_of] = history_options.options[:modifier_field_inverse_of] if history_options.options.key?(:modifier_field_inverse_of)
|
17
|
+
belongs_to_modifier_options[:optional] = true if history_options.options[:modifier_field_optional] && Mongoid::Compatibility::Version.mongoid6_or_newer?
|
18
|
+
belongs_to history_options.options[:modifier_field].to_sym, belongs_to_modifier_options
|
19
|
+
end
|
18
20
|
|
19
21
|
include MyInstanceMethods
|
20
22
|
extend SingletonMethods
|
@@ -22,12 +24,13 @@ module Mongoid
|
|
22
24
|
delegate :history_trackable_options, to: 'self.class'
|
23
25
|
delegate :track_history?, to: 'self.class'
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
callback_options = history_options.options.slice(:if, :unless)
|
28
|
+
around_update :track_update, callback_options if history_options.options[:track_update]
|
29
|
+
around_create :track_create, callback_options if history_options.options[:track_create]
|
30
|
+
around_destroy :track_destroy, callback_options if history_options.options[:track_destroy]
|
28
31
|
|
29
32
|
Mongoid::History.trackable_class_options ||= {}
|
30
|
-
Mongoid::History.trackable_class_options[
|
33
|
+
Mongoid::History.trackable_class_options[history_options.scope] = history_options
|
31
34
|
end
|
32
35
|
|
33
36
|
def history_settings(options = {})
|
@@ -219,11 +222,12 @@ module Mongoid
|
|
219
222
|
def history_tracker_attributes(action)
|
220
223
|
return @history_tracker_attributes if @history_tracker_attributes
|
221
224
|
|
225
|
+
modifier_field = history_trackable_options[:modifier_field]
|
222
226
|
@history_tracker_attributes = {
|
223
227
|
association_chain: traverse_association_chain,
|
224
|
-
scope: related_scope
|
225
|
-
modifier: send(history_trackable_options[:modifier_field])
|
228
|
+
scope: related_scope
|
226
229
|
}
|
230
|
+
@history_tracker_attributes[:modifier] = send(modifier_field) if modifier_field
|
227
231
|
|
228
232
|
original, modified = transform_changes(modified_attributes_for_action(action))
|
229
233
|
|
@@ -425,7 +429,12 @@ module Mongoid
|
|
425
429
|
#
|
426
430
|
# @return [ Array < String > ] the list of reserved database field names
|
427
431
|
def reserved_tracked_fields
|
428
|
-
@reserved_tracked_fields ||=
|
432
|
+
@reserved_tracked_fields ||= begin
|
433
|
+
fields = ['_id', history_trackable_options[:version_field].to_s]
|
434
|
+
modifier_field = history_trackable_options[:modifier_field]
|
435
|
+
fields << "#{modifier_field}_id" if modifier_field
|
436
|
+
fields
|
437
|
+
end
|
429
438
|
end
|
430
439
|
|
431
440
|
def field_formats
|
@@ -494,7 +503,7 @@ module Mongoid
|
|
494
503
|
end
|
495
504
|
|
496
505
|
def history_trackable_options
|
497
|
-
@history_trackable_options ||= Mongoid::History.trackable_class_options[trackable_scope]
|
506
|
+
@history_trackable_options ||= Mongoid::History.trackable_class_options[trackable_scope].prepared
|
498
507
|
end
|
499
508
|
|
500
509
|
def clear_trackable_memoization
|
@@ -14,7 +14,11 @@ module Mongoid
|
|
14
14
|
field :version, type: Integer
|
15
15
|
field :action, type: String
|
16
16
|
field :scope, type: String
|
17
|
-
|
17
|
+
modifier_options = {
|
18
|
+
class_name: Mongoid::History.modifier_class_name
|
19
|
+
}
|
20
|
+
modifier_options[:optional] = true if Mongoid::Compatibility::Version.mongoid6_or_newer?
|
21
|
+
belongs_to :modifier, modifier_options
|
18
22
|
|
19
23
|
index(scope: 1)
|
20
24
|
index(association_chain: 1)
|
@@ -50,7 +54,7 @@ module Mongoid
|
|
50
54
|
undo_hash = affected.easy_unmerge(modified)
|
51
55
|
undo_hash.easy_merge!(original)
|
52
56
|
modifier_field = trackable.history_trackable_options[:modifier_field]
|
53
|
-
undo_hash[modifier_field] = modifier
|
57
|
+
undo_hash[modifier_field] = modifier if modifier_field
|
54
58
|
(modified.keys - undo_hash.keys).each do |k|
|
55
59
|
undo_hash[k] = nil
|
56
60
|
end
|
@@ -61,7 +65,7 @@ module Mongoid
|
|
61
65
|
redo_hash = affected.easy_unmerge(original)
|
62
66
|
redo_hash.easy_merge!(modified)
|
63
67
|
modifier_field = trackable.history_trackable_options[:modifier_field]
|
64
|
-
redo_hash[modifier_field] = modifier
|
68
|
+
redo_hash[modifier_field] = modifier if modifier_field
|
65
69
|
localize_keys(redo_hash)
|
66
70
|
end
|
67
71
|
|
data/mongoid-history.gemspec
CHANGED
@@ -20,6 +20,6 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
s.add_runtime_dependency 'easy_diff'
|
22
22
|
s.add_runtime_dependency 'mongoid', '>= 3.0'
|
23
|
-
s.add_runtime_dependency 'mongoid-compatibility'
|
23
|
+
s.add_runtime_dependency 'mongoid-compatibility', '>= 0.5.1'
|
24
24
|
s.add_runtime_dependency 'activesupport'
|
25
25
|
end
|
@@ -122,6 +122,7 @@ describe Mongoid::History do
|
|
122
122
|
|
123
123
|
describe 'on destruction' do
|
124
124
|
it 'should have two history track records in post' do
|
125
|
+
post # This will create history track records for creation
|
125
126
|
expect do
|
126
127
|
post.destroy
|
127
128
|
end.to change(Tracker, :count).by(1)
|
@@ -147,12 +148,14 @@ describe Mongoid::History do
|
|
147
148
|
|
148
149
|
describe 'on update non-embedded' do
|
149
150
|
it 'should create a history track if changed attributes match tracked attributes' do
|
151
|
+
post # This will create history track records for creation
|
150
152
|
expect do
|
151
153
|
post.update_attributes(title: 'Another Test')
|
152
154
|
end.to change(Tracker, :count).by(1)
|
153
155
|
end
|
154
156
|
|
155
157
|
it 'should not create a history track if changed attributes do not match tracked attributes' do
|
158
|
+
post # This will create history track records for creation
|
156
159
|
expect do
|
157
160
|
post.update_attributes(rating: 'untracked')
|
158
161
|
end.to change(Tracker, :count).by(0)
|
@@ -188,8 +191,9 @@ describe Mongoid::History do
|
|
188
191
|
end
|
189
192
|
|
190
193
|
it 'should assign version on post' do
|
194
|
+
expect(post.version).to eq(1) # Created
|
191
195
|
post.update_attributes(title: 'Another Test')
|
192
|
-
expect(post.version).to eq(
|
196
|
+
expect(post.version).to eq(2) # Updated
|
193
197
|
end
|
194
198
|
|
195
199
|
it 'should assign scope' do
|
@@ -205,16 +209,16 @@ describe Mongoid::History do
|
|
205
209
|
it 'should exclude defined options' do
|
206
210
|
name = user.name
|
207
211
|
user.update_attributes(name: 'Aaron2', email: 'aaronsnewemail@randomemail.com')
|
208
|
-
expect(user.history_tracks.
|
209
|
-
expect(user.history_tracks.
|
210
|
-
expect(user.history_tracks.
|
211
|
-
expect(user.history_tracks.
|
212
|
+
expect(user.history_tracks.last.original.keys).to eq(['n'])
|
213
|
+
expect(user.history_tracks.last.original['n']).to eq(name)
|
214
|
+
expect(user.history_tracks.last.modified.keys).to eq(['n'])
|
215
|
+
expect(user.history_tracks.last.modified['n']).to eq(user.name)
|
212
216
|
end
|
213
217
|
|
214
218
|
it 'should undo field changes' do
|
215
219
|
name = user.name
|
216
220
|
user.update_attributes(name: 'Aaron2', email: 'aaronsnewemail@randomemail.com')
|
217
|
-
user.history_tracks.
|
221
|
+
user.history_tracks.last.undo! nil
|
218
222
|
expect(user.reload.name).to eq(name)
|
219
223
|
end
|
220
224
|
|
@@ -223,21 +227,21 @@ describe Mongoid::History do
|
|
223
227
|
expect(post.reload.title).to be_nil
|
224
228
|
post.update_attributes(title: 'Aaron2')
|
225
229
|
expect(post.reload.title).to eq('Aaron2')
|
226
|
-
post.history_tracks.
|
230
|
+
post.history_tracks.last.undo! nil
|
227
231
|
expect(post.reload.title).to be_nil
|
228
232
|
end
|
229
233
|
|
230
234
|
it 'should track array changes' do
|
231
235
|
aliases = user.aliases
|
232
236
|
user.update_attributes(aliases: %w[bob joe])
|
233
|
-
expect(user.history_tracks.
|
234
|
-
expect(user.history_tracks.
|
237
|
+
expect(user.history_tracks.last.original['aliases']).to eq(aliases)
|
238
|
+
expect(user.history_tracks.last.modified['aliases']).to eq(user.aliases)
|
235
239
|
end
|
236
240
|
|
237
241
|
it 'should undo array changes' do
|
238
242
|
aliases = user.aliases
|
239
243
|
user.update_attributes(aliases: %w[bob joe])
|
240
|
-
user.history_tracks.
|
244
|
+
user.history_tracks.last.undo! nil
|
241
245
|
expect(user.reload.aliases).to eq(aliases)
|
242
246
|
end
|
243
247
|
end
|
@@ -259,7 +263,7 @@ describe Mongoid::History do
|
|
259
263
|
end
|
260
264
|
end
|
261
265
|
context 'update action' do
|
262
|
-
subject { user.history_tracks.
|
266
|
+
subject { user.history_tracks.last.tracked_changes }
|
263
267
|
before do
|
264
268
|
user.update_attributes(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
|
265
269
|
end
|
@@ -305,7 +309,7 @@ describe Mongoid::History do
|
|
305
309
|
end
|
306
310
|
end
|
307
311
|
context 'update action' do
|
308
|
-
subject { user.history_tracks.
|
312
|
+
subject { user.history_tracks.last.tracked_edits }
|
309
313
|
before do
|
310
314
|
user.update_attributes(name: 'Aaron2', email: nil, country: '', city: nil, phone: '867-5309', aliases: ['', 'bill', 'james'])
|
311
315
|
end
|
@@ -348,12 +352,14 @@ describe Mongoid::History do
|
|
348
352
|
|
349
353
|
describe 'on update non-embedded twice' do
|
350
354
|
it 'should assign version on post' do
|
355
|
+
expect(post.version).to eq(1)
|
351
356
|
post.update_attributes(title: 'Test2')
|
352
357
|
post.update_attributes(title: 'Test3')
|
353
|
-
expect(post.version).to eq(
|
358
|
+
expect(post.version).to eq(3)
|
354
359
|
end
|
355
360
|
|
356
361
|
it 'should create a history track if changed attributes match tracked attributes' do
|
362
|
+
post # Created
|
357
363
|
expect do
|
358
364
|
post.update_attributes(title: 'Test2')
|
359
365
|
post.update_attributes(title: 'Test3')
|
@@ -369,7 +375,7 @@ describe Mongoid::History do
|
|
369
375
|
it 'should assign modified fields' do
|
370
376
|
post.update_attributes(title: 'Test2')
|
371
377
|
post.update_attributes(title: 'Test3')
|
372
|
-
expect(post.history_tracks.where(version:
|
378
|
+
expect(post.history_tracks.where(version: 3).first.modified).to eq(
|
373
379
|
'title' => 'Test3'
|
374
380
|
)
|
375
381
|
end
|
@@ -377,7 +383,7 @@ describe Mongoid::History do
|
|
377
383
|
it 'should assign original fields' do
|
378
384
|
post.update_attributes(title: 'Test2')
|
379
385
|
post.update_attributes(title: 'Test3')
|
380
|
-
expect(post.history_tracks.where(version:
|
386
|
+
expect(post.history_tracks.where(version: 3).first.original).to eq(
|
381
387
|
'title' => 'Test2'
|
382
388
|
)
|
383
389
|
end
|
@@ -504,7 +510,7 @@ describe Mongoid::History do
|
|
504
510
|
it 'should be possible to create with redo after undo create embedded from parent' do
|
505
511
|
comment # initialize
|
506
512
|
post.comments.create!(title: 'The second one')
|
507
|
-
track = post.history_tracks
|
513
|
+
track = post.history_tracks[2]
|
508
514
|
track.undo!(user)
|
509
515
|
track.redo!(user)
|
510
516
|
post.reload
|
@@ -552,14 +558,14 @@ describe Mongoid::History do
|
|
552
558
|
describe 'non-embedded' do
|
553
559
|
it 'should undo changes' do
|
554
560
|
post.update_attributes(title: 'Test2')
|
555
|
-
post.history_tracks.where(version:
|
561
|
+
post.history_tracks.where(version: 2).last.undo!(user)
|
556
562
|
post.reload
|
557
563
|
expect(post.title).to eq('Test')
|
558
564
|
end
|
559
565
|
|
560
566
|
it 'should undo destruction' do
|
561
567
|
post.destroy
|
562
|
-
post.history_tracks.where(version:
|
568
|
+
post.history_tracks.where(version: 2).last.undo!(user)
|
563
569
|
expect(Post.find(post.id).title).to eq('Test')
|
564
570
|
end
|
565
571
|
|
@@ -568,12 +574,12 @@ describe Mongoid::History do
|
|
568
574
|
post.update_attributes(title: 'Test2')
|
569
575
|
post.history_tracks.last.undo!(user)
|
570
576
|
post.reload
|
571
|
-
expect(post.history_tracks.count).to eq(
|
577
|
+
expect(post.history_tracks.count).to eq(4)
|
572
578
|
end
|
573
579
|
|
574
580
|
it 'should assign user as the modifier of the newly created history track' do
|
575
581
|
post.update_attributes(title: 'Test2')
|
576
|
-
post.history_tracks.where(version:
|
582
|
+
post.history_tracks.where(version: 2).last.undo!(user)
|
577
583
|
post.reload
|
578
584
|
expect(post.history_tracks.where(version: 2).last.modifier).to eq(user)
|
579
585
|
end
|
@@ -590,7 +596,7 @@ describe Mongoid::History do
|
|
590
596
|
|
591
597
|
it 'should be destroyed after undo and redo' do
|
592
598
|
post.destroy
|
593
|
-
track = post.history_tracks.where(version:
|
599
|
+
track = post.history_tracks.where(version: 2).last
|
594
600
|
track.undo!(user)
|
595
601
|
track.redo!(user)
|
596
602
|
expect(Post.where(_id: post.id).first).to be_nil
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
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
|
8
|
+
|
9
|
+
field :foo
|
10
|
+
end
|
11
|
+
|
12
|
+
expect(NotModel.respond_to?(:tracked?)).to be false
|
13
|
+
end
|
14
|
+
|
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
|
19
|
+
|
20
|
+
track_history on: :fields
|
21
|
+
|
22
|
+
field :foo
|
23
|
+
end
|
24
|
+
|
25
|
+
expect(InsideBeforeModel.tracked?(:foo)).to be true
|
26
|
+
end
|
27
|
+
|
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
|
32
|
+
|
33
|
+
field :foo
|
34
|
+
|
35
|
+
track_history on: :fields
|
36
|
+
end
|
37
|
+
|
38
|
+
expect(InsideAfterModel.tracked?(:foo)).to be true
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should track fields when track_history called outside class' do
|
42
|
+
class OutsideModel
|
43
|
+
include Mongoid::Document
|
44
|
+
include Mongoid::History::Trackable
|
45
|
+
|
46
|
+
field :foo
|
47
|
+
end
|
48
|
+
|
49
|
+
OutsideModel.track_history on: :fields
|
50
|
+
expect(OutsideModel.tracked?(:foo)).to be true
|
51
|
+
end
|
52
|
+
end
|
@@ -28,7 +28,7 @@ describe Mongoid::History::Attributes::Destroy do
|
|
28
28
|
obj_one.save!
|
29
29
|
end
|
30
30
|
let(:obj_one) { model_one.new(foo: 'Foo', bar: 'Bar') }
|
31
|
-
it { is_expected.to eq('_id' => [obj_one._id, nil], 'foo' => ['Foo', nil]) }
|
31
|
+
it { is_expected.to eq('_id' => [obj_one._id, nil], 'foo' => ['Foo', nil], 'version' => [1, nil]) }
|
32
32
|
end
|
33
33
|
|
34
34
|
describe '#insert_embeds_one_changes' do
|
@@ -110,6 +110,44 @@ describe Mongoid::History::Attributes::Update do
|
|
110
110
|
Object.send(:remove_const, :EmbOne)
|
111
111
|
end
|
112
112
|
end
|
113
|
+
|
114
|
+
context 'when original and modified value same' do
|
115
|
+
before(:all) do
|
116
|
+
class DummyUpdateModel
|
117
|
+
include Mongoid::Document
|
118
|
+
include Mongoid::History::Trackable
|
119
|
+
store_in collection: :dummy_update_models
|
120
|
+
embeds_one :dummy_embedded_model
|
121
|
+
track_history on: :fields
|
122
|
+
end
|
123
|
+
|
124
|
+
class DummyEmbeddedModel
|
125
|
+
include Mongoid::Document
|
126
|
+
field :em_foo
|
127
|
+
field :em_bar
|
128
|
+
embedded_in :dummy_update_model
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
before(:each) do
|
133
|
+
DummyUpdateModel.clear_trackable_memoization
|
134
|
+
allow(base).to receive(:changes) { changes }
|
135
|
+
DummyUpdateModel.track_history on: :dummy_embedded_model
|
136
|
+
end
|
137
|
+
|
138
|
+
let(:obj_one) { DummyUpdateModel.new }
|
139
|
+
let(:base) { described_class.new(obj_one) }
|
140
|
+
let(:changes) do
|
141
|
+
{ 'dummy_embedded_model' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }] }
|
142
|
+
end
|
143
|
+
subject { base.attributes }
|
144
|
+
it { expect(subject.keys).to_not include 'dummy_embedded_model' }
|
145
|
+
|
146
|
+
after(:all) do
|
147
|
+
Object.send(:remove_const, :DummyUpdateModel)
|
148
|
+
Object.send(:remove_const, :DummyEmbeddedModel)
|
149
|
+
end
|
150
|
+
end
|
113
151
|
end
|
114
152
|
|
115
153
|
describe '#insert_embeds_many_changes' do
|
@@ -199,11 +237,84 @@ describe Mongoid::History::Attributes::Update do
|
|
199
237
|
it 'should save audit history under relation alias' do
|
200
238
|
expect(subject['eons']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
|
201
239
|
end
|
240
|
+
|
241
|
+
after(:all) do
|
242
|
+
Object.send(:remove_const, :ModelOne)
|
243
|
+
Object.send(:remove_const, :EmbOne)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
context 'when original and modified value same' do
|
248
|
+
before(:all) do
|
249
|
+
class ModelOne
|
250
|
+
include Mongoid::Document
|
251
|
+
include Mongoid::History::Trackable
|
252
|
+
store_in collection: :model_ones
|
253
|
+
embeds_many :emb_ones
|
254
|
+
track_history on: :fields
|
255
|
+
end
|
256
|
+
|
257
|
+
class EmbOne
|
258
|
+
include Mongoid::Document
|
259
|
+
field :em_foo
|
260
|
+
field :em_bar
|
261
|
+
embedded_in :model_one
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
before(:each) do
|
266
|
+
ModelOne.clear_trackable_memoization
|
267
|
+
allow(base).to receive(:changes) { changes }
|
268
|
+
ModelOne.track_history on: :emb_ones
|
269
|
+
end
|
270
|
+
|
271
|
+
let(:obj_one) { ModelOne.new }
|
272
|
+
let(:base) { described_class.new(obj_one) }
|
273
|
+
let(:changes) do
|
274
|
+
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }]] }
|
275
|
+
end
|
276
|
+
subject { base.attributes }
|
277
|
+
it { expect(subject.keys).to_not include 'emb_ones' }
|
278
|
+
|
279
|
+
after(:all) do
|
280
|
+
Object.send(:remove_const, :ModelOne)
|
281
|
+
Object.send(:remove_const, :EmbOne)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
context 'when original and modified values blank' do
|
287
|
+
before(:all) do
|
288
|
+
class DummyParent
|
289
|
+
include Mongoid::Document
|
290
|
+
include Mongoid::History::Trackable
|
291
|
+
store_in collection: :dummy_parents
|
292
|
+
has_and_belongs_to_many :other_dummy_parents
|
293
|
+
track_history on: :fields
|
294
|
+
end
|
295
|
+
|
296
|
+
class OtherDummyParent
|
297
|
+
include Mongoid::Document
|
298
|
+
has_and_belongs_to_many :dummy_parents
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
before(:each) do
|
303
|
+
DummyParent.clear_trackable_memoization
|
304
|
+
allow(base).to receive(:changes) { changes }
|
305
|
+
DummyParent.track_history on: :other_dummy_parent_ids
|
306
|
+
end
|
307
|
+
|
308
|
+
let(:base) { described_class.new(DummyParent.new) }
|
309
|
+
let(:changes) do
|
310
|
+
{ 'other_dummy_parent_ids' => [nil, []] }
|
202
311
|
end
|
312
|
+
subject { base.attributes }
|
313
|
+
it { expect(subject.keys).to_not include 'other_dummy_parent_ids' }
|
203
314
|
|
204
315
|
after(:all) do
|
205
|
-
Object.send(:remove_const, :
|
206
|
-
Object.send(:remove_const, :
|
316
|
+
Object.send(:remove_const, :DummyParent)
|
317
|
+
Object.send(:remove_const, :OtherDummyParent)
|
207
318
|
end
|
208
319
|
end
|
209
320
|
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::History::Options do
|
4
|
+
describe ':if' do
|
5
|
+
let(:dummy_model) do
|
6
|
+
Class.new do
|
7
|
+
include Mongoid::Document
|
8
|
+
include Mongoid::History::Trackable
|
9
|
+
store_in collection: :model_ones
|
10
|
+
field :foo
|
11
|
+
attr_accessor :bar
|
12
|
+
track_history track_create: true,
|
13
|
+
track_update: true,
|
14
|
+
track_destroy: true,
|
15
|
+
if: :bar
|
16
|
+
def self.name
|
17
|
+
'DummyModel'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
let(:obj) { dummy_model.new(foo: 'Foo') }
|
22
|
+
|
23
|
+
context 'when condition evaluates to true' do
|
24
|
+
before { obj.bar = true }
|
25
|
+
it 'should create history tracks' do
|
26
|
+
expect { obj.save }.to change(Tracker, :count).by(1)
|
27
|
+
expect do
|
28
|
+
obj.update_attributes(foo: 'Foo-2')
|
29
|
+
end.to change(Tracker, :count).by(1)
|
30
|
+
expect { obj.destroy }.to change(Tracker, :count).by(1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when condition evaluates to false' do
|
35
|
+
before { obj.bar = false }
|
36
|
+
it 'should not create history tracks' do
|
37
|
+
expect { obj.save }.to_not change(Tracker, :count)
|
38
|
+
expect do
|
39
|
+
obj.update_attributes(foo: 'Foo-2')
|
40
|
+
end.to_not change(Tracker, :count)
|
41
|
+
expect { obj.destroy }.to_not change(Tracker, :count)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe ':unless' do
|
47
|
+
let(:dummy_model) do
|
48
|
+
Class.new do
|
49
|
+
include Mongoid::Document
|
50
|
+
include Mongoid::History::Trackable
|
51
|
+
store_in collection: :model_ones
|
52
|
+
field :foo
|
53
|
+
attr_accessor :bar
|
54
|
+
track_history track_create: true,
|
55
|
+
track_update: true,
|
56
|
+
track_destroy: true,
|
57
|
+
unless: ->(obj) { obj.bar }
|
58
|
+
def self.name
|
59
|
+
'DummyModel'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
let(:obj) { dummy_model.new(foo: 'Foo') }
|
64
|
+
|
65
|
+
context 'when condition evaluates to true' do
|
66
|
+
before { obj.bar = true }
|
67
|
+
it 'should not create history tracks' do
|
68
|
+
expect { obj.save }.to_not change(Tracker, :count)
|
69
|
+
expect do
|
70
|
+
obj.update_attributes(foo: 'Foo-2')
|
71
|
+
end.to_not change(Tracker, :count)
|
72
|
+
expect { obj.destroy }.to_not change(Tracker, :count)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when condition evaluates to false' do
|
77
|
+
before { obj.bar = false }
|
78
|
+
it 'should create history tracks' do
|
79
|
+
expect { obj.save }.to change(Tracker, :count).by(1)
|
80
|
+
expect do
|
81
|
+
obj.update_attributes(foo: 'Foo-2')
|
82
|
+
end.to change(Tracker, :count).by(1)
|
83
|
+
expect { obj.destroy }.to change(Tracker, :count).by(1)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe ':if and :unless' do
|
89
|
+
let(:dummy_model) do
|
90
|
+
Class.new do
|
91
|
+
include Mongoid::Document
|
92
|
+
include Mongoid::History::Trackable
|
93
|
+
store_in collection: :model_ones
|
94
|
+
field :foo
|
95
|
+
attr_accessor :bar, :baz
|
96
|
+
track_history track_create: true,
|
97
|
+
track_update: true,
|
98
|
+
track_destroy: true,
|
99
|
+
if: :bar, unless: ->(obj) { obj.baz }
|
100
|
+
def self.name
|
101
|
+
'DummyModel'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
let(:obj) { dummy_model.new(foo: 'Foo') }
|
106
|
+
|
107
|
+
context 'when :if condition evaluates to true' do
|
108
|
+
before { obj.bar = true }
|
109
|
+
|
110
|
+
context 'and :unless condition evaluates to true' do
|
111
|
+
before { obj.baz = true }
|
112
|
+
it 'should not create history tracks' do
|
113
|
+
expect { obj.save }.to_not change(Tracker, :count)
|
114
|
+
expect do
|
115
|
+
obj.update_attributes(foo: 'Foo-2')
|
116
|
+
end.to_not change(Tracker, :count)
|
117
|
+
expect { obj.destroy }.to_not change(Tracker, :count)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'and :unless condition evaluates to false' do
|
122
|
+
before { obj.baz = false }
|
123
|
+
it 'should create history tracks' do
|
124
|
+
expect { obj.save }.to change(Tracker, :count).by(1)
|
125
|
+
expect do
|
126
|
+
obj.update_attributes(foo: 'Foo-2')
|
127
|
+
end.to change(Tracker, :count).by(1)
|
128
|
+
expect { obj.destroy }.to change(Tracker, :count).by(1)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'when :if condition evaluates to false' do
|
134
|
+
before { obj.bar = false }
|
135
|
+
|
136
|
+
context 'and :unless condition evaluates to true' do
|
137
|
+
before { obj.baz = true }
|
138
|
+
it 'should not create history tracks' do
|
139
|
+
expect { obj.save }.to_not change(Tracker, :count)
|
140
|
+
expect do
|
141
|
+
obj.update_attributes(foo: 'Foo-2')
|
142
|
+
end.to_not change(Tracker, :count)
|
143
|
+
expect { obj.destroy }.to_not change(Tracker, :count)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'and :unless condition evaluates to false' do
|
148
|
+
before { obj.baz = false }
|
149
|
+
it 'should not create history tracks' do
|
150
|
+
expect { obj.save }.to_not change(Tracker, :count)
|
151
|
+
expect do
|
152
|
+
obj.update_attributes(foo: 'Foo-2')
|
153
|
+
end.to_not change(Tracker, :count)
|
154
|
+
expect { obj.destroy }.to_not change(Tracker, :count)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
data/spec/unit/options_spec.rb
CHANGED
@@ -42,7 +42,8 @@ describe Mongoid::History::Options do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
let(:
|
45
|
+
let(:options) { {} }
|
46
|
+
let(:service) { described_class.new(ModelOne, options) }
|
46
47
|
|
47
48
|
subject { service }
|
48
49
|
|
@@ -67,9 +68,9 @@ describe Mongoid::History::Options do
|
|
67
68
|
version_field: :version,
|
68
69
|
changes_method: :changes,
|
69
70
|
scope: :model_one,
|
70
|
-
track_create:
|
71
|
+
track_create: true,
|
71
72
|
track_update: true,
|
72
|
-
track_destroy:
|
73
|
+
track_destroy: true,
|
73
74
|
format: nil }
|
74
75
|
end
|
75
76
|
it { expect(service.send(:default_options)).to eq expected_options }
|
@@ -77,7 +78,7 @@ describe Mongoid::History::Options do
|
|
77
78
|
|
78
79
|
describe '#prepare_skipped_fields' do
|
79
80
|
let(:options) { { except: value } }
|
80
|
-
subject { service.
|
81
|
+
subject { service.prepared }
|
81
82
|
|
82
83
|
context 'with field' do
|
83
84
|
let(:value) { :foo }
|
@@ -107,7 +108,7 @@ describe Mongoid::History::Options do
|
|
107
108
|
|
108
109
|
describe '#prepare_formatted_fields' do
|
109
110
|
let(:options) { { format: value } }
|
110
|
-
subject { service.
|
111
|
+
subject { service.prepared }
|
111
112
|
|
112
113
|
context 'with non-hash' do
|
113
114
|
let(:value) { :foo }
|
@@ -135,19 +136,19 @@ describe Mongoid::History::Options do
|
|
135
136
|
version_field: :version,
|
136
137
|
changes_method: :changes,
|
137
138
|
scope: :model_one,
|
138
|
-
track_create:
|
139
|
+
track_create: true,
|
139
140
|
track_update: true,
|
140
|
-
track_destroy:
|
141
|
+
track_destroy: true,
|
141
142
|
fields: %w[foo b],
|
142
143
|
dynamic: [],
|
143
144
|
relations: { embeds_one: {}, embeds_many: {} },
|
144
145
|
format: {} }
|
145
146
|
end
|
146
|
-
it { expect(service.
|
147
|
+
it { expect(service.prepared).to eq expected_options }
|
147
148
|
end
|
148
149
|
|
149
150
|
context 'when options passed' do
|
150
|
-
subject { service.
|
151
|
+
subject { service.prepared }
|
151
152
|
|
152
153
|
describe '@options' do
|
153
154
|
let(:options) { { on: value } }
|
data/spec/unit/trackable_spec.rb
CHANGED
@@ -28,9 +28,16 @@ describe Mongoid::History::Trackable do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
describe '#track_history' do
|
31
|
+
class MyModelWithNoModifier
|
32
|
+
include Mongoid::Document
|
33
|
+
include Mongoid::History::Trackable
|
34
|
+
field :foo
|
35
|
+
end
|
36
|
+
|
31
37
|
before :all do
|
32
38
|
MyModel.track_history
|
33
39
|
@persisted_history_options = Mongoid::History.trackable_class_options
|
40
|
+
MyModelWithNoModifier.track_history modifier_field: nil
|
34
41
|
end
|
35
42
|
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
36
43
|
let(:expected_option) do
|
@@ -41,9 +48,9 @@ describe Mongoid::History::Trackable do
|
|
41
48
|
version_field: :version,
|
42
49
|
changes_method: :changes,
|
43
50
|
scope: :my_model,
|
44
|
-
track_create:
|
51
|
+
track_create: true,
|
45
52
|
track_update: true,
|
46
|
-
track_destroy:
|
53
|
+
track_destroy: true,
|
47
54
|
fields: %w[foo],
|
48
55
|
relations: { embeds_one: {}, embeds_many: {} },
|
49
56
|
dynamic: [],
|
@@ -53,7 +60,7 @@ describe Mongoid::History::Trackable do
|
|
53
60
|
let(:reserved_fields) { %w[_id version modifier_id] }
|
54
61
|
|
55
62
|
it 'should have default options' do
|
56
|
-
expect(Mongoid::History.trackable_class_options[:my_model]).to eq(expected_option)
|
63
|
+
expect(Mongoid::History.trackable_class_options[:my_model].prepared).to eq(expected_option)
|
57
64
|
end
|
58
65
|
|
59
66
|
it 'should define callback function #track_update' do
|
@@ -72,6 +79,31 @@ describe Mongoid::History::Trackable do
|
|
72
79
|
expect(MyModel.history_trackable_options).to eq(expected_option)
|
73
80
|
end
|
74
81
|
|
82
|
+
describe '#modifier' do
|
83
|
+
context 'modifier_field set to nil' do
|
84
|
+
it 'should not have a modifier relationship' do
|
85
|
+
expect(MyModelWithNoModifier.reflect_on_association(:modifier)).to be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'modifier_field_optional true' do
|
90
|
+
class MyModelWithOptionalModifier
|
91
|
+
include Mongoid::Document
|
92
|
+
include Mongoid::History::Trackable
|
93
|
+
field :foo
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'marks modifier relationship optional' do
|
97
|
+
MyModelWithOptionalModifier.track_history modifier_field_optional: true
|
98
|
+
if Mongoid::Compatibility::Version.mongoid6_or_newer?
|
99
|
+
expect(MyModelWithOptionalModifier.reflect_on_association(:modifier)[:optional]).to be true
|
100
|
+
else
|
101
|
+
expect(MyModelWithOptionalModifier.reflect_on_association(:modifier)).not_to be_nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
75
107
|
describe '#tracked_fields' do
|
76
108
|
it 'should return the tracked field list' do
|
77
109
|
expect(MyModel.tracked_fields).to eq(regular_fields)
|
@@ -82,6 +114,10 @@ describe Mongoid::History::Trackable do
|
|
82
114
|
it 'should return the protected field list' do
|
83
115
|
expect(MyModel.reserved_tracked_fields).to eq(reserved_fields)
|
84
116
|
end
|
117
|
+
|
118
|
+
it 'should not include modifier_field if not specified' do
|
119
|
+
expect(MyModelWithNoModifier.reserved_tracked_fields).not_to include('modifier')
|
120
|
+
end
|
85
121
|
end
|
86
122
|
|
87
123
|
describe '#tracked_fields_for_action' do
|
@@ -191,7 +227,7 @@ describe Mongoid::History::Trackable do
|
|
191
227
|
end
|
192
228
|
|
193
229
|
it 'should have default options' do
|
194
|
-
expect(Mongoid::History.trackable_class_options[:my_model]).to eq(expected_option)
|
230
|
+
expect(Mongoid::History.trackable_class_options[:my_model].prepared).to eq(expected_option)
|
195
231
|
end
|
196
232
|
|
197
233
|
it 'should define #history_trackable_options' do
|
@@ -559,6 +595,7 @@ describe Mongoid::History::Trackable do
|
|
559
595
|
describe 'fields' do
|
560
596
|
context 'when custom method for changes' do
|
561
597
|
before(:each) do
|
598
|
+
ModelOne.clear_trackable_memoization
|
562
599
|
ModelOne.track_history(on: :foo, changes_method: :my_changes_method)
|
563
600
|
allow(ModelOne).to receive(:dynamic_enabled?) { false }
|
564
601
|
allow(model_one).to receive(:my_changes_method) { changes }
|
@@ -590,7 +627,7 @@ describe Mongoid::History::Trackable do
|
|
590
627
|
@persisted_history_options = Mongoid::History.trackable_class_options
|
591
628
|
end
|
592
629
|
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
593
|
-
let(:m) { MyModel.create!(foo: 'bar') }
|
630
|
+
let!(:m) { MyModel.create!(foo: 'bar') }
|
594
631
|
|
595
632
|
it 'should create history' do
|
596
633
|
expect { m.update_attributes!(foo: 'bar2') }.to change(Tracker, :count).by(1)
|
@@ -610,7 +647,7 @@ describe Mongoid::History::Trackable do
|
|
610
647
|
@persisted_history_options = Mongoid::History.trackable_class_options
|
611
648
|
end
|
612
649
|
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
613
|
-
let(:m) { MyModel.create!(foo: 'bar') }
|
650
|
+
let!(:m) { MyModel.create!(foo: 'bar') }
|
614
651
|
|
615
652
|
it 'should create history' do
|
616
653
|
expect { m.destroy }.to change(Tracker, :count).by(1)
|
@@ -628,6 +665,7 @@ describe Mongoid::History::Trackable do
|
|
628
665
|
before :all do
|
629
666
|
MyModel.track_history(on: :foo, track_create: true)
|
630
667
|
@persisted_history_options = Mongoid::History.trackable_class_options
|
668
|
+
MyModelWithNoModifier.track_history modifier_field: nil
|
631
669
|
end
|
632
670
|
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
|
633
671
|
|
@@ -635,6 +673,12 @@ describe Mongoid::History::Trackable do
|
|
635
673
|
expect { MyModel.create!(foo: 'bar') }.to change(Tracker, :count).by(1)
|
636
674
|
end
|
637
675
|
|
676
|
+
context 'no modifier_field' do
|
677
|
+
it 'should create history' do
|
678
|
+
expect { MyModelWithNoModifier.create!(foo: 'bar').to change(Tracker, :count).by(1) }
|
679
|
+
end
|
680
|
+
end
|
681
|
+
|
638
682
|
it 'should not create history when error raised' do
|
639
683
|
expect(MyModel).to receive(:create!).and_raise(StandardError)
|
640
684
|
expect do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-history
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Qian
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2018-01-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: easy_diff
|
@@ -46,14 +46,14 @@ dependencies:
|
|
46
46
|
requirements:
|
47
47
|
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version:
|
49
|
+
version: 0.5.1
|
50
50
|
type: :runtime
|
51
51
|
prerelease: false
|
52
52
|
version_requirements: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
54
|
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version:
|
56
|
+
version: 0.5.1
|
57
57
|
- !ruby/object:Gem::Dependency
|
58
58
|
name: activesupport
|
59
59
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- spec/integration/nested_embedded_documents_spec.rb
|
120
120
|
- spec/integration/nested_embedded_polymorphic_documents_spec.rb
|
121
121
|
- spec/integration/subclasses_spec.rb
|
122
|
+
- spec/integration/track_history_order_spec.rb
|
122
123
|
- spec/integration/validation_failure_spec.rb
|
123
124
|
- spec/spec_helper.rb
|
124
125
|
- spec/support/mongoid.rb
|
@@ -127,6 +128,7 @@ files:
|
|
127
128
|
- spec/unit/attributes/create_spec.rb
|
128
129
|
- spec/unit/attributes/destroy_spec.rb
|
129
130
|
- spec/unit/attributes/update_spec.rb
|
131
|
+
- spec/unit/callback_options_spec.rb
|
130
132
|
- spec/unit/embedded_methods_spec.rb
|
131
133
|
- spec/unit/history_spec.rb
|
132
134
|
- spec/unit/my_instance_methods_spec.rb
|
@@ -156,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
158
|
version: '0'
|
157
159
|
requirements: []
|
158
160
|
rubyforge_project:
|
159
|
-
rubygems_version: 2.6.
|
161
|
+
rubygems_version: 2.6.13
|
160
162
|
signing_key:
|
161
163
|
specification_version: 4
|
162
164
|
summary: Track and audit, undo and redo changes on Mongoid documents.
|
@@ -168,6 +170,7 @@ test_files:
|
|
168
170
|
- spec/integration/nested_embedded_documents_spec.rb
|
169
171
|
- spec/integration/nested_embedded_polymorphic_documents_spec.rb
|
170
172
|
- spec/integration/subclasses_spec.rb
|
173
|
+
- spec/integration/track_history_order_spec.rb
|
171
174
|
- spec/integration/validation_failure_spec.rb
|
172
175
|
- spec/spec_helper.rb
|
173
176
|
- spec/support/mongoid.rb
|
@@ -176,6 +179,7 @@ test_files:
|
|
176
179
|
- spec/unit/attributes/create_spec.rb
|
177
180
|
- spec/unit/attributes/destroy_spec.rb
|
178
181
|
- spec/unit/attributes/update_spec.rb
|
182
|
+
- spec/unit/callback_options_spec.rb
|
179
183
|
- spec/unit/embedded_methods_spec.rb
|
180
184
|
- spec/unit/history_spec.rb
|
181
185
|
- spec/unit/my_instance_methods_spec.rb
|