mongoid 8.0.13 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +17 -66
  6. data/lib/config/locales/en.yml +46 -40
  7. data/lib/mongoid/association/accessors.rb +3 -7
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/depending.rb +4 -8
  10. data/lib/mongoid/association/eager_loadable.rb +0 -3
  11. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  12. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  13. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  14. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  15. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  16. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  17. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +2 -2
  18. data/lib/mongoid/association/macros.rb +0 -6
  19. data/lib/mongoid/association/nested/many.rb +3 -38
  20. data/lib/mongoid/association/nested/nested_buildable.rb +0 -14
  21. data/lib/mongoid/association/nested/one.rb +40 -2
  22. data/lib/mongoid/association/proxy.rb +1 -1
  23. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  24. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  25. data/lib/mongoid/association/referenced/has_many/enumerable.rb +6 -23
  26. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -103
  27. data/lib/mongoid/association/reflections.rb +2 -2
  28. data/lib/mongoid/atomic.rb +7 -16
  29. data/lib/mongoid/attributes/dynamic.rb +1 -1
  30. data/lib/mongoid/attributes/nested.rb +2 -2
  31. data/lib/mongoid/attributes/processing.rb +5 -29
  32. data/lib/mongoid/attributes/projector.rb +1 -1
  33. data/lib/mongoid/attributes/readonly.rb +1 -1
  34. data/lib/mongoid/attributes.rb +8 -2
  35. data/lib/mongoid/changeable.rb +107 -5
  36. data/lib/mongoid/clients/factory.rb +0 -4
  37. data/lib/mongoid/clients/storage_options.rb +2 -5
  38. data/lib/mongoid/clients/validators/storage.rb +1 -13
  39. data/lib/mongoid/collection_configurable.rb +58 -0
  40. data/lib/mongoid/composable.rb +2 -0
  41. data/lib/mongoid/config/defaults.rb +60 -0
  42. data/lib/mongoid/config/options.rb +0 -3
  43. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  44. data/lib/mongoid/config/validators.rb +1 -0
  45. data/lib/mongoid/config.rb +88 -89
  46. data/lib/mongoid/contextual/aggregable/memory.rb +2 -5
  47. data/lib/mongoid/contextual/atomic.rb +1 -1
  48. data/lib/mongoid/contextual/memory.rb +240 -51
  49. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  50. data/lib/mongoid/contextual/mongo.rb +370 -133
  51. data/lib/mongoid/contextual/none.rb +162 -7
  52. data/lib/mongoid/contextual.rb +12 -0
  53. data/lib/mongoid/criteria/findable.rb +2 -2
  54. data/lib/mongoid/criteria/includable.rb +4 -3
  55. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -15
  56. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  57. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -13
  58. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  59. data/lib/mongoid/criteria/queryable/selectable.rb +43 -121
  60. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  61. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  62. data/lib/mongoid/criteria.rb +6 -5
  63. data/lib/mongoid/deprecable.rb +1 -2
  64. data/lib/mongoid/deprecation.rb +3 -3
  65. data/lib/mongoid/document.rb +1 -8
  66. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  67. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  68. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  69. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  70. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  71. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  72. data/lib/mongoid/errors.rb +4 -2
  73. data/lib/mongoid/extensions/hash.rb +3 -51
  74. data/lib/mongoid/extensions/object.rb +2 -2
  75. data/lib/mongoid/extensions/time.rb +2 -0
  76. data/lib/mongoid/fields/localized.rb +10 -0
  77. data/lib/mongoid/fields/standard.rb +10 -0
  78. data/lib/mongoid/fields.rb +59 -35
  79. data/lib/mongoid/findable.rb +27 -3
  80. data/lib/mongoid/interceptable.rb +6 -116
  81. data/lib/mongoid/matchable.rb +1 -6
  82. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  83. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +5 -3
  84. data/lib/mongoid/matcher/regex.rb +9 -8
  85. data/lib/mongoid/matcher/type.rb +1 -1
  86. data/lib/mongoid/matcher.rb +0 -1
  87. data/lib/mongoid/persistable/creatable.rb +1 -0
  88. data/lib/mongoid/persistable/deletable.rb +1 -1
  89. data/lib/mongoid/persistable/savable.rb +13 -1
  90. data/lib/mongoid/persistable/unsettable.rb +2 -2
  91. data/lib/mongoid/persistable/updatable.rb +51 -1
  92. data/lib/mongoid/persistable/upsertable.rb +20 -1
  93. data/lib/mongoid/persistable.rb +3 -0
  94. data/lib/mongoid/query_cache.rb +5 -1
  95. data/lib/mongoid/railties/database.rake +7 -2
  96. data/lib/mongoid/reloadable.rb +5 -3
  97. data/lib/mongoid/stateful.rb +22 -1
  98. data/lib/mongoid/tasks/database.rake +12 -0
  99. data/lib/mongoid/tasks/database.rb +20 -0
  100. data/lib/mongoid/threaded.rb +0 -3
  101. data/lib/mongoid/timestamps/created.rb +1 -8
  102. data/lib/mongoid/traversable.rb +0 -12
  103. data/lib/mongoid/utils.rb +22 -0
  104. data/lib/mongoid/validatable/associated.rb +17 -98
  105. data/lib/mongoid/validatable/macros.rb +5 -20
  106. data/lib/mongoid/validatable.rb +4 -10
  107. data/lib/mongoid/version.rb +1 -5
  108. data/lib/mongoid/warnings.rb +17 -2
  109. data/lib/mongoid.rb +16 -3
  110. data/spec/integration/app_spec.rb +2 -20
  111. data/spec/integration/associations/embeds_one_spec.rb +5 -25
  112. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -55
  113. data/spec/integration/callbacks_spec.rb +99 -12
  114. data/spec/integration/discriminator_key_spec.rb +4 -5
  115. data/spec/integration/dots_and_dollars_spec.rb +2 -12
  116. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  117. data/spec/integration/matcher_operator_data/regex.yml +0 -21
  118. data/spec/mongoid/association/eager_spec.rb +2 -24
  119. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  120. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  121. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  122. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +0 -4
  123. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  124. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  125. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +42 -103
  126. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -195
  127. data/spec/mongoid/association/syncable_spec.rb +1 -1
  128. data/spec/mongoid/attributes/nested_spec.rb +10 -204
  129. data/spec/mongoid/attributes_spec.rb +3 -33
  130. data/spec/mongoid/changeable_spec.rb +299 -24
  131. data/spec/mongoid/clients/factory_spec.rb +0 -30
  132. data/spec/mongoid/clients_spec.rb +122 -13
  133. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  134. data/spec/mongoid/config/defaults_spec.rb +160 -0
  135. data/spec/mongoid/config_spec.rb +154 -27
  136. data/spec/mongoid/contextual/aggregable/memory_spec.rb +0 -91
  137. data/spec/mongoid/contextual/memory_spec.rb +332 -253
  138. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  139. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  140. data/spec/mongoid/contextual/none_spec.rb +49 -2
  141. data/spec/mongoid/copyable_spec.rb +2 -10
  142. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  143. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  144. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -2
  145. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  146. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +0 -235
  147. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  148. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  149. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  150. data/spec/mongoid/criteria_spec.rb +5 -11
  151. data/spec/mongoid/document_spec.rb +0 -27
  152. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  153. data/spec/mongoid/extensions/hash_spec.rb +3 -239
  154. data/spec/mongoid/extensions/time_spec.rb +8 -43
  155. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  156. data/spec/mongoid/fields/localized_spec.rb +46 -28
  157. data/spec/mongoid/fields_spec.rb +136 -77
  158. data/spec/mongoid/findable_spec.rb +391 -34
  159. data/spec/mongoid/indexable_spec.rb +16 -10
  160. data/spec/mongoid/interceptable_spec.rb +153 -442
  161. data/spec/mongoid/interceptable_spec_models.rb +111 -51
  162. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  163. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  164. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  165. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  166. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  167. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  168. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  169. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  170. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  171. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  172. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  173. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  174. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  175. data/spec/mongoid/persistence_context_spec.rb +7 -57
  176. data/spec/mongoid/query_cache_spec.rb +56 -61
  177. data/spec/mongoid/reloadable_spec.rb +24 -28
  178. data/spec/mongoid/scopable_spec.rb +70 -0
  179. data/spec/mongoid/serializable_spec.rb +23 -44
  180. data/spec/mongoid/stateful_spec.rb +122 -8
  181. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  182. data/spec/mongoid/tasks/database_spec.rb +127 -0
  183. data/spec/mongoid/timestamps/created_spec.rb +0 -23
  184. data/spec/mongoid/timestamps_spec.rb +9 -11
  185. data/spec/mongoid/touchable_spec.rb +277 -5
  186. data/spec/mongoid/touchable_spec_models.rb +3 -1
  187. data/spec/mongoid/traversable_spec.rb +9 -24
  188. data/spec/mongoid/validatable/associated_spec.rb +34 -27
  189. data/spec/mongoid/validatable/numericality_spec.rb +0 -16
  190. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  191. data/spec/mongoid_spec.rb +36 -10
  192. data/spec/shared/lib/mrss/docker_runner.rb +1 -1
  193. data/spec/shared/lib/mrss/server_version_registry.rb +1 -1
  194. data/spec/shared/lib/mrss/spec_organizer.rb +3 -32
  195. data/spec/shared/share/Dockerfile.erb +85 -15
  196. data/spec/shared/shlib/distro.sh +0 -10
  197. data/spec/shared/shlib/server.sh +27 -34
  198. data/spec/shared/shlib/set_env.sh +68 -9
  199. data/spec/spec_helper.rb +5 -0
  200. data/spec/support/expectations.rb +17 -20
  201. data/spec/support/immutable_ids.rb +118 -0
  202. data/spec/support/macros.rb +47 -15
  203. data/spec/support/models/artist.rb +0 -1
  204. data/spec/support/models/band.rb +1 -0
  205. data/spec/support/models/building.rb +2 -0
  206. data/spec/support/models/name.rb +0 -10
  207. data/spec/support/models/person.rb +0 -1
  208. data/spec/support/models/product.rb +1 -0
  209. data.tar.gz.sig +0 -0
  210. metadata +715 -671
  211. metadata.gz.sig +2 -0
  212. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +0 -26
  213. data/lib/mongoid/field_readable.rb +0 -70
  214. data/lib/mongoid/matcher/regexp_budget.rb +0 -389
  215. data/lib/mongoid/validatable/numericality.rb +0 -19
  216. data/spec/integration/matcher_regexp_timeout_spec.rb +0 -215
  217. data/spec/integration/query_operator_guard_spec.rb +0 -89
  218. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -61
  219. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -61
  220. data/spec/mongoid/matcher/regexp_budget_spec.rb +0 -570
  221. data/spec/shared/CANDIDATE.md +0 -28
  222. data/spec/shared/lib/mrss/release/candidate.rb +0 -281
  223. data/spec/shared/lib/mrss/release/product_data.rb +0 -144
  224. data/spec/shared/lib/tasks/candidate.rake +0 -64
  225. data/spec/support/models/purse.rb +0 -9
