mongoid 3.0.0.rc → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +109 -4
- data/README.md +1 -1
- data/Rakefile +1 -0
- data/lib/config/locales/en.yml +15 -1
- data/lib/mongoid.rb +17 -2
- data/lib/mongoid/atomic.rb +54 -7
- data/lib/mongoid/attributes.rb +1 -1
- data/lib/mongoid/attributes/processing.rb +1 -1
- data/lib/mongoid/callbacks.rb +6 -1
- data/lib/mongoid/components.rb +2 -1
- data/lib/mongoid/config.rb +42 -17
- data/lib/mongoid/config/environment.rb +3 -1
- data/lib/mongoid/contextual/aggregable/memory.rb +21 -10
- data/lib/mongoid/contextual/find_and_modify.rb +14 -12
- data/lib/mongoid/contextual/memory.rb +24 -1
- data/lib/mongoid/contextual/mongo.rb +148 -29
- data/lib/mongoid/copyable.rb +6 -24
- data/lib/mongoid/criteria.rb +116 -34
- data/lib/mongoid/document.rb +7 -7
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/errors/no_metadata.rb +21 -0
- data/lib/mongoid/evolvable.rb +19 -0
- data/lib/mongoid/extensions.rb +1 -1
- data/lib/mongoid/extensions/array.rb +38 -1
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/date_time.rb +6 -1
- data/lib/mongoid/extensions/false_class.rb +12 -0
- data/lib/mongoid/extensions/float.rb +12 -0
- data/lib/mongoid/extensions/hash.rb +33 -1
- data/lib/mongoid/extensions/integer.rb +12 -0
- data/lib/mongoid/extensions/object.rb +51 -1
- data/lib/mongoid/extensions/object_id.rb +2 -1
- data/lib/mongoid/extensions/range.rb +24 -0
- data/lib/mongoid/extensions/string.rb +31 -5
- data/lib/mongoid/extensions/true_class.rb +12 -0
- data/lib/mongoid/fields.rb +20 -21
- data/lib/mongoid/fields/foreign_key.rb +23 -7
- data/lib/mongoid/fields/standard.rb +3 -3
- data/lib/mongoid/finders.rb +3 -7
- data/lib/mongoid/hierarchy.rb +19 -1
- data/lib/mongoid/identity_map.rb +20 -4
- data/lib/mongoid/indexes/validators/options.rb +1 -1
- data/lib/mongoid/multi_parameter_attributes.rb +1 -1
- data/lib/mongoid/paranoia.rb +3 -32
- data/lib/mongoid/persistence.rb +33 -15
- data/lib/mongoid/persistence/atomic/operation.rb +1 -1
- data/lib/mongoid/persistence/operations.rb +16 -0
- data/lib/mongoid/persistence/operations/remove.rb +1 -1
- data/lib/mongoid/persistence/operations/upsert.rb +28 -0
- data/lib/mongoid/persistence/upsertion.rb +30 -0
- data/lib/mongoid/relations.rb +16 -0
- data/lib/mongoid/relations/accessors.rb +1 -1
- data/lib/mongoid/relations/bindings/referenced/in.rb +1 -1
- data/lib/mongoid/relations/builder.rb +1 -1
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
- data/lib/mongoid/relations/builders/referenced/many.rb +1 -1
- data/lib/mongoid/relations/cascading.rb +4 -3
- data/lib/mongoid/relations/constraint.rb +1 -1
- data/lib/mongoid/relations/conversions.rb +1 -1
- data/lib/mongoid/relations/embedded/batchable.rb +3 -2
- data/lib/mongoid/relations/embedded/many.rb +4 -4
- data/lib/mongoid/relations/embedded/one.rb +1 -1
- data/lib/mongoid/relations/metadata.rb +67 -23
- data/lib/mongoid/relations/nested_builder.rb +2 -2
- data/lib/mongoid/relations/proxy.rb +9 -7
- data/lib/mongoid/relations/referenced/many.rb +69 -25
- data/lib/mongoid/relations/referenced/many_to_many.rb +14 -13
- data/lib/mongoid/scoping.rb +0 -17
- data/lib/mongoid/serialization.rb +95 -26
- data/lib/mongoid/sessions.rb +30 -6
- data/lib/mongoid/sessions/factory.rb +2 -0
- data/lib/mongoid/threaded.rb +52 -0
- data/lib/mongoid/timestamps/created.rb +1 -1
- data/lib/mongoid/timestamps/updated.rb +2 -1
- data/lib/mongoid/validations/uniqueness.rb +3 -2
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +8 -0
- data/lib/rails/mongoid.rb +8 -5
- metadata +30 -13
- data/lib/mongoid/collections/retry.rb +0 -58
- data/lib/mongoid/javascript.rb +0 -20
- data/lib/mongoid/javascript/functions.yml +0 -63
data/CHANGELOG.md
CHANGED
@@ -3,10 +3,41 @@
|
|
3
3
|
For instructions on upgrading to newer versions, visit
|
4
4
|
[mongoid.org](http://mongoid.org/docs/upgrading.html).
|
5
5
|
|
6
|
-
## 3.0.0
|
6
|
+
## 3.0.0
|
7
7
|
|
8
8
|
### New Features
|
9
9
|
|
10
|
+
* \#2151 When asking for metadata before persistence, Mongoid will now
|
11
|
+
raise a `Mongoid::Errors::NoMetadata` error if the metadata is not
|
12
|
+
present.
|
13
|
+
|
14
|
+
* \#2147 `Model#becomes` now copies over the embedded documents.
|
15
|
+
|
16
|
+
* A new callback has been introduced: `upsert`, which runs when calling
|
17
|
+
`document.upsert` since Mongoid does not know if the document is to be
|
18
|
+
treated as new or persisted. With this come the model callbacks:
|
19
|
+
|
20
|
+
before_upsert
|
21
|
+
after_upsert
|
22
|
+
around_upsert
|
23
|
+
|
24
|
+
* \#2080/\#2087 The database or session that Mongoid persists to can now be
|
25
|
+
overridden on a global level for cases where `Model#with` is not a viable
|
26
|
+
option.
|
27
|
+
|
28
|
+
Mongoid.override_database(:secondary)
|
29
|
+
Mongoid.override_session(:secondary)
|
30
|
+
|
31
|
+
Band.create(name: "Placebo") #=> Persists to secondary.
|
32
|
+
band.albums.create #=> Persists to secondary.
|
33
|
+
|
34
|
+
Note that this option is global and overrides for all models on the current
|
35
|
+
thread. It is the developer's responsibility to remember to set this back
|
36
|
+
to nil if you no longer want the override to happen.
|
37
|
+
|
38
|
+
Mongoid.override_database(nil)
|
39
|
+
Mongoid.override_session(nil)
|
40
|
+
|
10
41
|
* \#1989 Criteria `count`, `size` and `length` now behave as Active Record
|
11
42
|
with regards to database access.
|
12
43
|
|
@@ -529,6 +560,13 @@ For instructions on upgrading to newer versions, visit
|
|
529
560
|
|
530
561
|
### Major Changes (Backwards Incompatible)
|
531
562
|
|
563
|
+
* Polymorphic relations can not have ids other than object ids. This is
|
564
|
+
because Mongoid cannot properly figure out in an optimized way what the
|
565
|
+
various classes on the other side of the relation store their ids as, as
|
566
|
+
they could potentially all be different.
|
567
|
+
|
568
|
+
This was not allowed before, but nowhere was it explicitly stated.
|
569
|
+
|
532
570
|
* \#2039 Validating presence of a relation now checks both the relation and
|
533
571
|
the foreign key value.
|
534
572
|
|
@@ -645,8 +683,8 @@ For instructions on upgrading to newer versions, visit
|
|
645
683
|
* Custom application exceptions in various languages has been removed,
|
646
684
|
along with the `Mongoid.add_language` feature.
|
647
685
|
|
648
|
-
* Mongoid no longer supports 1.8
|
649
|
-
1.9 mode
|
686
|
+
* Mongoid no longer supports 1.8. MRI 1.9.3 and higher, or JRuby 1.6 and
|
687
|
+
higher in 1.9 mode are only supported.
|
650
688
|
|
651
689
|
* \#1734 When searching for documents via `Model.find` with multiple ids,
|
652
690
|
Mongoid will raise an error if not *all* ids are found, and tell you
|
@@ -769,8 +807,58 @@ For instructions on upgrading to newer versions, visit
|
|
769
807
|
|
770
808
|
Band.scoped(skip: 10, limit: 20)
|
771
809
|
|
810
|
+
* \#463 `Document#upsert` is no longer aliased to `Document#save` and now
|
811
|
+
actually performs a proper MongoDB upsert command when called. If you
|
812
|
+
were using this method before and want the same behaviour, please switch
|
813
|
+
to `save`.
|
814
|
+
|
815
|
+
band = Band.new(name: "Tool")
|
816
|
+
band.upsert #=> Inserts the document in the database.
|
817
|
+
band.name = "Placebo"
|
818
|
+
band.upsert #=> Updates the existing document.
|
819
|
+
|
772
820
|
### Resolved Issues
|
773
821
|
|
822
|
+
* \#2166 `Criteria#from_map_or_db` strips type selection when eager loading
|
823
|
+
since it will check if the type is correct after.
|
824
|
+
|
825
|
+
* \#2129 Fixed sorting for all fields on embeds many relations.
|
826
|
+
|
827
|
+
* \#2124 Fixed default scope and deleted scope on paranoid documents.
|
828
|
+
|
829
|
+
* \#2122 Allow embedded documents to sort on boolean fields.
|
830
|
+
|
831
|
+
* \#2119 Allow `Criteria#update_all` to accept atomic ops and normal sets.
|
832
|
+
|
833
|
+
* \#2118 Don't strip any precision during `DateTime` -> `Time` conversions.
|
834
|
+
|
835
|
+
* \#2117 Ensure embeds one relations have callbacks run when using nested
|
836
|
+
attributes.
|
837
|
+
|
838
|
+
* \#2110 `Model#touch` now works properly on embedded documents.
|
839
|
+
|
840
|
+
* \#2100 Allow atomic operations to properly execute on paranoid documents
|
841
|
+
that have a deleted_at set.
|
842
|
+
|
843
|
+
* \#2089 Allow proper separation of mongoization and evolving with respect to
|
844
|
+
foreign keys.
|
845
|
+
|
846
|
+
* \#2088 Allow finds by string ids to pull from the identity map if the ids
|
847
|
+
are stored as object ids.
|
848
|
+
|
849
|
+
* \#2085 Allow demongoization of floats and ints to big decimals.
|
850
|
+
|
851
|
+
* \#2084 Don't cascade if metadata does not exist.
|
852
|
+
|
853
|
+
* \#2078 Calling `Model#clone` or `Model#dup` now properly sets attributes
|
854
|
+
as dirty.
|
855
|
+
|
856
|
+
* \#2070 Allow for updated_at to be overridden manually for new documents that
|
857
|
+
also have a created_at.
|
858
|
+
|
859
|
+
* \#2041 Don't hit the database multiple times on relation access after an
|
860
|
+
eager load returned zero documents.
|
861
|
+
|
774
862
|
* \#1997 Cascading callbacks should be able to halt the callback chain when
|
775
863
|
terminating.
|
776
864
|
|
@@ -824,10 +912,25 @@ For instructions on upgrading to newer versions, visit
|
|
824
912
|
* \#685 Attempting to use versioning with embedded documents will now
|
825
913
|
raise a proper error alerting the developer this is not allowed.
|
826
914
|
|
827
|
-
## 2.4.
|
915
|
+
## 2.4.12 (branch: 2.4.0-stable)
|
828
916
|
|
829
917
|
### Resolved Issues
|
830
918
|
|
919
|
+
* \#2144 Uniqueness validation on paranoid documents now properly scopes.
|
920
|
+
|
921
|
+
* \#2127 Don't unbind parents of embedded documents mid nested
|
922
|
+
attributes deletion.
|
923
|
+
|
924
|
+
## 2.4.11
|
925
|
+
|
926
|
+
### Resolved Issues
|
927
|
+
|
928
|
+
* This release forces a cap on the mongo driver version at 1.6.2 due to
|
929
|
+
changes in the `Mongo::Connection.from_uri` API not allowing valid
|
930
|
+
connection options anymore.
|
931
|
+
|
932
|
+
* \#2040 Fixed bad interpolation for locale presence validation.
|
933
|
+
|
831
934
|
* \#2038 Allow inverse relations to be determined by foreign keys alone
|
832
935
|
if defined on both sides, not just an inverse_of declaration.
|
833
936
|
|
@@ -837,6 +940,8 @@ For instructions on upgrading to newer versions, visit
|
|
837
940
|
* \#2008 Presence validation should hit the db to check validity if the
|
838
941
|
relation in memory is blank.
|
839
942
|
|
943
|
+
* \#2006 Allow excluding only the _id field post execution of an #only call.
|
944
|
+
|
840
945
|
## 2.4.10
|
841
946
|
|
842
947
|
### Resolved Issues
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -20,6 +20,7 @@ task :release => :build do
|
|
20
20
|
system "git tag -a v#{Mongoid::VERSION} -m 'Tagging #{Mongoid::VERSION}'"
|
21
21
|
system "git push --tags"
|
22
22
|
system "gem push mongoid-#{Mongoid::VERSION}.gem"
|
23
|
+
system "rm mongoid-#{Mongoid::VERSION}.gem"
|
23
24
|
end
|
24
25
|
|
25
26
|
RSpec::Core::RakeTask.new("spec") do |spec|
|
data/lib/config/locales/en.yml
CHANGED
@@ -67,7 +67,7 @@ en:
|
|
67
67
|
document_with_attributes_not_found:
|
68
68
|
message: "Document not found for class %{klass} with attributes %{attributes}."
|
69
69
|
summary: "When calling %{klass}.find_by with a hash of attributes, all
|
70
|
-
attributes provided must match a document in the database
|
70
|
+
attributes provided must match a document in the database or this error
|
71
71
|
will be raised."
|
72
72
|
resolution: "Search for attributes that are in the database or set
|
73
73
|
the Mongoid.raise_not_found_error configuration option to false,
|
@@ -303,6 +303,20 @@ en:
|
|
303
303
|
\_\_merge: 'collection-name'\n
|
304
304
|
\_\_replace: 'collection-name'\n
|
305
305
|
\_\_reduce: 'collection-name'\n\n"
|
306
|
+
no_metadata:
|
307
|
+
message: "Metadata not found for document of type %{klass}."
|
308
|
+
summary: "Mongoid sets the metadata of a relation on the document
|
309
|
+
when it is either loaded from within the relation, or added to
|
310
|
+
one. The presence of the metadata is required in order to
|
311
|
+
provide various functionality around relations. Most likely you
|
312
|
+
are getting this error because the document is embedded and was
|
313
|
+
attempted to be persisted without being associated with a parent,
|
314
|
+
or the relation was not properly defined."
|
315
|
+
resolution: "Ensure that your relations on the %{klass} model
|
316
|
+
are all properly defined, and that the inverse relations
|
317
|
+
are also properly defined. Embedded relations must have both the
|
318
|
+
parent (embeds_one/embeds_many) and the inverse (embedded_in)
|
319
|
+
present in order to work properly."
|
306
320
|
no_parent:
|
307
321
|
message: "Cannot persist embedded document %{klass} without a
|
308
322
|
parent document."
|
data/lib/mongoid.rb
CHANGED
@@ -32,7 +32,6 @@ require "active_model"
|
|
32
32
|
|
33
33
|
require "origin"
|
34
34
|
require "moped"
|
35
|
-
BSON = Moped::BSON
|
36
35
|
|
37
36
|
require "mongoid/extensions"
|
38
37
|
require "mongoid/errors"
|
@@ -46,6 +45,7 @@ require "mongoid/contextual"
|
|
46
45
|
require "mongoid/copyable"
|
47
46
|
require "mongoid/criteria"
|
48
47
|
require "mongoid/dirty"
|
48
|
+
require "mongoid/evolvable"
|
49
49
|
require "mongoid/factory"
|
50
50
|
require "mongoid/fields"
|
51
51
|
require "mongoid/finders"
|
@@ -53,7 +53,6 @@ require "mongoid/hierarchy"
|
|
53
53
|
require "mongoid/identity_map"
|
54
54
|
require "mongoid/indexes"
|
55
55
|
require "mongoid/inspection"
|
56
|
-
require "mongoid/javascript"
|
57
56
|
require "mongoid/json"
|
58
57
|
require "mongoid/loggable"
|
59
58
|
require "mongoid/matchers"
|
@@ -113,10 +112,26 @@ module Mongoid
|
|
113
112
|
block_given? ? yield(Config) : Config
|
114
113
|
end
|
115
114
|
|
115
|
+
# Convenience method for getting the default session.
|
116
|
+
#
|
117
|
+
# @example Get the default session.
|
118
|
+
# Mongoid.default_session
|
119
|
+
#
|
120
|
+
# @return [ Moped::Session ] The default session.
|
121
|
+
#
|
122
|
+
# @since 3.0.0
|
116
123
|
def default_session
|
117
124
|
Sessions.default
|
118
125
|
end
|
119
126
|
|
127
|
+
# Convenience method for getting a named session.
|
128
|
+
#
|
129
|
+
# @example Get a named session.
|
130
|
+
# Mongoid.session(:default)
|
131
|
+
#
|
132
|
+
# @return [ Moped::Session ] The named session.
|
133
|
+
#
|
134
|
+
# @since 3.0.0
|
120
135
|
def session(name)
|
121
136
|
Sessions.with_name(name)
|
122
137
|
end
|
data/lib/mongoid/atomic.rb
CHANGED
@@ -14,9 +14,9 @@ module Mongoid
|
|
14
14
|
:atomic_array_pulls,
|
15
15
|
:atomic_array_add_to_sets,
|
16
16
|
:atomic_pulls,
|
17
|
-
:atomic_unsets,
|
18
17
|
:delayed_atomic_sets,
|
19
|
-
:delayed_atomic_pulls
|
18
|
+
:delayed_atomic_pulls,
|
19
|
+
:delayed_atomic_unsets
|
20
20
|
]
|
21
21
|
|
22
22
|
included do
|
@@ -36,7 +36,22 @@ module Mongoid
|
|
36
36
|
# @since 2.2.0
|
37
37
|
def add_atomic_pull(document)
|
38
38
|
document.flagged_for_destroy = true
|
39
|
-
(delayed_atomic_pulls[document.
|
39
|
+
(delayed_atomic_pulls[document.metadata_name.to_s] ||= []).push(document)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Add an atomic unset for the document.
|
43
|
+
#
|
44
|
+
# @example Add an atomic unset.
|
45
|
+
# document.add_atomic_unset(doc)
|
46
|
+
#
|
47
|
+
# @param [ Document ] document The child document.
|
48
|
+
#
|
49
|
+
# @return [ Array<Document> ] The children.
|
50
|
+
#
|
51
|
+
# @since 3.0.0
|
52
|
+
def add_atomic_unset(document)
|
53
|
+
document.flagged_for_destroy = true
|
54
|
+
(delayed_atomic_unsets[document.metadata_name.to_s] ||= []).push(document)
|
40
55
|
end
|
41
56
|
|
42
57
|
# For array fields these are the pushes that need to happen.
|
@@ -174,9 +189,7 @@ module Mongoid
|
|
174
189
|
delayed_atomic_pulls.each_pair do |_, docs|
|
175
190
|
path = nil
|
176
191
|
ids = docs.map do |doc|
|
177
|
-
path ||= doc.
|
178
|
-
doc.destroyed = true
|
179
|
-
doc.flagged_for_destroy = false
|
192
|
+
path ||= doc.flag_as_destroyed
|
180
193
|
doc.id
|
181
194
|
end
|
182
195
|
pulls[path] = { "_id" => { "$in" => ids }} and path = nil
|
@@ -228,7 +241,15 @@ module Mongoid
|
|
228
241
|
#
|
229
242
|
# @since 2.2.0
|
230
243
|
def atomic_unsets
|
231
|
-
|
244
|
+
unsets = []
|
245
|
+
delayed_atomic_unsets.each_pair do |name, docs|
|
246
|
+
path = nil
|
247
|
+
docs.each do |doc|
|
248
|
+
path ||= doc.flag_as_destroyed
|
249
|
+
end
|
250
|
+
unsets.push(path || name)
|
251
|
+
end
|
252
|
+
unsets
|
232
253
|
end
|
233
254
|
|
234
255
|
# Get all the atomic sets that have had their saves delayed.
|
@@ -255,6 +276,32 @@ module Mongoid
|
|
255
276
|
@delayed_atomic_pulls ||= {}
|
256
277
|
end
|
257
278
|
|
279
|
+
# Get the delayed atomic unsets.
|
280
|
+
#
|
281
|
+
# @example Get the delayed atomic unsets.
|
282
|
+
# document.delayed_atomic_unsets
|
283
|
+
#
|
284
|
+
# @return [ Hash ] The atomic unsets
|
285
|
+
#
|
286
|
+
# @since 3.0.0
|
287
|
+
def delayed_atomic_unsets
|
288
|
+
@delayed_atomic_unsets ||= {}
|
289
|
+
end
|
290
|
+
|
291
|
+
# Flag the document as destroyed and return the atomic path.
|
292
|
+
#
|
293
|
+
# @example Flag destroyed and return path.
|
294
|
+
# document.flag_as_destroyed
|
295
|
+
#
|
296
|
+
# @return [ String ] The atomic path.
|
297
|
+
#
|
298
|
+
# @since 3.0.0
|
299
|
+
def flag_as_destroyed
|
300
|
+
self.destroyed = true
|
301
|
+
self.flagged_for_destroy = false
|
302
|
+
atomic_path
|
303
|
+
end
|
304
|
+
|
258
305
|
private
|
259
306
|
|
260
307
|
# Get the atomic paths utility for this document.
|
data/lib/mongoid/attributes.rb
CHANGED
@@ -20,7 +20,7 @@ module Mongoid
|
|
20
20
|
# @since 2.0.0.rc.7
|
21
21
|
def process_attributes(attrs = nil, role = :default, guard_protected_attributes = true)
|
22
22
|
attrs ||= {}
|
23
|
-
if attrs.
|
23
|
+
if !attrs.empty?
|
24
24
|
attrs = sanitize_for_mass_assignment(attrs, role) if guard_protected_attributes
|
25
25
|
attrs.each_pair do |key, value|
|
26
26
|
next if pending_attribute?(key, value)
|
data/lib/mongoid/callbacks.rb
CHANGED
@@ -12,15 +12,18 @@ module Mongoid
|
|
12
12
|
:after_initialize,
|
13
13
|
:after_save,
|
14
14
|
:after_update,
|
15
|
+
:after_upsert,
|
15
16
|
:after_validation,
|
16
17
|
:around_create,
|
17
18
|
:around_destroy,
|
18
19
|
:around_save,
|
19
20
|
:around_update,
|
21
|
+
:around_upsert,
|
20
22
|
:before_create,
|
21
23
|
:before_destroy,
|
22
24
|
:before_save,
|
23
25
|
:before_update,
|
26
|
+
:before_upsert,
|
24
27
|
:before_validation
|
25
28
|
]
|
26
29
|
|
@@ -30,7 +33,7 @@ module Mongoid
|
|
30
33
|
|
31
34
|
define_model_callbacks :initialize, only: :after
|
32
35
|
define_model_callbacks :build, only: :after
|
33
|
-
define_model_callbacks :create, :destroy, :save, :update
|
36
|
+
define_model_callbacks :create, :destroy, :save, :update, :upsert
|
34
37
|
end
|
35
38
|
|
36
39
|
# Run only the after callbacks for the specific event.
|
@@ -112,7 +115,9 @@ module Mongoid
|
|
112
115
|
next unless metadata.cascading_callbacks?
|
113
116
|
without_autobuild do
|
114
117
|
delayed_pulls = delayed_atomic_pulls[name]
|
118
|
+
delayed_unsets = delayed_atomic_unsets[name]
|
115
119
|
children.merge(delayed_pulls) if delayed_pulls
|
120
|
+
children.merge(delayed_unsets) if delayed_unsets
|
116
121
|
relation = send(name)
|
117
122
|
Array.wrap(relation).each do |child|
|
118
123
|
next if children.include?(child)
|
data/lib/mongoid/components.rb
CHANGED
@@ -21,7 +21,7 @@ module Mongoid
|
|
21
21
|
include Mongoid::Atomic
|
22
22
|
include Mongoid::Dirty
|
23
23
|
include Mongoid::Attributes
|
24
|
-
include Mongoid::
|
24
|
+
include Mongoid::Evolvable
|
25
25
|
include Mongoid::Fields
|
26
26
|
include Mongoid::Hierarchy
|
27
27
|
include Mongoid::Indexes
|
@@ -41,6 +41,7 @@ module Mongoid
|
|
41
41
|
include Mongoid::Timestamps::Timeless
|
42
42
|
include Mongoid::Validations
|
43
43
|
include Mongoid::Callbacks
|
44
|
+
include Mongoid::Copyable
|
44
45
|
|
45
46
|
MODULES = [
|
46
47
|
Mongoid::Atomic,
|
data/lib/mongoid/config.rb
CHANGED
@@ -25,6 +25,25 @@ module Mongoid
|
|
25
25
|
option :use_activesupport_time_zone, default: true
|
26
26
|
option :use_utc, default: false
|
27
27
|
|
28
|
+
# Connect to the provided database name on the default session.
|
29
|
+
#
|
30
|
+
# @note Use only in development or test environments for convenience.
|
31
|
+
#
|
32
|
+
# @example Set the database to connect to.
|
33
|
+
# config.connect_to("mongoid_test")
|
34
|
+
#
|
35
|
+
# @param [ String ] name The database name.
|
36
|
+
#
|
37
|
+
# @since 3.0.0
|
38
|
+
def connect_to(name)
|
39
|
+
self.sessions = {
|
40
|
+
default: {
|
41
|
+
database: name,
|
42
|
+
hosts: [ "localhost:27017" ]
|
43
|
+
}
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
28
47
|
# Return field names that could cause destructive things to happen if
|
29
48
|
# defined in a Mongoid::Document.
|
30
49
|
#
|
@@ -52,23 +71,32 @@ module Mongoid
|
|
52
71
|
settings
|
53
72
|
end
|
54
73
|
|
55
|
-
#
|
74
|
+
# Override the database to use globally.
|
56
75
|
#
|
57
|
-
# @
|
76
|
+
# @example Override the database globally.
|
77
|
+
# config.override_database(:optional)
|
58
78
|
#
|
59
|
-
# @
|
60
|
-
# config.connect_to("mongoid_test")
|
79
|
+
# @param [ String, Symbol ] name The name of the database.
|
61
80
|
#
|
62
|
-
# @
|
81
|
+
# @return [ String, Symbol ] The global override.
|
63
82
|
#
|
64
83
|
# @since 3.0.0
|
65
|
-
def
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
84
|
+
def override_database(name)
|
85
|
+
Threaded.database_override = name
|
86
|
+
end
|
87
|
+
|
88
|
+
# Override the session to use globally.
|
89
|
+
#
|
90
|
+
# @example Override the session globally.
|
91
|
+
# config.override_session(:optional)
|
92
|
+
#
|
93
|
+
# @param [ String, Symbol ] name The name of the session.
|
94
|
+
#
|
95
|
+
# @return [ String, Symbol ] The global override.
|
96
|
+
#
|
97
|
+
# @since 3.0.0
|
98
|
+
def override_session(name)
|
99
|
+
Threaded.session_override = name ? name.to_s : nil
|
72
100
|
end
|
73
101
|
|
74
102
|
# Purge all data in all collections, including indexes.
|
@@ -80,11 +108,8 @@ module Mongoid
|
|
80
108
|
#
|
81
109
|
# @since 2.0.2
|
82
110
|
def purge!
|
83
|
-
|
84
|
-
|
85
|
-
collections.each do |collection|
|
86
|
-
_, name = collection["name"].split(".", 2)
|
87
|
-
session[name].drop
|
111
|
+
Sessions.default.collections.each do |collection|
|
112
|
+
collection.drop
|
88
113
|
end and true
|
89
114
|
end
|
90
115
|
|