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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d77abcedc5d3a8dd4dc76cc8d6320fbce3aaa1da
4
- data.tar.gz: 7ea0951e39d1ce44841a92dacd55d576c6542612
3
+ metadata.gz: 3e9cb97f94a3cb084d92c85412f2e4cf2cc67895
4
+ data.tar.gz: c61a511effb8ee2ebb559aad5a77c3ec800eff78
5
5
  SHA512:
6
- metadata.gz: 332314784de0fa89013bab41d9586eeae0e72ee743ce964561d384df270a255970fe23abbc6081f7b0a592085bba8d7cf4f056a3bf24e4deff0099a23628d2b2
7
- data.tar.gz: '0228bacf2516b4da7bd57db5171d3e92bc4b418c0df3fcc49fc366d783ed3bf789b2487abb9c32036996fb8ad975dd4adcfad1edeecfff72671bb3da5384b200'
6
+ metadata.gz: 0a6079af9f6834d5c65d38106fca027a243871087fd0669e1120b4b2c074dde16ddd5d992e01208c73cd4cd7959c0ef6d03e49af4de4b4fc8783a7445d9fac41
7
+ data.tar.gz: 3dd2f512a2591d3e2123ae2fcbacd615d70be5983b9d955e393bf0f54463a18cb08fec3afb8259cabe87163e4d1baa384f7225e91d181492ce01954f92c022ad
@@ -1,47 +1,56 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2017-12-08 18:06:04 +0530 using RuboCop version 0.48.1.
3
+ # on 2018-06-20 23:31:54 -0400 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
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 6
9
+ # Offense count: 7
10
10
  # Configuration parameters: Include.
11
11
  # Include: **/Gemfile, **/gems.rb
12
12
  Bundler/DuplicatedGem:
13
13
  Exclude:
14
14
  - 'Gemfile'
15
15
 
16
+ # Offense count: 1
17
+ # Cop supports --auto-correct.
18
+ # Configuration parameters: EnforcedStyleAlignWith, SupportedStylesAlignWith, AutoCorrect.
19
+ # SupportedStylesAlignWith: keyword, variable, start_of_line
20
+ Lint/EndAlignment:
21
+ Exclude:
22
+ - 'lib/mongoid/history/options.rb'
23
+
16
24
  # Offense count: 3
17
25
  Lint/HandleExceptions:
18
26
  Exclude:
19
27
  - 'spec/unit/trackable_spec.rb'
20
28
 
21
- # Offense count: 2
29
+ # Offense count: 3
22
30
  Lint/ParenthesesAsGroupedExpression:
23
31
  Exclude:
24
32
  - 'spec/integration/integration_spec.rb'
33
+ - 'spec/integration/nested_embedded_polymorphic_documents_spec.rb'
25
34
 
26
35
  # Offense count: 21
27
36
  Metrics/AbcSize:
28
- Max: 50
37
+ Max: 52
29
38
 
30
- # Offense count: 111
39
+ # Offense count: 117
31
40
  # Configuration parameters: CountComments, ExcludedMethods.
32
41
  Metrics/BlockLength:
33
- Max: 807
42
+ Max: 820
34
43
 
35
44
  # Offense count: 1
36
45
  # Configuration parameters: CountComments.
37
46
  Metrics/ClassLength:
38
- Max: 126
47
+ Max: 114
39
48
 
40
- # Offense count: 5
49
+ # Offense count: 6
41
50
  Metrics/CyclomaticComplexity:
42
- Max: 10
51
+ Max: 13
43
52
 
44
- # Offense count: 462
53
+ # Offense count: 392
45
54
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
46
55
  # URISchemes: http, https
47
56
  Metrics/LineLength:
@@ -55,11 +64,11 @@ Metrics/MethodLength:
55
64
  # Offense count: 2
56
65
  # Configuration parameters: CountComments.
57
66
  Metrics/ModuleLength:
58
- Max: 180
67
+ Max: 182
59
68
 
60
- # Offense count: 5
69
+ # Offense count: 6
61
70
  Metrics/PerceivedComplexity:
