mongoid-with-auth 1.9.4
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.
- data/MIT_LICENSE +20 -0
- data/README.rdoc +49 -0
- data/lib/mongoid.rb +122 -0
- data/lib/mongoid/associations.rb +300 -0
- data/lib/mongoid/associations/belongs_to_related.rb +58 -0
- data/lib/mongoid/associations/embedded_in.rb +72 -0
- data/lib/mongoid/associations/embeds_many.rb +254 -0
- data/lib/mongoid/associations/embeds_one.rb +96 -0
- data/lib/mongoid/associations/has_many_related.rb +181 -0
- data/lib/mongoid/associations/has_one_related.rb +85 -0
- data/lib/mongoid/associations/meta_data.rb +29 -0
- data/lib/mongoid/associations/options.rb +57 -0
- data/lib/mongoid/associations/proxy.rb +24 -0
- data/lib/mongoid/attributes.rb +204 -0
- data/lib/mongoid/callbacks.rb +23 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +41 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +41 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/components.rb +27 -0
- data/lib/mongoid/concern.rb +31 -0
- data/lib/mongoid/config.rb +205 -0
- data/lib/mongoid/contexts.rb +25 -0
- data/lib/mongoid/contexts/enumerable.rb +151 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +285 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/criteria.rb +239 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +110 -0
- data/lib/mongoid/criterion/optional.rb +136 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/deprecation.rb +22 -0
- data/lib/mongoid/dirty.rb +253 -0
- data/lib/mongoid/document.rb +311 -0
- data/lib/mongoid/errors.rb +108 -0
- data/lib/mongoid/extensions.rb +101 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/aliasing.rb +4 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -0
- data/lib/mongoid/extensions/array/conversions.rb +29 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +22 -0
- data/lib/mongoid/extensions/date/conversions.rb +24 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/accessors.rb +38 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +39 -0
- data/lib/mongoid/extensions/hash/conversions.rb +45 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
- data/lib/mongoid/extensions/object/conversions.rb +33 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/string/conversions.rb +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +36 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +59 -0
- data/lib/mongoid/fields.rb +65 -0
- data/lib/mongoid/finders.rb +136 -0
- data/lib/mongoid/identity.rb +39 -0
- data/lib/mongoid/indexes.rb +30 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/matchers.rb +36 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +26 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/memoization.rb +33 -0
- data/lib/mongoid/named_scope.rb +37 -0
- data/lib/mongoid/observable.rb +30 -0
- data/lib/mongoid/paths.rb +62 -0
- data/lib/mongoid/persistence.rb +222 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +50 -0
- data/lib/mongoid/persistence/insert_embedded.rb +38 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +37 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +63 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +39 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +27 -0
- metadata +284 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "mongoid/persistence/command"
|
|
3
|
+
require "mongoid/persistence/insert"
|
|
4
|
+
require "mongoid/persistence/insert_embedded"
|
|
5
|
+
require "mongoid/persistence/remove"
|
|
6
|
+
require "mongoid/persistence/remove_all"
|
|
7
|
+
require "mongoid/persistence/remove_embedded"
|
|
8
|
+
require "mongoid/persistence/update"
|
|
9
|
+
|
|
10
|
+
module Mongoid #:nodoc:
|
|
11
|
+
# The persistence module is a mixin to provide database accessor methods for
|
|
12
|
+
# the document. These correspond to the appropriate accessors on a
|
|
13
|
+
# +Mongo::Collection+ and retain the same DSL.
|
|
14
|
+
#
|
|
15
|
+
# Examples:
|
|
16
|
+
#
|
|
17
|
+
# <tt>document.insert</tt>
|
|
18
|
+
# <tt>document.update</tt>
|
|
19
|
+
# <tt>document.upsert</tt>
|
|
20
|
+
module Persistence
|
|
21
|
+
extend ActiveSupport::Concern
|
|
22
|
+
module InstanceMethods #:nodoc:
|
|
23
|
+
# Remove the +Document+ from the datbase with callbacks.
|
|
24
|
+
#
|
|
25
|
+
# Example:
|
|
26
|
+
#
|
|
27
|
+
# <tt>document.destroy</tt>
|
|
28
|
+
#
|
|
29
|
+
# TODO: Will get rid of other #destroy once new persistence complete.
|
|
30
|
+
def destroy
|
|
31
|
+
run_callbacks(:before_destroy)
|
|
32
|
+
if _remove
|
|
33
|
+
self.destroyed = true
|
|
34
|
+
run_callbacks(:after_destroy)
|
|
35
|
+
end; true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Insert a new +Document+ into the database. Will return the document
|
|
39
|
+
# itself whether or not the save was successful.
|
|
40
|
+
#
|
|
41
|
+
# Example:
|
|
42
|
+
#
|
|
43
|
+
# <tt>document.insert</tt>
|
|
44
|
+
def insert(validate = true)
|
|
45
|
+
Insert.new(self, validate).persist
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Remove the +Document+ from the datbase.
|
|
49
|
+
#
|
|
50
|
+
# Example:
|
|
51
|
+
#
|
|
52
|
+
# <tt>document._remove</tt>
|
|
53
|
+
#
|
|
54
|
+
# TODO: Will get rid of other #remove once observable pattern killed.
|
|
55
|
+
def _remove
|
|
56
|
+
Remove.new(self).persist
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
alias :delete :_remove
|
|
60
|
+
|
|
61
|
+
# Save the document - will perform an insert if the document is new, and
|
|
62
|
+
# update if not. If a validation error occurs a
|
|
63
|
+
# Mongoid::Errors::Validations error will get raised.
|
|
64
|
+
#
|
|
65
|
+
# Example:
|
|
66
|
+
#
|
|
67
|
+
# <tt>document.save!</tt>
|
|
68
|
+
#
|
|
69
|
+
# Returns:
|
|
70
|
+
#
|
|
71
|
+
# +true+ if validation passed, will raise error otherwise.
|
|
72
|
+
def save!
|
|
73
|
+
self.class.fail_validate!(self) unless upsert; true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Update the +Document+ in the datbase.
|
|
77
|
+
#
|
|
78
|
+
# Example:
|
|
79
|
+
#
|
|
80
|
+
# <tt>document.update</tt>
|
|
81
|
+
def update(validate = true)
|
|
82
|
+
Update.new(self, validate).persist
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Update the +Document+ attributes in the datbase.
|
|
86
|
+
#
|
|
87
|
+
# Example:
|
|
88
|
+
#
|
|
89
|
+
# <tt>document.update_attributes(:title => "Sir")</tt>
|
|
90
|
+
#
|
|
91
|
+
# Returns:
|
|
92
|
+
#
|
|
93
|
+
# +true+ if validation passed, +false+ if not.
|
|
94
|
+
def update_attributes(attributes = {})
|
|
95
|
+
write_attributes(attributes); update
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Update the +Document+ attributes in the datbase.
|
|
99
|
+
#
|
|
100
|
+
# Example:
|
|
101
|
+
#
|
|
102
|
+
# <tt>document.update_attributes(:title => "Sir")</tt>
|
|
103
|
+
#
|
|
104
|
+
# Returns:
|
|
105
|
+
#
|
|
106
|
+
# +true+ if validation passed, raises an error if not
|
|
107
|
+
def update_attributes!(attributes = {})
|
|
108
|
+
write_attributes(attributes)
|
|
109
|
+
result = update
|
|
110
|
+
self.class.fail_validate!(self) unless result
|
|
111
|
+
result
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Upsert the document - will perform an insert if the document is new, and
|
|
115
|
+
# update if not.
|
|
116
|
+
#
|
|
117
|
+
# Example:
|
|
118
|
+
#
|
|
119
|
+
# <tt>document.upsert</tt>
|
|
120
|
+
#
|
|
121
|
+
# Returns:
|
|
122
|
+
#
|
|
123
|
+
# A +Boolean+ for updates.
|
|
124
|
+
def upsert(validate = true)
|
|
125
|
+
validate = parse_validate(validate)
|
|
126
|
+
if new_record?
|
|
127
|
+
insert(validate).errors.any? ? false : true
|
|
128
|
+
else
|
|
129
|
+
update(validate)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Save is aliased so that users familiar with active record can have some
|
|
134
|
+
# semblance of a familiar API.
|
|
135
|
+
#
|
|
136
|
+
# Example:
|
|
137
|
+
#
|
|
138
|
+
# <tt>document.save</tt>
|
|
139
|
+
alias :save :upsert
|
|
140
|
+
|
|
141
|
+
protected
|
|
142
|
+
# Alternative validation params.
|
|
143
|
+
def parse_validate(validate)
|
|
144
|
+
if validate.is_a?(Hash) && validate.has_key?(:validate)
|
|
145
|
+
validate = validate[:validate]
|
|
146
|
+
end
|
|
147
|
+
validate
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
module ClassMethods #:nodoc:
|
|
152
|
+
|
|
153
|
+
# Create a new +Document+. This will instantiate a new document and
|
|
154
|
+
# insert it in a single call. Will always return the document
|
|
155
|
+
# whether save passed or not.
|
|
156
|
+
#
|
|
157
|
+
# Example:
|
|
158
|
+
#
|
|
159
|
+
# <tt>Person.create(:title => "Mr")</tt>
|
|
160
|
+
#
|
|
161
|
+
# Returns: the +Document+.
|
|
162
|
+
def create(attributes = {})
|
|
163
|
+
document = new(attributes); document.insert
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Create a new +Document+. This will instantiate a new document and
|
|
167
|
+
# insert it in a single call. Will always return the document
|
|
168
|
+
# whether save passed or not, and if validation fails an error will be
|
|
169
|
+
# raise.
|
|
170
|
+
#
|
|
171
|
+
# Example:
|
|
172
|
+
#
|
|
173
|
+
# <tt>Person.create!(:title => "Mr")</tt>
|
|
174
|
+
#
|
|
175
|
+
# Returns: the +Document+.
|
|
176
|
+
def create!(attributes = {})
|
|
177
|
+
document = new(attributes)
|
|
178
|
+
fail_validate!(document) if document.insert.errors.any?
|
|
179
|
+
document
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Delete all documents given the supplied conditions. If no conditions
|
|
183
|
+
# are passed, the entire collection will be dropped for performance
|
|
184
|
+
# benefits. Does not fire any callbacks.
|
|
185
|
+
#
|
|
186
|
+
# Example:
|
|
187
|
+
#
|
|
188
|
+
# <tt>Person.delete_all(:conditions => { :title => "Sir" })</tt>
|
|
189
|
+
# <tt>Person.delete_all</tt>
|
|
190
|
+
#
|
|
191
|
+
# Returns: true or raises an error.
|
|
192
|
+
def delete_all(conditions = {})
|
|
193
|
+
RemoveAll.new(
|
|
194
|
+
self,
|
|
195
|
+
false,
|
|
196
|
+
conditions[:conditions] || {}
|
|
197
|
+
).persist
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Delete all documents given the supplied conditions. If no conditions
|
|
201
|
+
# are passed, the entire collection will be dropped for performance
|
|
202
|
+
# benefits. Fires the destroy callbacks if conditions were passed.
|
|
203
|
+
#
|
|
204
|
+
# Example:
|
|
205
|
+
#
|
|
206
|
+
# <tt>Person.destroy_all(:conditions => { :title => "Sir" })</tt>
|
|
207
|
+
# <tt>Person.destroy_all</tt>
|
|
208
|
+
#
|
|
209
|
+
# Returns: true or raises an error.
|
|
210
|
+
def destroy_all(conditions = {})
|
|
211
|
+
documents = all(conditions)
|
|
212
|
+
count = documents.count
|
|
213
|
+
documents.each { |doc| doc.destroy }; count
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Raise an error if validation failed.
|
|
217
|
+
def fail_validate!(document)
|
|
218
|
+
raise Errors::Validations.new(document.errors)
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Persistence commands extend from this class to get basic functionality on
|
|
5
|
+
# initialization.
|
|
6
|
+
class Command
|
|
7
|
+
attr_reader \
|
|
8
|
+
:collection,
|
|
9
|
+
:document,
|
|
10
|
+
:klass,
|
|
11
|
+
:options,
|
|
12
|
+
:selector,
|
|
13
|
+
:validate
|
|
14
|
+
|
|
15
|
+
# Initialize the persistence +Command+.
|
|
16
|
+
#
|
|
17
|
+
# Options:
|
|
18
|
+
#
|
|
19
|
+
# document_or_class: The +Document+ or +Class+ to get the collection.
|
|
20
|
+
# validate: Is the document to be validated.
|
|
21
|
+
# selector: Optional selector to use in query.
|
|
22
|
+
#
|
|
23
|
+
# Example:
|
|
24
|
+
#
|
|
25
|
+
# <tt>DeleteAll.new(Person, false, {})</tt>
|
|
26
|
+
def initialize(document_or_class, validate = true, selector = {})
|
|
27
|
+
if document_or_class.is_a?(Mongoid::Document)
|
|
28
|
+
@document = document_or_class
|
|
29
|
+
@collection = @document.embedded? ? @document._root.collection : @document.collection
|
|
30
|
+
else
|
|
31
|
+
@klass = document_or_class
|
|
32
|
+
@collection = @klass.collection
|
|
33
|
+
end
|
|
34
|
+
@selector, @validate = selector, validate
|
|
35
|
+
@options = { :safe => Mongoid.persist_in_safe_mode }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Insert is a persistence command responsible for taking a document that
|
|
5
|
+
# has not been saved to the database and saving it.
|
|
6
|
+
#
|
|
7
|
+
# The underlying query resembles the following MongoDB query:
|
|
8
|
+
#
|
|
9
|
+
# collection.insert(
|
|
10
|
+
# { "_id" : 1, "field" : "value" },
|
|
11
|
+
# false
|
|
12
|
+
# );
|
|
13
|
+
class Insert < Command
|
|
14
|
+
# Insert the new document in the database. This delegates to the standard
|
|
15
|
+
# MongoDB collection's insert command.
|
|
16
|
+
#
|
|
17
|
+
# Example:
|
|
18
|
+
#
|
|
19
|
+
# <tt>Insert.persist</tt>
|
|
20
|
+
#
|
|
21
|
+
# Returns:
|
|
22
|
+
#
|
|
23
|
+
# The +Document+, whether the insert succeeded or not.
|
|
24
|
+
def persist
|
|
25
|
+
return @document if @validate && !@document.valid?
|
|
26
|
+
@document.run_callbacks(:before_create)
|
|
27
|
+
@document.run_callbacks(:before_save)
|
|
28
|
+
if insert
|
|
29
|
+
@document.new_record = false
|
|
30
|
+
# TODO: All child document new_record flags must get set to false
|
|
31
|
+
# here or somewhere - this will cause problems.
|
|
32
|
+
@document.move_changes
|
|
33
|
+
@document.run_callbacks(:after_create)
|
|
34
|
+
@document.run_callbacks(:after_save)
|
|
35
|
+
end
|
|
36
|
+
@document
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
# Insert the document into the database.
|
|
41
|
+
def insert
|
|
42
|
+
if @document.embedded?
|
|
43
|
+
Persistence::InsertEmbedded.new(@document, @validate).persist
|
|
44
|
+
else
|
|
45
|
+
@collection.insert(@document.raw_attributes, @options)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Insert is a persistence command responsible for taking a document that
|
|
5
|
+
# has not been saved to the database and saving it. This specific class
|
|
6
|
+
# handles the case when the document is embedded in another.
|
|
7
|
+
#
|
|
8
|
+
# The underlying query resembles the following MongoDB query:
|
|
9
|
+
#
|
|
10
|
+
# collection.insert(
|
|
11
|
+
# { "_id" : 1, "field" : "value" },
|
|
12
|
+
# false
|
|
13
|
+
# );
|
|
14
|
+
class InsertEmbedded < Command
|
|
15
|
+
# Insert the new document in the database. If the document's parent is a
|
|
16
|
+
# new record, we will call save on the parent, otherwise we will $push
|
|
17
|
+
# the document onto the parent.
|
|
18
|
+
#
|
|
19
|
+
# Example:
|
|
20
|
+
#
|
|
21
|
+
# <tt>Insert.persist</tt>
|
|
22
|
+
#
|
|
23
|
+
# Returns:
|
|
24
|
+
#
|
|
25
|
+
# The +Document+, whether the insert succeeded or not.
|
|
26
|
+
def persist
|
|
27
|
+
parent = @document._parent
|
|
28
|
+
if parent.new_record?
|
|
29
|
+
parent.insert
|
|
30
|
+
else
|
|
31
|
+
update = { @document._inserter => { @document._position => @document.raw_attributes } }
|
|
32
|
+
@collection.update(parent._selector, update, @options.merge(:multi => false))
|
|
33
|
+
end
|
|
34
|
+
@document.new_record = false; @document
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Remove is a persistence command responsible for deleting a document from
|
|
5
|
+
# the database.
|
|
6
|
+
#
|
|
7
|
+
# The underlying query resembles the following MongoDB query:
|
|
8
|
+
#
|
|
9
|
+
# collection.remove(
|
|
10
|
+
# { "_id" : 1 },
|
|
11
|
+
# false
|
|
12
|
+
# );
|
|
13
|
+
class Remove < Command
|
|
14
|
+
# Remove the document from the database: delegates to the MongoDB
|
|
15
|
+
# collection remove method.
|
|
16
|
+
#
|
|
17
|
+
# Example:
|
|
18
|
+
#
|
|
19
|
+
# <tt>Remove.persist</tt>
|
|
20
|
+
#
|
|
21
|
+
# Returns:
|
|
22
|
+
#
|
|
23
|
+
# +true+ if success, +false+ if not.
|
|
24
|
+
def persist
|
|
25
|
+
remove
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
protected
|
|
29
|
+
# Remove the document from the database.
|
|
30
|
+
def remove
|
|
31
|
+
if @document.embedded?
|
|
32
|
+
Persistence::RemoveEmbedded.new(@document, @validate).persist
|
|
33
|
+
else
|
|
34
|
+
@collection.remove({ :_id => @document.id }, @options)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Remove is a persistence command responsible for deleting a document from
|
|
5
|
+
# the database.
|
|
6
|
+
#
|
|
7
|
+
# The underlying query resembles the following MongoDB query:
|
|
8
|
+
#
|
|
9
|
+
# collection.remove(
|
|
10
|
+
# { "field" : value },
|
|
11
|
+
# false
|
|
12
|
+
# );
|
|
13
|
+
class RemoveAll < Command
|
|
14
|
+
# Remove the document from the database: delegates to the MongoDB
|
|
15
|
+
# collection remove method.
|
|
16
|
+
#
|
|
17
|
+
# Example:
|
|
18
|
+
#
|
|
19
|
+
# <tt>Remove.persist</tt>
|
|
20
|
+
#
|
|
21
|
+
# Returns:
|
|
22
|
+
#
|
|
23
|
+
# +true+ if success, +false+ if not.
|
|
24
|
+
def persist
|
|
25
|
+
remove
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
protected
|
|
29
|
+
# Remove the document from the database.
|
|
30
|
+
def remove
|
|
31
|
+
count = @collection.find(@selector.merge(:_type => @klass.name)).count
|
|
32
|
+
@collection.remove(@selector, @options)
|
|
33
|
+
count
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Remove is a persistence command responsible for deleting a document from
|
|
5
|
+
# the database.
|
|
6
|
+
#
|
|
7
|
+
# The underlying query resembles the following MongoDB query:
|
|
8
|
+
#
|
|
9
|
+
# collection.remove(
|
|
10
|
+
# { "_id" : 1 },
|
|
11
|
+
# false
|
|
12
|
+
# );
|
|
13
|
+
class RemoveEmbedded < Command
|
|
14
|
+
# Insert the new document in the database. If the document's parent is a
|
|
15
|
+
# new record, we will call save on the parent, otherwise we will $push
|
|
16
|
+
# the document onto the parent.
|
|
17
|
+
#
|
|
18
|
+
# Remove the document from the database. If the parent is a new record,
|
|
19
|
+
# it will get removed in Ruby only. If the parent is not a new record
|
|
20
|
+
# then either an $unset or $set will occur, depending if it's an
|
|
21
|
+
# embeds_one or embeds_many.
|
|
22
|
+
#
|
|
23
|
+
# Example:
|
|
24
|
+
#
|
|
25
|
+
# <tt>RemoveEmbedded.persist</tt>
|
|
26
|
+
#
|
|
27
|
+
# Returns:
|
|
28
|
+
#
|
|
29
|
+
# +true+ or +false+, depending on if the removal passed.
|
|
30
|
+
def persist
|
|
31
|
+
parent = @document._parent
|
|
32
|
+
parent.remove(@document)
|
|
33
|
+
unless parent.new_record?
|
|
34
|
+
update = { @document._remover => removal_selector }
|
|
35
|
+
@collection.update(parent._selector, update, @options.merge(:multi => false))
|
|
36
|
+
end; true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
# Get the value to pass to the removal modifier.
|
|
41
|
+
def setter
|
|
42
|
+
@document._index ? @document.id : true
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def removal_selector
|
|
46
|
+
@document._index ? { @document._pull => { "_id" => @document.id } } : { @document._path => setter }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|