mongoid 4.0.2 → 5.0.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
- data/CHANGELOG.md +66 -1
- data/README.md +11 -19
- data/lib/config/locales/en.yml +33 -30
- data/lib/mongoid/atomic.rb +2 -4
- data/lib/mongoid/attributes/processing.rb +4 -1
- data/lib/mongoid/attributes/readonly.rb +2 -2
- data/lib/mongoid/attributes.rb +7 -7
- data/lib/mongoid/changeable.rb +4 -6
- data/lib/mongoid/clients/factory.rb +78 -0
- data/lib/mongoid/{sessions → clients}/options.rb +30 -19
- data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
- data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
- data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
- data/lib/mongoid/clients/validators.rb +2 -0
- data/lib/mongoid/clients.rb +141 -0
- data/lib/mongoid/composable.rb +3 -3
- data/lib/mongoid/config/environment.rb +1 -1
- data/lib/mongoid/config/options.rb +1 -1
- data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
- data/lib/mongoid/config/validators/option.rb +1 -1
- data/lib/mongoid/config/validators.rb +1 -1
- data/lib/mongoid/config.rb +39 -41
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +11 -11
- data/lib/mongoid/contextual/command.rb +9 -6
- data/lib/mongoid/contextual/geo_near.rb +17 -1
- data/lib/mongoid/contextual/map_reduce.rb +12 -11
- data/lib/mongoid/contextual/memory.rb +2 -5
- data/lib/mongoid/contextual/mongo.rb +92 -82
- data/lib/mongoid/contextual/none.rb +13 -0
- data/lib/mongoid/copyable.rb +6 -1
- data/lib/mongoid/criteria.rb +37 -4
- data/lib/mongoid/document.rb +3 -4
- data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
- data/lib/mongoid/errors/mongoid_error.rb +15 -9
- data/lib/mongoid/errors/no_client_config.rb +22 -0
- data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
- data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
- data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
- data/lib/mongoid/errors/no_default_client.rb +23 -0
- data/lib/mongoid/errors.rb +6 -6
- data/lib/mongoid/extensions/hash.rb +5 -1
- data/lib/mongoid/extensions/object.rb +3 -2
- data/lib/mongoid/extensions/set.rb +5 -5
- data/lib/mongoid/factory.rb +4 -2
- data/lib/mongoid/fields.rb +7 -2
- data/lib/mongoid/findable.rb +4 -1
- data/lib/mongoid/indexable.rb +15 -9
- data/lib/mongoid/persistable/creatable.rb +2 -2
- data/lib/mongoid/persistable/deletable.rb +3 -3
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/poppable.rb +1 -1
- data/lib/mongoid/persistable/pullable.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +2 -2
- data/lib/mongoid/persistable/renamable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/unsettable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/persistable.rb +1 -2
- data/lib/mongoid/query_cache.rb +98 -104
- data/lib/mongoid/railtie.rb +4 -25
- data/lib/mongoid/railties/database.rake +1 -1
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +3 -2
- data/lib/mongoid/relations/builders.rb +3 -1
- data/lib/mongoid/relations/counter_cache.rb +1 -1
- data/lib/mongoid/relations/embedded/batchable.rb +3 -10
- data/lib/mongoid/relations/embedded/many.rb +4 -2
- data/lib/mongoid/relations/embedded/one.rb +1 -0
- data/lib/mongoid/relations/many.rb +1 -0
- data/lib/mongoid/relations/metadata.rb +14 -0
- data/lib/mongoid/relations/proxy.rb +6 -6
- data/lib/mongoid/relations/referenced/many.rb +2 -1
- data/lib/mongoid/relations/targets/enumerable.rb +11 -11
- data/lib/mongoid/relations/touchable.rb +1 -1
- data/lib/mongoid/reloadable.rb +2 -2
- data/lib/mongoid/scopable.rb +6 -17
- data/lib/mongoid/selectable.rb +1 -36
- data/lib/mongoid/serializable.rb +2 -2
- data/lib/mongoid/stateful.rb +0 -1
- data/lib/mongoid/tasks/database.rake +2 -2
- data/lib/mongoid/tasks/database.rb +24 -17
- data/lib/mongoid/threaded/lifecycle.rb +21 -16
- data/lib/mongoid/threaded.rb +54 -33
- data/lib/mongoid/traversable.rb +17 -1
- data/lib/mongoid/validatable/queryable.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +3 -20
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid.rb +28 -21
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
- data/lib/rails/mongoid.rb +2 -2
- data/spec/app/models/audio.rb +1 -1
- data/spec/app/models/band.rb +1 -0
- data/spec/app/models/code.rb +6 -0
- data/spec/app/models/company.rb +5 -0
- data/spec/app/models/label.rb +7 -0
- data/spec/app/models/pub.rb +6 -0
- data/spec/app/models/staff.rb +7 -0
- data/spec/app/models/store_as_dup_test1.rb +5 -0
- data/spec/app/models/store_as_dup_test2.rb +5 -0
- data/spec/config/mongoid.yml +7 -25
- data/spec/mongoid/atomic/paths_spec.rb +3 -11
- data/spec/mongoid/attributes/nested_spec.rb +16 -16
- data/spec/mongoid/attributes/readonly_spec.rb +80 -18
- data/spec/mongoid/attributes_spec.rb +3 -3
- data/spec/mongoid/changeable_spec.rb +156 -0
- data/spec/mongoid/clients/factory_spec.rb +284 -0
- data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
- data/spec/mongoid/clients_spec.rb +754 -0
- data/spec/mongoid/config/environment_spec.rb +14 -11
- data/spec/mongoid/config_spec.rb +33 -48
- data/spec/mongoid/contextual/atomic_spec.rb +1 -17
- data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
- data/spec/mongoid/contextual/mongo_spec.rb +26 -83
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/copyable_spec.rb +35 -1
- data/spec/mongoid/criteria/findable_spec.rb +197 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
- data/spec/mongoid/criteria_spec.rb +74 -91
- data/spec/mongoid/document_spec.rb +1 -1
- data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
- data/spec/mongoid/errors/mongoid_error_spec.rb +19 -3
- data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
- data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
- data/spec/mongoid/fields/localized_spec.rb +1 -0
- data/spec/mongoid/fields_spec.rb +1 -0
- data/spec/mongoid/findable_spec.rb +2 -23
- data/spec/mongoid/indexable_spec.rb +12 -8
- data/spec/mongoid/interceptable_spec.rb +15 -0
- data/spec/mongoid/persistable/settable_spec.rb +16 -0
- data/spec/mongoid/persistable/updatable_spec.rb +3 -2
- data/spec/mongoid/persistable_spec.rb +4 -4
- data/spec/mongoid/query_cache_spec.rb +13 -8
- data/spec/mongoid/relations/auto_save_spec.rb +1 -1
- data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
- data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
- data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
- data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
- data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
- data/spec/mongoid/relations/proxy_spec.rb +28 -0
- data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
- data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
- data/spec/mongoid/reloadable_spec.rb +6 -6
- data/spec/mongoid/scopable_spec.rb +41 -28
- data/spec/mongoid/selectable_spec.rb +6 -16
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +2 -2
- data/spec/mongoid/threaded_spec.rb +0 -7
- data/spec/mongoid/traversable_spec.rb +2 -2
- data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
- data/spec/mongoid_spec.rb +13 -15
- data/spec/rails/mongoid_spec.rb +13 -4
- data/spec/spec_helper.rb +44 -27
- data/spec/support/authorization.rb +12 -0
- data/spec/support/expectations.rb +14 -0
- metadata +50 -57
- data/lib/mongoid/contextual/find_and_modify.rb +0 -69
- data/lib/mongoid/contextual/text_search.rb +0 -178
- data/lib/mongoid/criteria/#findable.rb# +0 -141
- data/lib/mongoid/errors/no_default_session.rb +0 -23
- data/lib/mongoid/errors/no_session_config.rb +0 -22
- data/lib/mongoid/log_subscriber.rb +0 -55
- data/lib/mongoid/positional.rb +0 -71
- data/lib/mongoid/sessions/factory.rb +0 -131
- data/lib/mongoid/sessions/mongo_uri.rb +0 -93
- data/lib/mongoid/sessions/validators.rb +0 -2
- data/lib/mongoid/sessions.rb +0 -125
- data/lib/mongoid/support/query_counter.rb +0 -23
- data/spec/helpers.rb +0 -18
- data/spec/mongoid/#atomic_spec.rb# +0 -365
- data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
- data/spec/mongoid/contextual/text_search_spec.rb +0 -209
- data/spec/mongoid/log_subscriber_spec.rb +0 -75
- data/spec/mongoid/positional_spec.rb +0 -222
- data/spec/mongoid/sessions/factory_spec.rb +0 -333
- data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
- data/spec/mongoid/sessions_spec.rb +0 -1252
data/lib/mongoid/criteria.rb
CHANGED
|
@@ -4,7 +4,6 @@ require "mongoid/criteria/inspectable"
|
|
|
4
4
|
require "mongoid/criteria/marshalable"
|
|
5
5
|
require "mongoid/criteria/modifiable"
|
|
6
6
|
require "mongoid/criteria/scopable"
|
|
7
|
-
require "mongoid/sessions/options"
|
|
8
7
|
|
|
9
8
|
module Mongoid
|
|
10
9
|
|
|
@@ -23,7 +22,7 @@ module Mongoid
|
|
|
23
22
|
include Marshalable
|
|
24
23
|
include Modifiable
|
|
25
24
|
include Scopable
|
|
26
|
-
include
|
|
25
|
+
include Clients::Options
|
|
27
26
|
|
|
28
27
|
# Static array used to check with method missing - we only need to ever
|
|
29
28
|
# instantiate once.
|
|
@@ -196,7 +195,7 @@ module Mongoid
|
|
|
196
195
|
end
|
|
197
196
|
|
|
198
197
|
# Eager loads all the provided relations. Will load all the documents
|
|
199
|
-
# into the identity map
|
|
198
|
+
# into the identity map whose ids match based on the extra query for the
|
|
200
199
|
# ids.
|
|
201
200
|
#
|
|
202
201
|
# @note This will work for embedded relations that reference another
|
|
@@ -293,7 +292,7 @@ module Mongoid
|
|
|
293
292
|
options.merge!(criteria.options)
|
|
294
293
|
self.documents = criteria.documents.dup unless criteria.documents.empty?
|
|
295
294
|
self.scoping_options = criteria.scoping_options
|
|
296
|
-
self.inclusions = (inclusions + criteria.inclusions
|
|
295
|
+
self.inclusions = (inclusions + criteria.inclusions).uniq
|
|
297
296
|
self
|
|
298
297
|
end
|
|
299
298
|
|
|
@@ -335,6 +334,9 @@ module Mongoid
|
|
|
335
334
|
def only(*args)
|
|
336
335
|
return clone if args.flatten.empty?
|
|
337
336
|
args = args.flatten
|
|
337
|
+
if (args & Fields::IDS).empty?
|
|
338
|
+
args.unshift(:_id)
|
|
339
|
+
end
|
|
338
340
|
if klass.hereditary?
|
|
339
341
|
super(*args.push(:_type))
|
|
340
342
|
else
|
|
@@ -342,6 +344,37 @@ module Mongoid
|
|
|
342
344
|
end
|
|
343
345
|
end
|
|
344
346
|
|
|
347
|
+
# Set the read preference for the criteria.
|
|
348
|
+
#
|
|
349
|
+
# @example Set the read preference.
|
|
350
|
+
# criteria.read(mode: :primary_preferred)
|
|
351
|
+
#
|
|
352
|
+
# @param [ Hash ] value The mode preference.
|
|
353
|
+
#
|
|
354
|
+
# @return [ Criteria ] The cloned criteria.
|
|
355
|
+
#
|
|
356
|
+
# @since 5.0.0
|
|
357
|
+
def read(value = nil)
|
|
358
|
+
clone.tap do |criteria|
|
|
359
|
+
criteria.options.merge!(read: value)
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# Overriden to exclude _id from the fields.
|
|
364
|
+
#
|
|
365
|
+
# @example Exclude fields returned from the database.
|
|
366
|
+
# Band.without(:name)
|
|
367
|
+
#
|
|
368
|
+
# @param [ Array<Symbol> ] args The names of the fields.
|
|
369
|
+
#
|
|
370
|
+
# @return [ Criteria ] The cloned criteria.
|
|
371
|
+
#
|
|
372
|
+
# @since 4.0.3
|
|
373
|
+
def without(*args)
|
|
374
|
+
args -= Fields::IDS
|
|
375
|
+
super(*args)
|
|
376
|
+
end
|
|
377
|
+
|
|
345
378
|
# Returns true if criteria responds to the given method.
|
|
346
379
|
#
|
|
347
380
|
# @example Does the criteria respond to the method?
|
data/lib/mongoid/document.rb
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
|
-
require "mongoid/positional"
|
|
3
2
|
require "mongoid/evolvable"
|
|
4
3
|
require "mongoid/extensions"
|
|
5
4
|
require "mongoid/errors"
|
|
@@ -168,7 +167,7 @@ module Mongoid
|
|
|
168
167
|
embedded_relations.each_pair do |name, meta|
|
|
169
168
|
without_autobuild do
|
|
170
169
|
relation, stored = send(name), meta.store_as
|
|
171
|
-
if attributes.
|
|
170
|
+
if attributes.key?(stored) || !relation.blank?
|
|
172
171
|
if relation
|
|
173
172
|
attributes[stored] = relation.as_document
|
|
174
173
|
else
|
|
@@ -262,7 +261,7 @@ module Mongoid
|
|
|
262
261
|
#
|
|
263
262
|
# @since 2.4.0
|
|
264
263
|
def model_key
|
|
265
|
-
@model_cache_key ||=
|
|
264
|
+
@model_cache_key ||= self.class.model_name.cache_key
|
|
266
265
|
end
|
|
267
266
|
|
|
268
267
|
# Implement this for calls to flatten on array.
|
|
@@ -327,7 +326,7 @@ module Mongoid
|
|
|
327
326
|
#
|
|
328
327
|
# @since 1.0.0
|
|
329
328
|
def _types
|
|
330
|
-
@_type ||= (descendants + [ self ]).uniq.map
|
|
329
|
+
@_type ||= (descendants + [ self ]).uniq.map(&:to_s)
|
|
331
330
|
end
|
|
332
331
|
|
|
333
332
|
# Set the i18n scope to overwrite ActiveModel.
|
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
module Mongoid
|
|
3
3
|
module Errors
|
|
4
4
|
|
|
5
|
-
# This error is raised when a
|
|
5
|
+
# This error is raised when a client configuration contains both a uri and
|
|
6
6
|
# other standard options.
|
|
7
|
-
class
|
|
7
|
+
class MixedClientConfiguration < MongoidError
|
|
8
8
|
|
|
9
9
|
# Initialize the error.
|
|
10
10
|
#
|
|
11
11
|
# @example Initialize the error.
|
|
12
|
-
#
|
|
12
|
+
# MixedClientConfiguration.new(:name, {})
|
|
13
13
|
#
|
|
14
|
-
# @param [ Symbol ] name The name of the
|
|
14
|
+
# @param [ Symbol ] name The name of the client config.
|
|
15
15
|
# @param [ Hash ] config The configuration options.
|
|
16
16
|
#
|
|
17
17
|
# @since 3.0.0
|
|
18
18
|
def initialize(name, config)
|
|
19
19
|
super(
|
|
20
20
|
compose_message(
|
|
21
|
-
"
|
|
21
|
+
"mixed_client_configuration",
|
|
22
22
|
{ name: name, config: config }
|
|
23
23
|
)
|
|
24
24
|
)
|
|
@@ -7,6 +7,8 @@ module Mongoid
|
|
|
7
7
|
# translating the messages.
|
|
8
8
|
class MongoidError < StandardError
|
|
9
9
|
|
|
10
|
+
attr_reader :problem, :summary, :resolution
|
|
11
|
+
|
|
10
12
|
BASE_KEY = "mongoid.errors.messages"
|
|
11
13
|
|
|
12
14
|
# Compose the message.
|
|
@@ -18,13 +20,17 @@ module Mongoid
|
|
|
18
20
|
#
|
|
19
21
|
# @since 3.0.0
|
|
20
22
|
def compose_message(key, attributes)
|
|
21
|
-
@problem =
|
|
22
|
-
@summary =
|
|
23
|
-
@resolution =
|
|
23
|
+
@problem = translate_problem(key, attributes)
|
|
24
|
+
@summary = translate_summary(key, attributes)
|
|
25
|
+
@resolution = translate_resolution(key, attributes)
|
|
26
|
+
@problem_title = translate("message_title", {})
|
|
27
|
+
@summary_title = translate("summary_title", {})
|
|
28
|
+
@resolution_title = translate("resolution_title", {})
|
|
29
|
+
|
|
24
30
|
|
|
25
|
-
"\
|
|
26
|
-
"\
|
|
27
|
-
"\
|
|
31
|
+
"\n#{@problem_title}:\n #{@problem}"+
|
|
32
|
+
"\n#{@summary_title}:\n #{@summary}"+
|
|
33
|
+
"\n#{@resolution_title}:\n #{@resolution}"
|
|
28
34
|
end
|
|
29
35
|
|
|
30
36
|
private
|
|
@@ -54,7 +60,7 @@ module Mongoid
|
|
|
54
60
|
# @return [ String ] The problem.
|
|
55
61
|
#
|
|
56
62
|
# @since 3.0.0
|
|
57
|
-
def
|
|
63
|
+
def translate_problem(key, attributes)
|
|
58
64
|
translate("#{key}.message", attributes)
|
|
59
65
|
end
|
|
60
66
|
|
|
@@ -69,7 +75,7 @@ module Mongoid
|
|
|
69
75
|
# @return [ String ] The summary.
|
|
70
76
|
#
|
|
71
77
|
# @since 3.0.0
|
|
72
|
-
def
|
|
78
|
+
def translate_summary(key, attributes)
|
|
73
79
|
translate("#{key}.summary", attributes)
|
|
74
80
|
end
|
|
75
81
|
|
|
@@ -84,7 +90,7 @@ module Mongoid
|
|
|
84
90
|
# @return [ String ] The resolution.
|
|
85
91
|
#
|
|
86
92
|
# @since 3.0.0
|
|
87
|
-
def
|
|
93
|
+
def translate_resolution(key, attributes)
|
|
88
94
|
translate("#{key}.resolution", attributes)
|
|
89
95
|
end
|
|
90
96
|
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid
|
|
3
|
+
module Errors
|
|
4
|
+
|
|
5
|
+
# This error is raised when attempting to create a new client that does
|
|
6
|
+
# not have a named configuration.
|
|
7
|
+
class NoClientConfig < MongoidError
|
|
8
|
+
|
|
9
|
+
# Create the new error.
|
|
10
|
+
#
|
|
11
|
+
# @example Create the error.
|
|
12
|
+
# NoClientConfig.new(:secondary)
|
|
13
|
+
#
|
|
14
|
+
# @param [ String, Symbol ] name The name of the client.
|
|
15
|
+
#
|
|
16
|
+
# @since 3.0.0
|
|
17
|
+
def initialize(name)
|
|
18
|
+
super(compose_message("no_client_config", { name: name }))
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
module Mongoid
|
|
3
3
|
module Errors
|
|
4
4
|
|
|
5
|
-
# This error is raised when a
|
|
6
|
-
class
|
|
5
|
+
# This error is raised when a client is configured without a database.
|
|
6
|
+
class NoClientDatabase < MongoidError
|
|
7
7
|
|
|
8
8
|
# Create the new error.
|
|
9
9
|
#
|
|
10
10
|
# @example Create the new error.
|
|
11
|
-
#
|
|
11
|
+
# NoClientDatabase.new(:default, {}})
|
|
12
12
|
#
|
|
13
13
|
# @param [ Symbol, String ] name The db config key.
|
|
14
14
|
# @param [ Hash ] config The hash configuration options.
|
|
@@ -17,7 +17,7 @@ module Mongoid
|
|
|
17
17
|
def initialize(name, config)
|
|
18
18
|
super(
|
|
19
19
|
compose_message(
|
|
20
|
-
"
|
|
20
|
+
"no_client_database",
|
|
21
21
|
{ name: name, config: config }
|
|
22
22
|
)
|
|
23
23
|
)
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
module Mongoid
|
|
3
3
|
module Errors
|
|
4
4
|
|
|
5
|
-
# This error is raised when a
|
|
6
|
-
class
|
|
5
|
+
# This error is raised when a client is configured without hosts.
|
|
6
|
+
class NoClientHosts < MongoidError
|
|
7
7
|
|
|
8
8
|
# Create the new error.
|
|
9
9
|
#
|
|
10
10
|
# @example Create the new error.
|
|
11
|
-
#
|
|
11
|
+
# NoClientHosts.new(:default, {}})
|
|
12
12
|
#
|
|
13
13
|
# @param [ Symbol, String ] name The db config key.
|
|
14
14
|
# @param [ Hash ] config The hash configuration options.
|
|
@@ -17,7 +17,7 @@ module Mongoid
|
|
|
17
17
|
def initialize(name, config)
|
|
18
18
|
super(
|
|
19
19
|
compose_message(
|
|
20
|
-
"
|
|
20
|
+
"no_client_hosts",
|
|
21
21
|
{ name: name, config: config }
|
|
22
22
|
)
|
|
23
23
|
)
|
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
module Mongoid
|
|
3
3
|
module Errors
|
|
4
4
|
|
|
5
|
-
# This error is raised when no
|
|
5
|
+
# This error is raised when no clients exists in the database
|
|
6
6
|
# configuration.
|
|
7
|
-
class
|
|
7
|
+
class NoClientsConfig < MongoidError
|
|
8
8
|
|
|
9
9
|
# Create the new error.
|
|
10
10
|
#
|
|
11
11
|
# @example Create the error.
|
|
12
|
-
#
|
|
12
|
+
# NoClientsConfig.new
|
|
13
13
|
#
|
|
14
14
|
# @since 3.0.0
|
|
15
15
|
def initialize
|
|
16
|
-
super(compose_message("
|
|
16
|
+
super(compose_message("no_clients_config", {}))
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid
|
|
3
|
+
module Errors
|
|
4
|
+
|
|
5
|
+
# This error is raised when a default client is not defined.
|
|
6
|
+
class NoDefaultClient < MongoidError
|
|
7
|
+
|
|
8
|
+
# Create the new error with the defined client names.
|
|
9
|
+
#
|
|
10
|
+
# @example Create the new error.
|
|
11
|
+
# NoDefaultClient.new([ :secondary ])
|
|
12
|
+
#
|
|
13
|
+
# @param [ Array<Symbol> ] keys The defined clients.
|
|
14
|
+
#
|
|
15
|
+
# @since 3.0.0
|
|
16
|
+
def initialize(keys)
|
|
17
|
+
super(
|
|
18
|
+
compose_message("no_default_client", { keys: keys.join(", ") })
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/mongoid/errors.rb
CHANGED
|
@@ -22,17 +22,17 @@ require "mongoid/errors/invalid_time"
|
|
|
22
22
|
require "mongoid/errors/invalid_value"
|
|
23
23
|
require "mongoid/errors/inverse_not_found"
|
|
24
24
|
require "mongoid/errors/mixed_relations"
|
|
25
|
-
require "mongoid/errors/
|
|
25
|
+
require "mongoid/errors/mixed_client_configuration"
|
|
26
26
|
require "mongoid/errors/nested_attributes_metadata_not_found"
|
|
27
|
-
require "mongoid/errors/
|
|
27
|
+
require "mongoid/errors/no_default_client"
|
|
28
28
|
require "mongoid/errors/no_environment"
|
|
29
29
|
require "mongoid/errors/no_map_reduce_output"
|
|
30
30
|
require "mongoid/errors/no_metadata"
|
|
31
31
|
require "mongoid/errors/no_parent"
|
|
32
|
-
require "mongoid/errors/
|
|
33
|
-
require "mongoid/errors/
|
|
34
|
-
require "mongoid/errors/
|
|
35
|
-
require "mongoid/errors/
|
|
32
|
+
require "mongoid/errors/no_client_config"
|
|
33
|
+
require "mongoid/errors/no_clients_config"
|
|
34
|
+
require "mongoid/errors/no_client_database"
|
|
35
|
+
require "mongoid/errors/no_client_hosts"
|
|
36
36
|
require "mongoid/errors/readonly_attribute"
|
|
37
37
|
require "mongoid/errors/readonly_document"
|
|
38
38
|
require "mongoid/errors/scope_overwrite"
|
|
@@ -24,7 +24,11 @@ module Mongoid
|
|
|
24
24
|
#
|
|
25
25
|
# @since 3.0.0
|
|
26
26
|
def __mongoize_object_id__
|
|
27
|
-
|
|
27
|
+
if id = self['$oid']
|
|
28
|
+
BSON::ObjectId.from_string(id)
|
|
29
|
+
else
|
|
30
|
+
update_values(&:__mongoize_object_id__)
|
|
31
|
+
end
|
|
28
32
|
end
|
|
29
33
|
|
|
30
34
|
# Consolidate the key/values in the hash under an atomic $set.
|
|
@@ -115,8 +115,9 @@ module Mongoid
|
|
|
115
115
|
#
|
|
116
116
|
# @since 2.0.0.rc.1
|
|
117
117
|
def ivar(name)
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
var_name = "@_#{name}"
|
|
119
|
+
if instance_variable_defined?(var_name)
|
|
120
|
+
return instance_variable_get(var_name)
|
|
120
121
|
else
|
|
121
122
|
false
|
|
122
123
|
end
|
|
@@ -9,7 +9,7 @@ module Mongoid
|
|
|
9
9
|
# @example Mongoize the object.
|
|
10
10
|
# set.mongoize
|
|
11
11
|
#
|
|
12
|
-
# @return [
|
|
12
|
+
# @return [ Array ] The object mongoized.
|
|
13
13
|
#
|
|
14
14
|
# @since 3.0.0
|
|
15
15
|
def mongoize
|
|
@@ -21,9 +21,9 @@ module Mongoid
|
|
|
21
21
|
# Convert the object from its mongo friendly ruby type to this type.
|
|
22
22
|
#
|
|
23
23
|
# @example Demongoize the object.
|
|
24
|
-
# Set.demongoize(
|
|
24
|
+
# Set.demongoize([1, 2, 3])
|
|
25
25
|
#
|
|
26
|
-
# @param [
|
|
26
|
+
# @param [ Array ] object The object to demongoize.
|
|
27
27
|
#
|
|
28
28
|
# @return [ Set ] The set.
|
|
29
29
|
#
|
|
@@ -36,11 +36,11 @@ module Mongoid
|
|
|
36
36
|
# type.
|
|
37
37
|
#
|
|
38
38
|
# @example Mongoize the object.
|
|
39
|
-
# Set.mongoize(1
|
|
39
|
+
# Set.mongoize(Set.new([1,2,3]))
|
|
40
40
|
#
|
|
41
41
|
# @param [ Set ] object The object to mongoize.
|
|
42
42
|
#
|
|
43
|
-
# @return [
|
|
43
|
+
# @return [ Array ] The object mongoized.
|
|
44
44
|
#
|
|
45
45
|
# @since 3.0.0
|
|
46
46
|
def mongoize(object)
|
data/lib/mongoid/factory.rb
CHANGED
|
@@ -5,6 +5,8 @@ module Mongoid
|
|
|
5
5
|
module Factory
|
|
6
6
|
extend self
|
|
7
7
|
|
|
8
|
+
TYPE = "_type".freeze
|
|
9
|
+
|
|
8
10
|
# Builds a new +Document+ from the supplied attributes.
|
|
9
11
|
#
|
|
10
12
|
# @example Build the document.
|
|
@@ -16,7 +18,7 @@ module Mongoid
|
|
|
16
18
|
#
|
|
17
19
|
# @return [ Document ] The instantiated document.
|
|
18
20
|
def build(klass, attributes = nil)
|
|
19
|
-
type = (attributes || {})[
|
|
21
|
+
type = (attributes || {})[TYPE]
|
|
20
22
|
if type && klass._types.include?(type)
|
|
21
23
|
type.constantize.new(attributes)
|
|
22
24
|
else
|
|
@@ -37,7 +39,7 @@ module Mongoid
|
|
|
37
39
|
#
|
|
38
40
|
# @return [ Document ] The instantiated document.
|
|
39
41
|
def from_db(klass, attributes = nil, selected_fields = nil)
|
|
40
|
-
type = (attributes || {})[
|
|
42
|
+
type = (attributes || {})[TYPE]
|
|
41
43
|
if type.blank?
|
|
42
44
|
klass.instantiate(attributes, selected_fields)
|
|
43
45
|
else
|
data/lib/mongoid/fields.rb
CHANGED
|
@@ -32,6 +32,11 @@ module Mongoid
|
|
|
32
32
|
time: Time
|
|
33
33
|
}.with_indifferent_access
|
|
34
34
|
|
|
35
|
+
# Constant for all names of the id field in a document.
|
|
36
|
+
#
|
|
37
|
+
# @since 5.0.0
|
|
38
|
+
IDS = [ :_id, :id, '_id', 'id' ].freeze
|
|
39
|
+
|
|
35
40
|
included do
|
|
36
41
|
class_attribute :aliased_fields
|
|
37
42
|
class_attribute :localized_fields
|
|
@@ -93,7 +98,7 @@ module Mongoid
|
|
|
93
98
|
#
|
|
94
99
|
# @since 2.4.0
|
|
95
100
|
def apply_default(name)
|
|
96
|
-
unless attributes.
|
|
101
|
+
unless attributes.key?(name)
|
|
97
102
|
if field = fields[name]
|
|
98
103
|
default = field.eval_default(self)
|
|
99
104
|
unless default.nil? || field.lazy?
|
|
@@ -358,7 +363,7 @@ module Mongoid
|
|
|
358
363
|
field_options = field.options
|
|
359
364
|
|
|
360
365
|
Fields.options.each_pair do |option_name, handler|
|
|
361
|
-
if field_options.
|
|
366
|
+
if field_options.key?(option_name)
|
|
362
367
|
handler.call(self, field, field_options[option_name])
|
|
363
368
|
end
|
|
364
369
|
end
|
data/lib/mongoid/findable.rb
CHANGED
|
@@ -19,7 +19,9 @@ module Mongoid
|
|
|
19
19
|
:each,
|
|
20
20
|
:each_with_index,
|
|
21
21
|
:extras,
|
|
22
|
-
:
|
|
22
|
+
:find_one_and_delete,
|
|
23
|
+
:find_one_and_replace,
|
|
24
|
+
:find_one_and_update,
|
|
23
25
|
:find_or_create_by,
|
|
24
26
|
:find_or_create_by!,
|
|
25
27
|
:find_or_initialize_by,
|
|
@@ -34,6 +36,7 @@ module Mongoid
|
|
|
34
36
|
:min,
|
|
35
37
|
:none,
|
|
36
38
|
:pluck,
|
|
39
|
+
:read,
|
|
37
40
|
:sum,
|
|
38
41
|
:text_search,
|
|
39
42
|
:update,
|
data/lib/mongoid/indexable.rb
CHANGED
|
@@ -31,10 +31,10 @@ module Mongoid
|
|
|
31
31
|
index_specifications.each do |spec|
|
|
32
32
|
key, options = spec.key, spec.options
|
|
33
33
|
if database = options[:database]
|
|
34
|
-
with(read: :primary, database: database).
|
|
35
|
-
collection.indexes.
|
|
34
|
+
with(read: { mode: :primary }, database: database).
|
|
35
|
+
collection.indexes.create_one(key, options.except(:database))
|
|
36
36
|
else
|
|
37
|
-
with(read: :primary).collection.indexes.
|
|
37
|
+
with(read: { mode: :primary }).collection.indexes.create_one(key, options)
|
|
38
38
|
end
|
|
39
39
|
end and true
|
|
40
40
|
end
|
|
@@ -50,12 +50,18 @@ module Mongoid
|
|
|
50
50
|
# @since 3.0.0
|
|
51
51
|
def remove_indexes
|
|
52
52
|
indexed_database_names.each do |database|
|
|
53
|
-
collection = with(read: :primary, database: database).collection
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
collection = with(read: { mode: :primary }, database: database).collection
|
|
54
|
+
begin
|
|
55
|
+
collection.indexes.each do |spec|
|
|
56
|
+
unless spec["name"] == "_id_"
|
|
57
|
+
collection.indexes.drop_one(spec["key"])
|
|
58
|
+
logger.info(
|
|
59
|
+
"MONGOID: Removed index '#{spec["name"]}' on collection " +
|
|
60
|
+
"'#{collection.name}' in database '#{database}'."
|
|
61
|
+
)
|
|
62
|
+
end
|
|
57
63
|
end
|
|
58
|
-
end
|
|
64
|
+
rescue Mongo::Error::OperationFailure; end
|
|
59
65
|
end and true
|
|
60
66
|
end
|
|
61
67
|
|
|
@@ -142,7 +148,7 @@ module Mongoid
|
|
|
142
148
|
#
|
|
143
149
|
# @since 4.0.0
|
|
144
150
|
def index_keys
|
|
145
|
-
index_specifications.map
|
|
151
|
+
index_specifications.map(&:key)
|
|
146
152
|
end
|
|
147
153
|
end
|
|
148
154
|
end
|
|
@@ -61,7 +61,7 @@ module Mongoid
|
|
|
61
61
|
_parent.insert
|
|
62
62
|
else
|
|
63
63
|
selector = _parent.atomic_selector
|
|
64
|
-
_root.collection.find(selector).
|
|
64
|
+
_root.collection.find(selector).update_one(atomic_inserts)
|
|
65
65
|
end
|
|
66
66
|
end
|
|
67
67
|
|
|
@@ -76,7 +76,7 @@ module Mongoid
|
|
|
76
76
|
#
|
|
77
77
|
# @since 4.0.0
|
|
78
78
|
def insert_as_root
|
|
79
|
-
collection.
|
|
79
|
+
collection.insert_one(as_document)
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
# Post process an insert, which sets the new record attribute to false
|
|
@@ -62,7 +62,7 @@ module Mongoid
|
|
|
62
62
|
_parent.remove_child(self) if notifying_parent?(options)
|
|
63
63
|
if _parent.persisted?
|
|
64
64
|
selector = _parent.atomic_selector
|
|
65
|
-
_root.collection.find(selector).
|
|
65
|
+
_root.collection.find(selector).update_one(atomic_deletes)
|
|
66
66
|
end
|
|
67
67
|
true
|
|
68
68
|
end
|
|
@@ -78,7 +78,7 @@ module Mongoid
|
|
|
78
78
|
#
|
|
79
79
|
# @since 4.0.0
|
|
80
80
|
def delete_as_root
|
|
81
|
-
collection.find(atomic_selector).
|
|
81
|
+
collection.find(atomic_selector).delete_one
|
|
82
82
|
true
|
|
83
83
|
end
|
|
84
84
|
|
|
@@ -140,7 +140,7 @@ module Mongoid
|
|
|
140
140
|
selector.merge!(_type: name) if hereditary?
|
|
141
141
|
coll = collection
|
|
142
142
|
deleted = coll.find(selector).count
|
|
143
|
-
coll.find(selector).
|
|
143
|
+
coll.find(selector).delete_many
|
|
144
144
|
deleted
|
|
145
145
|
end
|
|
146
146
|
end
|
|
@@ -17,7 +17,7 @@ module Mongoid
|
|
|
17
17
|
#
|
|
18
18
|
# @param [ Hash ] pulls The field/value pull pairs.
|
|
19
19
|
#
|
|
20
|
-
# @return [
|
|
20
|
+
# @return [ Document ] The document.
|
|
21
21
|
#
|
|
22
22
|
# @since 4.0.0
|
|
23
23
|
def pull(pulls)
|
|
@@ -37,7 +37,7 @@ module Mongoid
|
|
|
37
37
|
#
|
|
38
38
|
# @param [ Hash ] pulls The pull all operations.
|
|
39
39
|
#
|
|
40
|
-
# @return [
|
|
40
|
+
# @return [ Document ] The document.
|
|
41
41
|
#
|
|
42
42
|
# @since 4.0.0
|
|
43
43
|
def pull_all(pulls)
|
|
@@ -16,7 +16,7 @@ module Mongoid
|
|
|
16
16
|
#
|
|
17
17
|
# @param [ Hash ] adds The field/value pairs to add.
|
|
18
18
|
#
|
|
19
|
-
# @return [
|
|
19
|
+
# @return [ Document ] The document.
|
|
20
20
|
#
|
|
21
21
|
# @since 4.0.0
|
|
22
22
|
def add_to_set(adds)
|
|
@@ -43,7 +43,7 @@ module Mongoid
|
|
|
43
43
|
#
|
|
44
44
|
# @param [ Hash ] pushes The $push operations.
|
|
45
45
|
#
|
|
46
|
-
# @return [
|
|
46
|
+
# @return [ Document ] The document.
|
|
47
47
|
#
|
|
48
48
|
# @since 4.0.0
|
|
49
49
|
def push(pushes)
|