@@ -6,10 +6,7 @@ module Mongoid
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
-
10
- cattr_accessor :storage_options, instance_writer: false do
11
- storage_options_defaults
12
- end
9
+ class_attribute :storage_options, instance_writer: false, default: storage_options_defaults
13
10
  end
14
11
 
15
12
  module ClassMethods
@@ -49,7 +46,7 @@ module Mongoid
49
46
  # @return [ Class ] The model class.
50
47
  def store_in(options)
51
48
  Validators::Storage.validate(self, options)
52
- storage_options.merge!(options)
49
+ self.storage_options = self.storage_options.merge(options)
53
50
  end
54
51
 
55
52
  # Reset the store_in options
@@ -9,7 +9,7 @@ module Mongoid
9
9
  extend self
10
10
 
11
11
  # The valid options for storage.
12
- VALID_OPTIONS = [ :collection, :database, :client ].freeze
12
+ VALID_OPTIONS = [ :collection, :collection_options, :database, :client ].freeze
13
13
 
14
14
  # Validate the options provided to :store_in.
15
15
  #
@@ -20,21 +20,9 @@ module Mongoid
20
20
  # @param [ Hash | String | Symbol ] options The provided options.
21
21
  def validate(klass, options)
22
22
  valid_keys?(options) or raise Errors::InvalidStorageOptions.new(klass, options)
