mongoid 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -1
- data/Rakefile +3 -2
- data/lib/config/locales/bg.yml +6 -0
- data/lib/config/locales/de.yml +6 -0
- data/lib/config/locales/en-GB.yml +48 -0
- data/lib/config/locales/en.yml +6 -3
- data/lib/config/locales/es.yml +6 -0
- data/lib/config/locales/fr.yml +6 -0
- data/lib/config/locales/hi.yml +39 -0
- data/lib/config/locales/hu.yml +13 -7
- data/lib/config/locales/id.yml +3 -0
- data/lib/config/locales/it.yml +7 -1
- data/lib/config/locales/ja.yml +4 -1
- data/lib/config/locales/kr.yml +9 -34
- data/lib/config/locales/nl.yml +6 -0
- data/lib/config/locales/pl.yml +6 -0
- data/lib/config/locales/pt-BR.yml +6 -0
- data/lib/config/locales/pt.yml +6 -0
- data/lib/config/locales/ro.yml +6 -0
- data/lib/config/locales/ru.yml +6 -0
- data/lib/config/locales/sv.yml +6 -0
- data/lib/config/locales/vi.yml +3 -0
- data/lib/config/locales/zh-CN.yml +6 -0
- data/lib/mongoid.rb +51 -45
- data/lib/mongoid/atomic.rb +145 -0
- data/lib/mongoid/atomic/modifiers.rb +109 -0
- data/lib/mongoid/atomic/paths.rb +3 -0
- data/lib/mongoid/atomic/paths/embedded.rb +43 -0
- data/lib/mongoid/atomic/paths/embedded/many.rb +44 -0
- data/lib/mongoid/atomic/paths/embedded/one.rb +43 -0
- data/lib/mongoid/atomic/paths/root.rb +40 -0
- data/lib/mongoid/attributes.rb +12 -23
- data/lib/mongoid/attributes/processing.rb +5 -5
- data/lib/mongoid/callbacks.rb +2 -0
- data/lib/mongoid/collection.rb +12 -59
- data/lib/mongoid/collections.rb +23 -20
- data/lib/mongoid/collections/master.rb +6 -4
- data/lib/mongoid/collections/operations.rb +1 -0
- data/lib/mongoid/collections/retry.rb +7 -0
- data/lib/mongoid/components.rb +2 -2
- data/lib/mongoid/config.rb +42 -55
- data/lib/mongoid/config/database.rb +6 -2
- data/lib/mongoid/config/replset_database.rb +7 -3
- data/lib/mongoid/contexts.rb +9 -3
- data/lib/mongoid/contexts/enumerable.rb +7 -3
- data/lib/mongoid/contexts/mongo.rb +139 -101
- data/lib/mongoid/criteria.rb +86 -69
- data/lib/mongoid/criterion/complex.rb +32 -5
- data/lib/mongoid/criterion/inclusion.rb +4 -2
- data/lib/mongoid/criterion/optional.rb +111 -86
- data/lib/mongoid/criterion/selector.rb +8 -4
- data/lib/mongoid/cursor.rb +27 -27
- data/lib/mongoid/dirty.rb +54 -214
- data/lib/mongoid/document.rb +37 -39
- data/lib/mongoid/errors/document_not_found.rb +3 -4
- data/lib/mongoid/errors/invalid_collection.rb +2 -3
- data/lib/mongoid/errors/invalid_database.rb +2 -3
- data/lib/mongoid/errors/invalid_field.rb +2 -3
- data/lib/mongoid/errors/invalid_options.rb +19 -7
- data/lib/mongoid/errors/invalid_type.rb +2 -3
- data/lib/mongoid/errors/mongoid_error.rb +5 -6
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +2 -3
- data/lib/mongoid/errors/unsupported_version.rb +2 -3
- data/lib/mongoid/errors/validations.rb +2 -3
- data/lib/mongoid/extensions.rb +8 -62
- data/lib/mongoid/extensions/array/deletion.rb +29 -0
- data/lib/mongoid/extensions/false_class/equality.rb +14 -1
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +21 -10
- data/lib/mongoid/extensions/hash/scoping.rb +14 -1
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -1
- data/lib/mongoid/extensions/object/reflections.rb +33 -2
- data/lib/mongoid/extensions/object_id/conversions.rb +2 -36
- data/lib/mongoid/extensions/proc/scoping.rb +14 -1
- data/lib/mongoid/extensions/string/conversions.rb +4 -16
- data/lib/mongoid/extensions/string/inflections.rb +35 -14
- data/lib/mongoid/extensions/symbol/inflections.rb +38 -12
- data/lib/mongoid/extensions/true_class/equality.rb +14 -1
- data/lib/mongoid/extras.rb +11 -30
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields.rb +121 -29
- data/lib/mongoid/fields/mappings.rb +36 -0
- data/lib/mongoid/fields/serializable.rb +131 -0
- data/lib/mongoid/fields/serializable/array.rb +64 -0
- data/lib/mongoid/fields/serializable/big_decimal.rb +42 -0
- data/lib/mongoid/fields/serializable/bignum.rb +10 -0
- data/lib/mongoid/fields/serializable/binary.rb +11 -0
- data/lib/mongoid/fields/serializable/boolean.rb +44 -0
- data/lib/mongoid/fields/serializable/date.rb +51 -0
- data/lib/mongoid/fields/serializable/date_time.rb +28 -0
- data/lib/mongoid/fields/serializable/fixnum.rb +10 -0
- data/lib/mongoid/fields/serializable/float.rb +33 -0
- data/lib/mongoid/fields/serializable/foreign_keys/array.rb +56 -0
- data/lib/mongoid/fields/serializable/foreign_keys/object.rb +43 -0
- data/lib/mongoid/fields/serializable/hash.rb +25 -0
- data/lib/mongoid/fields/serializable/integer.rb +33 -0
- data/lib/mongoid/fields/serializable/object.rb +11 -0
- data/lib/mongoid/fields/serializable/object_id.rb +32 -0
- data/lib/mongoid/fields/serializable/range.rb +42 -0
- data/lib/mongoid/fields/serializable/set.rb +42 -0
- data/lib/mongoid/fields/serializable/string.rb +28 -0
- data/lib/mongoid/fields/serializable/symbol.rb +28 -0
- data/lib/mongoid/fields/serializable/time.rb +12 -0
- data/lib/mongoid/fields/serializable/time_with_zone.rb +12 -0
- data/lib/mongoid/fields/serializable/timekeeping.rb +102 -0
- data/lib/mongoid/finders.rb +61 -37
- data/lib/mongoid/hierarchy.rb +43 -8
- data/lib/mongoid/identity_map.rb +106 -0
- data/lib/mongoid/indexes.rb +17 -1
- data/lib/mongoid/javascript.rb +2 -3
- data/lib/mongoid/keys.rb +10 -21
- data/lib/mongoid/logger.rb +22 -1
- data/lib/mongoid/matchers/all.rb +10 -0
- data/lib/mongoid/matchers/default.rb +1 -1
- data/lib/mongoid/matchers/exists.rb +10 -0
- data/lib/mongoid/matchers/gt.rb +10 -0
- data/lib/mongoid/matchers/gte.rb +10 -0
- data/lib/mongoid/matchers/in.rb +10 -0
- data/lib/mongoid/matchers/lt.rb +10 -0
- data/lib/mongoid/matchers/lte.rb +10 -0
- data/lib/mongoid/matchers/ne.rb +10 -0
- data/lib/mongoid/matchers/nin.rb +10 -0
- data/lib/mongoid/matchers/or.rb +7 -4
- data/lib/mongoid/matchers/size.rb +10 -0
- data/lib/mongoid/multi_database.rb +26 -6
- data/lib/mongoid/multi_parameter_attributes.rb +40 -17
- data/lib/mongoid/named_scope.rb +1 -2
- data/lib/mongoid/nested_attributes.rb +4 -1
- data/lib/mongoid/observer.rb +108 -5
- data/lib/mongoid/paranoia.rb +26 -26
- data/lib/mongoid/persistence.rb +15 -21
- data/lib/mongoid/persistence/atomic.rb +135 -0
- data/lib/mongoid/persistence/atomic/add_to_set.rb +11 -8
- data/lib/mongoid/persistence/atomic/bit.rb +37 -0
- data/lib/mongoid/persistence/atomic/inc.rb +9 -6
- data/lib/mongoid/persistence/atomic/operation.rb +48 -7
- data/lib/mongoid/persistence/atomic/pop.rb +34 -0
- data/lib/mongoid/persistence/atomic/pull.rb +34 -0
- data/lib/mongoid/persistence/atomic/pull_all.rb +10 -9
- data/lib/mongoid/persistence/atomic/push.rb +8 -5
- data/lib/mongoid/persistence/atomic/push_all.rb +31 -0
- data/lib/mongoid/persistence/atomic/rename.rb +31 -0
- data/lib/mongoid/persistence/atomic/set.rb +30 -0
- data/lib/mongoid/persistence/atomic/unset.rb +28 -0
- data/lib/mongoid/persistence/deletion.rb +32 -0
- data/lib/mongoid/persistence/insertion.rb +41 -0
- data/lib/mongoid/persistence/modification.rb +37 -0
- data/lib/mongoid/persistence/operations.rb +214 -0
- data/lib/mongoid/persistence/operations/embedded/insert.rb +42 -0
- data/lib/mongoid/persistence/operations/embedded/remove.rb +40 -0
- data/lib/mongoid/persistence/operations/insert.rb +34 -0
- data/lib/mongoid/persistence/operations/remove.rb +33 -0
- data/lib/mongoid/persistence/operations/update.rb +53 -0
- data/lib/mongoid/railtie.rb +21 -33
- data/lib/mongoid/railties/database.rake +12 -12
- data/lib/mongoid/relations.rb +9 -5
- data/lib/mongoid/relations/accessors.rb +15 -36
- data/lib/mongoid/relations/auto_save.rb +2 -2
- data/lib/mongoid/relations/binding.rb +28 -1
- data/lib/mongoid/relations/bindings/embedded/in.rb +17 -30
- data/lib/mongoid/relations/bindings/embedded/many.rb +16 -21
- data/lib/mongoid/relations/bindings/embedded/one.rb +11 -16
- data/lib/mongoid/relations/bindings/referenced/in.rb +31 -32
- data/lib/mongoid/relations/bindings/referenced/many.rb +19 -61
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -63
- data/lib/mongoid/relations/bindings/referenced/one.rb +18 -26
- data/lib/mongoid/relations/builder.rb +4 -2
- data/lib/mongoid/relations/builders.rb +21 -2
- data/lib/mongoid/relations/builders/embedded/in.rb +5 -1
- data/lib/mongoid/relations/builders/embedded/many.rb +12 -4
- data/lib/mongoid/relations/builders/embedded/one.rb +5 -1
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +2 -2
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
- data/lib/mongoid/relations/builders/referenced/in.rb +2 -5
- data/lib/mongoid/relations/builders/referenced/many.rb +2 -3
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +14 -5
- data/lib/mongoid/relations/builders/referenced/one.rb +2 -3
- data/lib/mongoid/relations/embedded/atomic.rb +2 -2
- data/lib/mongoid/relations/embedded/in.rb +72 -41
- data/lib/mongoid/relations/embedded/many.rb +116 -120
- data/lib/mongoid/relations/embedded/one.rb +59 -41
- data/lib/mongoid/relations/embedded/sort.rb +31 -0
- data/lib/mongoid/relations/macros.rb +28 -24
- data/lib/mongoid/relations/many.rb +10 -103
- data/lib/mongoid/relations/metadata.rb +335 -38
- data/lib/mongoid/relations/one.rb +7 -32
- data/lib/mongoid/relations/options.rb +47 -0
- data/lib/mongoid/relations/proxy.rb +29 -28
- data/lib/mongoid/relations/referenced/batch.rb +2 -3
- data/lib/mongoid/relations/referenced/in.rb +66 -53
- data/lib/mongoid/relations/referenced/many.rb +216 -143
- data/lib/mongoid/relations/referenced/many_to_many.rb +132 -163
- data/lib/mongoid/relations/referenced/one.rb +76 -58
- data/lib/mongoid/relations/synchronization.rb +113 -0
- data/lib/mongoid/relations/targets.rb +2 -0
- data/lib/mongoid/relations/targets/enumerable.rb +329 -0
- data/lib/mongoid/safety.rb +24 -156
- data/lib/mongoid/serialization.rb +21 -0
- data/lib/mongoid/state.rb +34 -0
- data/lib/mongoid/threaded.rb +175 -0
- data/lib/mongoid/timestamps/updated.rb +1 -1
- data/lib/mongoid/validations.rb +3 -7
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/versioning.rb +61 -7
- data/lib/rack/mongoid.rb +2 -0
- data/lib/rack/mongoid/middleware/identity_map.rb +38 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +1 -1
- data/lib/rails/generators/mongoid/model/templates/{model.rb → model.rb.tt} +0 -0
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +1 -1
- data/lib/rails/generators/mongoid/observer/templates/{observer.rb → observer.rb.tt} +0 -0
- data/lib/rails/mongoid.rb +17 -17
- metadata +136 -102
- data/lib/mongoid/atomicity.rb +0 -111
- data/lib/mongoid/collections/cyclic_iterator.rb +0 -34
- data/lib/mongoid/collections/slaves.rb +0 -61
- data/lib/mongoid/extensions/array/conversions.rb +0 -23
- data/lib/mongoid/extensions/array/parentization.rb +0 -13
- data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
- data/lib/mongoid/extensions/binary/conversions.rb +0 -17
- data/lib/mongoid/extensions/boolean/conversions.rb +0 -27
- data/lib/mongoid/extensions/date/conversions.rb +0 -25
- data/lib/mongoid/extensions/datetime/conversions.rb +0 -12
- data/lib/mongoid/extensions/float/conversions.rb +0 -20
- data/lib/mongoid/extensions/hash/conversions.rb +0 -19
- data/lib/mongoid/extensions/integer/conversions.rb +0 -20
- data/lib/mongoid/extensions/object/conversions.rb +0 -25
- data/lib/mongoid/extensions/range/conversions.rb +0 -25
- data/lib/mongoid/extensions/set/conversions.rb +0 -20
- data/lib/mongoid/extensions/symbol/conversions.rb +0 -21
- data/lib/mongoid/extensions/time_conversions.rb +0 -38
- data/lib/mongoid/field.rb +0 -162
- data/lib/mongoid/paths.rb +0 -61
- data/lib/mongoid/persistence/command.rb +0 -71
- data/lib/mongoid/persistence/insert.rb +0 -53
- data/lib/mongoid/persistence/insert_embedded.rb +0 -43
- data/lib/mongoid/persistence/remove.rb +0 -44
- data/lib/mongoid/persistence/remove_all.rb +0 -40
- data/lib/mongoid/persistence/remove_embedded.rb +0 -48
- data/lib/mongoid/persistence/update.rb +0 -77
- data/lib/mongoid/safe.rb +0 -23
- data/lib/mongoid/validations/referenced.rb +0 -58
@@ -0,0 +1,214 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "mongoid/persistence/operations/insert"
|
3
|
+
require "mongoid/persistence/operations/remove"
|
4
|
+
require "mongoid/persistence/operations/update"
|
5
|
+
require "mongoid/persistence/operations/embedded/insert"
|
6
|
+
require "mongoid/persistence/operations/embedded/remove"
|
7
|
+
|
8
|
+
module Mongoid #:nodoc:
|
9
|
+
module Persistence #:nodoc:
|
10
|
+
|
11
|
+
# Persistence operations include this module to get basic functionality
|
12
|
+
# on initialization.
|
13
|
+
module Operations
|
14
|
+
|
15
|
+
attr_reader :conflicts, :document
|
16
|
+
|
17
|
+
# Get the collection we should be persisting to.
|
18
|
+
#
|
19
|
+
# @example Get the collection.
|
20
|
+
# operation.collection
|
21
|
+
#
|
22
|
+
# @return [ Collection ] The collection to persist to.
|
23
|
+
#
|
24
|
+
# @since 2.1.0
|
25
|
+
def collection
|
26
|
+
@collection ||= document._root.collection
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get the atomic delete operations for embedded documents.
|
30
|
+
#
|
31
|
+
# @example Get the atomic deletes.
|
32
|
+
# operation.deletes
|
33
|
+
#
|
34
|
+
# @return [ Hash ] The atomic delete selector.
|
35
|
+
#
|
36
|
+
# @since 2.1.0
|
37
|
+
def deletes
|
38
|
+
{ document.atomic_delete_modifier =>
|
39
|
+
{ document.atomic_path =>
|
40
|
+
document._index ? { "_id" => document.id } : true } }
|
41
|
+
end
|
42
|
+
|
43
|
+
# Instantiate the new persistence operation.
|
44
|
+
#
|
45
|
+
# @example Create the operation.
|
46
|
+
# Operation.new(document, { :safe => true }, { "field" => "value" })
|
47
|
+
#
|
48
|
+
# @param [ Document ] document The document to persist.
|
49
|
+
# @param [ Hash ] options The persistence options.
|
50
|
+
#
|
51
|
+
# @since 2.1.0
|
52
|
+
def initialize(document, options = {})
|
53
|
+
@document, @options = document, options
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get the atomic insert for embedded documents, either a push or set.
|
57
|
+
#
|
58
|
+
# @example Get the inserts.
|
59
|
+
# operation.inserts
|
60
|
+
#
|
61
|
+
# @return [ Hash ] The insert ops.
|
62
|
+
#
|
63
|
+
# @since 2.1.0
|
64
|
+
def inserts
|
65
|
+
{ document.atomic_insert_modifier =>
|
66
|
+
{ document.atomic_position => document.as_document } }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Should the parent document (in the case of embedded persistence) be
|
70
|
+
# notified of the child deletion. This is used when calling delete from
|
71
|
+
# the associations themselves.
|
72
|
+
#
|
73
|
+
# @example Should the parent be notified?
|
74
|
+
# operation.notifying_parent?
|
75
|
+
#
|
76
|
+
# @return [ true, false ] If the parent should be notified.
|
77
|
+
#
|
78
|
+
# @since 2.1.0
|
79
|
+
def notifying_parent?
|
80
|
+
@notifying_parent ||= !@options.delete(:suppress)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Get all the options that will be sent to the database. Right now this
|
84
|
+
# is only safe mode opts.
|
85
|
+
#
|
86
|
+
# @example Get the options hash.
|
87
|
+
# operation.options
|
88
|
+
#
|
89
|
+
# @return [ Hash ] The options for the database.
|
90
|
+
#
|
91
|
+
# @since 2.1.0
|
92
|
+
def options
|
93
|
+
Safety.merge_safety_options(@options)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Get the parent of the provided document.
|
97
|
+
#
|
98
|
+
# @example Get the parent.
|
99
|
+
# operation.parent
|
100
|
+
#
|
101
|
+
# @return [ Document ] The parent document.
|
102
|
+
#
|
103
|
+
# @since 2.1.0
|
104
|
+
def parent
|
105
|
+
document._parent
|
106
|
+
end
|
107
|
+
|
108
|
+
# Get the atomic selector for the document.
|
109
|
+
#
|
110
|
+
# @example Get the selector.
|
111
|
+
# operation.selector.
|
112
|
+
#
|
113
|
+
# @return [ Hash ] The mongodb selector.
|
114
|
+
#
|
115
|
+
# @since 2.1.0
|
116
|
+
def selector
|
117
|
+
@selector ||= document.atomic_selector
|
118
|
+
end
|
119
|
+
|
120
|
+
# Get the atomic updates for the document without the conflicting
|
121
|
+
# modifications.
|
122
|
+
#
|
123
|
+
# @example Get the atomic updates.
|
124
|
+
# operation.updates
|
125
|
+
#
|
126
|
+
# @return [ Hash ] The updates sans conflicting mods.
|
127
|
+
#
|
128
|
+
# @since 2.1.0
|
129
|
+
def updates
|
130
|
+
@updates ||= init_updates
|
131
|
+
end
|
132
|
+
|
133
|
+
# Should we be running validations on this persistence operation?
|
134
|
+
# Defaults to true.
|
135
|
+
#
|
136
|
+
# @example Run validations?
|
137
|
+
# operation.validating?
|
138
|
+
#
|
139
|
+
# @return [ true, false ] If we run validations.
|
140
|
+
#
|
141
|
+
# @since 2.1.0
|
142
|
+
def validating?
|
143
|
+
@validating ||= @options[:validate].nil? ? true : @options[:validate]
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
# Initialize the atomic updates and conflicting modifications.
|
149
|
+
#
|
150
|
+
# @example Initialize the updates.
|
151
|
+
# operation.init_updates
|
152
|
+
#
|
153
|
+
# @return [ Hash ] The atomic updates.
|
154
|
+
#
|
155
|
+
# @since 2.1.0
|
156
|
+
def init_updates
|
157
|
+
document.atomic_updates.tap do |updates|
|
158
|
+
conflicts = updates.delete(:other)
|
159
|
+
if conflicts
|
160
|
+
@conflicts = { "$pushAll" => conflicts }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class << self
|
166
|
+
|
167
|
+
# Get the appropriate removal operation based on the document.
|
168
|
+
#
|
169
|
+
# @example Get the deletion operation.
|
170
|
+
# Operations.remove(doc, options)
|
171
|
+
#
|
172
|
+
# @param [ Document ] doc The document to persist.
|
173
|
+
# @param [ Hash ] options The persistence options.
|
174
|
+
#
|
175
|
+
# @return [ Operations ] The operation.
|
176
|
+
#
|
177
|
+
# @since 2.1.0
|
178
|
+
def remove(doc, options = {})
|
179
|
+
(doc.embedded? ? Embedded::Remove : Remove).new(doc, options)
|
180
|
+
end
|
181
|
+
|
182
|
+
# Get the appropriate insertion operation based on the document.
|
183
|
+
#
|
184
|
+
# @example Get the insertion operation.
|
185
|
+
# Operations.insert(doc, options)
|
186
|
+
#
|
187
|
+
# @param [ Document ] doc The document to persist.
|
188
|
+
# @param [ Hash ] options The persistence options.
|
189
|
+
#
|
190
|
+
# @return [ Operations ] The operation.
|
191
|
+
#
|
192
|
+
# @since 2.1.0
|
193
|
+
def insert(doc, options = {})
|
194
|
+
(doc.embedded? ? Embedded::Insert : Insert).new(doc, options)
|
195
|
+
end
|
196
|
+
|
197
|
+
# Get the appropriate update operation based on the document.
|
198
|
+
#
|
199
|
+
# @example Get the update operation.
|
200
|
+
# Operations.update(doc, options)
|
201
|
+
#
|
202
|
+
# @param [ Document ] doc The document to persist.
|
203
|
+
# @param [ Hash ] options The persistence options.
|
204
|
+
#
|
205
|
+
# @return [ Operations ] The operation.
|
206
|
+
#
|
207
|
+
# @since 2.1.0
|
208
|
+
def update(doc, options = {})
|
209
|
+
Update.new(doc, options)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Persistence #:nodoc:
|
4
|
+
module Operations #:nodoc:
|
5
|
+
module Embedded #:nodoc:
|
6
|
+
|
7
|
+
# Insert is a persistence command responsible for taking a document that
|
8
|
+
# has not been saved to the database and saving it. This specific class
|
9
|
+
# handles the case when the document is embedded in another.
|
10
|
+
#
|
11
|
+
# The underlying query resembles the following MongoDB query:
|
12
|
+
#
|
13
|
+
# collection.update(
|
14
|
+
# { "_id" : 1 },
|
15
|
+
# { "$push" : { "field" : "value" } },
|
16
|
+
# false
|
17
|
+
# );
|
18
|
+
class Insert
|
19
|
+
include Insertion, Operations
|
20
|
+
|
21
|
+
# Insert the new document in the database. If the document's parent is a
|
22
|
+
# new record, we will call save on the parent, otherwise we will $push
|
23
|
+
# the document onto the parent.
|
24
|
+
#
|
25
|
+
# @example Insert an embedded document.
|
26
|
+
# Insert.persist
|
27
|
+
#
|
28
|
+
# @return [ Document ] The document to be inserted.
|
29
|
+
def persist
|
30
|
+
prepare do
|
31
|
+
if parent.new?
|
32
|
+
parent.insert
|
33
|
+
else
|
34
|
+
collection.update(parent.atomic_selector, inserts, options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Persistence #:nodoc:
|
4
|
+
module Operations #:nodoc:
|
5
|
+
module Embedded #:nodoc:
|
6
|
+
|
7
|
+
# Remove is a persistence command responsible for deleting a document from
|
8
|
+
# the database.
|
9
|
+
#
|
10
|
+
# The underlying query resembles the following MongoDB query:
|
11
|
+
#
|
12
|
+
# collection.remove(
|
13
|
+
# { "_id" : 1 },
|
14
|
+
# false
|
15
|
+
# );
|
16
|
+
class Remove
|
17
|
+
include Deletion, Operations
|
18
|
+
|
19
|
+
# Remove the document from the database. If the parent is a new record,
|
20
|
+
# it will get removed in Ruby only. If the parent is not a new record
|
21
|
+
# then either an $unset or $set will occur, depending if it's an
|
22
|
+
# embeds_one or embeds_many.
|
23
|
+
#
|
24
|
+
# @example Remove an embedded document.
|
25
|
+
# RemoveEmbedded.persist
|
26
|
+
#
|
27
|
+
# @return [ true ] Always true.
|
28
|
+
def persist
|
29
|
+
prepare do |doc|
|
30
|
+
parent.remove_child(doc) if notifying_parent?
|
31
|
+
if parent.persisted?
|
32
|
+
collection.update(parent.atomic_selector, deletes, options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Persistence #:nodoc:
|
4
|
+
module Operations #:nodoc:
|
5
|
+
|
6
|
+
# Insert is a persistence command responsible for taking a document that
|
7
|
+
# has not been saved to the database and saving it.
|
8
|
+
#
|
9
|
+
# The underlying query resembles the following MongoDB query:
|
10
|
+
#
|
11
|
+
# collection.insert(
|
12
|
+
# { "_id" : 1, "field" : "value" },
|
13
|
+
# false
|
14
|
+
# );
|
15
|
+
class Insert
|
16
|
+
include Insertion, Operations
|
17
|
+
|
18
|
+
# Insert the new document in the database. This delegates to the standard
|
19
|
+
# MongoDB collection's insert command.
|
20
|
+
#
|
21
|
+
# @example Insert the document.
|
22
|
+
# Insert.persist
|
23
|
+
#
|
24
|
+
# @return [ Document ] The document to be inserted.
|
25
|
+
def persist
|
26
|
+
prepare do |doc|
|
27
|
+
collection.insert(doc.as_document, options)
|
28
|
+
IdentityMap.set(doc)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Persistence #:nodoc:
|
4
|
+
module Operations #:nodoc:
|
5
|
+
|
6
|
+
# Remove is a persistence command responsible for deleting a document from
|
7
|
+
# the database.
|
8
|
+
#
|
9
|
+
# The underlying query resembles the following MongoDB query:
|
10
|
+
#
|
11
|
+
# collection.remove(
|
12
|
+
# { "_id" : 1 },
|
13
|
+
# false
|
14
|
+
# );
|
15
|
+
class Remove
|
16
|
+
include Deletion, Operations
|
17
|
+
|
18
|
+
# Remove the document from the database: delegates to the MongoDB
|
19
|
+
# collection remove method.
|
20
|
+
#
|
21
|
+
# @example Remove the document.
|
22
|
+
# Remove.persist
|
23
|
+
#
|
24
|
+
# @return [ true ] Always true.
|
25
|
+
def persist
|
26
|
+
prepare do |doc|
|
27
|
+
collection.remove({ :_id => doc.id }, options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Persistence #:nodoc:
|
4
|
+
module Operations #:nodoc:
|
5
|
+
|
6
|
+
# Update is a persistence command responsible for taking a document that
|
7
|
+
# has already been saved to the database and saving it, depending on
|
8
|
+
# whether or not the document has been modified.
|
9
|
+
#
|
10
|
+
# Before persisting the command will check via dirty attributes if the
|
11
|
+
# document has changed, if not, it will simply return true. If it has it
|
12
|
+
# will go through the validation steps, run callbacks, and set the changed
|
13
|
+
# fields atomically on the document. The underlying query resembles the
|
14
|
+
# following MongoDB query:
|
15
|
+
#
|
16
|
+
# collection.update(
|
17
|
+
# { "_id" : 1,
|
18
|
+
# { "$set" : { "field" : "value" },
|
19
|
+
# false,
|
20
|
+
# false
|
21
|
+
# );
|
22
|
+
#
|
23
|
+
# For embedded documents it will use the positional locator:
|
24
|
+
#
|
25
|
+
# collection.update(
|
26
|
+
# { "_id" : 1, "addresses._id" : 2 },
|
27
|
+
# { "$set" : { "addresses.$.field" : "value" },
|
28
|
+
# false,
|
29
|
+
# false
|
30
|
+
# );
|
31
|
+
#
|
32
|
+
class Update
|
33
|
+
include Modification, Operations
|
34
|
+
|
35
|
+
# Persist the document that is to be updated to the database. This will
|
36
|
+
# only write changed fields via MongoDB's $set modifier operation.
|
37
|
+
#
|
38
|
+
# @example Update the document.
|
39
|
+
# Update.persist
|
40
|
+
#
|
41
|
+
# @return [ true, false ] If the save passed.
|
42
|
+
def persist
|
43
|
+
prepare do
|
44
|
+
unless updates.empty?
|
45
|
+
collection.update(selector, updates, options)
|
46
|
+
collection.update(selector, conflicts, options) if conflicts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/mongoid/railtie.rb
CHANGED
@@ -60,8 +60,7 @@ module Rails #:nodoc:
|
|
60
60
|
initializer "setup database" do
|
61
61
|
config_file = Rails.root.join("config", "mongoid.yml")
|
62
62
|
if config_file.file?
|
63
|
-
|
64
|
-
::Mongoid.from_hash(settings) if settings.present?
|
63
|
+
::Mongoid.load!(config_file)
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
@@ -76,17 +75,6 @@ module Rails #:nodoc:
|
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
79
|
-
# Due to all models not getting loaded and messing up inheritance queries
|
80
|
-
# and indexing, we need to preload the models in order to address this.
|
81
|
-
#
|
82
|
-
# This will happen every request in development, once in ther other
|
83
|
-
# environments.
|
84
|
-
initializer "preload all application models" do |app|
|
85
|
-
config.to_prepare do
|
86
|
-
::Rails::Mongoid.load_models(app) unless $rails_rake_task
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
78
|
# Set the proper error types for Rails. DocumentNotFound errors should be
|
91
79
|
# 404s and not 500s, validation errors are 422s.
|
92
80
|
initializer "load http errors" do |app|
|
@@ -98,27 +86,27 @@ module Rails #:nodoc:
|
|
98
86
|
end
|
99
87
|
end
|
100
88
|
|
101
|
-
#
|
102
|
-
#
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
::Mongoid.reconnect!(false)
|
112
|
-
end
|
89
|
+
# Due to all models not getting loaded and messing up inheritance queries
|
90
|
+
# and indexing, we need to preload the models in order to address this.
|
91
|
+
#
|
92
|
+
# This will happen every request in development, once in ther other
|
93
|
+
# environments.
|
94
|
+
initializer "preload all application models" do |app|
|
95
|
+
config.to_prepare do
|
96
|
+
::Rails::Mongoid.load_models(app) unless $rails_rake_task
|
97
|
+
end
|
98
|
+
end
|
113
99
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
100
|
+
# This initializer warns the user that preloading models is set to false,
|
101
|
+
# and queries will be inconsistent in dev mode if models are using
|
102
|
+
# inheritance.
|
103
|
+
initializer "warn of preload models configuration" do |app|
|
104
|
+
config.after_initialize do
|
105
|
+
if !::Mongoid.preload_models && !Rails.configuration.cache_classes
|
106
|
+
puts "\nMongoid preload_models is set to false. If you are using"
|
107
|
+
puts "inheritance in your application model please set this to true or "
|
108
|
+
puts "you will experience querying inconsistencies in dev mode. Note that"
|
109
|
+
puts "this will severely decrease performance in dev mode only.\n\n"
|
122
110
|
end
|
123
111
|
end
|
124
112
|
end
|