mongoid 2.4.10 → 3.0.0
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/CHANGELOG.md +949 -10
- data/LICENSE +1 -1
- data/README.md +10 -11
- data/Rakefile +3 -17
- data/lib/config/locales/en.yml +455 -51
- data/lib/mongoid/atomic/modifiers.rb +2 -2
- data/lib/mongoid/atomic/paths/embedded/many.rb +6 -6
- data/lib/mongoid/atomic/paths/embedded/one.rb +5 -5
- data/lib/mongoid/atomic/paths/embedded.rb +4 -5
- data/lib/mongoid/atomic/paths/root.rb +4 -5
- data/lib/mongoid/atomic.rb +70 -16
- data/lib/mongoid/attributes/processing.rb +14 -10
- data/lib/mongoid/attributes/readonly.rb +56 -0
- data/lib/mongoid/attributes.rb +95 -32
- data/lib/mongoid/callbacks.rb +106 -43
- data/lib/mongoid/components.rb +12 -23
- data/lib/mongoid/config/environment.rb +7 -4
- data/lib/mongoid/config/inflections.rb +6 -0
- data/lib/mongoid/config/options.rb +1 -1
- data/lib/mongoid/config/validators/option.rb +25 -0
- data/lib/mongoid/config/validators/session.rb +140 -0
- data/lib/mongoid/config/validators.rb +3 -0
- data/lib/mongoid/config.rb +104 -225
- data/lib/mongoid/contextual/aggregable/memory.rb +109 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +181 -0
- data/lib/mongoid/contextual/atomic.rb +179 -0
- data/lib/mongoid/contextual/command.rb +43 -0
- data/lib/mongoid/contextual/find_and_modify.rb +68 -0
- data/lib/mongoid/contextual/map_reduce.rb +273 -0
- data/lib/mongoid/contextual/memory.rb +406 -0
- data/lib/mongoid/contextual/mongo.rb +662 -0
- data/lib/mongoid/contextual.rb +50 -0
- data/lib/mongoid/copyable.rb +7 -25
- data/lib/mongoid/criteria.rb +515 -245
- data/lib/mongoid/criterion/inspection.rb +14 -8
- data/lib/mongoid/criterion/scoping.rb +114 -44
- data/lib/mongoid/dirty.rb +152 -67
- data/lib/mongoid/document.rb +71 -52
- data/lib/mongoid/errors/ambiguous_relationship.rb +51 -0
- data/lib/mongoid/errors/callback.rb +5 -6
- data/lib/mongoid/errors/delete_restriction.rb +29 -0
- data/lib/mongoid/errors/document_not_found.rb +98 -17
- data/lib/mongoid/errors/eager_load.rb +3 -6
- data/lib/mongoid/errors/invalid_collection.rb +3 -3
- data/lib/mongoid/errors/invalid_config_option.rb +27 -0
- data/lib/mongoid/errors/invalid_database.rb +3 -3
- data/lib/mongoid/errors/invalid_field.rb +54 -8
- data/lib/mongoid/errors/invalid_field_option.rb +35 -0
- data/lib/mongoid/errors/invalid_find.rb +3 -3
- data/lib/mongoid/errors/invalid_index.rb +28 -0
- data/lib/mongoid/errors/invalid_options.rb +4 -4
- data/lib/mongoid/errors/invalid_scope.rb +24 -0
- data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +38 -0
- data/lib/mongoid/errors/invalid_storage_options.rb +27 -0
- data/lib/mongoid/errors/invalid_time.rb +3 -6
- data/lib/mongoid/errors/inverse_not_found.rb +29 -0
- data/lib/mongoid/errors/mixed_relations.rb +4 -9
- data/lib/mongoid/errors/mixed_session_configuration.rb +28 -0
- data/lib/mongoid/errors/mongoid_error.rb +54 -3
- data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +28 -0
- data/lib/mongoid/errors/no_default_session.rb +23 -0
- data/lib/mongoid/errors/no_environment.rb +3 -3
- data/lib/mongoid/errors/no_map_reduce_output.rb +24 -0
- data/lib/mongoid/errors/no_metadata.rb +21 -0
- data/lib/mongoid/errors/no_parent.rb +24 -0
- data/lib/mongoid/errors/no_session_config.rb +22 -0
- data/lib/mongoid/errors/no_session_database.rb +27 -0
- data/lib/mongoid/errors/no_session_hosts.rb +27 -0
- data/lib/mongoid/errors/no_sessions_config.rb +20 -0
- data/lib/mongoid/errors/readonly_attribute.rb +25 -0
- data/lib/mongoid/errors/scope_overwrite.rb +4 -4
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -4
- data/lib/mongoid/errors/unknown_attribute.rb +25 -0
- data/lib/mongoid/errors/unsaved_document.rb +4 -8
- data/lib/mongoid/errors/unsupported_javascript.rb +27 -0
- data/lib/mongoid/errors/unsupported_version.rb +4 -4
- data/lib/mongoid/errors/validations.rb +7 -6
- data/lib/mongoid/errors/versioning_not_on_root.rb +23 -0
- data/lib/mongoid/errors.rb +23 -1
- data/lib/mongoid/evolvable.rb +19 -0
- data/lib/mongoid/extensions/array.rb +164 -0
- data/lib/mongoid/extensions/big_decimal.rb +42 -0
- data/lib/mongoid/extensions/boolean.rb +24 -0
- data/lib/mongoid/extensions/date.rb +70 -0
- data/lib/mongoid/extensions/date_time.rb +73 -0
- data/lib/mongoid/extensions/false_class.rb +38 -0
- data/lib/mongoid/extensions/float.rb +56 -0
- data/lib/mongoid/extensions/hash.rb +123 -0
- data/lib/mongoid/extensions/integer.rb +66 -0
- data/lib/mongoid/extensions/module.rb +28 -0
- data/lib/mongoid/extensions/nil_class.rb +21 -0
- data/lib/mongoid/extensions/object.rb +238 -0
- data/lib/mongoid/extensions/object_id.rb +54 -0
- data/lib/mongoid/extensions/range.rb +79 -0
- data/lib/mongoid/extensions/regexp.rb +27 -0
- data/lib/mongoid/extensions/set.rb +55 -0
- data/lib/mongoid/extensions/string.rb +181 -0
- data/lib/mongoid/extensions/symbol.rb +54 -0
- data/lib/mongoid/extensions/time.rb +78 -0
- data/lib/mongoid/extensions/time_with_zone.rb +55 -0
- data/lib/mongoid/extensions/true_class.rb +38 -0
- data/lib/mongoid/extensions.rb +28 -76
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields/foreign_key.rb +150 -0
- data/lib/mongoid/fields/localized.rb +73 -0
- data/lib/mongoid/fields/standard.rb +268 -0
- data/lib/mongoid/fields/validators/macro.rb +83 -0
- data/lib/mongoid/fields/validators.rb +2 -0
- data/lib/mongoid/fields.rb +142 -208
- data/lib/mongoid/finders.rb +45 -50
- data/lib/mongoid/hierarchy.rb +44 -15
- data/lib/mongoid/identity_map.rb +49 -21
- data/lib/mongoid/indexes/validators/options.rb +80 -0
- data/lib/mongoid/indexes.rb +66 -15
- data/lib/mongoid/inspection.rb +1 -1
- data/lib/mongoid/json.rb +2 -2
- data/lib/mongoid/loggable.rb +69 -0
- data/lib/mongoid/matchers/all.rb +7 -8
- data/lib/mongoid/matchers/and.rb +3 -3
- data/lib/mongoid/matchers/default.rb +6 -4
- data/lib/mongoid/matchers/exists.rb +2 -2
- data/lib/mongoid/matchers/gt.rb +2 -2
- data/lib/mongoid/matchers/gte.rb +2 -2
- data/lib/mongoid/matchers/in.rb +3 -7
- data/lib/mongoid/matchers/lt.rb +2 -2
- data/lib/mongoid/matchers/lte.rb +2 -2
- data/lib/mongoid/matchers/ne.rb +2 -2
- data/lib/mongoid/matchers/nin.rb +2 -2
- data/lib/mongoid/matchers/or.rb +2 -2
- data/lib/mongoid/matchers/size.rb +2 -2
- data/lib/mongoid/matchers/strategies.rb +3 -3
- data/lib/mongoid/matchers.rb +1 -1
- data/lib/mongoid/multi_parameter_attributes.rb +9 -11
- data/lib/mongoid/nested_attributes.rb +17 -9
- data/lib/mongoid/observer.rb +1 -2
- data/lib/mongoid/paranoia.rb +10 -44
- data/lib/mongoid/persistence/atomic/add_to_set.rb +5 -9
- data/lib/mongoid/persistence/atomic/bit.rb +5 -7
- data/lib/mongoid/persistence/atomic/inc.rb +5 -7
- data/lib/mongoid/persistence/atomic/operation.rb +45 -6
- data/lib/mongoid/persistence/atomic/pop.rb +5 -7
- data/lib/mongoid/persistence/atomic/pull.rb +5 -7
- data/lib/mongoid/persistence/atomic/pull_all.rb +5 -7
- data/lib/mongoid/persistence/atomic/push.rb +4 -10
- data/lib/mongoid/persistence/atomic/push_all.rb +4 -10
- data/lib/mongoid/persistence/atomic/rename.rb +6 -7
- data/lib/mongoid/persistence/atomic/sets.rb +5 -7
- data/lib/mongoid/persistence/atomic/unset.rb +4 -5
- data/lib/mongoid/persistence/atomic.rb +2 -2
- data/lib/mongoid/persistence/deletion.rb +2 -2
- data/lib/mongoid/persistence/insertion.rb +10 -16
- data/lib/mongoid/persistence/modification.rb +5 -9
- data/lib/mongoid/persistence/operations/embedded/insert.rb +7 -6
- data/lib/mongoid/persistence/operations/embedded/remove.rb +5 -5
- data/lib/mongoid/persistence/operations/insert.rb +4 -4
- data/lib/mongoid/persistence/operations/remove.rb +4 -4
- data/lib/mongoid/persistence/operations/update.rb +5 -5
- data/lib/mongoid/persistence/operations/upsert.rb +28 -0
- data/lib/mongoid/persistence/operations.rb +22 -19
- data/lib/mongoid/persistence/upsertion.rb +30 -0
- data/lib/mongoid/persistence.rb +94 -39
- data/lib/mongoid/railtie.rb +26 -11
- data/lib/mongoid/railties/database.rake +22 -108
- data/lib/mongoid/relations/accessors.rb +120 -20
- data/lib/mongoid/relations/auto_save.rb +59 -5
- data/lib/mongoid/relations/binding.rb +211 -2
- data/lib/mongoid/relations/bindings/embedded/in.rb +16 -22
- data/lib/mongoid/relations/bindings/embedded/many.rb +9 -50
- data/lib/mongoid/relations/bindings/embedded/one.rb +10 -16
- data/lib/mongoid/relations/bindings/referenced/in.rb +31 -57
- data/lib/mongoid/relations/bindings/referenced/many.rb +8 -20
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -19
- data/lib/mongoid/relations/bindings/referenced/one.rb +10 -24
- data/lib/mongoid/relations/builder.rb +4 -4
- data/lib/mongoid/relations/builders/embedded/in.rb +5 -5
- data/lib/mongoid/relations/builders/embedded/many.rb +12 -12
- data/lib/mongoid/relations/builders/embedded/one.rb +6 -6
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +8 -8
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +5 -5
- data/lib/mongoid/relations/builders/referenced/in.rb +4 -4
- data/lib/mongoid/relations/builders/referenced/many.rb +6 -6
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +7 -5
- data/lib/mongoid/relations/builders/referenced/one.rb +5 -5
- data/lib/mongoid/relations/builders.rb +19 -16
- data/lib/mongoid/relations/cascading/delete.rb +3 -5
- data/lib/mongoid/relations/cascading/destroy.rb +3 -3
- data/lib/mongoid/relations/cascading/nullify.rb +3 -3
- data/lib/mongoid/relations/cascading/restrict.rb +37 -0
- data/lib/mongoid/relations/cascading.rb +10 -7
- data/lib/mongoid/relations/constraint.rb +4 -3
- data/lib/mongoid/relations/conversions.rb +6 -7
- data/lib/mongoid/relations/cyclic.rb +7 -7
- data/lib/mongoid/relations/embedded/batchable.rb +347 -0
- data/lib/mongoid/relations/embedded/in.rb +23 -12
- data/lib/mongoid/relations/embedded/many.rb +100 -161
- data/lib/mongoid/relations/embedded/one.rb +25 -14
- data/lib/mongoid/relations/macros.rb +105 -60
- data/lib/mongoid/relations/many.rb +93 -14
- data/lib/mongoid/relations/metadata.rb +277 -47
- data/lib/mongoid/relations/nested_builder.rb +4 -6
- data/lib/mongoid/relations/one.rb +2 -2
- data/lib/mongoid/relations/options.rb +2 -2
- data/lib/mongoid/relations/polymorphic.rb +9 -9
- data/lib/mongoid/relations/proxy.rb +62 -31
- data/lib/mongoid/relations/referenced/in.rb +40 -15
- data/lib/mongoid/relations/referenced/many.rb +162 -130
- data/lib/mongoid/relations/referenced/many_to_many.rb +104 -48
- data/lib/mongoid/relations/referenced/one.rb +34 -13
- data/lib/mongoid/relations/reflections.rb +3 -3
- data/lib/mongoid/relations/synchronization.rb +19 -23
- data/lib/mongoid/relations/targets/enumerable.rb +86 -57
- data/lib/mongoid/relations/touchable.rb +32 -0
- data/lib/mongoid/relations.rb +22 -6
- data/lib/mongoid/reloading.rb +12 -14
- data/lib/mongoid/scoping.rb +312 -0
- data/lib/mongoid/serialization.rb +94 -42
- data/lib/mongoid/sessions/factory.rb +108 -0
- data/lib/mongoid/sessions/mongo_uri.rb +93 -0
- data/lib/mongoid/sessions/validators/storage.rb +49 -0
- data/lib/mongoid/sessions/validators.rb +2 -0
- data/lib/mongoid/sessions.rb +383 -0
- data/lib/mongoid/sharding.rb +6 -6
- data/lib/mongoid/state.rb +6 -7
- data/lib/mongoid/threaded/lifecycle.rb +21 -21
- data/lib/mongoid/threaded/sessions.rb +0 -0
- data/lib/mongoid/threaded.rb +219 -59
- data/lib/mongoid/timestamps/created.rb +8 -4
- data/lib/mongoid/timestamps/timeless.rb +6 -4
- data/lib/mongoid/timestamps/updated.rb +4 -3
- data/lib/mongoid/timestamps.rb +1 -1
- data/lib/mongoid/unit_of_work.rb +61 -0
- data/lib/mongoid/validations/associated.rb +2 -2
- data/lib/mongoid/validations/format.rb +2 -2
- data/lib/mongoid/validations/presence.rb +34 -4
- data/lib/mongoid/validations/uniqueness.rb +11 -12
- data/lib/mongoid/validations.rb +28 -20
- data/lib/mongoid/version.rb +2 -2
- data/lib/mongoid/versioning.rb +25 -26
- data/lib/mongoid.rb +37 -37
- data/lib/rack/mongoid/middleware/identity_map.rb +3 -3
- data/lib/rails/generators/mongoid/config/config_generator.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +65 -17
- data/lib/rails/generators/mongoid/model/model_generator.rb +7 -7
- data/lib/rails/generators/mongoid/model/templates/model.rb.tt +2 -2
- data/lib/rails/generators/mongoid/observer/observer_generator.rb +4 -4
- data/lib/rails/generators/mongoid_generator.rb +5 -5
- data/lib/rails/mongoid.rb +73 -26
- metadata +103 -148
- data/lib/config/locales/bg.yml +0 -61
- data/lib/config/locales/de.yml +0 -61
- data/lib/config/locales/en-GB.yml +0 -64
- data/lib/config/locales/es.yml +0 -59
- data/lib/config/locales/fr.yml +0 -62
- data/lib/config/locales/hi.yml +0 -53
- data/lib/config/locales/hu.yml +0 -64
- data/lib/config/locales/id.yml +0 -62
- data/lib/config/locales/it.yml +0 -59
- data/lib/config/locales/ja.yml +0 -57
- data/lib/config/locales/kr.yml +0 -54
- data/lib/config/locales/nl.yml +0 -61
- data/lib/config/locales/pl.yml +0 -59
- data/lib/config/locales/pt-BR.yml +0 -60
- data/lib/config/locales/pt.yml +0 -60
- data/lib/config/locales/ro.yml +0 -66
- data/lib/config/locales/ru.yml +0 -61
- data/lib/config/locales/sv.yml +0 -60
- data/lib/config/locales/vi.yml +0 -62
- data/lib/config/locales/zh-CN.yml +0 -53
- data/lib/mongoid/collection.rb +0 -157
- data/lib/mongoid/collections/master.rb +0 -45
- data/lib/mongoid/collections/operations.rb +0 -44
- data/lib/mongoid/collections/retry.rb +0 -59
- data/lib/mongoid/collections.rb +0 -120
- data/lib/mongoid/config/database.rb +0 -181
- data/lib/mongoid/config/replset_database.rb +0 -80
- data/lib/mongoid/contexts/enumerable/sort.rb +0 -43
- data/lib/mongoid/contexts/enumerable.rb +0 -313
- data/lib/mongoid/contexts/mongo.rb +0 -487
- data/lib/mongoid/contexts.rb +0 -25
- data/lib/mongoid/criterion/builder.rb +0 -34
- data/lib/mongoid/criterion/complex.rb +0 -84
- data/lib/mongoid/criterion/creational.rb +0 -34
- data/lib/mongoid/criterion/exclusion.rb +0 -108
- data/lib/mongoid/criterion/inclusion.rb +0 -290
- data/lib/mongoid/criterion/optional.rb +0 -259
- data/lib/mongoid/criterion/selector.rb +0 -177
- data/lib/mongoid/cursor.rb +0 -88
- data/lib/mongoid/default_scope.rb +0 -36
- data/lib/mongoid/errors/invalid_type.rb +0 -25
- data/lib/mongoid/extensions/array/deep_copy.rb +0 -25
- data/lib/mongoid/extensions/array/deletion.rb +0 -29
- data/lib/mongoid/extensions/false_class/equality.rb +0 -26
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +0 -47
- data/lib/mongoid/extensions/hash/deep_copy.rb +0 -25
- data/lib/mongoid/extensions/hash/scoping.rb +0 -25
- data/lib/mongoid/extensions/integer/checks.rb +0 -23
- data/lib/mongoid/extensions/nil/collectionization.rb +0 -23
- data/lib/mongoid/extensions/object/checks.rb +0 -29
- data/lib/mongoid/extensions/object/deep_copy.rb +0 -21
- data/lib/mongoid/extensions/object/reflections.rb +0 -48
- data/lib/mongoid/extensions/object/substitutable.rb +0 -15
- data/lib/mongoid/extensions/object/yoda.rb +0 -44
- data/lib/mongoid/extensions/object_id/conversions.rb +0 -60
- data/lib/mongoid/extensions/proc/scoping.rb +0 -25
- data/lib/mongoid/extensions/string/checks.rb +0 -36
- data/lib/mongoid/extensions/string/conversions.rb +0 -22
- data/lib/mongoid/extensions/string/inflections.rb +0 -118
- data/lib/mongoid/extensions/symbol/checks.rb +0 -23
- data/lib/mongoid/extensions/symbol/inflections.rb +0 -67
- data/lib/mongoid/extensions/true_class/equality.rb +0 -26
- data/lib/mongoid/extras.rb +0 -31
- data/lib/mongoid/fields/internal/array.rb +0 -76
- data/lib/mongoid/fields/internal/big_decimal.rb +0 -63
- data/lib/mongoid/fields/internal/bignum.rb +0 -10
- data/lib/mongoid/fields/internal/binary.rb +0 -11
- data/lib/mongoid/fields/internal/boolean.rb +0 -58
- data/lib/mongoid/fields/internal/date.rb +0 -51
- data/lib/mongoid/fields/internal/date_time.rb +0 -28
- data/lib/mongoid/fields/internal/false_class.rb +0 -10
- data/lib/mongoid/fields/internal/fixnum.rb +0 -10
- data/lib/mongoid/fields/internal/float.rb +0 -47
- data/lib/mongoid/fields/internal/foreign_keys/array.rb +0 -88
- data/lib/mongoid/fields/internal/foreign_keys/object.rb +0 -56
- data/lib/mongoid/fields/internal/hash.rb +0 -11
- data/lib/mongoid/fields/internal/integer.rb +0 -59
- data/lib/mongoid/fields/internal/localized.rb +0 -62
- data/lib/mongoid/fields/internal/nil_class.rb +0 -53
- data/lib/mongoid/fields/internal/object.rb +0 -11
- data/lib/mongoid/fields/internal/object_id.rb +0 -46
- data/lib/mongoid/fields/internal/range.rb +0 -61
- data/lib/mongoid/fields/internal/set.rb +0 -57
- data/lib/mongoid/fields/internal/string.rb +0 -42
- data/lib/mongoid/fields/internal/symbol.rb +0 -43
- data/lib/mongoid/fields/internal/time.rb +0 -23
- data/lib/mongoid/fields/internal/time_with_zone.rb +0 -23
- data/lib/mongoid/fields/internal/timekeeping.rb +0 -122
- data/lib/mongoid/fields/internal/true_class.rb +0 -10
- data/lib/mongoid/fields/mappings.rb +0 -42
- data/lib/mongoid/fields/serializable.rb +0 -270
- data/lib/mongoid/identity.rb +0 -92
- data/lib/mongoid/javascript/functions.yml +0 -63
- data/lib/mongoid/javascript.rb +0 -20
- data/lib/mongoid/keys.rb +0 -144
- data/lib/mongoid/logger.rb +0 -45
- data/lib/mongoid/multi_database.rb +0 -36
- data/lib/mongoid/named_scope.rb +0 -166
- data/lib/mongoid/relations/embedded/atomic/operation.rb +0 -63
- data/lib/mongoid/relations/embedded/atomic/pull.rb +0 -65
- data/lib/mongoid/relations/embedded/atomic/push_all.rb +0 -59
- data/lib/mongoid/relations/embedded/atomic/set.rb +0 -61
- data/lib/mongoid/relations/embedded/atomic/unset.rb +0 -41
- data/lib/mongoid/relations/embedded/atomic.rb +0 -89
- data/lib/mongoid/relations/referenced/batch/insert.rb +0 -57
- data/lib/mongoid/relations/referenced/batch.rb +0 -73
- data/lib/mongoid/safety.rb +0 -105
- data/lib/mongoid/scope.rb +0 -31
data/lib/mongoid/config.rb
CHANGED
|
@@ -1,89 +1,47 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
|
-
require "uri"
|
|
3
|
-
require "mongoid/config/database"
|
|
4
2
|
require "mongoid/config/environment"
|
|
5
|
-
require "mongoid/config/
|
|
3
|
+
require "mongoid/config/inflections"
|
|
6
4
|
require "mongoid/config/options"
|
|
5
|
+
require "mongoid/config/validators"
|
|
7
6
|
|
|
8
|
-
module Mongoid
|
|
7
|
+
module Mongoid
|
|
9
8
|
|
|
10
9
|
# This module defines all the configuration options for Mongoid, including the
|
|
11
10
|
# database connections.
|
|
12
|
-
#
|
|
13
|
-
# @todo Durran: This module needs an overhaul, remove singleton, etc.
|
|
14
11
|
module Config
|
|
15
12
|
extend self
|
|
16
13
|
extend Options
|
|
17
14
|
include ActiveModel::Observing
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
option :allow_dynamic_fields, default: true
|
|
17
|
+
option :identity_map_enabled, default: false
|
|
18
|
+
option :include_root_in_json, default: false
|
|
19
|
+
option :include_type_for_serialization, default: false
|
|
20
|
+
option :preload_models, default: false
|
|
21
|
+
option :protect_sensitive_fields, default: true
|
|
22
|
+
option :raise_not_found_error, default: true
|
|
23
|
+
option :scope_overwrite_exception, default: false
|
|
24
|
+
option :skip_version_check, default: false
|
|
25
|
+
option :use_activesupport_time_zone, default: true
|
|
26
|
+
option :use_utc, default: false
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
option :autocreate_indexes, :default => false
|
|
24
|
-
option :identity_map_enabled, :default => false
|
|
25
|
-
option :include_root_in_json, :default => false
|
|
26
|
-
option :include_type_for_serialization, :default => false
|
|
27
|
-
option :max_retries_on_connection_failure, :default => 0
|
|
28
|
-
option :parameterize_keys, :default => true
|
|
29
|
-
option :scope_overwrite_exception, :default => false
|
|
30
|
-
option :persist_in_safe_mode, :default => false
|
|
31
|
-
option :preload_models, :default => false
|
|
32
|
-
option :raise_not_found_error, :default => true
|
|
33
|
-
option :skip_version_check, :default => false
|
|
34
|
-
option :time_zone, :default => nil
|
|
35
|
-
option :use_utc, :default => false
|
|
36
|
-
|
|
37
|
-
# Adds a new I18n locale file to the load path.
|
|
38
|
-
#
|
|
39
|
-
# @example Add a portuguese locale.
|
|
40
|
-
# Mongoid::Config.add_language('pt')
|
|
41
|
-
#
|
|
42
|
-
# @example Add all available languages.
|
|
43
|
-
# Mongoid::Config.add_language('*')
|
|
44
|
-
#
|
|
45
|
-
# @param [ String ] language_code The language to add.
|
|
46
|
-
def add_language(language_code = nil)
|
|
47
|
-
Dir[
|
|
48
|
-
File.join(
|
|
49
|
-
File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml"
|
|
50
|
-
)
|
|
51
|
-
].each do |file|
|
|
52
|
-
I18n.load_path << File.expand_path(file)
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# keys to remove from self to not pass through to Mongo::Connection
|
|
57
|
-
PRIVATE_OPTIONS =
|
|
58
|
-
%w(uri host hosts port database databases username
|
|
59
|
-
password logger use_activesupport_time_zone)
|
|
60
|
-
|
|
61
|
-
# Get the blacklisted options from passing through to the driver.
|
|
28
|
+
# Connect to the provided database name on the default session.
|
|
62
29
|
#
|
|
63
|
-
# @
|
|
64
|
-
# Config.blacklisted_options
|
|
30
|
+
# @note Use only in development or test environments for convenience.
|
|
65
31
|
#
|
|
66
|
-
# @
|
|
32
|
+
# @example Set the database to connect to.
|
|
33
|
+
# config.connect_to("mongoid_test")
|
|
67
34
|
#
|
|
68
|
-
# @
|
|
69
|
-
def blacklisted_options
|
|
70
|
-
PRIVATE_OPTIONS + settings.keys.map(&:to_s)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# Get any extra databases that have been configured.
|
|
74
|
-
#
|
|
75
|
-
# @example Get the extras.
|
|
76
|
-
# config.databases
|
|
35
|
+
# @param [ String ] name The database name.
|
|
77
36
|
#
|
|
78
|
-
# @
|
|
79
|
-
def
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def databases=(databases)
|
|
37
|
+
# @since 3.0.0
|
|
38
|
+
def connect_to(name)
|
|
39
|
+
self.sessions = {
|
|
40
|
+
default: {
|
|
41
|
+
database: name,
|
|
42
|
+
hosts: [ "localhost:27017" ]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
87
45
|
end
|
|
88
46
|
|
|
89
47
|
# Return field names that could cause destructive things to happen if
|
|
@@ -97,21 +55,6 @@ module Mongoid #:nodoc
|
|
|
97
55
|
Components.prohibited_methods
|
|
98
56
|
end
|
|
99
57
|
|
|
100
|
-
# Configure mongoid from a hash. This is usually called after parsing a
|
|
101
|
-
# yaml config file such as mongoid.yml.
|
|
102
|
-
#
|
|
103
|
-
# @example Configure Mongoid.
|
|
104
|
-
# config.from_hash({})
|
|
105
|
-
#
|
|
106
|
-
# @param [ Hash ] options The settings to use.
|
|
107
|
-
def from_hash(options = {})
|
|
108
|
-
options.except("database", "slaves", "databases").each_pair do |name, value|
|
|
109
|
-
send("#{name}=", value) if respond_to?("#{name}=")
|
|
110
|
-
end
|
|
111
|
-
@master, @slaves = configure_databases(options)
|
|
112
|
-
configure_extras(options["databases"])
|
|
113
|
-
end
|
|
114
|
-
|
|
115
58
|
# Load the settings from a compliant mongoid.yml file. This can be used for
|
|
116
59
|
# easy setup with frameworks other than Rails.
|
|
117
60
|
#
|
|
@@ -119,48 +62,41 @@ module Mongoid #:nodoc
|
|
|
119
62
|
# Mongoid.load!("/path/to/mongoid.yml")
|
|
120
63
|
#
|
|
121
64
|
# @param [ String ] path The path to the file.
|
|
65
|
+
# @param [ String, Symbol ] environment The environment to load.
|
|
122
66
|
#
|
|
123
67
|
# @since 2.0.1
|
|
124
|
-
def load!(path)
|
|
125
|
-
Environment.load_yaml(path)
|
|
126
|
-
|
|
127
|
-
|
|
68
|
+
def load!(path, environment = nil)
|
|
69
|
+
settings = Environment.load_yaml(path, environment)
|
|
70
|
+
load_configuration(settings) if settings.present?
|
|
71
|
+
settings
|
|
128
72
|
end
|
|
129
73
|
|
|
130
|
-
#
|
|
74
|
+
# Override the database to use globally.
|
|
131
75
|
#
|
|
132
|
-
# @example
|
|
133
|
-
# config.
|
|
76
|
+
# @example Override the database globally.
|
|
77
|
+
# config.override_database(:optional)
|
|
134
78
|
#
|
|
135
|
-
# @
|
|
136
|
-
def default_logger
|
|
137
|
-
defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout)
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# Returns the logger, or defaults to Rails logger or stdout logger.
|
|
79
|
+
# @param [ String, Symbol ] name The name of the database.
|
|
141
80
|
#
|
|
142
|
-
# @
|
|
143
|
-
# config.logger
|
|
81
|
+
# @return [ String, Symbol ] The global override.
|
|
144
82
|
#
|
|
145
|
-
# @
|
|
146
|
-
def
|
|
147
|
-
|
|
148
|
-
@logger
|
|
83
|
+
# @since 3.0.0
|
|
84
|
+
def override_database(name)
|
|
85
|
+
Threaded.database_override = name
|
|
149
86
|
end
|
|
150
87
|
|
|
151
|
-
#
|
|
152
|
-
#
|
|
153
|
-
# @example
|
|
154
|
-
# config.
|
|
155
|
-
#
|
|
156
|
-
# @
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
end
|
|
88
|
+
# Override the session to use globally.
|
|
89
|
+
#
|
|
90
|
+
# @example Override the session globally.
|
|
91
|
+
# config.override_session(:optional)
|
|
92
|
+
#
|
|
93
|
+
# @param [ String, Symbol ] name The name of the session.
|
|
94
|
+
#
|
|
95
|
+
# @return [ String, Symbol ] The global override.
|
|
96
|
+
#
|
|
97
|
+
# @since 3.0.0
|
|
98
|
+
def override_session(name)
|
|
99
|
+
Threaded.session_override = name ? name.to_s : nil
|
|
164
100
|
end
|
|
165
101
|
|
|
166
102
|
# Purge all data in all collections, including indexes.
|
|
@@ -168,143 +104,86 @@ module Mongoid #:nodoc
|
|
|
168
104
|
# @example Purge all data.
|
|
169
105
|
# Mongoid::Config.purge!
|
|
170
106
|
#
|
|
107
|
+
# @return [ true ] true.
|
|
108
|
+
#
|
|
171
109
|
# @since 2.0.2
|
|
172
110
|
def purge!
|
|
173
|
-
|
|
174
|
-
collection.drop
|
|
175
|
-
end
|
|
111
|
+
Sessions.default.collections.each do |collection|
|
|
112
|
+
collection.drop
|
|
113
|
+
end and true
|
|
176
114
|
end
|
|
177
115
|
|
|
178
|
-
#
|
|
179
|
-
# the ActiveSupport time zone.
|
|
116
|
+
# Set the configuration options. Will validate each one individually.
|
|
180
117
|
#
|
|
181
|
-
# @
|
|
118
|
+
# @example Set the options.
|
|
119
|
+
# config.options = { raise_not_found_error: true }
|
|
182
120
|
#
|
|
183
|
-
# @
|
|
184
|
-
# Config.use_activesupport_time_zone = true
|
|
121
|
+
# @param [ Hash ] options The configuration options.
|
|
185
122
|
#
|
|
186
|
-
# @
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
123
|
+
# @since 3.0.0
|
|
124
|
+
def options=(options)
|
|
125
|
+
if options
|
|
126
|
+
options.each_pair do |option, value|
|
|
127
|
+
Validators::Option.validate(option)
|
|
128
|
+
send("#{option}=", value)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
191
131
|
end
|
|
192
|
-
attr_reader :use_activesupport_time_zone
|
|
193
|
-
alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone
|
|
194
132
|
|
|
195
|
-
#
|
|
196
|
-
# set is not a valid +Mongo::DB+, then an error will be raised.
|
|
133
|
+
# Get the session configuration or an empty hash.
|
|
197
134
|
#
|
|
198
|
-
# @example
|
|
199
|
-
# config.
|
|
135
|
+
# @example Get the sessions configuration.
|
|
136
|
+
# config.sessions
|
|
200
137
|
#
|
|
201
|
-
# @
|
|
138
|
+
# @return [ Hash ] The sessions configuration.
|
|
202
139
|
#
|
|
203
|
-
# @
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
def master=(db)
|
|
207
|
-
check_database!(db)
|
|
208
|
-
@master = db
|
|
140
|
+
# @since 3.0.0
|
|
141
|
+
def sessions
|
|
142
|
+
@sessions ||= {}
|
|
209
143
|
end
|
|
210
|
-
alias :database= :master=
|
|
211
144
|
|
|
212
|
-
#
|
|
213
|
-
# error.
|
|
145
|
+
# Set the session configuration options.
|
|
214
146
|
#
|
|
215
|
-
# @example
|
|
216
|
-
# config.
|
|
147
|
+
# @example Set the session configuration options.
|
|
148
|
+
# config.sessions = { default: { hosts: [ "localhost:27017" ] }}
|
|
217
149
|
#
|
|
218
|
-
# @
|
|
150
|
+
# @param [ Hash ] sessions The configuration options.
|
|
219
151
|
#
|
|
220
|
-
# @
|
|
221
|
-
def
|
|
222
|
-
unless
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
@reconnect = false
|
|
228
|
-
reconnect!
|
|
229
|
-
end
|
|
230
|
-
@master
|
|
152
|
+
# @since 3.0.0
|
|
153
|
+
def sessions=(sessions)
|
|
154
|
+
raise Errors::NoSessionsConfig.new unless sessions
|
|
155
|
+
sess = sessions.with_indifferent_access
|
|
156
|
+
Validators::Session.validate(sess)
|
|
157
|
+
@sessions = sess
|
|
158
|
+
sess
|
|
231
159
|
end
|
|
232
|
-
alias :database :master
|
|
233
|
-
|
|
234
|
-
# Convenience method for connecting to the master database after forking a
|
|
235
|
-
# new process.
|
|
236
|
-
#
|
|
237
|
-
# @example Reconnect to the master.
|
|
238
|
-
# Mongoid.reconnect!
|
|
239
|
-
#
|
|
240
|
-
# @param [ true, false ] now Perform the reconnection immediately?
|
|
241
|
-
def reconnect!(now = true)
|
|
242
|
-
if now
|
|
243
|
-
master.connection.connect
|
|
244
|
-
else
|
|
245
|
-
# We set a @reconnect flag so that #master knows to reconnect the next
|
|
246
|
-
# time the connection is accessed.
|
|
247
|
-
@reconnect = true
|
|
248
|
-
end
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
protected
|
|
252
160
|
|
|
253
|
-
#
|
|
161
|
+
# Get the time zone to use.
|
|
254
162
|
#
|
|
255
|
-
# @example
|
|
256
|
-
#
|
|
163
|
+
# @example Get the time zone.
|
|
164
|
+
# Config.time_zone
|
|
257
165
|
#
|
|
258
|
-
# @
|
|
166
|
+
# @return [ String ] The time zone.
|
|
259
167
|
#
|
|
260
|
-
# @
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
|
|
264
|
-
unless skip_version_check
|
|
265
|
-
version = database.connection.server_version
|
|
266
|
-
raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
|
|
267
|
-
end
|
|
168
|
+
# @since 3.0.0
|
|
169
|
+
def time_zone
|
|
170
|
+
use_utc? ? "UTC" : ::Time.zone
|
|
268
171
|
end
|
|
269
172
|
|
|
270
|
-
|
|
271
|
-
#
|
|
272
|
-
# @example Configure the master and slave dbs.
|
|
273
|
-
# config.configure_databases("database" => "mongoid")
|
|
274
|
-
#
|
|
275
|
-
# @param [ Hash ] options The options to use.
|
|
276
|
-
#
|
|
277
|
-
# @option options [ String ] :database The database name.
|
|
278
|
-
# @option options [ String ] :host The database host.
|
|
279
|
-
# @option options [ String ] :password The password for authentication.
|
|
280
|
-
# @option options [ Integer ] :port The port for the database.
|
|
281
|
-
# @option options [ Array<Hash> ] :slaves The slave db options.
|
|
282
|
-
# @option options [ String ] :uri The uri for the database.
|
|
283
|
-
# @option options [ String ] :username The user for authentication.
|
|
284
|
-
#
|
|
285
|
-
# @since 2.0.0.rc.1
|
|
286
|
-
def configure_databases(options)
|
|
287
|
-
if options.has_key?('hosts')
|
|
288
|
-
ReplsetDatabase.new(options).configure
|
|
289
|
-
else
|
|
290
|
-
Database.new(options).configure
|
|
291
|
-
end
|
|
292
|
-
end
|
|
173
|
+
private
|
|
293
174
|
|
|
294
|
-
#
|
|
175
|
+
# From a hash of settings, load all the configuration.
|
|
295
176
|
#
|
|
296
|
-
# @example
|
|
297
|
-
# config.
|
|
177
|
+
# @example Load the configuration.
|
|
178
|
+
# config.load_configuration(settings)
|
|
298
179
|
#
|
|
299
|
-
# @param [ Hash ]
|
|
180
|
+
# @param [ Hash ] settings The configuration settings.
|
|
300
181
|
#
|
|
301
|
-
# @since
|
|
302
|
-
def
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
end
|
|
307
|
-
end
|
|
182
|
+
# @since 3.0.0
|
|
183
|
+
def load_configuration(settings)
|
|
184
|
+
configuration = settings.with_indifferent_access
|
|
185
|
+
self.options = configuration[:options]
|
|
186
|
+
self.sessions = configuration[:sessions]
|
|
308
187
|
end
|
|
309
188
|
end
|
|
310
189
|
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
module Mongoid
|
|
3
|
+
module Contextual
|
|
4
|
+
module Aggregable
|
|
5
|
+
# Contains behaviour for aggregating values in memory.
|
|
6
|
+
module Memory
|
|
7
|
+
|
|
8
|
+
# Get the average value of the provided field.
|
|
9
|
+
#
|
|
10
|
+
# @example Get the average of a single field.
|
|
11
|
+
# aggregable.avg(:likes)
|
|
12
|
+
#
|
|
13
|
+
# @param [ Symbol ] field The field to average.
|
|
14
|
+
#
|
|
15
|
+
# @return [ Float ] The average.
|
|
16
|
+
#
|
|
17
|
+
# @since 3.0.0
|
|
18
|
+
def avg(field)
|
|
19
|
+
count > 0 ? sum(field).to_f / count.to_f : nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Get the max value of the provided field. If provided a block, will
|
|
23
|
+
# return the Document with the greatest value for the field, in
|
|
24
|
+
# accordance with Ruby's enumerable API.
|
|
25
|
+
#
|
|
26
|
+
# @example Get the max of a single field.
|
|
27
|
+
# aggregable.max(:likes)
|
|
28
|
+
#
|
|
29
|
+
# @example Get the document with the max value.
|
|
30
|
+
# aggregable.max do |a, b|
|
|
31
|
+
# a.likes <=> b.likes
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# @param [ Symbol ] field The field to max.
|
|
35
|
+
#
|
|
36
|
+
# @return [ Float, Document ] The max value or document with the max
|
|
37
|
+
# value.
|
|
38
|
+
#
|
|
39
|
+
# @since 3.0.0
|
|
40
|
+
def max(field = nil)
|
|
41
|
+
block_given? ? super() : aggregate_by(field, :max_by)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Get the min value of the provided field. If provided a block, will
|
|
45
|
+
# return the Document with the smallest value for the field, in
|
|
46
|
+
# accordance with Ruby's enumerable API.
|
|
47
|
+
#
|
|
48
|
+
# @example Get the min of a single field.
|
|
49
|
+
# aggregable.min(:likes)
|
|
50
|
+
#
|
|
51
|
+
# @example Get the document with the min value.
|
|
52
|
+
# aggregable.min do |a, b|
|
|
53
|
+
# a.likes <=> b.likes
|
|
54
|
+
# end
|
|
55
|
+
#
|
|
56
|
+
# @param [ Symbol ] field The field to min.
|
|
57
|
+
#
|
|
58
|
+
# @return [ Float, Document ] The min value or document with the min
|
|
59
|
+
# value.
|
|
60
|
+
#
|
|
61
|
+
# @since 3.0.0
|
|
62
|
+
def min(field = nil)
|
|
63
|
+
block_given? ? super() : aggregate_by(field, :min_by)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Get the sum value of the provided field. If provided a block, will
|
|
67
|
+
# return the sum in accordance with Ruby's enumerable API.
|
|
68
|
+
#
|
|
69
|
+
# @example Get the sum of a single field.
|
|
70
|
+
# aggregable.sum(:likes)
|
|
71
|
+
#
|
|
72
|
+
# @example Get the sum for the provided block.
|
|
73
|
+
# aggregable.sum(&:likes)
|
|
74
|
+
#
|
|
75
|
+
# @param [ Symbol ] field The field to sum.
|
|
76
|
+
#
|
|
77
|
+
# @return [ Float ] The sum value.
|
|
78
|
+
#
|
|
79
|
+
# @since 3.0.0
|
|
80
|
+
def sum(field = nil)
|
|
81
|
+
if block_given?
|
|
82
|
+
super()
|
|
83
|
+
else
|
|
84
|
+
count > 0 ? super(0) { |doc| doc.send(field) } : nil
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
# Aggregate by the provided field and method.
|
|
91
|
+
#
|
|
92
|
+
# @api private
|
|
93
|
+
#
|
|
94
|
+
# @example Aggregate by the field and method.
|
|
95
|
+
# aggregable.aggregate_by(:name, :min_by)
|
|
96
|
+
#
|
|
97
|
+
# @param [ Symbol ] field The field to aggregate on.
|
|
98
|
+
# @param [ Symbol ] method The method (min_by or max_by).
|
|
99
|
+
#
|
|
100
|
+
# @return [ Integer ] The aggregate.
|
|
101
|
+
#
|
|
102
|
+
# @since 3.0.0
|
|
103
|
+
def aggregate_by(field, method)
|
|
104
|
+
count > 0 ? send(method) { |doc| doc.send(field) }.send(field) : nil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|