mongoid 4.0.2 → 5.0.0.beta
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 +14 -13
- data/lib/config/locales/en.yml +28 -28
- data/lib/mongoid.rb +28 -21
- data/lib/mongoid/atomic.rb +2 -4
- data/lib/mongoid/attributes.rb +7 -7
- data/lib/mongoid/attributes/processing.rb +4 -1
- data/lib/mongoid/attributes/readonly.rb +2 -2
- data/lib/mongoid/changeable.rb +4 -6
- data/lib/mongoid/clients.rb +142 -0
- 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/clients/validators.rb +2 -0
- data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
- data/lib/mongoid/composable.rb +3 -3
- data/lib/mongoid/config.rb +39 -41
- data/lib/mongoid/config/environment.rb +1 -1
- data/lib/mongoid/config/validators.rb +1 -1
- data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
- 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 +36 -3
- data/lib/mongoid/document.rb +3 -4
- data/lib/mongoid/errors.rb +6 -6
- data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
- 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/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.rb +1 -2
- 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/query_cache.rb +98 -104
- data/lib/mongoid/railtie.rb +1 -21
- data/lib/mongoid/railties/database.rake +1 -1
- 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/many.rb +1 -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 +23 -16
- data/lib/mongoid/threaded.rb +54 -33
- data/lib/mongoid/threaded/lifecycle.rb +21 -16
- data/lib/mongoid/traversable.rb +16 -1
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/validatable/queryable.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +3 -20
- data/lib/mongoid/version.rb +1 -1
- 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/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 +70 -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 +739 -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/{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 +52 -59
- 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.rb +0 -125
- 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/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/railtie.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require "mongoid"
|
3
|
-
require "mongoid/config"
|
4
2
|
require "mongoid/railties/document"
|
5
3
|
require "rails"
|
6
4
|
require "rails/mongoid"
|
@@ -60,7 +58,7 @@ module Rails
|
|
60
58
|
if config_file.file?
|
61
59
|
begin
|
62
60
|
::Mongoid.load!(config_file)
|
63
|
-
rescue ::Mongoid::Errors::
|
61
|
+
rescue ::Mongoid::Errors::NoClientsConfig => e
|
64
62
|
handle_configuration_error(e)
|
65
63
|
rescue ::Mongoid::Errors::NoDefaultSession => e
|
66
64
|
handle_configuration_error(e)
|
@@ -95,24 +93,6 @@ module Rails
|
|
95
93
|
end
|
96
94
|
end
|
97
95
|
|
98
|
-
config.after_initialize do
|
99
|
-
# Unicorn clears the START_CTX when a worker is forked, so if we have
|
100
|
-
# data in START_CTX then we know we're being preloaded. Unicorn does
|
101
|
-
# not provide application-level hooks for executing code after the
|
102
|
-
# process has forked, so we reconnect lazily.
|
103
|
-
if defined?(Unicorn) && !Unicorn::HttpServer::START_CTX.empty?
|
104
|
-
::Mongoid.default_session.disconnect if ::Mongoid.configured?
|
105
|
-
end
|
106
|
-
|
107
|
-
# Passenger provides the :starting_worker_process event for executing
|
108
|
-
# code after it has forked, so we use that and reconnect immediately.
|
109
|
-
if ::Mongoid::Config.running_with_passenger?
|
110
|
-
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
111
|
-
::Mongoid.default_session.disconnect if forked
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
96
|
# Rails runs all initializers first before getting into any generator
|
117
97
|
# code, so we have no way in the intitializer to know if we are
|
118
98
|
# generating a mongoid.yml. So instead of failing, we catch all the
|
@@ -91,7 +91,9 @@ module Mongoid
|
|
91
91
|
re_define_method("create_#{name}") do |*args|
|
92
92
|
attributes, options = parse_args(*args)
|
93
93
|
document = Factory.build(metadata.klass, attributes)
|
94
|
-
doc =
|
94
|
+
doc = _assigning do
|
95
|
+
send("#{name}=", document)
|
96
|
+
end
|
95
97
|
doc.save
|
96
98
|
save if new_record? && metadata.stores_foreign_key?
|
97
99
|
doc
|
@@ -116,7 +116,7 @@ module Mongoid
|
|
116
116
|
|
117
117
|
before_destroy do
|
118
118
|
if record = __send__(name)
|
119
|
-
record[cache_column] = (record[cache_column] || 0) - 1
|
119
|
+
record[cache_column] = (record[cache_column] || 0) - 1 unless record.frozen?
|
120
120
|
|
121
121
|
if record.persisted?
|
122
122
|
record.class.decrement_counter(cache_column, record._id)
|
@@ -6,7 +6,6 @@ module Mongoid
|
|
6
6
|
# Contains behaviour for executing operations in batch on embedded
|
7
7
|
# documents.
|
8
8
|
module Batchable
|
9
|
-
include Positional
|
10
9
|
|
11
10
|
# Insert new documents as a batch push ($pushAll). This ensures that
|
12
11
|
# all callbacks are run at the appropriate time and only 1 request is
|
@@ -37,9 +36,7 @@ module Mongoid
|
|
37
36
|
def batch_clear(docs)
|
38
37
|
pre_process_batch_remove(docs, :delete)
|
39
38
|
unless docs.empty?
|
40
|
-
collection.find(selector).
|
41
|
-
positionally(selector, "$unset" => { path => true })
|
42
|
-
)
|
39
|
+
collection.find(selector).update_one("$unset" => { path => true })
|
43
40
|
post_process_batch_remove(docs, :delete)
|
44
41
|
end
|
45
42
|
_unscoped.clear
|
@@ -57,9 +54,7 @@ module Mongoid
|
|
57
54
|
def batch_remove(docs, method = :delete)
|
58
55
|
removals = pre_process_batch_remove(docs, method)
|
59
56
|
if !docs.empty?
|
60
|
-
collection.find(selector).
|
61
|
-
positionally(selector, "$pullAll" => { path => removals })
|
62
|
-
)
|
57
|
+
collection.find(selector).update_one("$pullAll" => { path => removals })
|
63
58
|
post_process_batch_remove(docs, method)
|
64
59
|
end
|
65
60
|
reindex
|
@@ -130,9 +125,7 @@ module Mongoid
|
|
130
125
|
self.inserts_valid = true
|
131
126
|
inserts = pre_process_batch_insert(docs)
|
132
127
|
if insertable?
|
133
|
-
collection.find(selector).
|
134
|
-
positionally(selector, operation => { path => inserts })
|
135
|
-
)
|
128
|
+
collection.find(selector).update_one(operation => { path => inserts })
|
136
129
|
post_process_batch_insert(docs)
|
137
130
|
end
|
138
131
|
inserts
|
@@ -84,6 +84,7 @@ module Mongoid
|
|
84
84
|
doc.apply_post_processed_defaults
|
85
85
|
yield(doc) if block_given?
|
86
86
|
doc.run_callbacks(:build) { doc }
|
87
|
+
base._reset_memoized_children!
|
87
88
|
doc
|
88
89
|
end
|
89
90
|
alias :new :build
|
@@ -161,7 +162,7 @@ module Mongoid
|
|
161
162
|
#
|
162
163
|
# @example Delete the matching documents.
|
163
164
|
# person.addresses.delete_if do |doc|
|
164
|
-
# doc.state
|
165
|
+
# doc.state == "GA"
|
165
166
|
# end
|
166
167
|
#
|
167
168
|
# @return [ Many, Enumerator ] The relation or an enumerator if no
|
@@ -170,7 +171,8 @@ module Mongoid
|
|
170
171
|
# @since 3.1.0
|
171
172
|
def delete_if
|
172
173
|
if block_given?
|
173
|
-
target.
|
174
|
+
dup_target = target.dup
|
175
|
+
dup_target.each do |doc|
|
174
176
|
delete(doc) if yield(doc)
|
175
177
|
end
|
176
178
|
self
|
@@ -12,7 +12,7 @@ module Mongoid
|
|
12
12
|
# We undefine most methods to get them sent through to the target.
|
13
13
|
instance_methods.each do |method|
|
14
14
|
undef_method(method) unless
|
15
|
-
method =~ /(^__|^send|^object_id|^respond_to|^tap|extend_proxy|extend_proxies)/
|
15
|
+
method =~ /(^__|^send|^object_id|^respond_to|^tap|^public_send|extend_proxy|extend_proxies)/
|
16
16
|
end
|
17
17
|
|
18
18
|
include Threaded::Lifecycle
|
@@ -85,7 +85,7 @@ module Mongoid
|
|
85
85
|
end
|
86
86
|
|
87
87
|
# Tell the next persistance operation to store in a specific collection,
|
88
|
-
# database or
|
88
|
+
# database or client.
|
89
89
|
#
|
90
90
|
# @example Save the current document to a different collection.
|
91
91
|
# model.with(collection: "secondary").save
|
@@ -93,17 +93,17 @@ module Mongoid
|
|
93
93
|
# @example Save the current document to a different database.
|
94
94
|
# model.with(database: "secondary").save
|
95
95
|
#
|
96
|
-
# @example Save the current document to a different
|
97
|
-
# model.with(
|
96
|
+
# @example Save the current document to a different client.
|
97
|
+
# model.with(client: "replica_set").save
|
98
98
|
#
|
99
99
|
# @example Save with a combination of options.
|
100
|
-
# model.with(
|
100
|
+
# model.with(client: "sharded", database: "secondary").save
|
101
101
|
#
|
102
102
|
# @param [ Hash ] options The storage options.
|
103
103
|
#
|
104
104
|
# @option options [ String, Symbol ] :collection The collection name.
|
105
105
|
# @option options [ String, Symbol ] :database The database name.
|
106
|
-
# @option options [ String, Symbol ] :
|
106
|
+
# @option options [ String, Symbol ] :client The client name.
|
107
107
|
#
|
108
108
|
# @return [ Document ] The current document.
|
109
109
|
#
|
@@ -320,6 +320,7 @@ module Mongoid
|
|
320
320
|
#
|
321
321
|
# @since 2.0.0.rc.1
|
322
322
|
def append(document)
|
323
|
+
# @todo: remove?
|
323
324
|
document.with(@persistence_options) if @persistence_options
|
324
325
|
|
325
326
|
execute_callback :before_add, document
|
@@ -428,7 +429,7 @@ module Mongoid
|
|
428
429
|
# @since 3.0.0
|
429
430
|
def persist_delayed(docs, inserts)
|
430
431
|
unless docs.empty?
|
431
|
-
collection.
|
432
|
+
collection.insert_many(inserts)
|
432
433
|
docs.each do |doc|
|
433
434
|
doc.new_record = false
|
434
435
|
doc.run_after_callbacks(:create, :save)
|
@@ -45,7 +45,7 @@ module Mongoid
|
|
45
45
|
#
|
46
46
|
# @since 3.1.4
|
47
47
|
def ===(other)
|
48
|
-
other.class == Class ? Array == other : self == other
|
48
|
+
other.class == Class ? (Array == other || Enumerable == other) : self == other
|
49
49
|
end
|
50
50
|
|
51
51
|
# Append a document to the enumerable.
|
@@ -175,7 +175,8 @@ module Mongoid
|
|
175
175
|
end
|
176
176
|
if _loaded?
|
177
177
|
_loaded.each_pair do |id, doc|
|
178
|
-
|
178
|
+
document = _added.delete(doc._id) || doc
|
179
|
+
yield(document)
|
179
180
|
end
|
180
181
|
else
|
181
182
|
unloaded_documents.each do |doc|
|
@@ -217,7 +218,10 @@ module Mongoid
|
|
217
218
|
#
|
218
219
|
# @since 2.1.0
|
219
220
|
def first
|
220
|
-
|
221
|
+
_loaded.try(:values).try(:first) ||
|
222
|
+
_added[(ul = _unloaded.try(:first)).try(:id)] ||
|
223
|
+
ul ||
|
224
|
+
_added.values.try(:first)
|
221
225
|
end
|
222
226
|
|
223
227
|
# Initialize the new enumerable either with a criteria or an array.
|
@@ -298,7 +302,10 @@ module Mongoid
|
|
298
302
|
#
|
299
303
|
# @since 2.1.0
|
300
304
|
def last
|
301
|
-
|
305
|
+
_added.values.try(:last) ||
|
306
|
+
_loaded.try(:values).try(:last) ||
|
307
|
+
_added[(ul = _unloaded.try(:last)).try(:id)] ||
|
308
|
+
ul
|
302
309
|
end
|
303
310
|
|
304
311
|
# Loads all the documents in the enumerable from the database.
|
@@ -457,13 +464,6 @@ module Mongoid
|
|
457
464
|
entries.send(name, *args, &block)
|
458
465
|
end
|
459
466
|
|
460
|
-
def matching_document(location)
|
461
|
-
_loaded.try(:values).try(location) ||
|
462
|
-
_added[(ul = _unloaded.try(location)).try(:id)] ||
|
463
|
-
ul ||
|
464
|
-
_added.values.try(location)
|
465
|
-
end
|
466
|
-
|
467
467
|
def unloaded_documents
|
468
468
|
_unloaded.selector.values.any?(&:blank_criteria?) ? [] : _unloaded
|
469
469
|
end
|
@@ -31,7 +31,7 @@ module Mongoid
|
|
31
31
|
touches = touch_atomic_updates(field)
|
32
32
|
unless touches.empty?
|
33
33
|
selector = atomic_selector
|
34
|
-
_root.collection.
|
34
|
+
_root.collection.find(selector).update_one(touches)
|
35
35
|
end
|
36
36
|
run_callbacks(:touch)
|
37
37
|
true
|
data/lib/mongoid/reloadable.rb
CHANGED
@@ -57,7 +57,7 @@ module Mongoid
|
|
57
57
|
#
|
58
58
|
# @since 2.3.2
|
59
59
|
def reload_root_document
|
60
|
-
{}.merge(
|
60
|
+
{}.merge(collection.find(_id: _id).read(mode: :primary).first || {})
|
61
61
|
end
|
62
62
|
|
63
63
|
# Reload the embedded document.
|
@@ -70,7 +70,7 @@ module Mongoid
|
|
70
70
|
# @since 2.3.2
|
71
71
|
def reload_embedded_document
|
72
72
|
extract_embedded_attributes({}.merge(
|
73
|
-
_root.
|
73
|
+
_root.collection.find(_id: _root._id).read(mode: :primary).first
|
74
74
|
))
|
75
75
|
end
|
76
76
|
|
data/lib/mongoid/scopable.rb
CHANGED
@@ -31,7 +31,7 @@ module Mongoid
|
|
31
31
|
def apply_default_scoping
|
32
32
|
if default_scoping
|
33
33
|
default_scoping.call.selector.each do |field, value|
|
34
|
-
attributes[field] = value unless value.respond_to?(:
|
34
|
+
attributes[field] = value unless value.respond_to?(:each)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -115,7 +115,7 @@ module Mongoid
|
|
115
115
|
#
|
116
116
|
# @since 3.0.0
|
117
117
|
def queryable
|
118
|
-
|
118
|
+
Threaded.current_scope || Criteria.new(self)
|
119
119
|
end
|
120
120
|
|
121
121
|
# Create a scope that can be accessed from the class level or chained to
|
@@ -150,18 +150,6 @@ module Mongoid
|
|
150
150
|
define_scope_method(normalized)
|
151
151
|
end
|
152
152
|
|
153
|
-
# Initializes and returns the current scope stack.
|
154
|
-
#
|
155
|
-
# @example Get the scope stack.
|
156
|
-
# Person.scope_stack
|
157
|
-
#
|
158
|
-
# @return [ Array<Criteria> ] The scope stack.
|
159
|
-
#
|
160
|
-
# @since 1.0.0
|
161
|
-
def scope_stack
|
162
|
-
Threaded.scope_stack[object_id] ||= []
|
163
|
-
end
|
164
|
-
|
165
153
|
# Get a criteria for the document with normal scoping.
|
166
154
|
#
|
167
155
|
# @example Get the criteria.
|
@@ -234,11 +222,11 @@ module Mongoid
|
|
234
222
|
#
|
235
223
|
# @since 1.0.0
|
236
224
|
def with_scope(criteria)
|
237
|
-
|
225
|
+
Threaded.current_scope = criteria
|
238
226
|
begin
|
239
227
|
yield criteria
|
240
228
|
ensure
|
241
|
-
|
229
|
+
Threaded.current_scope = nil
|
242
230
|
end
|
243
231
|
end
|
244
232
|
|
@@ -325,7 +313,8 @@ module Mongoid
|
|
325
313
|
singleton_class.class_eval do
|
326
314
|
define_method name do |*args|
|
327
315
|
scoping = _declared_scopes[name]
|
328
|
-
scope
|
316
|
+
scope = instance_exec(*args, &scoping[:scope])
|
317
|
+
extension = scoping[:extension]
|
329
318
|
criteria = with_default_scope.merge(scope || queryable)
|
330
319
|
criteria.extend(extension)
|
331
320
|
criteria
|
data/lib/mongoid/selectable.rb
CHANGED
@@ -18,42 +18,7 @@ module Mongoid
|
|
18
18
|
#
|
19
19
|
# @since 1.0.0
|
20
20
|
def atomic_selector
|
21
|
-
@atomic_selector ||=
|
22
|
-
(embedded? ? embedded_atomic_selector : root_atomic_selector)
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
# Get the atomic selector for an embedded document.
|
28
|
-
#
|
29
|
-
# @api private
|
30
|
-
#
|
31
|
-
# @example Get the embedded atomic selector.
|
32
|
-
# document.embedded_atomic_selector
|
33
|
-
#
|
34
|
-
# @return [ Hash ] The embedded document selector.
|
35
|
-
#
|
36
|
-
# @since 4.0.0
|
37
|
-
def embedded_atomic_selector
|
38
|
-
if persisted? && _id_changed?
|
39
|
-
_parent.atomic_selector
|
40
|
-
else
|
41
|
-
_parent.atomic_selector.merge("#{atomic_path}._id" => _id)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Get the atomic selector for a root document.
|
46
|
-
#
|
47
|
-
# @api private
|
48
|
-
#
|
49
|
-
# @example Get the root atomic selector.
|
50
|
-
# document.root_atomic_selector
|
51
|
-
#
|
52
|
-
# @return [ Hash ] The root document selector.
|
53
|
-
#
|
54
|
-
# @since 4.0.0
|
55
|
-
def root_atomic_selector
|
56
|
-
{ "_id" => _id }.merge!(shard_key_selector)
|
21
|
+
@atomic_selector ||= { "_id" => _root._id }.merge!(shard_key_selector)
|
57
22
|
end
|
58
23
|
end
|
59
24
|
end
|
data/lib/mongoid/serializable.rb
CHANGED
@@ -97,10 +97,10 @@ module Mongoid
|
|
97
97
|
#
|
98
98
|
# @since 3.0.0
|
99
99
|
def serialize_attribute(attrs, name, names, options)
|
100
|
-
if relations.
|
100
|
+
if relations.key?(name)
|
101
101
|
value = send(name)
|
102
102
|
attrs[name] = value ? value.serializable_hash(options) : nil
|
103
|
-
elsif names.include?(name) && !fields.
|
103
|
+
elsif names.include?(name) && !fields.key?(name)
|
104
104
|
attrs[name] = read_attribute(name)
|
105
105
|
elsif !attribute_missing?(name)
|
106
106
|
attrs[name] = send(name)
|
data/lib/mongoid/stateful.rb
CHANGED
@@ -18,9 +18,9 @@ namespace :db do
|
|
18
18
|
::Mongoid::Tasks::Database.remove_indexes
|
19
19
|
end
|
20
20
|
|
21
|
-
desc "Drops the default
|
21
|
+
desc "Drops the default client database"
|
22
22
|
task :drop => :environment do
|
23
|
-
::Mongoid::
|
23
|
+
::Mongoid::Clients.default.database.drop
|
24
24
|
end
|
25
25
|
|
26
26
|
desc "Drop all collections except the system collections"
|
@@ -42,18 +42,20 @@ module Mongoid
|
|
42
42
|
|
43
43
|
models.each do |model|
|
44
44
|
unless model.embedded?
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
begin
|
46
|
+
model.collection.indexes.each do |index|
|
47
|
+
# ignore default index
|
48
|
+
unless index['name'] == '_id_'
|
49
|
+
key = index['key'].symbolize_keys
|
50
|
+
spec = model.index_specification(key)
|
51
|
+
unless spec
|
52
|
+
# index not specified
|
53
|
+
undefined_by_model[model] ||= []
|
54
|
+
undefined_by_model[model] << index
|
55
|
+
end
|
54
56
|
end
|
55
57
|
end
|
56
|
-
end
|
58
|
+
rescue Mongo::Error::OperationFailure; end
|
57
59
|
end
|
58
60
|
end
|
59
61
|
|
@@ -73,8 +75,12 @@ module Mongoid
|
|
73
75
|
undefined_indexes(models).each do |model, indexes|
|
74
76
|
indexes.each do |index|
|
75
77
|
key = index['key'].symbolize_keys
|
76
|
-
model.collection
|
77
|
-
|
78
|
+
collection = model.collection
|
79
|
+
collection.indexes.drop_one(key)
|
80
|
+
logger.info(
|
81
|
+
"MONGOID: Removed index '#{index['name']}' on collection " +
|
82
|
+
"'#{collection.name}' in database '#{collection.database.name}'."
|
83
|
+
)
|
78
84
|
end
|
79
85
|
end
|
80
86
|
end
|
@@ -90,10 +96,11 @@ module Mongoid
|
|
90
96
|
def remove_indexes(models = ::Mongoid.models)
|
91
97
|
models.each do |model|
|
92
98
|
next if model.embedded?
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
99
|
+
begin
|
100
|
+
model.remove_indexes
|
101
|
+
rescue Mongo::Error::OperationFailure
|
102
|
+
next
|
103
|
+
end
|
97
104
|
model
|
98
105
|
end.compact
|
99
106
|
end
|