mongoid 8.0.10 → 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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +3 -3
- data/README.md +3 -3
- data/Rakefile +18 -67
- data/lib/config/locales/en.yml +46 -14
- data/lib/mongoid/association/accessors.rb +3 -7
- data/lib/mongoid/association/builders.rb +1 -1
- data/lib/mongoid/association/eager_loadable.rb +0 -3
- data/lib/mongoid/association/embedded/batchable.rb +2 -2
- data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
- data/lib/mongoid/association/macros.rb +0 -6
- data/lib/mongoid/association/nested/one.rb +40 -2
- data/lib/mongoid/association/proxy.rb +1 -1
- data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +6 -23
- data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
- data/lib/mongoid/association/reflections.rb +2 -2
- data/lib/mongoid/atomic.rb +7 -16
- data/lib/mongoid/attributes/dynamic.rb +1 -1
- data/lib/mongoid/attributes/nested.rb +2 -2
- data/lib/mongoid/attributes/processing.rb +5 -29
- data/lib/mongoid/attributes/projector.rb +1 -1
- data/lib/mongoid/attributes/readonly.rb +1 -1
- data/lib/mongoid/attributes.rb +8 -2
- data/lib/mongoid/changeable.rb +107 -5
- data/lib/mongoid/clients/storage_options.rb +2 -5
- data/lib/mongoid/clients/validators/storage.rb +1 -13
- data/lib/mongoid/collection_configurable.rb +58 -0
- data/lib/mongoid/composable.rb +2 -0
- data/lib/mongoid/config/defaults.rb +60 -0
- data/lib/mongoid/config/options.rb +0 -3
- data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
- data/lib/mongoid/config/validators.rb +1 -0
- data/lib/mongoid/config.rb +88 -27
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/memory.rb +233 -33
- data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
- data/lib/mongoid/contextual/mongo.rb +370 -133
- data/lib/mongoid/contextual/none.rb +162 -7
- data/lib/mongoid/contextual.rb +12 -0
- data/lib/mongoid/criteria/findable.rb +2 -2
- data/lib/mongoid/criteria/includable.rb +4 -3
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -15
- data/lib/mongoid/criteria/queryable/key.rb +1 -1
- data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
- data/lib/mongoid/criteria/queryable/optional.rb +8 -8
- data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
- data/lib/mongoid/criteria/queryable/selector.rb +1 -1
- data/lib/mongoid/criteria/queryable/storable.rb +1 -1
- data/lib/mongoid/criteria.rb +6 -5
- data/lib/mongoid/deprecable.rb +1 -2
- data/lib/mongoid/deprecation.rb +3 -3
- data/lib/mongoid/document.rb +1 -8
- data/lib/mongoid/errors/create_collection_failure.rb +33 -0
- data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
- data/lib/mongoid/errors/immutable_attribute.rb +26 -0
- data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
- data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
- data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
- data/lib/mongoid/errors.rb +4 -1
- data/lib/mongoid/extensions/hash.rb +2 -24
- data/lib/mongoid/extensions/object.rb +2 -2
- data/lib/mongoid/extensions/time.rb +2 -0
- data/lib/mongoid/fields/localized.rb +10 -0
- data/lib/mongoid/fields/standard.rb +10 -0
- data/lib/mongoid/fields.rb +59 -35
- data/lib/mongoid/findable.rb +27 -3
- data/lib/mongoid/interceptable.rb +6 -116
- data/lib/mongoid/matcher/eq_impl.rb +1 -1
- data/lib/mongoid/matcher/type.rb +1 -1
- data/lib/mongoid/persistable/creatable.rb +1 -0
- data/lib/mongoid/persistable/deletable.rb +1 -1
- data/lib/mongoid/persistable/savable.rb +13 -1
- data/lib/mongoid/persistable/unsettable.rb +2 -2
- data/lib/mongoid/persistable/updatable.rb +51 -1
- data/lib/mongoid/persistable/upsertable.rb +20 -1
- data/lib/mongoid/persistable.rb +3 -0
- data/lib/mongoid/query_cache.rb +5 -1
- data/lib/mongoid/railties/database.rake +7 -2
- data/lib/mongoid/reloadable.rb +5 -3
- data/lib/mongoid/stateful.rb +22 -1
- data/lib/mongoid/tasks/database.rake +12 -0
- data/lib/mongoid/tasks/database.rb +20 -0
- data/lib/mongoid/timestamps/created.rb +1 -8
- data/lib/mongoid/traversable.rb +0 -12
- data/lib/mongoid/utils.rb +22 -0
- data/lib/mongoid/validatable/associated.rb +17 -98
- data/lib/mongoid/validatable/macros.rb +5 -5
- data/lib/mongoid/validatable.rb +4 -9
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +17 -1
- data/lib/mongoid.rb +16 -3
- data/spec/integration/app_spec.rb +2 -6
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -40
- data/spec/integration/callbacks_spec.rb +99 -12
- data/spec/integration/discriminator_key_spec.rb +4 -5
- data/spec/integration/i18n_fallbacks_spec.rb +3 -2
- data/spec/mongoid/association/eager_spec.rb +2 -24
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
- data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +0 -4
- data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +42 -55
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
- data/spec/mongoid/association/syncable_spec.rb +1 -1
- data/spec/mongoid/association_spec.rb +0 -60
- data/spec/mongoid/attributes_spec.rb +3 -33
- data/spec/mongoid/changeable_spec.rb +299 -24
- data/spec/mongoid/clients_spec.rb +122 -13
- data/spec/mongoid/collection_configurable_spec.rb +158 -0
- data/spec/mongoid/config/defaults_spec.rb +160 -0
- data/spec/mongoid/config_spec.rb +154 -27
- data/spec/mongoid/contextual/memory_spec.rb +332 -76
- data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
- data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
- data/spec/mongoid/contextual/none_spec.rb +49 -2
- data/spec/mongoid/copyable_spec.rb +2 -10
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
- data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
- data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
- data/spec/mongoid/criteria_projection_spec.rb +1 -4
- data/spec/mongoid/criteria_spec.rb +5 -9
- data/spec/mongoid/document_spec.rb +0 -27
- data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
- data/spec/mongoid/extensions/hash_spec.rb +3 -3
- data/spec/mongoid/extensions/time_spec.rb +8 -43
- data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
- data/spec/mongoid/fields/localized_spec.rb +46 -28
- data/spec/mongoid/fields_spec.rb +136 -77
- data/spec/mongoid/findable_spec.rb +391 -34
- data/spec/mongoid/indexable_spec.rb +16 -10
- data/spec/mongoid/interceptable_spec.rb +153 -442
- data/spec/mongoid/interceptable_spec_models.rb +111 -51
- data/spec/mongoid/persistable/deletable_spec.rb +26 -6
- data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
- data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
- data/spec/mongoid/persistable/logical_spec.rb +37 -0
- data/spec/mongoid/persistable/poppable_spec.rb +36 -0
- data/spec/mongoid/persistable/pullable_spec.rb +72 -0
- data/spec/mongoid/persistable/pushable_spec.rb +72 -0
- data/spec/mongoid/persistable/renamable_spec.rb +36 -0
- data/spec/mongoid/persistable/savable_spec.rb +96 -0
- data/spec/mongoid/persistable/settable_spec.rb +37 -0
- data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
- data/spec/mongoid/persistable/updatable_spec.rb +20 -28
- data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
- data/spec/mongoid/persistence_context_spec.rb +7 -57
- data/spec/mongoid/query_cache_spec.rb +56 -61
- data/spec/mongoid/reloadable_spec.rb +24 -28
- data/spec/mongoid/scopable_spec.rb +70 -0
- data/spec/mongoid/serializable_spec.rb +23 -44
- data/spec/mongoid/stateful_spec.rb +122 -8
- data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
- data/spec/mongoid/tasks/database_spec.rb +127 -0
- data/spec/mongoid/timestamps/created_spec.rb +0 -23
- data/spec/mongoid/timestamps_spec.rb +9 -11
- data/spec/mongoid/touchable_spec.rb +277 -5
- data/spec/mongoid/touchable_spec_models.rb +3 -1
- data/spec/mongoid/traversable_spec.rb +9 -24
- data/spec/mongoid/validatable/associated_spec.rb +34 -27
- data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
- data/spec/mongoid_spec.rb +36 -10
- data/spec/shared/LICENSE +20 -0
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
- data/spec/shared/lib/mrss/cluster_config.rb +231 -0
- data/spec/shared/lib/mrss/constraints.rb +378 -0
- data/spec/shared/lib/mrss/docker_runner.rb +298 -0
- data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +113 -0
- data/spec/shared/lib/mrss/session_registry.rb +69 -0
- data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
- data/spec/shared/lib/mrss/utils.rb +37 -0
- data/spec/shared/share/Dockerfile.erb +321 -0
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/config.sh +27 -0
- data/spec/shared/shlib/distro.sh +74 -0
- data/spec/shared/shlib/server.sh +416 -0
- data/spec/shared/shlib/set_env.sh +169 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/support/immutable_ids.rb +118 -0
- data/spec/support/macros.rb +47 -15
- data/spec/support/models/artist.rb +0 -1
- data/spec/support/models/band.rb +1 -0
- data/spec/support/models/building.rb +2 -0
- data/spec/support/models/name.rb +0 -10
- data/spec/support/models/person.rb +0 -1
- data/spec/support/models/product.rb +1 -0
- data.tar.gz.sig +0 -0
- metadata +745 -637
- metadata.gz.sig +2 -0
- data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
- data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
- data/spec/support/models/purse.rb +0 -9
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Errors
|
5
|
+
|
6
|
+
# Raised when an attempt to drop a collection failed.
|
7
|
+
class DropCollectionFailure < MongoidError
|
8
|
+
|
9
|
+
# Instantiate the drop collection error.
|
10
|
+
#
|
11
|
+
# @param [ String ] collection_name The name of the collection that
|
12
|
+
# Mongoid failed to drop.
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def initialize(collection_name, collection_options, error)
|
16
|
+
super(
|
17
|
+
compose_message(
|
18
|
+
"drop_collection_failure",
|
19
|
+
{
|
20
|
+
collection_name: collection_name
|
21
|
+
}
|
22
|
+
)
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Errors
|
5
|
+
|
6
|
+
# This error is raised when attempting the change the value of an
|
7
|
+
# immutable attribute. For example, the _id attribute is immutable,
|
8
|
+
# and attempting to change it on a document that has already been
|
9
|
+
# persisted will result in this error.
|
10
|
+
class ImmutableAttribute < MongoidError
|
11
|
+
|
12
|
+
# Create the new error.
|
13
|
+
#
|
14
|
+
# @example Create the new error.
|
15
|
+
# ImmutableAttribute.new(:_id, "1234")
|
16
|
+
#
|
17
|
+
# @param [ Symbol | String ] name The name of the attribute.
|
18
|
+
# @param [ Object ] value The attempted set value.
|
19
|
+
def initialize(name, value)
|
20
|
+
super(
|
21
|
+
compose_message("immutable_attribute", { name: name, value: value })
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Errors
|
5
|
+
|
6
|
+
# This error is raised when a bad async query executor option is attempted
|
7
|
+
# to be set.
|
8
|
+
class InvalidQueryExecutor < MongoidError
|
9
|
+
|
10
|
+
# Create the new error.
|
11
|
+
#
|
12
|
+
# @param [ Symbol | String ] executor The attempted async query executor.
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def initialize(executor)
|
16
|
+
super(
|
17
|
+
compose_message(
|
18
|
+
"invalid_async_query_executor",
|
19
|
+
{ executor: executor, options: [:immediate, :global_thread_pool] }
|
20
|
+
)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Errors
|
5
|
+
|
6
|
+
# This error is raised when a bad global executor concurrency option is attempted
|
7
|
+
# to be set.
|
8
|
+
class InvalidGlobalExecutorConcurrency < MongoidError
|
9
|
+
|
10
|
+
# Create the new error.
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
def initialize
|
14
|
+
super(
|
15
|
+
compose_message(
|
16
|
+
"invalid_global_executor_concurrency"
|
17
|
+
)
|
18
|
+
)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/mongoid/errors.rb
CHANGED
@@ -3,11 +3,14 @@
|
|
3
3
|
require "mongoid/errors/mongoid_error"
|
4
4
|
require "mongoid/errors/ambiguous_relationship"
|
5
5
|
require "mongoid/errors/callback"
|
6
|
+
require "mongoid/errors/create_collection_failure"
|
6
7
|
require "mongoid/errors/criteria_argument_required"
|
7
8
|
require "mongoid/errors/document_not_destroyed"
|
8
9
|
require "mongoid/errors/document_not_found"
|
9
10
|
require "mongoid/errors/empty_config_file"
|
11
|
+
require "mongoid/errors/immutable_attribute"
|
10
12
|
require "mongoid/errors/in_memory_collation_not_supported"
|
13
|
+
require "mongoid/errors/invalid_async_query_executor"
|
11
14
|
require "mongoid/errors/invalid_collection"
|
12
15
|
require "mongoid/errors/invalid_config_file"
|
13
16
|
require "mongoid/errors/invalid_config_option"
|
@@ -16,6 +19,7 @@ require "mongoid/errors/invalid_field"
|
|
16
19
|
require "mongoid/errors/invalid_field_option"
|
17
20
|
require "mongoid/errors/invalid_field_type"
|
18
21
|
require "mongoid/errors/invalid_find"
|
22
|
+
require "mongoid/errors/invalid_global_executor_concurrency"
|
19
23
|
require "mongoid/errors/invalid_includes"
|
20
24
|
require "mongoid/errors/invalid_index"
|
21
25
|
require "mongoid/errors/invalid_options"
|
@@ -35,7 +39,6 @@ require "mongoid/errors/invalid_scope"
|
|
35
39
|
require "mongoid/errors/invalid_session_use"
|
36
40
|
require "mongoid/errors/invalid_set_polymorphic_relation"
|
37
41
|
require "mongoid/errors/invalid_storage_options"
|
38
|
-
require "mongoid/errors/invalid_storage_parent"
|
39
42
|
require "mongoid/errors/invalid_time"
|
40
43
|
require "mongoid/errors/inverse_not_found"
|
41
44
|
require "mongoid/errors/mixed_relations"
|
@@ -38,12 +38,8 @@ module Mongoid
|
|
38
38
|
consolidated = {}
|
39
39
|
each_pair do |key, value|
|
40
40
|
if key =~ /\$/
|
41
|
-
value.
|
42
|
-
|
43
|
-
real_key = klass.database_field_name(key2)
|
44
|
-
|
45
|
-
value.delete(key2) if real_key != key2
|
46
|
-
value[real_key] = value_for(key, klass, real_key, value2)
|
41
|
+
value.each_pair do |_key, _value|
|
42
|
+
value[_key] = (key == "$rename") ? _value.to_s : mongoize_for(key, klass, _key, _value)
|
47
43
|
end
|
48
44
|
consolidated[key] ||= {}
|
49
45
|
consolidated[key].update(value)
|
@@ -185,24 +181,6 @@ module Mongoid
|
|
185
181
|
|
186
182
|
private
|
187
183
|
|
188
|
-
# Get the value for the provided operator, klass, key and value.
|
189
|
-
#
|
190
|
-
# This is necessary for special cases like $rename, $addToSet and $push.
|
191
|
-
#
|
192
|
-
# @param [ String ] operator The operator.
|
193
|
-
# @param [ Class ] klass The model class.
|
194
|
-
# @param [ String | Symbol ] key The field key.
|
195
|
-
# @param [ Object ] value The original value.
|
196
|
-
#
|
197
|
-
# @return [ Object ] Value prepared for the provided operator.
|
198
|
-
def value_for(operator, klass, key, value)
|
199
|
-
case operator
|
200
|
-
when "$rename" then value.to_s
|
201
|
-
when "$addToSet", "$push" then value.mongoize
|
202
|
-
else mongoize_for(operator, klass, operator, value)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
184
|
# Mongoize for the klass, key and value.
|
207
185
|
#
|
208
186
|
# @api private
|
@@ -91,7 +91,7 @@ module Mongoid
|
|
91
91
|
# object.do_or_do_not(:use, "The Force")
|
92
92
|
#
|
93
93
|
# @param [ String | Symbol ] name The method name.
|
94
|
-
# @param [
|
94
|
+
# @param [ Object... ] *args The arguments.
|
95
95
|
#
|
96
96
|
# @return [ Object | nil ] The result of the method call or nil if the
|
97
97
|
# method does not exist.
|
@@ -190,7 +190,7 @@ module Mongoid
|
|
190
190
|
# object.you_must(:use, "The Force")
|
191
191
|
#
|
192
192
|
# @param [ String | Symbol ] name The method name.
|
193
|
-
# @param [
|
193
|
+
# @param [ Object... ] *args The arguments.
|
194
194
|
#
|
195
195
|
# @return [ Object | nil ] The result of the method call or nil if the
|
196
196
|
# method does not exist. Nil if the object is frozen.
|
@@ -31,6 +31,16 @@ module Mongoid
|
|
31
31
|
true
|
32
32
|
end
|
33
33
|
|
34
|
+
# Is the localized field enforcing values to be present?
|
35
|
+
#
|
36
|
+
# @example Is the localized field enforcing values to be present?
|
37
|
+
# field.localize_present?
|
38
|
+
#
|
39
|
+
# @return [ true | false ] If the field enforces present.
|
40
|
+
def localize_present?
|
41
|
+
options[:localize] == :present
|
42
|
+
end
|
43
|
+
|
34
44
|
# Convert the provided string into a hash for the locale.
|
35
45
|
#
|
36
46
|
# @example Serialize the value.
|
@@ -97,6 +97,16 @@ module Mongoid
|
|
97
97
|
false
|
98
98
|
end
|
99
99
|
|
100
|
+
# Is the localized field enforcing values to be present?
|
101
|
+
#
|
102
|
+
# @example Is the localized field enforcing values to be present?
|
103
|
+
# field.localize_present?
|
104
|
+
#
|
105
|
+
# @return [ true | false ] If the field enforces present.
|
106
|
+
def localize_present?
|
107
|
+
false
|
108
|
+
end
|
109
|
+
|
100
110
|
# Get the metadata for the field if its a foreign key.
|
101
111
|
#
|
102
112
|
# @example Get the metadata.
|
data/lib/mongoid/fields.rb
CHANGED
@@ -47,11 +47,6 @@ module Mongoid
|
|
47
47
|
# @api private
|
48
48
|
INVALID_BSON_CLASSES = [ BSON::Decimal128, BSON::Int32, BSON::Int64 ].freeze
|
49
49
|
|
50
|
-
# The suffix for generated translated fields.
|
51
|
-
#
|
52
|
-
# @api private
|
53
|
-
TRANSLATIONS_SFX = '_translations'
|
54
|
-
|
55
50
|
module ClassMethods
|
56
51
|
# Returns the list of id fields for this model class, as both strings
|
57
52
|
# and symbols.
|
@@ -104,8 +99,8 @@ module Mongoid
|
|
104
99
|
ar.each_with_index do |fn, i|
|
105
100
|
key = fn
|
106
101
|
unless klass.fields.key?(fn) || klass.relations.key?(fn)
|
107
|
-
if fn.
|
108
|
-
key =
|
102
|
+
if tr = fn.match(/(.*)_translations\z/)&.captures&.first
|
103
|
+
key = tr
|
109
104
|
else
|
110
105
|
key = fn
|
111
106
|
end
|
@@ -543,6 +538,8 @@ module Mongoid
|
|
543
538
|
# Model.add_defaults(field)
|
544
539
|
#
|
545
540
|
# @param [ Field ] field The field to add for.
|
541
|
+
#
|
542
|
+
# @api private
|
546
543
|
def add_defaults(field)
|
547
544
|
default, name = field.default_val, field.name.to_s
|
548
545
|
remove_defaults(name)
|
@@ -562,6 +559,8 @@ module Mongoid
|
|
562
559
|
#
|
563
560
|
# @param [ Symbol ] name The name of the field.
|
564
561
|
# @param [ Hash ] options The hash of options.
|
562
|
+
#
|
563
|
+
# @api private
|
565
564
|
def add_field(name, options = {})
|
566
565
|
aliased = options[:as]
|
567
566
|
aliased_fields[aliased.to_s] = name if aliased
|
@@ -589,6 +588,8 @@ module Mongoid
|
|
589
588
|
# # => "called"
|
590
589
|
#
|
591
590
|
# @param [ Field ] field the field to process
|
591
|
+
#
|
592
|
+
# @api private
|
592
593
|
def process_options(field)
|
593
594
|
field_options = field.options
|
594
595
|
|
@@ -611,6 +612,8 @@ module Mongoid
|
|
611
612
|
# @param [ Symbol ] name The name of the field.
|
612
613
|
# @param [ Symbol ] meth The name of the accessor.
|
613
614
|
# @param [ Hash ] options The options.
|
615
|
+
#
|
616
|
+
# @api private
|
614
617
|
def create_accessors(name, meth, options = {})
|
615
618
|
field = fields[name]
|
616
619
|
|
@@ -634,6 +637,8 @@ module Mongoid
|
|
634
637
|
# @param [ String ] name The name of the attribute.
|
635
638
|
# @param [ String ] meth The name of the method.
|
636
639
|
# @param [ Field ] field The field.
|
640
|
+
#
|
641
|
+
# @api private
|
637
642
|
def create_field_getter(name, meth, field)
|
638
643
|
generated_methods.module_eval do
|
639
644
|
re_define_method(meth) do
|
@@ -656,6 +661,8 @@ module Mongoid
|
|
656
661
|
#
|
657
662
|
# @param [ String ] name The name of the attribute.
|
658
663
|
# @param [ String ] meth The name of the method.
|
664
|
+
#
|
665
|
+
# @api private
|
659
666
|
def create_field_getter_before_type_cast(name, meth)
|
660
667
|
generated_methods.module_eval do
|
661
668
|
re_define_method("#{meth}_before_type_cast") do
|
@@ -676,6 +683,8 @@ module Mongoid
|
|
676
683
|
# @param [ String ] name The name of the attribute.
|
677
684
|
# @param [ String ] meth The name of the method.
|
678
685
|
# @param [ Field ] field The field.
|
686
|
+
#
|
687
|
+
# @api private
|
679
688
|
def create_field_setter(name, meth, field)
|
680
689
|
generated_methods.module_eval do
|
681
690
|
re_define_method("#{meth}=") do |value|
|
@@ -695,6 +704,8 @@ module Mongoid
|
|
695
704
|
#
|
696
705
|
# @param [ String ] name The name of the attribute.
|
697
706
|
# @param [ String ] meth The name of the method.
|
707
|
+
#
|
708
|
+
# @api private
|
698
709
|
def create_field_check(name, meth)
|
699
710
|
generated_methods.module_eval do
|
700
711
|
re_define_method("#{meth}?") do
|
@@ -711,13 +722,15 @@ module Mongoid
|
|
711
722
|
#
|
712
723
|
# @param [ String ] name The name of the attribute.
|
713
724
|
# @param [ String ] meth The name of the method.
|
725
|
+
#
|
726
|
+
# @api private
|
714
727
|
def create_translations_getter(name, meth)
|
715
728
|
generated_methods.module_eval do
|
716
|
-
re_define_method("#{meth}
|
729
|
+
re_define_method("#{meth}_translations") do
|
717
730
|
attributes[name] ||= {}
|
718
731
|
attributes[name].with_indifferent_access
|
719
732
|
end
|
720
|
-
alias_method :"#{meth}_t", :"#{meth}
|
733
|
+
alias_method :"#{meth}_t", :"#{meth}_translations"
|
721
734
|
end
|
722
735
|
end
|
723
736
|
|
@@ -729,16 +742,18 @@ module Mongoid
|
|
729
742
|
# @param [ String ] name The name of the attribute.
|
730
743
|
# @param [ String ] meth The name of the method.
|
731
744
|
# @param [ Field ] field The field.
|
745
|
+
#
|
746
|
+
# @api private
|
732
747
|
def create_translations_setter(name, meth, field)
|
733
748
|
generated_methods.module_eval do
|
734
|
-
re_define_method("#{meth}
|
749
|
+
re_define_method("#{meth}_translations=") do |value|
|
735
750
|
attribute_will_change!(name)
|
736
751
|
value&.transform_values! do |_value|
|
737
752
|
field.type.mongoize(_value)
|
738
753
|
end
|
739
754
|
attributes[name] = value
|
740
755
|
end
|
741
|
-
alias_method :"#{meth}_t=", :"#{meth}
|
756
|
+
alias_method :"#{meth}_t=", :"#{meth}_translations="
|
742
757
|
end
|
743
758
|
end
|
744
759
|
|
@@ -748,6 +763,8 @@ module Mongoid
|
|
748
763
|
# Person.generated_methods
|
749
764
|
#
|
750
765
|
# @return [ Module ] The module of generated methods.
|
766
|
+
#
|
767
|
+
# @api private
|
751
768
|
def generated_methods
|
752
769
|
@generated_methods ||= begin
|
753
770
|
mod = Module.new
|
@@ -762,11 +779,21 @@ module Mongoid
|
|
762
779
|
# Model.remove_defaults(name)
|
763
780
|
#
|
764
781
|
# @param [ String ] name The field name.
|
782
|
+
#
|
783
|
+
# @api private
|
765
784
|
def remove_defaults(name)
|
766
785
|
pre_processed_defaults.delete_one(name)
|
767
786
|
post_processed_defaults.delete_one(name)
|
768
787
|
end
|
769
788
|
|
789
|
+
# Create a field for the given name and options.
|
790
|
+
#
|
791
|
+
# @param [ Symbol ] name The name of the field.
|
792
|
+
# @param [ Hash ] options The hash of options.
|
793
|
+
#
|
794
|
+
# @return [ Field ] The created field.
|
795
|
+
#
|
796
|
+
# @api private
|
770
797
|
def field_for(name, options)
|
771
798
|
opts = options.merge(klass: self)
|
772
799
|
opts[:type] = retrieve_and_validate_type(name, options[:type])
|
@@ -787,22 +814,32 @@ module Mongoid
|
|
787
814
|
#
|
788
815
|
# @api private
|
789
816
|
def retrieve_and_validate_type(name, type)
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
if result
|
796
|
-
warn_message
|
797
|
-
|
798
|
-
|
817
|
+
type_mapping = TYPE_MAPPINGS[type]
|
818
|
+
result = type_mapping || unmapped_type(type)
|
819
|
+
if !result.is_a?(Class)
|
820
|
+
raise Errors::InvalidFieldType.new(self, name, type)
|
821
|
+
else
|
822
|
+
if INVALID_BSON_CLASSES.include?(result)
|
823
|
+
warn_message = "Using #{result} as the field type is not supported. "
|
824
|
+
if result == BSON::Decimal128
|
825
|
+
warn_message += "In BSON <= 4, the BSON::Decimal128 type will work as expected for both storing and querying, but will return a BigDecimal on query in BSON 5+."
|
826
|
+
else
|
827
|
+
warn_message += "Saving values of this type to the database will work as expected, however, querying them will return a value of the native Ruby Integer type."
|
828
|
+
end
|
829
|
+
Mongoid.logger.warn(warn_message)
|
799
830
|
end
|
800
|
-
Mongoid.logger.warn(warn_message)
|
801
831
|
end
|
802
|
-
|
803
832
|
result
|
804
833
|
end
|
805
834
|
|
835
|
+
# Returns the type of the field if the type was not in the TYPE_MAPPINGS
|
836
|
+
# hash.
|
837
|
+
#
|
838
|
+
# @param [ Symbol | Class ] type The type of the field.
|
839
|
+
#
|
840
|
+
# @return [ Class ] The type of the field.
|
841
|
+
#
|
842
|
+
# @api private
|
806
843
|
def unmapped_type(type)
|
807
844
|
if "Boolean" == type.to_s
|
808
845
|
Mongoid::Boolean
|
@@ -810,19 +847,6 @@ module Mongoid
|
|
810
847
|
type || Object
|
811
848
|
end
|
812
849
|
end
|
813
|
-
|
814
|
-
# Queries whether or not the given type is permitted as a declared field
|
815
|
-
# type.
|
816
|
-
#
|
817
|
-
# @param [ Class ] type The type to query
|
818
|
-
#
|
819
|
-
# @return [ true | false ] whether or not the type is supported
|
820
|
-
#
|
821
|
-
# @api private
|
822
|
-
def unsupported_type?(type)
|
823
|
-
return !Mongoid::Config.allow_bson5_decimal128? if type == BSON::Decimal128
|
824
|
-
INVALID_BSON_CLASSES.include?(type)
|
825
|
-
end
|
826
850
|
end
|
827
851
|
end
|
828
852
|
end
|
data/lib/mongoid/findable.rb
CHANGED
@@ -22,18 +22,24 @@ module Mongoid
|
|
22
22
|
:each,
|
23
23
|
:each_with_index,
|
24
24
|
:extras,
|
25
|
+
:fifth,
|
26
|
+
:fifth!,
|
25
27
|
:find_one_and_delete,
|
26
28
|
:find_one_and_replace,
|
27
29
|
:find_one_and_update,
|
28
30
|
:find_or_create_by,
|
29
31
|
:find_or_create_by!,
|
30
32
|
:find_or_initialize_by,
|
33
|
+
:first!,
|
31
34
|
:first_or_create,
|
32
35
|
:first_or_create!,
|
33
36
|
:first_or_initialize,
|
34
37
|
:for_js,
|
38
|
+
:fourth,
|
39
|
+
:fourth!,
|
35
40
|
:geo_near,
|
36
41
|
:includes,
|
42
|
+
:last!,
|
37
43
|
:map_reduce,
|
38
44
|
:max,
|
39
45
|
:min,
|
@@ -41,11 +47,19 @@ module Mongoid
|
|
41
47
|
:pick,
|
42
48
|
:pluck,
|
43
49
|
:read,
|
50
|
+
:second,
|
51
|
+
:second!,
|
52
|
+
:second_to_last,
|
53
|
+
:second_to_last!,
|
44
54
|
:sum,
|
45
55
|
:take,
|
46
56
|
:take!,
|
47
57
|
:tally,
|
48
58
|
:text_search,
|
59
|
+
:third,
|
60
|
+
:third!,
|
61
|
+
:third_to_last,
|
62
|
+
:third_to_last!,
|
49
63
|
:update,
|
50
64
|
:update_all,
|
51
65
|
|
@@ -87,9 +101,19 @@ module Mongoid
|
|
87
101
|
# @example Do any documents exist for the conditions?
|
88
102
|
# Person.exists?
|
89
103
|
#
|
104
|
+
# @example Do any documents exist for given _id.
|
105
|
+
# Person.exists?(BSON::ObjectId(...))
|
106
|
+
#
|
107
|
+
# @example Do any documents exist for given conditions.
|
108
|
+
# Person.exists?(name: "...")
|
109
|
+
#
|
110
|
+
# @param [ Hash | Object | false ] id_or_conditions an _id to
|
111
|
+
# search for, a hash of conditions, nil or false.
|
112
|
+
#
|
90
113
|
# @return [ true | false ] If any documents exist for the conditions.
|
91
|
-
|
92
|
-
|
114
|
+
# Always false if passed nil or false.
|
115
|
+
def exists?(id_or_conditions = :none)
|
116
|
+
with_default_scope.exists?(id_or_conditions)
|
93
117
|
end
|
94
118
|
|
95
119
|
# Finds a +Document+ or multiple documents by their _id values.
|
@@ -135,7 +159,7 @@ module Mongoid
|
|
135
159
|
# @note Each argument can be an individual id, an array of ids or
|
136
160
|
# a nested array. Each array will be flattened.
|
137
161
|
#
|
138
|
-
# @param [ Object | Array<Object> ] *args The
|
162
|
+
# @param [ [ Object | Array<Object> ]... ] *args The id(s) to find.
|
139
163
|
#
|
140
164
|
# @return [ Document | Array<Document> | nil ] A document or matching documents.
|
141
165
|
#
|