mongoid-locomotive 2.0.0.beta9
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 +47 -0
- data/lib/mongoid.rb +141 -0
- data/lib/mongoid/associations.rb +306 -0
- data/lib/mongoid/associations/embedded_in.rb +74 -0
- data/lib/mongoid/associations/embeds_many.rb +280 -0
- data/lib/mongoid/associations/embeds_one.rb +97 -0
- data/lib/mongoid/associations/foreign_key.rb +35 -0
- data/lib/mongoid/associations/meta_data.rb +38 -0
- data/lib/mongoid/associations/options.rb +62 -0
- data/lib/mongoid/associations/proxy.rb +33 -0
- data/lib/mongoid/associations/referenced_in.rb +59 -0
- data/lib/mongoid/associations/references_many.rb +245 -0
- data/lib/mongoid/associations/references_many_as_array.rb +78 -0
- data/lib/mongoid/associations/references_one.rb +99 -0
- data/lib/mongoid/atomicity.rb +55 -0
- data/lib/mongoid/attributes.rb +242 -0
- data/lib/mongoid/callbacks.rb +21 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +71 -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 +34 -0
- data/lib/mongoid/config.rb +263 -0
- data/lib/mongoid/contexts.rb +24 -0
- data/lib/mongoid/contexts/enumerable.rb +156 -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 +248 -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 +189 -0
- data/lib/mongoid/cursor.rb +81 -0
- data/lib/mongoid/deprecation.rb +21 -0
- data/lib/mongoid/dirty.rb +252 -0
- data/lib/mongoid/document.rb +210 -0
- data/lib/mongoid/errors.rb +131 -0
- data/lib/mongoid/extensions.rb +115 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -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 +24 -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/accessors.rb +42 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +40 -0
- data/lib/mongoid/extensions/hash/conversions.rb +42 -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 +21 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -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 +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +40 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extensions/true_class/equality.rb +13 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +83 -0
- data/lib/mongoid/fields.rb +62 -0
- data/lib/mongoid/finders.rb +145 -0
- data/lib/mongoid/hierarchy.rb +74 -0
- data/lib/mongoid/identity.rb +47 -0
- data/lib/mongoid/indexes.rb +27 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/logger.rb +19 -0
- data/lib/mongoid/matchers.rb +35 -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/paranoia.rb +106 -0
- data/lib/mongoid/paths.rb +61 -0
- data/lib/mongoid/persistence.rb +216 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +48 -0
- data/lib/mongoid/persistence/insert_embedded.rb +44 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +38 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +71 -0
- data/lib/mongoid/railtie.rb +67 -0
- data/lib/mongoid/railties/database.rake +60 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +32 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +5 -0
- data/lib/mongoid/validations/uniqueness.rb +56 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +26 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +25 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- data/spec/integration/mongoid/association_attributes_spec.rb +71 -0
- data/spec/integration/mongoid/associations_spec.rb +768 -0
- data/spec/integration/mongoid/attributes_spec.rb +59 -0
- data/spec/integration/mongoid/callback_spec.rb +33 -0
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/mongoid/criteria_spec.rb +281 -0
- data/spec/integration/mongoid/dirty_spec.rb +85 -0
- data/spec/integration/mongoid/document_spec.rb +741 -0
- data/spec/integration/mongoid/extensions_spec.rb +22 -0
- data/spec/integration/mongoid/finders_spec.rb +119 -0
- data/spec/integration/mongoid/inheritance_spec.rb +171 -0
- data/spec/integration/mongoid/named_scope_spec.rb +58 -0
- data/spec/integration/mongoid/paranoia_spec.rb +44 -0
- data/spec/integration/mongoid/persistence/update_spec.rb +46 -0
- data/spec/integration/mongoid/persistence_spec.rb +311 -0
- data/spec/integration/mongoid/validations/uniqueness_spec.rb +206 -0
- data/spec/models/account.rb +5 -0
- data/spec/models/address.rb +40 -0
- data/spec/models/agent.rb +7 -0
- data/spec/models/animal.rb +15 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/callbacks.rb +47 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/comment.rb +10 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/favorite.rb +8 -0
- data/spec/models/game.rb +9 -0
- data/spec/models/inheritance.rb +72 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +106 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/post.rb +25 -0
- data/spec/models/preference.rb +7 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/survey.rb +6 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/user.rb +6 -0
- data/spec/models/user_accout.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +5 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
- data/spec/unit/mongoid/associations/embeds_many_spec.rb +626 -0
- data/spec/unit/mongoid/associations/embeds_one_spec.rb +287 -0
- data/spec/unit/mongoid/associations/foreign_key_spec.rb +90 -0
- data/spec/unit/mongoid/associations/meta_data_spec.rb +110 -0
- data/spec/unit/mongoid/associations/options_spec.rb +215 -0
- data/spec/unit/mongoid/associations/referenced_in_spec.rb +145 -0
- data/spec/unit/mongoid/associations/references_many_as_array_spec.rb +424 -0
- data/spec/unit/mongoid/associations/references_many_spec.rb +502 -0
- data/spec/unit/mongoid/associations/references_one_spec.rb +204 -0
- data/spec/unit/mongoid/associations_spec.rb +688 -0
- data/spec/unit/mongoid/atomicity_spec.rb +164 -0
- data/spec/unit/mongoid/attributes_spec.rb +646 -0
- data/spec/unit/mongoid/callbacks_spec.rb +85 -0
- data/spec/unit/mongoid/collection_spec.rb +187 -0
- data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
- data/spec/unit/mongoid/collections/master_spec.rb +41 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/collections_spec.rb +98 -0
- data/spec/unit/mongoid/config_spec.rb +298 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +447 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +703 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +873 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +17 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +121 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +274 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +483 -0
- data/spec/unit/mongoid/cursor_spec.rb +80 -0
- data/spec/unit/mongoid/deprecation_spec.rb +24 -0
- data/spec/unit/mongoid/dirty_spec.rb +430 -0
- data/spec/unit/mongoid/document_spec.rb +623 -0
- data/spec/unit/mongoid/errors_spec.rb +154 -0
- data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
- data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +52 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +36 -0
- data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +145 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/false_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
- data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +59 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +35 -0
- data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
- data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +29 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +44 -0
- data/spec/unit/mongoid/extensions/objectid/conversions_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/set/conversions_spec.rb +21 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +28 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +107 -0
- data/spec/unit/mongoid/extensions/time_conversions_spec.rb +186 -0
- data/spec/unit/mongoid/extensions/true_class/equality_spec.rb +35 -0
- data/spec/unit/mongoid/extras_spec.rb +102 -0
- data/spec/unit/mongoid/factory_spec.rb +31 -0
- data/spec/unit/mongoid/field_spec.rb +169 -0
- data/spec/unit/mongoid/fields_spec.rb +181 -0
- data/spec/unit/mongoid/finders_spec.rb +439 -0
- data/spec/unit/mongoid/hierarchy_spec.rb +68 -0
- data/spec/unit/mongoid/identity_spec.rb +109 -0
- data/spec/unit/mongoid/indexes_spec.rb +99 -0
- data/spec/unit/mongoid/javascript_spec.rb +48 -0
- data/spec/unit/mongoid/logger_spec.rb +38 -0
- data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
- data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
- data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
- data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
- data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
- data/spec/unit/mongoid/matchers_spec.rb +329 -0
- data/spec/unit/mongoid/memoization_spec.rb +75 -0
- data/spec/unit/mongoid/named_scope_spec.rb +123 -0
- data/spec/unit/mongoid/paranoia_spec.rb +108 -0
- data/spec/unit/mongoid/paths_spec.rb +272 -0
- data/spec/unit/mongoid/persistence/insert_embedded_spec.rb +154 -0
- data/spec/unit/mongoid/persistence/insert_spec.rb +144 -0
- data/spec/unit/mongoid/persistence/remove_all_spec.rb +82 -0
- data/spec/unit/mongoid/persistence/remove_embedded_spec.rb +152 -0
- data/spec/unit/mongoid/persistence/remove_spec.rb +89 -0
- data/spec/unit/mongoid/persistence/update_spec.rb +177 -0
- data/spec/unit/mongoid/persistence_spec.rb +452 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/serialization_spec.rb +43 -0
- data/spec/unit/mongoid/state_spec.rb +94 -0
- data/spec/unit/mongoid/timestamps_spec.rb +30 -0
- data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +201 -0
- data/spec/unit/mongoid/validations_spec.rb +43 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +46 -0
- metadata +433 -0
|
@@ -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,48 @@
|
|
|
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.invalid?(:create)
|
|
26
|
+
@document.run_callbacks(:create) do
|
|
27
|
+
@document.run_callbacks(:save) do
|
|
28
|
+
if insert
|
|
29
|
+
@document.new_record = false
|
|
30
|
+
@document._children.each { |child| child.new_record = false }
|
|
31
|
+
@document.move_changes
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end; @document
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
protected
|
|
38
|
+
# Insert the document into the database.
|
|
39
|
+
def insert
|
|
40
|
+
if @document.embedded?
|
|
41
|
+
Persistence::InsertEmbedded.new(@document, @validate).persist
|
|
42
|
+
else
|
|
43
|
+
@collection.insert(@document.raw_attributes, @options)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
return @document if @validate && @document.invalid?(:create)
|
|
28
|
+
parent = @document._parent
|
|
29
|
+
@document.run_callbacks(:create) do
|
|
30
|
+
@document.run_callbacks(:save) do
|
|
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
|
+
end
|
|
39
|
+
end
|
|
40
|
+
@document
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
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,38 @@
|
|
|
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
|
+
selector = (@klass.hereditary ? @selector.merge(:_type => @klass.name) : @selector)
|
|
32
|
+
count = @collection.find(selector).count
|
|
33
|
+
@collection.remove(selector, @options)
|
|
34
|
+
count
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
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
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid #:nodoc:
|
|
3
|
+
module Persistence #:nodoc:
|
|
4
|
+
# Update is a persistence command responsible for taking a document that
|
|
5
|
+
# has already been saved to the database and saving it, depending on
|
|
6
|
+
# whether or not the document has been modified.
|
|
7
|
+
#
|
|
8
|
+
# Before persisting the command will check via dirty attributes if the
|
|
9
|
+
# document has changed, if not, it will simply return true. If it has it
|
|
10
|
+
# will go through the validation steps, run callbacks, and set the changed
|
|
11
|
+
# fields atomically on the document. The underlying query resembles the
|
|
12
|
+
# following MongoDB query:
|
|
13
|
+
#
|
|
14
|
+
# collection.update(
|
|
15
|
+
# { "_id" : 1,
|
|
16
|
+
# { "$set" : { "field" : "value" },
|
|
17
|
+
# false,
|
|
18
|
+
# false
|
|
19
|
+
# );
|
|
20
|
+
#
|
|
21
|
+
# For embedded documents it will use the positional locator:
|
|
22
|
+
#
|
|
23
|
+
# collection.update(
|
|
24
|
+
# { "_id" : 1, "addresses._id" : 2 },
|
|
25
|
+
# { "$set" : { "addresses.$.field" : "value" },
|
|
26
|
+
# false,
|
|
27
|
+
# false
|
|
28
|
+
# );
|
|
29
|
+
#
|
|
30
|
+
class Update < Command
|
|
31
|
+
# Persist the document that is to be updated to the database. This will
|
|
32
|
+
# only write changed fields via MongoDB's $set modifier operation.
|
|
33
|
+
#
|
|
34
|
+
# Example:
|
|
35
|
+
#
|
|
36
|
+
# <tt>Update.persist</tt>
|
|
37
|
+
#
|
|
38
|
+
# Returns:
|
|
39
|
+
#
|
|
40
|
+
# +true+ or +false+, depending on validation.
|
|
41
|
+
def persist
|
|
42
|
+
return false if validate && @document.invalid?(:update)
|
|
43
|
+
@document.run_callbacks(:save) do
|
|
44
|
+
@document.run_callbacks(:update) do
|
|
45
|
+
if update
|
|
46
|
+
@document.move_changes
|
|
47
|
+
@document._children.each do |child|
|
|
48
|
+
child.move_changes
|
|
49
|
+
child.new_record = false if child.new_record?
|
|
50
|
+
end
|
|
51
|
+
else
|
|
52
|
+
return false
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end; true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
protected
|
|
59
|
+
# Update the document in the database atomically.
|
|
60
|
+
def update
|
|
61
|
+
updates = @document._updates
|
|
62
|
+
unless updates.empty?
|
|
63
|
+
other_pushes = updates.delete(:other)
|
|
64
|
+
|
|
65
|
+
@collection.update(@document._selector, updates, @options.merge(:multi => false))
|
|
66
|
+
@collection.update(@document._selector, { "$pushAll" => other_pushes }, @options.merge(:multi => false)) if other_pushes
|
|
67
|
+
end; true
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require "singleton"
|
|
2
|
+
require "rails"
|
|
3
|
+
require "mongoid/config"
|
|
4
|
+
module Rails #:nodoc:
|
|
5
|
+
module Mongoid #:nodoc:
|
|
6
|
+
class Railtie < Rails::Railtie #:nodoc:
|
|
7
|
+
|
|
8
|
+
config.generators.orm :mongoid, :migration => false
|
|
9
|
+
|
|
10
|
+
rake_tasks do
|
|
11
|
+
load "mongoid/railties/database.rake"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Exposes Mongoid's configuration to the Rails application configuration.
|
|
15
|
+
#
|
|
16
|
+
# Example:
|
|
17
|
+
#
|
|
18
|
+
# module MyApplication
|
|
19
|
+
# class Application < Rails::Application
|
|
20
|
+
# config.mongoid.logger = Logger.new($stdout, :warn)
|
|
21
|
+
# config.mongoid.reconnect_time = 10
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
config.mongoid = ::Mongoid::Config.instance
|
|
25
|
+
|
|
26
|
+
# Initialize Mongoid. This will look for a mongoid.yml in the config
|
|
27
|
+
# directory and configure mongoid appropriately.
|
|
28
|
+
#
|
|
29
|
+
# Example mongoid.yml:
|
|
30
|
+
#
|
|
31
|
+
# defaults: &defaults
|
|
32
|
+
# host: localhost
|
|
33
|
+
# slaves:
|
|
34
|
+
# # - host: localhost
|
|
35
|
+
# # port: 27018
|
|
36
|
+
# # - host: localhost
|
|
37
|
+
# # port: 27019
|
|
38
|
+
# allow_dynamic_fields: false
|
|
39
|
+
# parameterize_keys: false
|
|
40
|
+
# persist_in_safe_mode: false
|
|
41
|
+
#
|
|
42
|
+
# development:
|
|
43
|
+
# <<: *defaults
|
|
44
|
+
# database: mongoid
|
|
45
|
+
initializer "setup database" do
|
|
46
|
+
config_file = Rails.root.join("config", "mongoid.yml")
|
|
47
|
+
if config_file.file?
|
|
48
|
+
settings = YAML.load(ERB.new(config_file.read).result)[Rails.env]
|
|
49
|
+
::Mongoid.from_hash(settings) if settings.present?
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
initializer "verify that mongoid is configured" do
|
|
54
|
+
config.after_initialize do
|
|
55
|
+
begin
|
|
56
|
+
::Mongoid.master
|
|
57
|
+
rescue ::Mongoid::Errors::InvalidDatabase => e
|
|
58
|
+
unless Rails.root.join("config", "mongoid.yml").file?
|
|
59
|
+
puts "\nMongoid config not found. Create a config file at: config/mongoid.yml"
|
|
60
|
+
puts "to generate one run: script/rails generate mongoid:config\n\n"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
namespace :db do
|
|
2
|
+
|
|
3
|
+
if not Rake::Task.task_defined?("db:drop")
|
|
4
|
+
desc 'Drops all the collections for the database for the current Rails.env'
|
|
5
|
+
task :drop => :environment do
|
|
6
|
+
Mongoid.master.collections.each{|col| col.drop unless col.name == 'system.users' || col.name == "system.indexes" }
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
if not Rake::Task.task_defined?("db:seed")
|
|
11
|
+
# if another ORM has defined db:seed, don't run it twice.
|
|
12
|
+
desc 'Load the seed data from db/seeds.rb'
|
|
13
|
+
task :seed => :environment do
|
|
14
|
+
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
|
15
|
+
load(seed_file) if File.exist?(seed_file)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
if not Rake::Task.task_defined?("db:setup")
|
|
20
|
+
desc 'Create the database, and initialize with the seed data'
|
|
21
|
+
task :setup => [ 'db:create', 'db:seed' ]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if not Rake::Task.task_defined?("db:reseed")
|
|
25
|
+
desc 'Delete data and seed'
|
|
26
|
+
task :reseed => [ 'db:drop', 'db:seed' ]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
if not Rake::Task.task_defined?("db:create")
|
|
30
|
+
task :create => :environment do
|
|
31
|
+
# noop
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
if not Rake::Task.task_defined?("db:migrate")
|
|
36
|
+
task :migrate => :environment do
|
|
37
|
+
# noop
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
if not Rake::Task.task_defined?("db:schema:load")
|
|
42
|
+
namespace :schema do
|
|
43
|
+
task :load do
|
|
44
|
+
# noop
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
if not Rake::Task.task_defined?("db:test:prepare")
|
|
50
|
+
namespace :test do
|
|
51
|
+
task :prepare do
|
|
52
|
+
# noop
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
########
|
|
58
|
+
# TODO: lots more useful db tasks can be added here. stuff like copyDatabase, etc
|
|
59
|
+
########
|
|
60
|
+
end
|