mongoid 1.9.5 → 2.0.0.alpha
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/.gitignore +6 -0
- data/.watchr +29 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/caliper.yml +4 -0
- data/lib/mongoid.rb +20 -9
- data/lib/mongoid/associations.rb +100 -123
- data/lib/mongoid/associations/belongs_to_related.rb +3 -2
- data/lib/mongoid/associations/{embeds_many.rb → embed_many.rb} +29 -90
- data/lib/mongoid/associations/{embeds_one.rb → embed_one.rb} +7 -8
- data/lib/mongoid/associations/embedded_in.rb +4 -12
- data/lib/mongoid/associations/has_many_related.rb +4 -52
- data/lib/mongoid/associations/has_one_related.rb +4 -8
- data/lib/mongoid/associations/meta_data.rb +1 -2
- data/lib/mongoid/associations/options.rb +1 -6
- data/lib/mongoid/associations/proxy.rb +21 -14
- data/lib/mongoid/attributes.rb +13 -33
- data/lib/mongoid/callbacks.rb +11 -16
- data/lib/mongoid/collection.rb +3 -4
- data/lib/mongoid/collections/master.rb +2 -3
- data/lib/mongoid/collections/mimic.rb +46 -0
- data/lib/mongoid/collections/slaves.rb +2 -3
- data/lib/mongoid/commands.rb +161 -0
- data/lib/mongoid/commands/create.rb +19 -0
- data/lib/mongoid/commands/delete.rb +16 -0
- data/lib/mongoid/commands/delete_all.rb +23 -0
- data/lib/mongoid/commands/deletion.rb +18 -0
- data/lib/mongoid/commands/destroy.rb +17 -0
- data/lib/mongoid/commands/destroy_all.rb +23 -0
- data/lib/mongoid/commands/save.rb +29 -0
- data/lib/mongoid/components.rb +6 -4
- data/lib/mongoid/config.rb +15 -134
- data/lib/mongoid/contexts.rb +1 -1
- data/lib/mongoid/contexts/enumerable.rb +1 -1
- data/lib/mongoid/contexts/mongo.rb +1 -1
- data/lib/mongoid/contexts/paging.rb +2 -10
- data/lib/mongoid/criterion/inclusion.rb +0 -17
- data/lib/mongoid/criterion/optional.rb +1 -1
- data/lib/mongoid/cursor.rb +1 -0
- data/lib/mongoid/document.rb +45 -49
- data/lib/mongoid/errors.rb +1 -32
- data/lib/mongoid/extensions.rb +10 -12
- data/lib/mongoid/extensions/array/conversions.rb +6 -8
- data/lib/mongoid/extensions/big_decimal/conversions.rb +2 -2
- data/lib/mongoid/extensions/boolean/conversions.rb +2 -8
- data/lib/mongoid/extensions/date/conversions.rb +4 -13
- data/lib/mongoid/extensions/datetime/conversions.rb +6 -1
- data/lib/mongoid/extensions/float/conversions.rb +1 -5
- data/lib/mongoid/extensions/hash/assimilation.rb +3 -12
- data/lib/mongoid/extensions/hash/conversions.rb +4 -34
- data/lib/mongoid/extensions/integer/conversions.rb +1 -5
- data/lib/mongoid/extensions/nil/assimilation.rb +0 -4
- data/lib/mongoid/extensions/object/conversions.rb +2 -8
- data/lib/mongoid/extensions/objectid/conversions.rb +1 -1
- data/lib/mongoid/extensions/string/conversions.rb +1 -1
- data/lib/mongoid/extensions/symbol/inflections.rb +1 -1
- data/lib/mongoid/extensions/time/conversions.rb +18 -0
- data/lib/mongoid/factory.rb +1 -2
- data/lib/mongoid/field.rb +2 -9
- data/lib/mongoid/fields.rb +7 -11
- data/lib/mongoid/finders.rb +2 -2
- data/lib/mongoid/identity.rb +4 -4
- data/lib/mongoid/indexes.rb +7 -10
- data/lib/mongoid/memoization.rb +2 -8
- data/lib/mongoid/named_scope.rb +5 -0
- data/lib/mongoid/observable.rb +1 -1
- data/lib/mongoid/paths.rb +22 -30
- data/lib/mongoid/state.rb +21 -28
- data/lib/mongoid/timestamps.rb +1 -1
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +4 -0
- data/lib/mongoid/validations/uniqueness.rb +22 -0
- data/lib/mongoid/versioning.rb +1 -2
- data/mongoid.gemspec +408 -0
- data/perf/benchmark.rb +77 -0
- data/spec/integration/mongoid/associations_spec.rb +340 -0
- data/spec/integration/mongoid/attributes_spec.rb +22 -0
- data/spec/integration/mongoid/commands_spec.rb +227 -0
- data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
- data/spec/integration/mongoid/criteria_spec.rb +272 -0
- data/spec/integration/mongoid/document_spec.rb +650 -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 +137 -0
- data/spec/integration/mongoid/named_scope_spec.rb +46 -0
- data/spec/models/address.rb +39 -0
- data/spec/models/animal.rb +6 -0
- data/spec/models/callbacks.rb +18 -0
- data/spec/models/comment.rb +8 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/game.rb +7 -0
- data/spec/models/inheritance.rb +56 -0
- data/spec/models/location.rb +5 -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/patient.rb +4 -0
- data/spec/models/person.rb +99 -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 +15 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +145 -0
- data/spec/unit/mongoid/associations/embed_many_spec.rb +516 -0
- data/spec/unit/mongoid/associations/embed_one_spec.rb +282 -0
- data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
- data/spec/unit/mongoid/associations/has_many_related_spec.rb +418 -0
- data/spec/unit/mongoid/associations/has_one_related_spec.rb +179 -0
- data/spec/unit/mongoid/associations/meta_data_spec.rb +88 -0
- data/spec/unit/mongoid/associations/options_spec.rb +192 -0
- data/spec/unit/mongoid/associations_spec.rb +595 -0
- data/spec/unit/mongoid/attributes_spec.rb +507 -0
- data/spec/unit/mongoid/callbacks_spec.rb +55 -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/mimic_spec.rb +43 -0
- data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
- data/spec/unit/mongoid/commands/create_spec.rb +31 -0
- data/spec/unit/mongoid/commands/delete_all_spec.rb +58 -0
- data/spec/unit/mongoid/commands/delete_spec.rb +38 -0
- data/spec/unit/mongoid/commands/destroy_all_spec.rb +21 -0
- data/spec/unit/mongoid/commands/destroy_spec.rb +51 -0
- data/spec/unit/mongoid/commands/save_spec.rb +107 -0
- data/spec/unit/mongoid/commands_spec.rb +270 -0
- data/spec/unit/mongoid/config_spec.rb +172 -0
- data/spec/unit/mongoid/contexts/enumerable_spec.rb +421 -0
- data/spec/unit/mongoid/contexts/mongo_spec.rb +682 -0
- data/spec/unit/mongoid/contexts_spec.rb +25 -0
- data/spec/unit/mongoid/criteria_spec.rb +824 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +19 -0
- data/spec/unit/mongoid/criterion/exclusion_spec.rb +91 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +219 -0
- data/spec/unit/mongoid/criterion/optional_spec.rb +319 -0
- data/spec/unit/mongoid/cursor_spec.rb +74 -0
- data/spec/unit/mongoid/deprecation_spec.rb +24 -0
- data/spec/unit/mongoid/document_spec.rb +818 -0
- data/spec/unit/mongoid/errors_spec.rb +103 -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 +35 -0
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
- data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +22 -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 +102 -0
- data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +67 -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 +46 -0
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +21 -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 +24 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +57 -0
- data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +17 -0
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +91 -0
- data/spec/unit/mongoid/extensions/time/conversions_spec.rb +70 -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 +143 -0
- data/spec/unit/mongoid/fields_spec.rb +181 -0
- data/spec/unit/mongoid/finders_spec.rb +404 -0
- data/spec/unit/mongoid/identity_spec.rb +109 -0
- data/spec/unit/mongoid/indexes_spec.rb +93 -0
- data/spec/unit/mongoid/javascript_spec.rb +48 -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/observable_spec.rb +46 -0
- data/spec/unit/mongoid/paths_spec.rb +124 -0
- data/spec/unit/mongoid/scope_spec.rb +240 -0
- data/spec/unit/mongoid/state_spec.rb +83 -0
- data/spec/unit/mongoid/timestamps_spec.rb +25 -0
- data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
- data/spec/unit/mongoid/validations/uniqueness_spec.rb +47 -0
- data/spec/unit/mongoid/validations_spec.rb +190 -0
- data/spec/unit/mongoid/versioning_spec.rb +41 -0
- data/spec/unit/mongoid_spec.rb +46 -0
- metadata +316 -110
- data/lib/mongoid/collections.rb +0 -41
- data/lib/mongoid/concern.rb +0 -31
- data/lib/mongoid/dirty.rb +0 -253
- data/lib/mongoid/extensions/time_conversions.rb +0 -35
- data/lib/mongoid/persistence.rb +0 -222
- data/lib/mongoid/persistence/command.rb +0 -39
- data/lib/mongoid/persistence/insert.rb +0 -50
- data/lib/mongoid/persistence/insert_embedded.rb +0 -38
- data/lib/mongoid/persistence/remove.rb +0 -39
- data/lib/mongoid/persistence/remove_all.rb +0 -37
- data/lib/mongoid/persistence/remove_embedded.rb +0 -50
- data/lib/mongoid/persistence/update.rb +0 -63
- data/lib/mongoid/version.rb +0 -4
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Commands
|
4
|
+
class Create
|
5
|
+
# Performs a create of the supplied Document, with the necessary
|
6
|
+
# callbacks. It then delegates to the Save command.
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
#
|
10
|
+
# doc: A new +Document+ that is going to be persisted.
|
11
|
+
#
|
12
|
+
# Returns: +Document+.
|
13
|
+
def self.execute(doc, validate = true)
|
14
|
+
doc.run_callbacks(:create) { Save.execute(doc, validate) }
|
15
|
+
doc
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Commands
|
4
|
+
class Delete
|
5
|
+
extend Deletion
|
6
|
+
# Performs a delete of the supplied +Document+ without any callbacks.
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
#
|
10
|
+
# doc: A new +Document+ that is going to be deleted.
|
11
|
+
def self.execute(doc)
|
12
|
+
doc.destroyed = true if delete(doc)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Commands
|
4
|
+
class DeleteAll
|
5
|
+
# Performs a delete of the all the +Documents+ that match the criteria
|
6
|
+
# supplied.
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
#
|
10
|
+
# params: A set of conditions to find the +Documents+ by.
|
11
|
+
# klass: The class of the +Document+ to execute the find on.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
#
|
15
|
+
# <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt>
|
16
|
+
def self.execute(klass, params = {})
|
17
|
+
safe = Mongoid.persist_in_safe_mode
|
18
|
+
collection = klass.collection
|
19
|
+
collection.remove((params[:conditions] || {}).merge(:_type => klass.name), :safe => safe)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc
|
3
|
+
module Commands #:nodoc
|
4
|
+
module Deletion #:nodoc
|
5
|
+
# If the +Document+ has a parent, delete it from the parent's attributes,
|
6
|
+
# otherwise delete it from it's collection.
|
7
|
+
def delete(doc)
|
8
|
+
parent = doc._parent
|
9
|
+
if parent
|
10
|
+
parent.remove(doc)
|
11
|
+
parent.save
|
12
|
+
else
|
13
|
+
doc.collection.remove(:_id => doc.id)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Commands
|
4
|
+
class Destroy
|
5
|
+
extend Deletion
|
6
|
+
# Performs a destroy of the supplied +Document+, with the necessary
|
7
|
+
# callbacks. It then deletes the record from the collection.
|
8
|
+
#
|
9
|
+
# Options:
|
10
|
+
#
|
11
|
+
# doc: A new +Document+ that is going to be destroyed.
|
12
|
+
def self.execute(doc)
|
13
|
+
doc.run_callbacks(:destroy) { doc.destroyed = true if delete(doc) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Commands
|
4
|
+
class DestroyAll
|
5
|
+
# Performs a destroy of the all the +Documents+ that match the criteria
|
6
|
+
# supplied. Will execute all the destroy callbacks for each +Document+.
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
#
|
10
|
+
# params: A set of conditions to find the +Documents+ by.
|
11
|
+
# klass: The class of the +Document+ to execute the find on.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
#
|
15
|
+
# <tt>DestroyAll.execute(Person, :conditions => { :field => "value" })</tt>
|
16
|
+
def self.execute(klass, params)
|
17
|
+
conditions = params[:conditions] || {}
|
18
|
+
params[:conditions] = conditions.merge(:_type => klass.name)
|
19
|
+
klass.find(:all, params).each { |doc| Destroy.execute(doc) }; true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Commands
|
4
|
+
class Save
|
5
|
+
# Performs a save of the supplied +Document+, handling all associated
|
6
|
+
# callbacks and validation.
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
#
|
10
|
+
# doc: A +Document+ that is going to be persisted.
|
11
|
+
#
|
12
|
+
# Returns: +true+ if validation passes, +false+ if not.
|
13
|
+
def self.execute(doc, validate = true)
|
14
|
+
return false if validate && !doc.valid?
|
15
|
+
doc.run_callbacks :save do
|
16
|
+
parent = doc._parent
|
17
|
+
doc.new_record = false
|
18
|
+
saved = if parent
|
19
|
+
Save.execute(parent, validate)
|
20
|
+
else
|
21
|
+
doc.collection.save(doc.raw_attributes, :safe => Mongoid.persist_in_safe_mode)
|
22
|
+
end
|
23
|
+
return false unless saved
|
24
|
+
end
|
25
|
+
return true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/mongoid/components.rb
CHANGED
@@ -5,11 +5,13 @@ module Mongoid #:nodoc
|
|
5
5
|
included do
|
6
6
|
# All modules that a +Document+ is composed of are defined in this
|
7
7
|
# module, to keep the document class from getting too cluttered.
|
8
|
+
include ActiveModel::Conversion
|
9
|
+
include ActiveModel::Naming
|
10
|
+
include ActiveModel::Serialization
|
8
11
|
include Mongoid::Associations
|
9
12
|
include Mongoid::Attributes
|
10
13
|
include Mongoid::Callbacks
|
11
|
-
include Mongoid::
|
12
|
-
include Mongoid::Dirty
|
14
|
+
include Mongoid::Commands
|
13
15
|
include Mongoid::Extras
|
14
16
|
include Mongoid::Fields
|
15
17
|
include Mongoid::Indexes
|
@@ -17,9 +19,9 @@ module Mongoid #:nodoc
|
|
17
19
|
include Mongoid::Memoization
|
18
20
|
include Mongoid::Observable
|
19
21
|
include Mongoid::Paths
|
20
|
-
include Mongoid::Persistence
|
21
22
|
include Mongoid::State
|
22
|
-
include
|
23
|
+
include Mongoid::Validations
|
24
|
+
extend ActiveModel::Translation
|
23
25
|
extend Mongoid::Finders
|
24
26
|
extend Mongoid::NamedScope
|
25
27
|
end
|
data/lib/mongoid/config.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require "uri"
|
3
|
-
|
4
2
|
module Mongoid #:nodoc
|
5
3
|
class Config #:nodoc
|
6
4
|
include Singleton
|
@@ -10,44 +8,23 @@ module Mongoid #:nodoc
|
|
10
8
|
:reconnect_time,
|
11
9
|
:parameterize_keys,
|
12
10
|
:persist_in_safe_mode,
|
11
|
+
:persist_types,
|
13
12
|
:raise_not_found_error,
|
14
13
|
:use_object_ids
|
15
14
|
|
16
15
|
# Defaults the configuration options to true.
|
17
16
|
def initialize
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
# Example:
|
26
|
-
#
|
27
|
-
# <tt>Config.use_utc = true</tt>
|
28
|
-
#
|
29
|
-
# Returns:
|
30
|
-
#
|
31
|
-
# A boolean
|
32
|
-
def use_utc=(value)
|
33
|
-
@use_utc = value || false
|
17
|
+
@allow_dynamic_fields = true
|
18
|
+
@parameterize_keys = true
|
19
|
+
@persist_in_safe_mode = true
|
20
|
+
@persist_types = true
|
21
|
+
@raise_not_found_error = true
|
22
|
+
@reconnect_time = 3
|
23
|
+
@use_object_ids = false
|
34
24
|
end
|
35
25
|
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
# Example:
|
40
|
-
#
|
41
|
-
# <tt>Config.use_utc</tt>
|
42
|
-
#
|
43
|
-
# Returns:
|
44
|
-
#
|
45
|
-
# A boolean
|
46
|
-
attr_reader :use_utc
|
47
|
-
alias_method :use_utc?, :use_utc
|
48
|
-
|
49
|
-
# Sets the Mongo::DB master database to be used. If the object trying to be
|
50
|
-
# set is not a valid +Mongo::DB+, then an error will be raised.
|
26
|
+
# Sets the Mongo::DB master database to be used. If the object trying to me
|
27
|
+
# set is not a valid +Mongo::DB+, then an error will be raise.
|
51
28
|
#
|
52
29
|
# Example:
|
53
30
|
#
|
@@ -57,7 +34,7 @@ module Mongoid #:nodoc
|
|
57
34
|
#
|
58
35
|
# The Master DB instance.
|
59
36
|
def master=(db)
|
60
|
-
|
37
|
+
raise Errors::InvalidDatabase.new(db) unless db.kind_of?(Mongo::DB)
|
61
38
|
@master = db
|
62
39
|
end
|
63
40
|
|
@@ -78,8 +55,8 @@ module Mongoid #:nodoc
|
|
78
55
|
alias :database :master
|
79
56
|
alias :database= :master=
|
80
57
|
|
81
|
-
# Sets the Mongo::DB slave databases to be used. If the objects
|
82
|
-
# are not valid +Mongo::DBs
|
58
|
+
# Sets the Mongo::DB slave databases to be used. If the objects trying to me
|
59
|
+
# set are not valid +Mongo::DBs+, then an error will be raise.
|
83
60
|
#
|
84
61
|
# Example:
|
85
62
|
#
|
@@ -87,12 +64,9 @@ module Mongoid #:nodoc
|
|
87
64
|
#
|
88
65
|
# Returns:
|
89
66
|
#
|
90
|
-
# The
|
67
|
+
# The slaves DB instances.
|
91
68
|
def slaves=(dbs)
|
92
|
-
|
93
|
-
dbs.each do |db|
|
94
|
-
check_database!(db)
|
95
|
-
end
|
69
|
+
dbs.each { |db| raise Errors::InvalidDatabase.new(db) unless db.kind_of?(Mongo::DB) }
|
96
70
|
@slaves = dbs
|
97
71
|
end
|
98
72
|
|
@@ -108,98 +82,5 @@ module Mongoid #:nodoc
|
|
108
82
|
def slaves
|
109
83
|
@slaves
|
110
84
|
end
|
111
|
-
|
112
|
-
# Confiure mongoid from a hash that was usually parsed out of yml.
|
113
|
-
#
|
114
|
-
# Example:
|
115
|
-
#
|
116
|
-
# <tt>Mongoid::Config.instance.from_hash({})</tt>
|
117
|
-
def from_hash(settings)
|
118
|
-
_master(settings)
|
119
|
-
_slaves(settings)
|
120
|
-
settings.except("database").each_pair do |name, value|
|
121
|
-
send("#{name}=", value) if respond_to?(name)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
# Reset the configuration options to the defaults.
|
126
|
-
#
|
127
|
-
# Example:
|
128
|
-
#
|
129
|
-
# <tt>config.reset</tt>
|
130
|
-
def reset
|
131
|
-
@allow_dynamic_fields = true
|
132
|
-
@parameterize_keys = true
|
133
|
-
@persist_in_safe_mode = true
|
134
|
-
@raise_not_found_error = true
|
135
|
-
@reconnect_time = 3
|
136
|
-
@use_object_ids = false
|
137
|
-
@time_zone = nil
|
138
|
-
end
|
139
|
-
|
140
|
-
protected
|
141
|
-
|
142
|
-
# Check if the database is valid and the correct version.
|
143
|
-
#
|
144
|
-
# Example:
|
145
|
-
#
|
146
|
-
# <tt>config.check_database!</tt>
|
147
|
-
def check_database!(database)
|
148
|
-
raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
|
149
|
-
version = database.connection.server_version
|
150
|
-
raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
|
151
|
-
end
|
152
|
-
|
153
|
-
# Get a Rails logger or stdout logger.
|
154
|
-
#
|
155
|
-
# Example:
|
156
|
-
#
|
157
|
-
# <tt>config.logger</tt>
|
158
|
-
def logger
|
159
|
-
defined?(Rails) ? Rails.logger : Logger.new($stdout)
|
160
|
-
end
|
161
|
-
|
162
|
-
# Get a master database from settings.
|
163
|
-
#
|
164
|
-
# Example:
|
165
|
-
#
|
166
|
-
# <tt>config._master({}, "test")</tt>
|
167
|
-
def _master(settings)
|
168
|
-
self.master = database_from_hash(settings)
|
169
|
-
end
|
170
|
-
|
171
|
-
# Get a bunch-o-slaves from settings and names.
|
172
|
-
#
|
173
|
-
# Example:
|
174
|
-
#
|
175
|
-
# <tt>config._slaves({}, "test")</tt>
|
176
|
-
def _slaves(settings)
|
177
|
-
self.slaves = settings["slaves"].to_a.map do |slave|
|
178
|
-
database_from_hash({"database" => master.name}.merge(slave), :slave_ok => true)
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
def database_from_hash(settings, connection_options={})
|
183
|
-
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
|
184
|
-
|
185
|
-
name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
|
186
|
-
host = settings["host"] || mongo_uri.host || "localhost"
|
187
|
-
port = settings["port"] || mongo_uri.port || 27017
|
188
|
-
pool_size = settings["pool_size"] || 1
|
189
|
-
username = settings["username"] || mongo_uri.user
|
190
|
-
password = settings["password"] || mongo_uri.password
|
191
|
-
|
192
|
-
local_options = {
|
193
|
-
:logger => logger,
|
194
|
-
:pool_size => pool_size
|
195
|
-
}.merge(connection_options)
|
196
|
-
|
197
|
-
Mongo::Connection.new(host, port, local_options).tap do |connection|
|
198
|
-
if username || password
|
199
|
-
connection.add_auth(name, username, password)
|
200
|
-
connection.apply_saved_authentication
|
201
|
-
end
|
202
|
-
end.db(name)
|
203
|
-
end
|
204
85
|
end
|
205
86
|
end
|
data/lib/mongoid/contexts.rb
CHANGED
@@ -6,7 +6,7 @@ module Mongoid #:nodoc:
|
|
6
6
|
attr_reader :criteria
|
7
7
|
|
8
8
|
delegate :blank?, :empty?, :first, :last, :to => :execute
|
9
|
-
delegate :
|
9
|
+
delegate :documents, :options, :selector, :to => :criteria
|
10
10
|
|
11
11
|
# Return aggregation counts of the grouped documents. This will count by
|
12
12
|
# the first field provided in the fields array.
|
@@ -133,7 +133,7 @@ module Mongoid #:nodoc:
|
|
133
133
|
# <tt>Mongoid::Contexts::Mongo.new(criteria)</tt>
|
134
134
|
def initialize(criteria)
|
135
135
|
@criteria = criteria
|
136
|
-
if klass.hereditary
|
136
|
+
if klass.hereditary && Mongoid.persist_types
|
137
137
|
criteria.in(:_type => criteria.klass._types)
|
138
138
|
end
|
139
139
|
criteria.enslave if klass.enslaved?
|
@@ -6,20 +6,12 @@ module Mongoid #:nodoc:
|
|
6
6
|
#
|
7
7
|
# Example:
|
8
8
|
#
|
9
|
-
# <tt>context.paginate
|
9
|
+
# <tt>context.paginate</tt>
|
10
10
|
#
|
11
11
|
# Returns:
|
12
12
|
#
|
13
13
|
# A collection of documents paginated.
|
14
|
-
|
15
|
-
def paginate(pager_options={})
|
16
|
-
if pager_options[:per_page]
|
17
|
-
options[:limit] = pager_options[:per_page].to_i
|
18
|
-
if pager_options[:page]
|
19
|
-
options[:skip] = (pager_options[:page].to_i - 1) * pager_options[:per_page].to_i
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
14
|
+
def paginate
|
23
15
|
@collection ||= execute(true)
|
24
16
|
WillPaginate::Collection.create(page, per_page, count) do |pager|
|
25
17
|
pager.replace(@collection.to_a)
|
@@ -64,23 +64,6 @@ module Mongoid #:nodoc:
|
|
64
64
|
end
|
65
65
|
alias :any_in :in
|
66
66
|
|
67
|
-
# Adds a criterion to the +Criteria+ that specifies values to do
|
68
|
-
# geospacial searches by. The field must be indexed with the "2d" option.
|
69
|
-
#
|
70
|
-
# Options:
|
71
|
-
#
|
72
|
-
# attributes: A +Hash+ where the keys are the field names and the values are
|
73
|
-
# +Arrays+ of [latitude, longitude] pairs.
|
74
|
-
#
|
75
|
-
# Example:
|
76
|
-
#
|
77
|
-
# <tt>criteria.near(:field1 => [30, -44])</tt>
|
78
|
-
#
|
79
|
-
# Returns: <tt>self</tt>
|
80
|
-
def near(attributes = {})
|
81
|
-
update_selector(attributes, "$near")
|
82
|
-
end
|
83
|
-
|
84
67
|
# Adds a criterion to the +Criteria+ that specifies values that must
|
85
68
|
# be matched in order to return results. This is similar to a SQL "WHERE"
|
86
69
|
# clause. This is the actual selector that will be provided to MongoDB,
|