23
- valid_parent?(klass) or raise Errors::InvalidStorageParent.new(klass)
24
23
  end
25
24
 
26
25
  private
27
- # Determine if the current klass is valid to change store_in
28
- # options
29
- #
30
- # @api private
31
- #
32
- # @param [ Class ] klass
33
- #
34
- # @return [ true | false ] If the class is valid.
35
- def valid_parent?(klass)
36
- !klass.superclass.include?(Mongoid::Document)
37
- end
38
26
 
39
27
  # Determine if all keys in the options hash are valid.
40
28
  #
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+
5
+ # Encapsulates behavior around defining collections.
6
+ module CollectionConfigurable
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ # Create the collection for the called upon Mongoid model.
11
+ #
12
+ # This method does not re-create existing collections.
13
+ #
14
+ # If the document includes `store_in` macro with `collection_options` key,
15
+ # these options are used when creating the collection.
16
+ #
17
+ # @param [ true | false ] force If true, the method will drop existing
18
+ # collections before creating new ones. If false, the method will create
19
+ # only new collection (that do not exist in the database).
20
+ #
21
+ # @raise [ Errors::CreateCollectionFailure ] If collection creation failed.
22
+ # @raise [ Errors::DropCollectionFailure ] If an attempt to drop collection failed.
23
+ def create_collection(force: false)
24
+ if collection_name.empty?
25
+ # This is most probably an anonymous class, we ignore them.
26
+ return
27
+ end
28
+ if collection_name.match(/^system\./)
29
+ # We do not do anything with system collections.
30
+ return
31
+ end
32
+ if force
33
+ collection.drop
34
+ end
35
+ if coll_options = collection.database.list_collections(filter: { name: collection_name.to_s }).first
36
+ if force
37
+ raise Errors::DropCollectionFailure.new(collection_name)
38
+ else
39
+ logger.debug(
40
+ "MONGOID: Collection '#{collection_name}' already exists " +
41
+ "in database '#{database_name}' with options '#{coll_options}'."
42
+ )
43
+ end
44
+ else
45
+ begin
46
+ collection.database[collection_name, storage_options.fetch(:collection_options, {})].create
47
+ rescue Mongo::Error::OperationFailure => e
48
+ raise Errors::CreateCollectionFailure.new(
49
+ collection_name,
50
+ storage_options[:collection_options],
51
+ e
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "mongoid/changeable"
4
+ require "mongoid/collection_configurable"
4
5
  require "mongoid/findable"
5
6
  require "mongoid/indexable"
6
7
  require "mongoid/inspectable"
@@ -36,6 +37,7 @@ module Mongoid
36
37
  include Atomic
37
38
  include Changeable
38
39
  include Clients
40
+ include CollectionConfigurable
39
41
  include Attributes
40
42
  include Evolvable
41
43
  include Fields
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Config
5
+
6
+ # Encapsulates logic for loading defaults.
7
+ module Defaults
8
+
9
+ # Load the defaults for the feature flags in the given Mongoid version.
10
+ # Note that this method will load the *new* functionality introduced in
11
+ # the given Mongoid version.
12
+ #
13
+ # @param [ String | Float ] The version number as X.y.
14
+ #
15
+ # raises [ ArgumentError ] if an invalid version is given.
16
+ def load_defaults(version)
17
+ # Note that for 7.x, since all of the feature flag defaults have been
18
+ # flipped to the new functionality, all of the settings for those
19
+ # versions are to give old functionality. Because of this, it is
20
+ # possible to recurse to later version to get all of the options to
21
+ # turn off. Note that this won't be true when adding feature flags to
22
+ # 9.x, since the default will be the old functionality until the next
23
+ # major version is released. More likely, the recursion will have to go
24
+ # in the other direction (towards earlier versions).
25
+
26
+ case version.to_s
27
+ when "7.3"
28
+ # flags introduced in 7.4 - old functionality
29
+ self.broken_aggregables = true
30
+ self.broken_alias_handling = true
31
+ self.broken_and = true
32
+ self.broken_scoping = true
33
+ self.broken_updates = true
34
+ self.compare_time_by_ms = false
35
+ self.legacy_pluck_distinct = true
36
+ self.legacy_triple_equals = true
37
+ self.object_id_as_json_oid = true
38
+
39
+ load_defaults "7.4"
40
+ when "7.4"
41
+ # flags introduced in 7.5 - old functionality
42
+ self.legacy_attributes = true
43
+ self.overwrite_chained_operators = true
44
+
45
+ load_defaults "7.5"
46
+ when "7.5"
47
+ # flags introduced in 8.0 - old functionality
48
+ self.map_big_decimal_to_decimal128 = false
49
+ when "8.0"
50
+ # All flag defaults currently reflect 8.0 behavior.
51
+ when "8.1"
52
+ # flags introduced in 8.1 - new functionality
53
+ self.legacy_readonly = false
54
+ else
55
+ raise ArgumentError, "Unknown version: #{version}"
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -25,8 +25,6 @@ module Mongoid
25
25
  # @param [ Hash ] options Extras for the option.
26
26
  #
27
27
  # @option options [ Object ] :default The default value.
28
- # @option options [ Proc | nil ] :on_change The callback to invoke when the
29
- # setter is invoked.
30
28
  def option(name, options = {})
31
29
  defaults[name] = settings[name] = options[:default]
32
30
 
@@ -40,7 +38,6 @@ module Mongoid
40
38
 
41
39
  define_method("#{name}=") do |value|
42
40
  settings[name] = value
43
- options[:on_change]&.call(value)
44
41
  end
45
42
 
46
43
  define_method("#{name}?") do
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Config
5
+ module Validators
6
+
7
+ # Validator for async query executor configuration.
8
+ #
9
+ # @api private
10
+ module AsyncQueryExecutor
11
+ extend self
12
+
13
+
14
+ def validate(options)
15
+ if options.key?(:async_query_executor)
16
+ if options[:async_query_executor].to_sym == :immediate && !options[:global_executor_concurrency].nil?
17
+ raise Errors::InvalidGlobalExecutorConcurrency
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "mongoid/config/validators/async_query_executor"
3
4
  require "mongoid/config/validators/option"
4
5
  require "mongoid/config/validators/client"
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "mongoid/config/defaults"
3
4
  require "mongoid/config/environment"
4
5
  require "mongoid/config/options"
5
6
  require "mongoid/config/validators"
@@ -11,6 +12,7 @@ module Mongoid
11
12
  module Config
12
13
  extend Forwardable
13
14
  extend Options
15
+ extend Defaults
14
16
  extend self
15
17
 
16
18
  def_delegators ::Mongoid, :logger, :logger=
@@ -70,6 +72,7 @@ module Mongoid
70
72
 
71
73
  # Use ActiveSupport's time zone in time operations instead of the
72
74
  # Ruby default time zone.
75
+ # @deprecated
73
76
  option :use_activesupport_time_zone, default: true
74
77
 
75
78
  # Return stored times as UTC.
@@ -125,22 +128,35 @@ module Mongoid
125
128
  # always return a Hash.
126
129
  option :legacy_attributes, default: false
127
130
 
128
- # Allow BSON::Decimal128 to be parsed and returned directly in
129
- # field values. When BSON 5 is present and the this option is set to false
130
- # (the default), BSON::Decimal128 values in the database will be returned
131
- # as BigDecimal.
132
- #
133
- # @note this option only has effect when BSON 5+ is present. Otherwise,
134
- # the setting is ignored.
135
- option :allow_bson5_decimal128, default: false, on_change: -> (allow) do
136
- if BSON::VERSION >= '5.0.0'
137
- if allow
138
- BSON::Registry.register(BSON::Decimal128::BSON_TYPE, BSON::Decimal128)
139
- else
140
- BSON::Registry.register(BSON::Decimal128::BSON_TYPE, BigDecimal)
141
- end
142
- end
143
- end
131
+ # Sets the async_query_executor for the application. By default the thread pool executor
132
+ # is set to `:immediate. Options are:
133
+ #
134
+ # - :immediate - Initializes a single +Concurrent::ImmediateExecutor+
135
+ # - :global_thread_pool - Initializes a single +Concurrent::ThreadPoolExecutor+
136
+ # that uses the +async_query_concurrency+ for the +max_threads+ value.
137
+ option :async_query_executor, default: :immediate
138
+
139
+ # Defines how many asynchronous queries can be executed concurrently.
140
+ # This option should be set only if `async_query_executor` is set
141
+ # to `:global_thread_pool`.
142
+ option :global_executor_concurrency, default: nil
143
+
144
+ # When this flag is false, a document will become read-only only once the
145
+ # #readonly! method is called, and an error will be raised on attempting
146
+ # to save or update such documents, instead of just on delete. When this
147
+ # flag is true, a document is only read-only if it has been projected
148
+ # using #only or #without, and read-only documents will not be
149
+ # deletable/destroyable, but they will be savable/updatable.
150
+ # When this feature flag is turned on, the read-only state will be reset on
151
+ # reload, but when it is turned off, it won't be.
152
+ option :legacy_readonly, default: true
153
+
154
+ # When this flag is true, any attempt to change the _id of a persisted
155
+ # document will raise an exception (`Errors::ImmutableAttribute`).
156
+ # This will be the default in 9.0. When this flag is false (the default
157
+ # in 8.x), changing the _id of a persisted document might be ignored,
158
+ # or it might work, depending on the situation.
159
+ option :immutable_ids, default: false
144
160
 
145
161
  # When this flag is true, callbacks for every embedded document will be
146
162
  # called only once, even if the embedded document is embedded in multiple
@@ -152,80 +168,12 @@ module Mongoid
152
168
  # See https://jira.mongodb.org/browse/MONGOID-5542
153
169
  option :prevent_multiple_calls_of_embedded_callbacks, default: false
154
170
 
155
- # When this flag is true, callbacks for embedded documents will not be
156
- # called. This is the default in 8.x, but will be changed to false in 9.0.
157
- #
158
- # Setting this flag to true (as it is in 8.x) may lead to stack
159
- # overflow errors if there are more than cicrca 1000 embedded
160
- # documents in the root document's dependencies graph.
161
- #
162
- # It is strongly recommended to set this flag to false in 8.x, if you
163
- # are not using around callbacks for embedded documents.
164
- #
165
- # See https://jira.mongodb.org/browse/MONGOID-5658 for more details.
166
- option :around_callbacks_for_embeds, default: true
167
-
168
- # The maximum number of seconds that evaluating a single query in memory
169
- # may spend executing regular expressions. Queries against an embedded
170
- # association are evaluated in the calling thread, so a pattern built from
171
- # user input runs locally and can otherwise consume unbounded CPU. The
172
- # limit is cumulative over the whole query, since cost grows with the
173
- # number of documents and conditions as well as with the pattern.
174
- #
175
- # Set to nil to remove the limit. On Ruby 3.2 and later the remaining
176
- # budget is compiled into the pattern, so the limit counts only the time
177
- # spent matching. Earlier Rubies have no per-Regexp timeout, so the query
178
- # is bounded with Timeout instead and the limit is wall clock over the
179
- # whole in-memory evaluation.
180
- #
181
- # See https://jira.mongodb.org/browse/MONGOID-5981 for details.
182
- option :in_memory_regexp_time_limit, default: 5.0
183
-
184
- # When false (default), query operators are restricted when building a
185
- # selector, so that user-supplied input reaching the query builder cannot
186
- # make MongoDB execute arbitrary JavaScript. Two rules are enforced:
187
- #
188
- # - An operator at the top level of an expression must appear in
189
- # +Criteria::Queryable::Selectable::ALLOWED_QUERY_OPERATORS+.
190
- # - +$where+, +$function+, and +$accumulator+ are rejected at any depth,
191
- # including inside +$expr+ and the logical operators.
192
- #
193
- # This applies to every query method that accepts an expression, including
194
- # +where+, +find_by+, +and+, +or+, +nor+, +not+, +any_of+, and +none_of+.
195
- # It also governs the string form of +where+, e.g.
196
- # +where("this.name == 'admin'")+, which the server evaluates as +$where+.
197
- # Applications relying on that form must set this option to true.
198
- #
199
- # The APIs that request JavaScript explicitly, +Criteria#for_js+ and
200
- # +js_query+, are unaffected: there the developer has asked for it.
201
- #
202
- # Set to true to restore the unrestricted pass-through behavior.
203
- #
204
- # Note that this option is deliberately not tied to +load_defaults+: an
205
- # application that has opted into older defaults still gets the guard, and
206
- # must set this option explicitly to turn it off.
207
- #
208
- # See https://jira.mongodb.org/browse/MONGOID-5939,
209
- # https://jira.mongodb.org/browse/MONGOID-5993,
210
- # https://jira.mongodb.org/browse/MONGOID-5994 for details.
211
- option :allow_unsafe_query_operators, default: false
212
-
213
- # When this flag is true, it will be possible to add a record to a
214
- # "has_many" or "has_and_belongs_to_many" association by passing that
215
- # record's id in the nested attributes for another parent record, even
216
- # when the record does not already belong to that association. For a
217
- # "has_many" association this moves the record to the new parent.
218
- #
219
- # When this flag is false, an id in nested attributes is only resolved
220
- # within the association itself, and anything else raises an error.
221
- #
222
- # The default is `false`. Note that allowing reparenting via nested attributes
223
- # is a potential security risk, since it could allow a malicious user to move
224
- # records that they do not own to a parent record that they do own.
171
+ # Returns the Config singleton, for use in the configure DSL.
225
172
  #
226
- # This option will be removed in Mongoid 10, and the only behavior will be
227
- # as if this option were set to false.
228
- option :allow_reparenting_via_nested_attributes, default: false
173
+ # @return [ self ] The Config singleton.
174
+ def config
175
+ self
176
+ end
229
177
 
230
178
  # Has Mongoid been configured? This is checking that at least a valid
231
179
  # client config exists.
@@ -308,6 +256,17 @@ module Mongoid
308
256
  end
309
257
  end
310
258
 
259
+ # Deregister a model in the application with Mongoid.
260
+ #
261
+ # @param [ Class ] klass The model to deregister.
262
+ #
263
+ # @api private
264
+ def deregister_model(klass)
265
+ LOCK.synchronize do
266
+ models.delete(klass)
267
+ end
268
+ end
269
+
311
270
  # From a hash of settings, load all the configuration.
312
271
  #
313
272
  # @example Load the configuration.
@@ -380,6 +339,7 @@ module Mongoid
380
339
  # @param [ Hash ] options The configuration options.
381
340
  def options=(options)
382
341
  if options
342
+ Validators::AsyncQueryExecutor.validate(options)
383
343
  options.each_pair do |option, value|
384
344
  Validators::Option.validate(option)
385
345
  send("#{option}=", value)
@@ -447,5 +407,44 @@ module Mongoid
447
407
  client
448
408
  end
449
409
  end
410
+
411
+ module DeprecatedOptions
412
+ OPTIONS = %i[ use_activesupport_time_zone
413
+ broken_aggregables
414
+ broken_alias_handling
415
+ broken_and
416
+ broken_scoping
417
+ broken_updates
418
+ compare_time_by_ms
419
+ legacy_attributes
420
+ legacy_pluck_distinct
421
+ legacy_triple_equals
422
+ object_id_as_json_oid
423
+ overwrite_chained_operators ]
424
+
425
+ if RUBY_VERSION < '3.0'
426
+ def self.prepended(klass)
427
+ klass.class_eval do
428
+ OPTIONS.each do |option|
429
+ alias_method :"#{option}_without_deprecation=", :"#{option}="
430
+
431
+ define_method(:"#{option}=") do |value|
432
+ Mongoid::Warnings.send(:"warn_#{option}_deprecated")
433
+ send(:"#{option}_without_deprecation=", value)
434
+ end
435
+ end
436
+ end
437
+ end
438
+ else
439
+ OPTIONS.each do |option|
440
+ define_method(:"#{option}=") do |value|
441
+ Mongoid::Warnings.send(:"warn_#{option}_deprecated")
442
+ super(value)
443
+ end
444
+ end
445
+ end
446
+ end
447
+
448
+ prepend DeprecatedOptions
450
449
  end
451
450
  end
@@ -1,13 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'mongoid/field_readable'
4
-
5
3
  module Mongoid
6
4
  module Contextual
7
5
  module Aggregable
8
6
  # Contains behavior for aggregating values in memory.
9
7
  module Memory
10
- include FieldReadable
11
8
 
12
9
  # Get all the aggregate values for the provided field.
13
10
  # Provided for interface consistency with Aggregable::Mongo.
@@ -32,7 +29,7 @@ module Mongoid
32
29
  #
33
30
  # @return [ Numeric ] The average.
34
31
  def avg(field)
35
- total = count { |doc| !read_field_value(doc, field).nil? }
32
+ total = count { |doc| !doc.send(field).nil? }
36
33
  return nil unless total > 0
37
34
 
38
35
  total = total.to_f if total.is_a?(Integer)
@@ -117,7 +114,7 @@ module Mongoid
117
114
  def aggregate_by(field, method)
118
115
  return nil unless any?
119
116
 
120
- map { |doc| read_field_value(doc, field) }.compact.public_send(method)
117
+ map { |doc| doc.public_send(field) }.compact.public_send(method)
121
118
  end
122
119
  end
123
120
  end
@@ -150,7 +150,7 @@ module Mongoid
150
150
  # @example Unset the field on the matches.
151
151
  # context.unset(:name)
152
152
  #
153
- # @param [ String | Symbol | Array<String | Symbol> | Hash ] args
153
+ # @param [ [ String | Symbol | Array<String | Symbol> | Hash ]... ] *args
154
154
  # The name(s) of the field(s) to unset.
155
155
  # If a Hash is specified, its keys will be used irrespective of what
156
156
  # each key's value is, even if the value is nil or false.