62
- Max: 12
71
+ Max: 15
63
72
 
64
73
  # Offense count: 12
65
74
  Style/Documentation:
@@ -90,11 +99,6 @@ Style/FileName:
90
99
  - 'Dangerfile'
91
100
  - 'lib/mongoid-history.rb'
92
101
 
93
- # Offense count: 1
94
- Style/IfInsideElse:
95
- Exclude:
96
- - 'lib/mongoid/history/trackable.rb'
97
-
98
102
  # Offense count: 1
99
103
  Style/MultilineBlockChain:
100
104
  Exclude:
@@ -12,6 +12,7 @@ env:
12
12
  - MONGOID_VERSION=4
13
13
  - MONGOID_VERSION=5
14
14
  - MONGOID_VERSION=6
15
+ - MONGOID_VERSION=7
15
16
  - MONGOID_VERSION=HEAD
16
17
 
17
18
  rvm:
@@ -1,3 +1,7 @@
1
+ ### 0.8.1 (2018/06/28)
2
+
3
+ * [#221](https://github.com/mongoid/mongoid-history/pull/221): Mongoid 7 support - [@dblock](https://github.com/dblock).
4
+
1
5
  ### 0.8.0 (2018/01/16)
2
6
 
3
7
  * [#180](https://github.com/mongoid/mongoid-history/pull/180): Removed deprecation notice - [@sivagollapalli](https://github.com/sivagollapalli).
@@ -1,16 +1,16 @@
1
1
  Contributing to Mongoid-History
2
2
  ===============================
3
3
 
4
- Mongoid-History is work of [many of contributors](https://github.com/intridea/mongoid-history/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/intridea/mongoid-history/pulls), [propose features, ask questions and discuss issues](https://github.com/intridea/mongoid-history/issues).
4
+ Mongoid-History is work of [many of contributors](https://github.com/mongoid/mongoid-history/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/mongoid/mongoid-history/pulls), [propose features, ask questions and discuss issues](https://github.com/mongoid/mongoid-history/issues).
5
5
 
6
6
  #### Fork the Project
7
7
 
8
- Fork the [project on Github](https://github.com/intridea/mongoid-history) and check out your copy.
8
+ Fork the [project on Github](https://github.com/mongoid/mongoid-history) and check out your copy.
9
9
 
10
10
  ```
11
11
  git clone https://github.com/contributor/mongoid-history.git
12
12
  cd mongoid-history
13
- git remote add upstream https://github.com/intridea/mongoid-history.git
13
+ git remote add upstream https://github.com/mongoid/mongoid-history.git
14
14
  ```
15
15
 
16
16
  #### Create a Topic Branch
@@ -95,7 +95,7 @@ git push origin my-feature-branch -f
95
95
  Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
96
96
 
97
97
  ```
98
- * [#123](https://github.com/intridea/mongoid-history/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
98
+ * [#123](https://github.com/mongoid/mongoid-history/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
99
99
  ```
100
100
 
101
101
  Amend your previous commit and force push the changes.
data/Gemfile CHANGED
@@ -2,19 +2,21 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- case version = ENV['MONGOID_VERSION'] || '~> 6.0.0'
5
+ case version = ENV['MONGOID_VERSION'] || '~> 7.0.0'
6
6
  when 'HEAD'
7
7
  gem 'mongoid', github: 'mongodb/mongoid'
8
- when /6/
8
+ when /\b7/
9
+ gem 'mongoid', '~> 7.0.0'
10
+ when /\b6/
9
11
  gem 'mongoid', '~> 6.0.0'
10
- when /5/
12
+ when /\b5/
11
13
  gem 'mongoid', '~> 5.0'
12
14
  gem 'mongoid-observers', '~> 0.2.0'
13
- when /4/
15
+ when /\b4/
14
16
  gem 'mongoid', '~> 4.0'
15
17
  gem 'mongoid-observers', '~> 0.2.0'
16
- when /3/
17
- gem 'mongoid', '~> 3.1'
18
+ when /\b3/
19
+ gem 'mongoid', '~> 3.1.7'
18
20
  else
19
21
  gem 'mongoid', version
20
22
  end
data/README.md CHANGED
@@ -3,7 +3,6 @@ mongoid-history
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/mongoid-history.svg)](http://badge.fury.io/rb/mongoid-history)
5
5
  [![Build Status](https://secure.travis-ci.org/mongoid/mongoid-history.svg?branch=master)](http://travis-ci.org/mongoid/mongoid-history)
6
- [![Dependency Status](https://gemnasium.com/mongoid/mongoid-history.svg)](https://gemnasium.com/mongoid/mongoid-history)
7
6
  [![Code Climate](https://codeclimate.com/github/mongoid/mongoid-history.svg)](https://codeclimate.com/github/mongoid/mongoid-history)
8
7
  [![Coverage Status](https://coveralls.io/repos/mongoid/mongoid-history/badge.svg)](https://coveralls.io/r/mongoid/mongoid-history?branch=coveralls)
9
8
 
@@ -14,7 +13,7 @@ This gem also implements multi-user undo, which allows users to undo any history
14
13
  Install
15
14
  -------
16
15
 
17
- This gem supports Mongoid 3, 4, 5 on Ruby 1.9.3 or newer and Mongoid 6 on Ruby 2.2.2+. Add it to your `Gemfile` or run `gem install mongoid-history`.
16
+ This gem supports Mongoid 3, 4, 5 on Ruby 1.9.3 or newer and Mongoid 6 and 7 on Ruby 2.2.2+. Add it to your `Gemfile` or run `gem install mongoid-history`.
18
17
 
19
18
  ```ruby
20
19
  gem 'mongoid-history'
@@ -389,7 +388,7 @@ In your View, you might do something like (example in HAML format):
389
388
 
390
389
  **Adding Userstamp on History Trackers**
391
390
 
392
- To track the User in the application who created the HistoryTracker, please add the
391
+ To track the User in the application who created the HistoryTracker, add the
393
392
  [Mongoid::Userstamp gem](https://github.com/tbpro/mongoid_userstamp) to your HistoryTracker class.
394
393
  This will add a field called `created_by` and an accessor `creator` to the model (you can rename these via gem config).
395
394
 
@@ -400,15 +399,6 @@ class MyHistoryTracker
400
399
  end
401
400
  ```
402
401
 
403
- *Migrating Userstamp from Previous Versions*
404
-
405
- Since October 2013 (mongoid-history version 0.4.1 and onwards), Mongoid::History itself no longer supports the userstamp natively. In order to migrate, follow the
406
- instructions above then run the following command:
407
-
408
- ```
409
- MyHistoryTracker.all.rename(modifier_id: :created_by)
410
- ```
411
-
412
402
  **Setting Modifier Class Name**
413
403
 
414
404
  If your app will track history changes to a user, Mongoid History looks for these modifiers in the ``User`` class by default. If you have named your 'user' accounts differently, you will need to add that to your Mongoid History config:
@@ -31,4 +31,23 @@ See [#202](https://github.com/mongoid/mongoid-history/pull/202) for more informa
31
31
 
32
32
  See [#189](https://github.com/mongoid/mongoid-history/pull/189) for more information.
33
33
 
34
- * Currently, the `:all` option behaves identically to `:fields`. Future versions will track all fields and relations of trackable class when using `:all`.
34
+ * Currently, the `:all` option behaves identically to `:fields`. Future versions will track all fields and relations of trackable class when using `:all`.
35
+
36
+ ### Upgrading to 0.4.1
37
+
38
+ #### Migrate Userstamp
39
+
40
+ `Mongoid::History` no longer supports the userstamp natively. To track the User in the application who created the HistoryTracker, add the [Mongoid::Userstamp gem](https://github.com/tbpro/mongoid_userstamp) to your HistoryTracker class.
41
+
42
+ ```ruby
43
+ class MyHistoryTracker
44
+ include Mongoid::History::Tracker
45
+ include Mongoid::Userstamp
46
+ end
47
+ ```
48
+
49
+ Rename the field.
50
+
51
+ ```ruby
52
+ MyHistoryTracker.all.rename(modifier_id: :created_by)
53
+ ```
@@ -42,11 +42,15 @@ module Mongoid
42
42
  def trackable_class_settings(trackable_class)
43
43
  trackable_settings[trackable_class.name.to_sym] || default_settings
44
44
  end
45
+
46
+ def reset!
47
+ Mongoid::History.modifier_class_name = 'User'
48
+ Mongoid::History.trackable_class_options = {}
49
+ Mongoid::History.trackable_settings = {}
50
+ Mongoid::History.current_user_method ||= :current_user
51
+ end
45
52
  end
46
53
  end
47
54
  end
48
55
 
49
- Mongoid::History.modifier_class_name = 'User'
50
- Mongoid::History.trackable_class_options = {}
51
- Mongoid::History.trackable_settings = {}
52
- Mongoid::History.current_user_method ||= :current_user
56
+ Mongoid::History.reset!
@@ -32,7 +32,7 @@ module Mongoid
32
32
 
33
33
  def format_embeds_one_relation(rel, obj)
34
34
  rel = trackable_class.database_field_name(rel)
35
- relation_class = trackable_class.embeds_one_class(rel)
35
+ relation_class = trackable_class.relation_class_of(rel)
36
36
  permitted_attrs = trackable_class.tracked_embeds_one_attributes(rel)
37
37
  formats = trackable_class.field_format(rel)
38
38
  format_relation(relation_class, obj, permitted_attrs, formats)
@@ -40,7 +40,7 @@ module Mongoid
40
40
 
41
41
  def format_embeds_many_relation(rel, obj)
42
42
  rel = trackable_class.database_field_name(rel)
43
- relation_class = trackable_class.embeds_many_class(rel)
43
+ relation_class = trackable_class.relation_class_of(rel)
44
44
  permitted_attrs = trackable_class.tracked_embeds_many_attributes(rel)
45
45
  formats = trackable_class.field_format(rel)
46
46
  format_relation(relation_class, obj, permitted_attrs, formats)
@@ -22,7 +22,7 @@ module Mongoid
22
22
 
23
23
  def insert_embeds_one_changes
24
24
  trackable_class.tracked_embeds_one.each do |rel|
25
- rel_class = trackable_class.embeds_one_class(rel)
25
+ rel_class = trackable_class.relation_class_of(rel)
26
26
  paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field]
27
27
  paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field
28
28
  rel = aliased_fields.key(rel) || rel
@@ -34,7 +34,7 @@ module Mongoid
34
34
 
35
35
  def insert_embeds_many_changes
36
36
  trackable_class.tracked_embeds_many.each do |rel|
37
- rel_class = trackable_class.embeds_many_class(rel)
37
+ rel_class = trackable_class.relation_class_of(rel)
38
38
  paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field]
39
39
  paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field
40
40
  rel = aliased_fields.key(rel) || rel
@@ -20,7 +20,7 @@ module Mongoid
20
20
 
21
21
  def insert_embeds_one_changes(relation, value)
22
22
  relation = trackable_class.database_field_name(relation)
23
- relation_class = trackable_class.embeds_one_class(relation)
23
+ relation_class = trackable_class.relation_class_of(relation)
24
24
  paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
25
25
  original_value = value[0][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[0])
26
26
  modified_value = value[1][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[1])
@@ -30,7 +30,7 @@ module Mongoid
30
30
 
31
31
  def insert_embeds_many_changes(relation, value)
32
32
  relation = trackable_class.database_field_name(relation)
33
- relation_class = trackable_class.embeds_many_class(relation)
33
+ relation_class = trackable_class.relation_class_of(relation)
34
34
  paranoia_field = Mongoid::History.trackable_class_settings(relation_class)[:paranoia_field]
35
35
  original_value = value[0].reject { |rel| rel[paranoia_field].present? }
36
36
  .map { |v_attrs| format_embeds_many_relation(relation, v_attrs) }
@@ -57,10 +57,10 @@ module Mongoid
57
57
  field = trackable.database_field_name(field)
58
58
 
59
59
  if format.class == Hash && trackable.embeds_many?(field)
60
- relation_class = trackable.embeds_many_class(field)
60
+ relation_class = trackable.relation_class_of(field)
61
61
  formats[field] = format.inject({}) { |a, e| a.merge(relation_class.database_field_name(e.first) => e.last) }
62
62
  elsif format.class == Hash && trackable.embeds_one?(field)
63
- relation_class = trackable.embeds_one_class(field)
63
+ relation_class = trackable.relation_class_of(field)
64
64
  formats[field] = format.inject({}) { |a, e| a.merge(relation_class.database_field_name(e.first) => e.last) }
65
65
  else
66
66
  formats[field] = format
@@ -143,9 +143,9 @@ module Mongoid
143
143
  field_options = Array(field_options)
144
144
 
145
145
  if trackable.embeds_one?(field)
146
- track_embeds_one(field, field_options)
146
+ track_relation(field, :embeds_one, field_options)
147
147
  elsif trackable.embeds_many?(field)
148
- track_embeds_many(field, field_options)
148
+ track_relation(field, :embeds_many, field_options)
149
149
  elsif trackable.fields.keys.include?(field)
150
150
  @options[:fields] << field
151
151
  else
@@ -153,22 +153,13 @@ module Mongoid
153
153
  end
154
154
  end
155
155
 
156
- def track_embeds_one(field, field_options)
157
- relation_class = trackable.embeds_one_class(field)
158
- @options[:relations][:embeds_one][field] = if field_options.blank?
159
- relation_class.fields.keys
160
- else
161
- %w[_id] | field_options.map { |opt| relation_class.database_field_name(opt) }
162
- end
163
- end
164
-
165
- def track_embeds_many(field, field_options)
166
- relation_class = trackable.embeds_many_class(field)
167
- @options[:relations][:embeds_many][field] = if field_options.blank?
168
- relation_class.fields.keys
169
- else
170
- %w[_id] | field_options.map { |opt| relation_class.database_field_name(opt) }
171
- end
156
+ def track_relation(field, kind, field_options)
157
+ relation_class = trackable.relation_class_of(field)
158
+ @options[:relations][kind][field] = if field_options.blank?
159
+ relation_class.fields.keys
160
+ else
161
+ %w[_id] | field_options.map { |opt| relation_class.database_field_name(opt) }
162
+ end
172
163
  end
173
164
 
174
165
  def reserved_fields
@@ -5,7 +5,7 @@ module Mongoid
5
5
 
6
6
  module ClassMethods
7
7
  def track_history(options = {})
8
- extend EmbeddedMethods
8
+ extend RelationMethods
9
9
 
10
10
  history_options = Mongoid::History::Options.new(self, options)
11
11
 
@@ -114,12 +114,12 @@ module Mongoid
114
114
  end
115
115
  end
116
116
 
117
- def get_embedded(name)
118
- send(self.class.embedded_alias(name))
117
+ def _get_relation(name)
118
+ send(self.class.relation_alias(name))
119
119
  end
120
120
 
121
- def create_embedded(name, value)
122
- send("create_#{self.class.embedded_alias(name)}!", value)
121
+ def _create_relation(name, value)
122
+ send("create_#{self.class.relation_alias(name)}!", value)
123
123
  end
124
124
 
125
125
  private
@@ -157,8 +157,10 @@ module Mongoid
157
157
  scope = _parent.collection_name.to_s.singularize.to_sym if scope.is_a?(Array)
158
158
  if Mongoid::Compatibility::Version.mongoid3?
159
159
  scope = metadata.inverse_class_name.tableize.singularize.to_sym if metadata.present? && scope == metadata.as
160
- else
160
+ elsif Mongoid::Compatibility::Version.mongoid6_or_older?
161
161
  scope = relation_metadata.inverse_class_name.tableize.singularize.to_sym if relation_metadata.present? && scope == relation_metadata.as
162
+ elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
163
+ scope = _association.inverse_class_name.tableize.singularize.to_sym if _association.present? && scope == _association.as
162
164
  end
163
165
  end
164
166
 
@@ -173,15 +175,18 @@ module Mongoid
173
175
 
174
176
  def association_hash(node = self)
175
177
  # We prefer to look up associations through the parent record because
176
- # we're assured, through the object creation, it'll exist. Whereas we're not guarenteed
178
+ # we're assured, through the object creation, it'll exist. Whereas we're not guaranteed
177
179
  # the child to parent (embedded_in, belongs_to) relation will be defined
178
180
  if node._parent
179
181
  meta = node._parent.relations.values.find do |relation|
180
182
  if Mongoid::Compatibility::Version.mongoid3?
181
183
  relation.class_name == node.metadata.class_name.to_s && relation.name == node.metadata.name
182
- else
184
+ elsif Mongoid::Compatibility::Version.mongoid6_or_older?
183
185
  relation.class_name == node.relation_metadata.class_name.to_s &&
184
186
  relation.name == node.relation_metadata.name
187
+ elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
188
+ relation.class_name == node._association.class_name.to_s &&
189
+ relation.name == node._association.name
185
190
  end
186
191
  end
187
192
  end
@@ -267,6 +272,12 @@ module Mongoid
267
272
  [original, modified]
268
273
  end
269
274
 
275
+ def increment_current_version
276
+ current_version = (send(history_trackable_options[:version_field]) || 0) + 1
277
+ send("#{history_trackable_options[:version_field]}=", current_version)
278
+ current_version
279
+ end
280
+
270
281
  protected
271
282
 
272
283
  def track_history_for_action?(action)
@@ -275,8 +286,7 @@ module Mongoid
275
286
 
276
287
  def track_history_for_action(action)
277
288
  if track_history_for_action?(action)
278
- current_version = (send(history_trackable_options[:version_field]) || 0) + 1
279
- send("#{history_trackable_options[:version_field]}=", current_version)
289
+ current_version = increment_current_version
280
290
  last_track = self.class.tracker_class.create!(history_tracker_attributes(action.to_sym).merge(version: current_version, action: action.to_s, trackable: self))
281
291
  end
282
292
 
@@ -294,80 +304,85 @@ module Mongoid
294
304
  end
295
305
  end
296
306
 
297
- module EmbeddedMethods
298
- # Indicates whether there is an Embedded::One relation for the given embedded field.
307
+ module RelationMethods
308
+ # Returns a relation class for the given field.
299
309
  #
300
- # @param [ String | Symbol ] embed The name of the embedded field
310
+ # @param [ String | Symbol ] field The name of the field.
301
311
  #
302
- # @return [ Boolean ] true if there is an Embedded::One relation for the given embedded field
303
- def embeds_one?(embed)
304
- relation_of(embed) == Mongoid::Relations::Embedded::One
312
+ # @return [ nil | Constant ] Class being related.
313
+ def relation_class_of(field)
314
+ meta = meta_of(field)
315
+ return meta.class_name.constantize if meta
305
316
  end
306
317
 
307
- # Return the class for embeds_one relation
318
+ # Indicates whether there is an Embedded::One relation for the given embedded field.
308
319
  #
309
- # @param [ String ] field The database field name for embedded relation
320
+ # @param [ String | Symbol ] embed The name of the embedded field.
310
321
  #
311
- # @return [ nil | Constant ]
312
- def embeds_one_class(field)
313
- @embeds_one_class ||= {}
314
- return @embeds_one_class[field] if @embeds_one_class.key?(field)
315
- field_alias = aliased_fields.key(field)
316
- relation = relations
317
- .select { |_, v| v.relation == Mongoid::Relations::Embedded::One }
318
- .detect { |rel_k, _| rel_k == field_alias }
319
- @embeds_one_class[field] = relation && relation.last.class_name.constantize
322
+ # @return [ Boolean ] true if there is an Embedded::One relation for the given embedded field.
323
+ def embeds_one?(field)
324
+ relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
325
+ Mongoid::Association::Embedded::EmbedsOne::Proxy
326
+ else
327
+ Mongoid::Relations::Embedded::One
328
+ end
320
329
  end
321
330
 
322
331
  # Indicates whether there is an Embedded::Many relation for the given embedded field.
323
332
  #
324
- # @param [ String | Symbol ] embed The name of the embedded field
333
+ # @param [ String | Symbol ] field The name of the embedded field.
325
334
  #
326
- # @return [ Boolean ] true if there is an Embedded::Many relation for the given embedded field
327
- def embeds_many?(embed)
328
- relation_of(embed) == Mongoid::Relations::Embedded::Many
335
+ # @return [ Boolean ] true if there is an Embedded::Many relation for the given embedded field.
336
+ def embeds_many?(field)
337
+ relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
338
+ Mongoid::Association::Embedded::EmbedsMany::Proxy
339
+ else
340
+ Mongoid::Relations::Embedded::Many
341
+ end
329
342
  end
330
343
 
331
- # Return the class for embeds_many relation
344
+ # Retrieves the database representation of an embedded field name, in case the :store_as option is used.
332
345
  #
333
- # @param [ String ] field The database field name for embedded relation
346
+ # @param [ String | Symbol ] embed The name or alias of the embedded field.
334
347
  #
335
- # @return [ nil | Constant ]
336
- def embeds_many_class(field)
337
- @embeds_many_class ||= {}
338
- return @embeds_many_class[field] if @embeds_many_class.key?(field)
339
- field_alias = aliased_fields.key(field)
340
- relation = relations
341
- .select { |_, v| v.relation == Mongoid::Relations::Embedded::Many }
342
- .detect { |rel_k, _| rel_k == field_alias }
343
- @embeds_many_class[field] = relation && relation.last.class_name.constantize
348
+ # @return [ String ] The database name of the embedded field.
349
+ def relation_alias(embed)
350
+ relation_aliases[embed]
344
351
  end
345
352
 
346
- # Retrieves the database representation of an embedded field name, in case the :store_as option is used.
353
+ protected
354
+
355
+ # Return the reflected metadata for a relation.
347
356
  #
348
- # @param [ String | Symbol ] embed The name or alias of the embedded field
357
+ # @param [ String ] field The database field name for a relation.
349
358
  #
350
- # @return [ String ] the database name of the embedded field
351
- def embedded_alias(embed)
352
- embedded_aliases[embed]
359
+ # @return [ nil | Mongoid::Relations::Metadata ]
360
+ def meta_of(field)
361
+ @meta_of ||= {}
362
+ return @meta_of[field] if @meta_of.key?(field)
363
+ @meta_of[field] = reflect_on_association(relation_alias(field))
353
364
  end
354
365
 
355
- protected
366
+ # Returns a relation for the given field.
367
+ #
368
+ # @param [ String | Symbol ] field The name of the field.
369
+ #
370
+ # @return [ nil | Constant ] Type of relation.
371
+ def relation_of(field)
372
+ meta = meta_of(field)
373
+ meta ? meta.relation : nil
374
+ end
356
375
 
357
376
  # Retrieves the memoized hash of embedded aliases and their associated database representations.
358
377
  #
359
378
  # @return [ Hash < String, String > ] hash of embedded aliases (keys) to database representations (values)
360
- def embedded_aliases
361
- @embedded_aliases ||= relations.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
362
- h[v[:store_as] || k] = k
379
+ def relation_aliases
380
+ @relation_aliases ||= relations.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
381
+ store_as = Mongoid::Compatibility::Version.mongoid7_or_newer? ? v.store_as : v[:store_as]
382
+ h[store_as || k] = k
363
383
  h
364
384
  end
365
385
  end
366
-
367
- def relation_of(embed)
368
- meta = reflect_on_association(embedded_alias(embed))
369
- meta ? meta.relation : nil
370
- end
371
386
  end
372
387
 
373
388
  module SingletonMethods