stonegao-mongoid 2.0.0.rc.6
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/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +51 -0
- data/lib/config/locales/bg.yml +44 -0
- data/lib/config/locales/de.yml +44 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/config/locales/es.yml +44 -0
- data/lib/config/locales/fr.yml +45 -0
- data/lib/config/locales/hu.yml +47 -0
- data/lib/config/locales/it.yml +42 -0
- data/lib/config/locales/kr.yml +68 -0
- data/lib/config/locales/nl.yml +42 -0
- data/lib/config/locales/pl.yml +42 -0
- data/lib/config/locales/pt-br.yml +43 -0
- data/lib/config/locales/pt.yml +43 -0
- data/lib/config/locales/ro.yml +49 -0
- data/lib/config/locales/sv.yml +43 -0
- data/lib/config/locales/zh-CN.yml +34 -0
- data/lib/mongoid/atomicity.rb +111 -0
- data/lib/mongoid/attributes.rb +251 -0
- data/lib/mongoid/callbacks.rb +13 -0
- data/lib/mongoid/collection.rb +137 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +42 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/collections.rb +70 -0
- data/lib/mongoid/components.rb +45 -0
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/config/replset_database.rb +48 -0
- data/lib/mongoid/config.rb +343 -0
- data/lib/mongoid/contexts/enumerable/sort.rb +43 -0
- data/lib/mongoid/contexts/enumerable.rb +226 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +345 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/contexts.rb +21 -0
- data/lib/mongoid/copyable.rb +44 -0
- data/lib/mongoid/criteria.rb +325 -0
- data/lib/mongoid/criterion/complex.rb +34 -0
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/exclusion.rb +67 -0
- data/lib/mongoid/criterion/inclusion.rb +134 -0
- data/lib/mongoid/criterion/inspection.rb +20 -0
- data/lib/mongoid/criterion/optional.rb +213 -0
- data/lib/mongoid/criterion/selector.rb +74 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/default_scope.rb +28 -0
- data/lib/mongoid/dirty.rb +251 -0
- data/lib/mongoid/document.rb +256 -0
- data/lib/mongoid/errors/document_not_found.rb +29 -0
- data/lib/mongoid/errors/invalid_collection.rb +19 -0
- data/lib/mongoid/errors/invalid_database.rb +20 -0
- data/lib/mongoid/errors/invalid_field.rb +19 -0
- data/lib/mongoid/errors/invalid_options.rb +16 -0
- data/lib/mongoid/errors/invalid_type.rb +26 -0
- data/lib/mongoid/errors/mongoid_error.rb +27 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +21 -0
- data/lib/mongoid/errors/unsaved_document.rb +23 -0
- data/lib/mongoid/errors/unsupported_version.rb +21 -0
- data/lib/mongoid/errors/validations.rb +24 -0
- data/lib/mongoid/errors.rb +12 -0
- data/lib/mongoid/extensions/array/conversions.rb +23 -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 +27 -0
- data/lib/mongoid/extensions/date/conversions.rb +25 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/false_class/equality.rb +13 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/conversions.rb +19 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/conversions.rb +25 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/object_id/conversions.rb +57 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +34 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/conversions.rb +21 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +38 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extensions.rb +116 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +95 -0
- data/lib/mongoid/fields.rb +138 -0
- data/lib/mongoid/finders.rb +173 -0
- data/lib/mongoid/hierarchy.rb +85 -0
- data/lib/mongoid/identity.rb +89 -0
- data/lib/mongoid/indexes.rb +38 -0
- data/lib/mongoid/inspection.rb +58 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/json.rb +16 -0
- data/lib/mongoid/keys.rb +77 -0
- data/lib/mongoid/logger.rb +18 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +27 -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/matchers.rb +55 -0
- data/lib/mongoid/modifiers/command.rb +18 -0
- data/lib/mongoid/modifiers/inc.rb +24 -0
- data/lib/mongoid/modifiers.rb +24 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/multi_parameter_attributes.rb +80 -0
- data/lib/mongoid/named_scope.rb +36 -0
- data/lib/mongoid/nested_attributes.rb +43 -0
- data/lib/mongoid/paranoia.rb +103 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence/command.rb +59 -0
- data/lib/mongoid/persistence/insert.rb +53 -0
- data/lib/mongoid/persistence/insert_embedded.rb +42 -0
- data/lib/mongoid/persistence/remove.rb +44 -0
- data/lib/mongoid/persistence/remove_all.rb +40 -0
- data/lib/mongoid/persistence/remove_embedded.rb +48 -0
- data/lib/mongoid/persistence/update.rb +76 -0
- data/lib/mongoid/persistence.rb +237 -0
- data/lib/mongoid/railtie.rb +129 -0
- data/lib/mongoid/railties/database.rake +171 -0
- data/lib/mongoid/railties/document.rb +12 -0
- data/lib/mongoid/relations/accessors.rb +157 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +26 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +82 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +98 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +66 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +74 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +96 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +99 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +66 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/builder.rb +42 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/constraint.rb +45 -0
- data/lib/mongoid/relations/cyclic.rb +97 -0
- data/lib/mongoid/relations/embedded/in.rb +173 -0
- data/lib/mongoid/relations/embedded/many.rb +483 -0
- data/lib/mongoid/relations/embedded/one.rb +170 -0
- data/lib/mongoid/relations/macros.rb +306 -0
- data/lib/mongoid/relations/many.rb +171 -0
- data/lib/mongoid/relations/metadata.rb +533 -0
- data/lib/mongoid/relations/nested_builder.rb +68 -0
- data/lib/mongoid/relations/one.rb +47 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +128 -0
- data/lib/mongoid/relations/referenced/in.rb +216 -0
- data/lib/mongoid/relations/referenced/many.rb +443 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +344 -0
- data/lib/mongoid/relations/referenced/one.rb +206 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/relations.rb +105 -0
- data/lib/mongoid/safe.rb +23 -0
- data/lib/mongoid/safety.rb +207 -0
- data/lib/mongoid/scope.rb +31 -0
- data/lib/mongoid/serialization.rb +99 -0
- data/lib/mongoid/state.rb +66 -0
- data/lib/mongoid/timestamps.rb +38 -0
- data/lib/mongoid/validations/associated.rb +42 -0
- data/lib/mongoid/validations/uniqueness.rb +85 -0
- data/lib/mongoid/validations.rb +117 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +51 -0
- data/lib/mongoid.rb +139 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +23 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +17 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/lib/rails/mongoid.rb +57 -0
- metadata +380 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
# Include this module to get soft deletion of root level documents.
|
|
4
|
+
# This will add a deleted_at field to the +Document+, managed automatically.
|
|
5
|
+
# Potentially incompatible with unique indices. (if collisions with deleted items)
|
|
6
|
+
#
|
|
7
|
+
# To use:
|
|
8
|
+
#
|
|
9
|
+
# class Person
|
|
10
|
+
# include Mongoid::Document
|
|
11
|
+
# include Mongoid::Paranoia
|
|
12
|
+
# end
|
|
13
|
+
module Paranoia
|
|
14
|
+
extend ActiveSupport::Concern
|
|
15
|
+
|
|
16
|
+
included do
|
|
17
|
+
field :deleted_at, :type => Time
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Delete the paranoid +Document+ from the database completely. This will
|
|
21
|
+
# run the destroy callbacks.
|
|
22
|
+
#
|
|
23
|
+
# Example:
|
|
24
|
+
#
|
|
25
|
+
# <tt>document.destroy!</tt>
|
|
26
|
+
def destroy!
|
|
27
|
+
run_callbacks(:destroy) { delete! }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Delete the paranoid +Document+ from the database completely.
|
|
31
|
+
#
|
|
32
|
+
# Example:
|
|
33
|
+
#
|
|
34
|
+
# <tt>document.delete!</tt>
|
|
35
|
+
def delete!
|
|
36
|
+
@destroyed = true
|
|
37
|
+
Mongoid::Persistence::Remove.new(self).persist
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Delete the +Document+, will set the deleted_at timestamp and not actually
|
|
41
|
+
# delete it.
|
|
42
|
+
#
|
|
43
|
+
# Example:
|
|
44
|
+
#
|
|
45
|
+
# <tt>document._remove</tt>
|
|
46
|
+
#
|
|
47
|
+
# Returns:
|
|
48
|
+
#
|
|
49
|
+
# true
|
|
50
|
+
def _remove(options = {})
|
|
51
|
+
now = Time.now
|
|
52
|
+
collection.update({ :_id => self.id }, { '$set' => { :deleted_at => Time.now } })
|
|
53
|
+
@attributes["deleted_at"] = now
|
|
54
|
+
true
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
alias :delete :_remove
|
|
58
|
+
|
|
59
|
+
# Determines if this document is destroyed.
|
|
60
|
+
#
|
|
61
|
+
# Returns:
|
|
62
|
+
#
|
|
63
|
+
# true if the +Document+ was destroyed.
|
|
64
|
+
def destroyed?
|
|
65
|
+
@destroyed || !!deleted_at
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Restores a previously soft-deleted document. Handles this by removing the
|
|
69
|
+
# deleted_at flag.
|
|
70
|
+
#
|
|
71
|
+
# Example:
|
|
72
|
+
#
|
|
73
|
+
# <tt>document.restore</tt>
|
|
74
|
+
def restore
|
|
75
|
+
collection.update({ :_id => self.id }, { '$unset' => { :deleted_at => true } })
|
|
76
|
+
@attributes.delete("deleted_at")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
module ClassMethods #:nodoc:
|
|
80
|
+
|
|
81
|
+
# Override the default +Criteria+ accessor to only get existing
|
|
82
|
+
# documents.
|
|
83
|
+
#
|
|
84
|
+
# Returns:
|
|
85
|
+
#
|
|
86
|
+
# A +Criteria+ for deleted_at not existing.
|
|
87
|
+
def criteria(embedded = false)
|
|
88
|
+
super.where(:deleted_at.exists => false)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Find deleted documents
|
|
92
|
+
#
|
|
93
|
+
# Examples:
|
|
94
|
+
#
|
|
95
|
+
# <tt>Person.deleted</tt> # all deleted employees
|
|
96
|
+
# <tt>Company.first.employees.deleted</tt> # works with a join
|
|
97
|
+
# <tt>Person.deleted.find("4c188dea7b17235a2a000001").first</tt>
|
|
98
|
+
def deleted
|
|
99
|
+
where(:deleted_at.exists => true)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Paths #:nodoc:
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
included do
|
|
6
|
+
cattr_accessor :__path
|
|
7
|
+
attr_accessor :_index
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Get the insertion modifier for the document. Will be nil on root
|
|
11
|
+
# documents, $set on embeds_one, $push on embeds_many.
|
|
12
|
+
#
|
|
13
|
+
# Example:
|
|
14
|
+
#
|
|
15
|
+
# <tt>name.inserter</tt>
|
|
16
|
+
def _inserter
|
|
17
|
+
embedded? ? (embedded_many? ? "$push" : "$set") : nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Return the path to this +Document+ in JSON notation, used for atomic
|
|
21
|
+
# updates via $set in MongoDB.
|
|
22
|
+
#
|
|
23
|
+
# Example:
|
|
24
|
+
#
|
|
25
|
+
# <tt>address.path # returns "addresses"</tt>
|
|
26
|
+
def _path
|
|
27
|
+
_position.sub!(/\.\d+$/, '') || _position
|
|
28
|
+
end
|
|
29
|
+
alias :_pull :_path
|
|
30
|
+
|
|
31
|
+
# Returns the positional operator of this document for modification.
|
|
32
|
+
#
|
|
33
|
+
# Example:
|
|
34
|
+
#
|
|
35
|
+
# <tt>address.position</tt>
|
|
36
|
+
def _position
|
|
37
|
+
locator = _index ? (new_record? ? "" : ".#{_index}") : ""
|
|
38
|
+
embedded? ? "#{_parent._position}#{"." unless _parent._position.blank?}#{metadata.name.to_s}#{locator}" : ""
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Get the removal modifier for the document. Will be nil on root
|
|
42
|
+
# documents, $unset on embeds_one, $set on embeds_many.
|
|
43
|
+
#
|
|
44
|
+
# Example:
|
|
45
|
+
#
|
|
46
|
+
# <tt>name.remover</tt>
|
|
47
|
+
def _remover
|
|
48
|
+
embedded? ? (_index ? "$pull" : "$unset") : nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Return the selector for this document to be matched exactly for use
|
|
52
|
+
# with MongoDB's $ operator.
|
|
53
|
+
#
|
|
54
|
+
# Example:
|
|
55
|
+
#
|
|
56
|
+
# <tt>address.selector</tt>
|
|
57
|
+
def _selector
|
|
58
|
+
embedded? ? _parent._selector.merge("#{_path}._id" => id) : { "_id" => id }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
include Mongoid::Safe
|
|
8
|
+
|
|
9
|
+
attr_reader \
|
|
10
|
+
:collection,
|
|
11
|
+
:document,
|
|
12
|
+
:klass,
|
|
13
|
+
:options,
|
|
14
|
+
:selector,
|
|
15
|
+
:validate
|
|
16
|
+
|
|
17
|
+
# Initialize the persistence +Command+.
|
|
18
|
+
#
|
|
19
|
+
# Options:
|
|
20
|
+
#
|
|
21
|
+
# document_or_class: The +Document+ or +Class+ to get the collection.
|
|
22
|
+
# options: Options like validation or safe mode.
|
|
23
|
+
# selector: Optional selector to use in query.
|
|
24
|
+
#
|
|
25
|
+
# Example:
|
|
26
|
+
#
|
|
27
|
+
# <tt>DeleteAll.new(Person, { :validate => true }, {})</tt>
|
|
28
|
+
def initialize(document_or_class, options = {}, selector = {})
|
|
29
|
+
init(document_or_class)
|
|
30
|
+
validate = options[:validate]
|
|
31
|
+
@validate = (validate.nil? ? true : validate)
|
|
32
|
+
@selector = selector
|
|
33
|
+
@options = { :safe => safe_mode?(options) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
# Setup the proper instance variables based on if the supplied argument
|
|
39
|
+
# was a document object or a class object.
|
|
40
|
+
#
|
|
41
|
+
# Example:
|
|
42
|
+
#
|
|
43
|
+
# <tt>init(document_or_class)</tt>
|
|
44
|
+
#
|
|
45
|
+
# Options:
|
|
46
|
+
#
|
|
47
|
+
# document_or_class: A document or a class.
|
|
48
|
+
def init(document_or_class)
|
|
49
|
+
if document_or_class.is_a?(Mongoid::Document)
|
|
50
|
+
@document = document_or_class
|
|
51
|
+
@collection = @document.embedded? ? @document._root.collection : @document.collection
|
|
52
|
+
else
|
|
53
|
+
@klass = document_or_class
|
|
54
|
+
@collection = @klass.collection
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Insert is a persistence command responsible for taking a document that
|
|
6
|
+
# has not been saved to the database and saving it.
|
|
7
|
+
#
|
|
8
|
+
# The underlying query resembles the following MongoDB query:
|
|
9
|
+
#
|
|
10
|
+
# collection.insert(
|
|
11
|
+
# { "_id" : 1, "field" : "value" },
|
|
12
|
+
# false
|
|
13
|
+
# );
|
|
14
|
+
class Insert < Command
|
|
15
|
+
|
|
16
|
+
# Insert the new document in the database. This delegates to the standard
|
|
17
|
+
# MongoDB collection's insert command.
|
|
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
|
+
return document if validate && document.invalid?(:create)
|
|
28
|
+
document.run_callbacks(:create) do
|
|
29
|
+
document.run_callbacks(:save) do
|
|
30
|
+
if insert
|
|
31
|
+
document.new_record = false
|
|
32
|
+
document._children.each { |child| child.new_record = false }
|
|
33
|
+
document.move_changes
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end; document
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
# Insert the document into the database.
|
|
41
|
+
def insert
|
|
42
|
+
if document.embedded?
|
|
43
|
+
Persistence::InsertEmbedded.new(
|
|
44
|
+
document,
|
|
45
|
+
options.merge(:validate => validate)
|
|
46
|
+
).persist
|
|
47
|
+
else
|
|
48
|
+
collection.insert(document.to_hash, options)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Insert is a persistence command responsible for taking a document that
|
|
6
|
+
# has not been saved to the database and saving it. This specific class
|
|
7
|
+
# handles the case when the document is embedded in another.
|
|
8
|
+
#
|
|
9
|
+
# The underlying query resembles the following MongoDB query:
|
|
10
|
+
#
|
|
11
|
+
# collection.insert(
|
|
12
|
+
# { "_id" : 1, "field" : "value" },
|
|
13
|
+
# false
|
|
14
|
+
# );
|
|
15
|
+
class InsertEmbedded < Command
|
|
16
|
+
|
|
17
|
+
# Insert the new document in the database. If the document's parent is a
|
|
18
|
+
# new record, we will call save on the parent, otherwise we will $push
|
|
19
|
+
# the document onto the parent.
|
|
20
|
+
#
|
|
21
|
+
# Example:
|
|
22
|
+
#
|
|
23
|
+
# <tt>Insert.persist</tt>
|
|
24
|
+
#
|
|
25
|
+
# Returns:
|
|
26
|
+
#
|
|
27
|
+
# The +Document+, whether the insert succeeded or not.
|
|
28
|
+
def persist
|
|
29
|
+
return document if validate && document.invalid?(:create)
|
|
30
|
+
parent = document._parent
|
|
31
|
+
if parent.new_record?
|
|
32
|
+
parent.insert
|
|
33
|
+
else
|
|
34
|
+
update = { document._inserter => { document._position => document.raw_attributes } }
|
|
35
|
+
collection.update(parent._selector, update, options.merge(:multi => false))
|
|
36
|
+
document.new_record = false
|
|
37
|
+
end
|
|
38
|
+
document
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Remove is a persistence command responsible for deleting a document from
|
|
6
|
+
# the database.
|
|
7
|
+
#
|
|
8
|
+
# The underlying query resembles the following MongoDB query:
|
|
9
|
+
#
|
|
10
|
+
# collection.remove(
|
|
11
|
+
# { "_id" : 1 },
|
|
12
|
+
# false
|
|
13
|
+
# );
|
|
14
|
+
class Remove < Command
|
|
15
|
+
|
|
16
|
+
# Remove the document from the database: delegates to the MongoDB
|
|
17
|
+
# collection remove method.
|
|
18
|
+
#
|
|
19
|
+
# Example:
|
|
20
|
+
#
|
|
21
|
+
# <tt>Remove.persist</tt>
|
|
22
|
+
#
|
|
23
|
+
# Returns:
|
|
24
|
+
#
|
|
25
|
+
# +true+ if success, +false+ if not.
|
|
26
|
+
def persist
|
|
27
|
+
remove
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
protected
|
|
31
|
+
# Remove the document from the database.
|
|
32
|
+
def remove
|
|
33
|
+
if document.embedded?
|
|
34
|
+
Persistence::RemoveEmbedded.new(
|
|
35
|
+
document,
|
|
36
|
+
options.merge(:validate => validate)
|
|
37
|
+
).persist
|
|
38
|
+
else
|
|
39
|
+
collection.remove({ :_id => document.id }, options)
|
|
40
|
+
end; true
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Remove is a persistence command responsible for deleting a document from
|
|
6
|
+
# the database.
|
|
7
|
+
#
|
|
8
|
+
# The underlying query resembles the following MongoDB query:
|
|
9
|
+
#
|
|
10
|
+
# collection.remove(
|
|
11
|
+
# { "field" : value },
|
|
12
|
+
# false
|
|
13
|
+
# );
|
|
14
|
+
class RemoveAll < Command
|
|
15
|
+
|
|
16
|
+
# Remove the document from the database: delegates to the MongoDB
|
|
17
|
+
# collection remove method.
|
|
18
|
+
#
|
|
19
|
+
# Example:
|
|
20
|
+
#
|
|
21
|
+
# <tt>Remove.persist</tt>
|
|
22
|
+
#
|
|
23
|
+
# Returns:
|
|
24
|
+
#
|
|
25
|
+
# +true+ if success, +false+ if not.
|
|
26
|
+
def persist
|
|
27
|
+
remove
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
protected
|
|
31
|
+
# Remove the document from the database.
|
|
32
|
+
def remove
|
|
33
|
+
select = (klass.hereditary? ? selector.merge(:_type => klass.name) : selector)
|
|
34
|
+
collection.find(select).count.tap do
|
|
35
|
+
collection.remove(select, options)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Remove is a persistence command responsible for deleting a document from
|
|
6
|
+
# the database.
|
|
7
|
+
#
|
|
8
|
+
# The underlying query resembles the following MongoDB query:
|
|
9
|
+
#
|
|
10
|
+
# collection.remove(
|
|
11
|
+
# { "_id" : 1 },
|
|
12
|
+
# false
|
|
13
|
+
# );
|
|
14
|
+
class RemoveEmbedded < Command
|
|
15
|
+
|
|
16
|
+
# Remove the document from the database. If the parent is a new record,
|
|
17
|
+
# it will get removed in Ruby only. If the parent is not a new record
|
|
18
|
+
# then either an $unset or $set will occur, depending if it's an
|
|
19
|
+
# embeds_one or embeds_many.
|
|
20
|
+
#
|
|
21
|
+
# Example:
|
|
22
|
+
#
|
|
23
|
+
# <tt>RemoveEmbedded.persist</tt>
|
|
24
|
+
#
|
|
25
|
+
# Returns:
|
|
26
|
+
#
|
|
27
|
+
# +true+ or +false+, depending on if the removal passed.
|
|
28
|
+
def persist
|
|
29
|
+
parent = document._parent
|
|
30
|
+
parent.remove_child(document)
|
|
31
|
+
unless parent.new_record?
|
|
32
|
+
update = { document._remover => removal_selector }
|
|
33
|
+
collection.update(parent._selector, update, options.merge(:multi => false))
|
|
34
|
+
end; true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
protected
|
|
38
|
+
# Get the value to pass to the removal modifier.
|
|
39
|
+
def setter
|
|
40
|
+
document._index ? document.id : true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def removal_selector
|
|
44
|
+
document._index ? { document._pull => { "_id" => document.id } } : { document._path => setter }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Update is a persistence command responsible for taking a document that
|
|
6
|
+
# has already been saved to the database and saving it, depending on
|
|
7
|
+
# whether or not the document has been modified.
|
|
8
|
+
#
|
|
9
|
+
# Before persisting the command will check via dirty attributes if the
|
|
10
|
+
# document has changed, if not, it will simply return true. If it has it
|
|
11
|
+
# will go through the validation steps, run callbacks, and set the changed
|
|
12
|
+
# fields atomically on the document. The underlying query resembles the
|
|
13
|
+
# following MongoDB query:
|
|
14
|
+
#
|
|
15
|
+
# collection.update(
|
|
16
|
+
# { "_id" : 1,
|
|
17
|
+
# { "$set" : { "field" : "value" },
|
|
18
|
+
# false,
|
|
19
|
+
# false
|
|
20
|
+
# );
|
|
21
|
+
#
|
|
22
|
+
# For embedded documents it will use the positional locator:
|
|
23
|
+
#
|
|
24
|
+
# collection.update(
|
|
25
|
+
# { "_id" : 1, "addresses._id" : 2 },
|
|
26
|
+
# { "$set" : { "addresses.$.field" : "value" },
|
|
27
|
+
# false,
|
|
28
|
+
# false
|
|
29
|
+
# );
|
|
30
|
+
#
|
|
31
|
+
class Update < Command
|
|
32
|
+
|
|
33
|
+
# Persist the document that is to be updated to the database. This will
|
|
34
|
+
# only write changed fields via MongoDB's $set modifier operation.
|
|
35
|
+
#
|
|
36
|
+
# Example:
|
|
37
|
+
#
|
|
38
|
+
# <tt>Update.persist</tt>
|
|
39
|
+
#
|
|
40
|
+
# Returns:
|
|
41
|
+
#
|
|
42
|
+
# +true+ or +false+, depending on validation.
|
|
43
|
+
def persist
|
|
44
|
+
return false if validate && document.invalid?(:update)
|
|
45
|
+
document.run_callbacks(:save) do
|
|
46
|
+
document.run_callbacks(:update) do
|
|
47
|
+
if update
|
|
48
|
+
document.move_changes
|
|
49
|
+
document._children.each do |child|
|
|
50
|
+
child.move_changes
|
|
51
|
+
child.new_record = false if child.new_record?
|
|
52
|
+
end
|
|
53
|
+
else
|
|
54
|
+
return false
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end; true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
protected
|
|
61
|
+
# Update the document in the database atomically.
|
|
62
|
+
def update
|
|
63
|
+
updates = document._updates
|
|
64
|
+
unless updates.empty?
|
|
65
|
+
other_pushes = updates.delete(:other)
|
|
66
|
+
collection.update(document._selector, updates, options.merge(:multi => false))
|
|
67
|
+
collection.update(
|
|
68
|
+
document._selector,
|
|
69
|
+
{ "$pushAll" => other_pushes },
|
|
70
|
+
options.merge(:multi => false)
|
|
71
|
+
) if other_pushes
|
|
72
|
+
end; true
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|