mongoid 2.8.1 → 3.0.0.rc
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 +802 -58
- data/LICENSE +1 -1
- data/README.md +10 -11
- data/Rakefile +2 -8
- data/lib/config/locales/en.yml +441 -51
- data/lib/mongoid.rb +24 -39
- data/lib/mongoid/atomic.rb +16 -33
- data/lib/mongoid/atomic/modifiers.rb +2 -2
- data/lib/mongoid/atomic/paths/embedded.rb +4 -5
- 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/root.rb +4 -5
- data/lib/mongoid/attributes.rb +95 -32
- data/lib/mongoid/attributes/processing.rb +14 -10
- data/lib/mongoid/attributes/readonly.rb +56 -0
- data/lib/mongoid/callbacks.rb +90 -27
- data/lib/mongoid/collections/retry.rb +2 -3
- data/lib/mongoid/components.rb +11 -23
- data/lib/mongoid/config.rb +87 -233
- data/lib/mongoid/config/environment.rb +5 -6
- data/lib/mongoid/config/inflections.rb +6 -0
- data/lib/mongoid/config/options.rb +1 -1
- data/lib/mongoid/config/validators.rb +3 -0
- data/lib/mongoid/config/validators/option.rb +25 -0
- data/lib/mongoid/config/validators/session.rb +140 -0
- data/lib/mongoid/contextual.rb +50 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +98 -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 +66 -0
- data/lib/mongoid/contextual/map_reduce.rb +273 -0
- data/lib/mongoid/contextual/memory.rb +383 -0
- data/lib/mongoid/contextual/mongo.rb +543 -0
- data/lib/mongoid/copyable.rb +3 -34
- data/lib/mongoid/criteria.rb +436 -250
- 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 +69 -50
- data/lib/mongoid/errors.rb +22 -1
- 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_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/extensions.rb +28 -76
- data/lib/mongoid/extensions/array.rb +127 -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 +68 -0
- data/lib/mongoid/extensions/false_class.rb +26 -0
- data/lib/mongoid/extensions/float.rb +44 -0
- data/lib/mongoid/extensions/hash.rb +91 -0
- data/lib/mongoid/extensions/integer.rb +54 -0
- data/lib/mongoid/extensions/module.rb +28 -0
- data/lib/mongoid/extensions/nil_class.rb +21 -0
- data/lib/mongoid/extensions/object.rb +188 -0
- data/lib/mongoid/extensions/object_id.rb +53 -0
- data/lib/mongoid/extensions/range.rb +55 -0
- data/lib/mongoid/extensions/regexp.rb +27 -0
- data/lib/mongoid/extensions/set.rb +55 -0
- data/lib/mongoid/extensions/string.rb +155 -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 +26 -0
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields.rb +129 -194
- data/lib/mongoid/fields/foreign_key.rb +134 -0
- data/lib/mongoid/fields/localized.rb +73 -0
- data/lib/mongoid/fields/standard.rb +268 -0
- data/lib/mongoid/fields/validators.rb +2 -0
- data/lib/mongoid/fields/validators/macro.rb +83 -0
- data/lib/mongoid/finders.rb +42 -43
- data/lib/mongoid/hierarchy.rb +25 -14
- data/lib/mongoid/identity_map.rb +31 -19
- data/lib/mongoid/indexes.rb +66 -15
- data/lib/mongoid/indexes/validators/options.rb +80 -0
- data/lib/mongoid/inspection.rb +1 -1
- data/lib/mongoid/javascript.rb +1 -1
- data/lib/mongoid/json.rb +2 -2
- data/lib/mongoid/loggable.rb +69 -0
- data/lib/mongoid/matchers.rb +1 -1
- 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/multi_parameter_attributes.rb +8 -10
- data/lib/mongoid/nested_attributes.rb +17 -9
- data/lib/mongoid/observer.rb +1 -2
- data/lib/mongoid/paranoia.rb +13 -18
- data/lib/mongoid/persistence.rb +43 -39
- data/lib/mongoid/persistence/atomic.rb +2 -2
- 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/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.rb +6 -19
- 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/railtie.rb +26 -11
- data/lib/mongoid/railties/database.rake +22 -108
- data/lib/mongoid/relations.rb +4 -6
- data/lib/mongoid/relations/accessors.rb +119 -19
- 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 +3 -3
- data/lib/mongoid/relations/builders.rb +19 -16
- 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 +4 -4
- data/lib/mongoid/relations/builders/referenced/in.rb +4 -4
- data/lib/mongoid/relations/builders/referenced/many.rb +5 -5
- 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/cascading.rb +6 -4
- 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/constraint.rb +4 -3
- data/lib/mongoid/relations/conversions.rb +5 -6
- data/lib/mongoid/relations/cyclic.rb +7 -7
- data/lib/mongoid/relations/embedded/batchable.rb +346 -0
- data/lib/mongoid/relations/embedded/in.rb +23 -12
- data/lib/mongoid/relations/embedded/many.rb +99 -161
- data/lib/mongoid/relations/embedded/one.rb +25 -14
- data/lib/mongoid/relations/macros.rb +105 -61
- data/lib/mongoid/relations/many.rb +93 -14
- data/lib/mongoid/relations/metadata.rb +200 -45
- data/lib/mongoid/relations/nested_builder.rb +3 -5
- 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 +60 -31
- data/lib/mongoid/relations/referenced/in.rb +40 -15
- data/lib/mongoid/relations/referenced/many.rb +117 -132
- data/lib/mongoid/relations/referenced/many_to_many.rb +101 -46
- 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/reloading.rb +12 -14
- data/lib/mongoid/scoping.rb +329 -0
- data/lib/mongoid/serialization.rb +8 -27
- data/lib/mongoid/sessions.rb +359 -0
- data/lib/mongoid/sessions/factory.rb +106 -0
- data/lib/mongoid/sessions/mongo_uri.rb +93 -0
- data/lib/mongoid/sessions/validators.rb +2 -0
- data/lib/mongoid/sessions/validators/storage.rb +49 -0
- data/lib/mongoid/sharding.rb +6 -6
- data/lib/mongoid/state.rb +6 -7
- data/lib/mongoid/threaded.rb +167 -59
- data/lib/mongoid/threaded/lifecycle.rb +21 -22
- data/lib/mongoid/threaded/sessions.rb +0 -0
- data/lib/mongoid/timestamps.rb +1 -1
- data/lib/mongoid/timestamps/created.rb +8 -4
- data/lib/mongoid/timestamps/timeless.rb +6 -4
- data/lib/mongoid/timestamps/updated.rb +3 -3
- data/lib/mongoid/unit_of_work.rb +61 -0
- data/lib/mongoid/validations.rb +27 -19
- data/lib/mongoid/validations/associated.rb +2 -2
- data/lib/mongoid/validations/format.rb +2 -2
- data/lib/mongoid/validations/presence.rb +31 -5
- data/lib/mongoid/validations/uniqueness.rb +9 -12
- data/lib/mongoid/version.rb +2 -2
- data/lib/mongoid/versioning.rb +25 -26
- 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 +59 -19
- 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 +69 -25
- metadata +110 -137
- checksums.yaml +0 -7
- 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.rb +0 -120
- data/lib/mongoid/collections/master.rb +0 -45
- data/lib/mongoid/collections/operations.rb +0 -44
- data/lib/mongoid/config/database.rb +0 -181
- data/lib/mongoid/config/replset_database.rb +0 -80
- data/lib/mongoid/contexts.rb +0 -25
- data/lib/mongoid/contexts/enumerable.rb +0 -313
- data/lib/mongoid/contexts/enumerable/sort.rb +0 -43
- data/lib/mongoid/contexts/mongo.rb +0 -487
- 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 -110
- 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 -77
- 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/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.rb +0 -89
- 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/referenced/batch.rb +0 -73
- data/lib/mongoid/relations/referenced/batch/insert.rb +0 -57
- data/lib/mongoid/safety.rb +0 -105
- data/lib/mongoid/scope.rb +0 -31
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Attributes
|
2
|
+
module Mongoid
|
3
|
+
module Attributes
|
4
4
|
|
5
5
|
# This module contains the behavior for processing attributes.
|
6
6
|
module Processing
|
@@ -11,19 +11,21 @@ module Mongoid #:nodoc:
|
|
11
11
|
# put into the document's attributes.
|
12
12
|
#
|
13
13
|
# @example Process the attributes.
|
14
|
-
# person.
|
14
|
+
# person.process_attributes(:title => "sir", :age => 40)
|
15
15
|
#
|
16
16
|
# @param [ Hash ] attrs The attributes to set.
|
17
17
|
# @param [ Symbol ] role A role for scoped mass assignment.
|
18
18
|
# @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
|
19
19
|
#
|
20
20
|
# @since 2.0.0.rc.7
|
21
|
-
def
|
21
|
+
def process_attributes(attrs = nil, role = :default, guard_protected_attributes = true)
|
22
22
|
attrs ||= {}
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
if attrs.any?
|
24
|
+
attrs = sanitize_for_mass_assignment(attrs, role) if guard_protected_attributes
|
25
|
+
attrs.each_pair do |key, value|
|
26
|
+
next if pending_attribute?(key, value)
|
27
|
+
process_attribute(key, value)
|
28
|
+
end
|
27
29
|
end
|
28
30
|
yield self if block_given?
|
29
31
|
process_pending
|
@@ -50,7 +52,7 @@ module Mongoid #:nodoc:
|
|
50
52
|
pending_relations[name] = value
|
51
53
|
return true
|
52
54
|
end
|
53
|
-
if nested_attributes.
|
55
|
+
if nested_attributes.has_key?(name)
|
54
56
|
pending_nested[name] = value
|
55
57
|
return true
|
56
58
|
end
|
@@ -92,9 +94,11 @@ module Mongoid #:nodoc:
|
|
92
94
|
#
|
93
95
|
# @since 2.0.0.rc.7
|
94
96
|
def process_attribute(name, value)
|
95
|
-
|
97
|
+
responds = respond_to?("#{name}=")
|
98
|
+
if Mongoid.allow_dynamic_fields && !responds
|
96
99
|
write_attribute(name, value)
|
97
100
|
else
|
101
|
+
raise Errors::UnknownAttribute.new(self.class, name) unless responds
|
98
102
|
send("#{name}=", value)
|
99
103
|
end
|
100
104
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid
|
3
|
+
module Attributes
|
4
|
+
|
5
|
+
# This module defines behaviour for readonly attributes.
|
6
|
+
module Readonly
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
class_attribute :readonly_attributes
|
11
|
+
self.readonly_attributes = ::Set.new
|
12
|
+
end
|
13
|
+
|
14
|
+
# Are we able to write the attribute with the provided name?
|
15
|
+
#
|
16
|
+
# @example Can we write the attribute?
|
17
|
+
# model.attribute_writable?(:title)
|
18
|
+
#
|
19
|
+
# @param [ String, Symbol ] name The name of the field.
|
20
|
+
#
|
21
|
+
# @return [ true, false ] If the document is new, or if the field is not
|
22
|
+
# readonly.
|
23
|
+
#
|
24
|
+
# @since 3.0.0
|
25
|
+
def attribute_writable?(name)
|
26
|
+
new_record? || !readonly_attributes.include?(name.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
|
31
|
+
# Defines an attribute as readonly. This will ensure that the value for
|
32
|
+
# the attribute is only set when the document is new or we are
|
33
|
+
# creating. In other cases, the field write will be ignored with the
|
34
|
+
# exception of #remove_attribute and #update_attribute, where an error
|
35
|
+
# will get raised.
|
36
|
+
#
|
37
|
+
# @example Flag fields as readonly.
|
38
|
+
# class Band
|
39
|
+
# include Mongoid::Document
|
40
|
+
# field :name, type: String
|
41
|
+
# field :genre, type: String
|
42
|
+
# attr_readonly :name, :genre
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# @param [ Array<Symbol> ] names The names of the fields.
|
46
|
+
#
|
47
|
+
# @since 3.0.0
|
48
|
+
def attr_readonly(*names)
|
49
|
+
names.each do |name|
|
50
|
+
readonly_attributes << name.to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/mongoid/callbacks.rb
CHANGED
@@ -1,40 +1,74 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Mongoid
|
2
|
+
module Mongoid
|
3
3
|
|
4
4
|
# This module contains all the callback hooks for Mongoid.
|
5
5
|
module Callbacks
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
CALLBACKS = [
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
9
|
+
:after_build,
|
10
|
+
:after_create,
|
11
|
+
:after_destroy,
|
12
|
+
:after_initialize,
|
13
|
+
:after_save,
|
14
|
+
:after_update,
|
15
|
+
:after_validation,
|
16
|
+
:around_create,
|
17
|
+
:around_destroy,
|
18
|
+
:around_save,
|
19
|
+
:around_update,
|
20
|
+
:before_create,
|
21
|
+
:before_destroy,
|
22
|
+
:before_save,
|
23
|
+
:before_update,
|
24
|
+
:before_validation
|
15
25
|
]
|
16
26
|
|
17
27
|
included do
|
18
28
|
extend ActiveModel::Callbacks
|
19
29
|
include ActiveModel::Validations::Callbacks
|
20
30
|
|
21
|
-
define_model_callbacks :initialize, :
|
22
|
-
define_model_callbacks :build, :
|
31
|
+
define_model_callbacks :initialize, only: :after
|
32
|
+
define_model_callbacks :build, only: :after
|
23
33
|
define_model_callbacks :create, :destroy, :save, :update
|
24
34
|
end
|
25
35
|
|
26
|
-
#
|
36
|
+
# Run only the after callbacks for the specific event.
|
27
37
|
#
|
28
|
-
# @
|
29
|
-
#
|
38
|
+
# @note ActiveSupport does not allow this type of behaviour by default, so
|
39
|
+
# Mongoid has to get around it and implement itself.
|
30
40
|
#
|
31
|
-
# @
|
41
|
+
# @example Run only the after save callbacks.
|
42
|
+
# model.run_after_callbacks(:save)
|
32
43
|
#
|
33
|
-
# @
|
44
|
+
# @param [ Array<Symbol> ] kinds The events that are occurring.
|
34
45
|
#
|
35
|
-
# @
|
36
|
-
|
37
|
-
|
46
|
+
# @return [ Object ] The result of the chain executing.
|
47
|
+
#
|
48
|
+
# @since 3.0.0
|
49
|
+
def run_after_callbacks(*kinds)
|
50
|
+
kinds.each do |kind|
|
51
|
+
run_targeted_callbacks(:after, kind)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Run only the before callbacks for the specific event.
|
56
|
+
#
|
57
|
+
# @note ActiveSupport does not allow this type of behaviour by default, so
|
58
|
+
# Mongoid has to get around it and implement itself.
|
59
|
+
#
|
60
|
+
# @example Run only the before save callbacks.
|
61
|
+
# model.run_before_callbacks(:save, :create)
|
62
|
+
#
|
63
|
+
# @param [ Array<Symbol> ] kinds The events that are occurring.
|
64
|
+
#
|
65
|
+
# @return [ Object ] The result of the chain executing.
|
66
|
+
#
|
67
|
+
# @since 3.0.0
|
68
|
+
def run_before_callbacks(*kinds)
|
69
|
+
kinds.each do |kind|
|
70
|
+
run_targeted_callbacks(:before, kind)
|
71
|
+
end
|
38
72
|
end
|
39
73
|
|
40
74
|
# Run the callbacks for the document. This overrides active support's
|
@@ -58,7 +92,7 @@ module Mongoid #:nodoc:
|
|
58
92
|
return false
|
59
93
|
end
|
60
94
|
end
|
61
|
-
|
95
|
+
super(kind, *args, &block)
|
62
96
|
end
|
63
97
|
|
64
98
|
private
|
@@ -73,19 +107,21 @@ module Mongoid #:nodoc:
|
|
73
107
|
# @return [ Array<Document> ] The children.
|
74
108
|
#
|
75
109
|
# @since 2.3.0
|
76
|
-
def cascadable_children(kind)
|
77
|
-
|
78
|
-
|
79
|
-
|
110
|
+
def cascadable_children(kind, children = Set.new)
|
111
|
+
embedded_relations.each_pair do |name, metadata|
|
112
|
+
next unless metadata.cascading_callbacks?
|
113
|
+
without_autobuild do
|
80
114
|
delayed_pulls = delayed_atomic_pulls[name]
|
81
|
-
children.
|
82
|
-
|
83
|
-
Array.wrap(
|
84
|
-
|
85
|
-
children.
|
115
|
+
children.merge(delayed_pulls) if delayed_pulls
|
116
|
+
relation = send(name)
|
117
|
+
Array.wrap(relation).each do |child|
|
118
|
+
next if children.include?(child)
|
119
|
+
children.add(child) if cascadable_child?(kind, child)
|
120
|
+
children.merge(child.send(:cascadable_children, kind, children))
|
86
121
|
end
|
87
122
|
end
|
88
123
|
end
|
124
|
+
children.to_a
|
89
125
|
end
|
90
126
|
|
91
127
|
# Determine if the child should fire the callback.
|
@@ -127,5 +163,32 @@ module Mongoid #:nodoc:
|
|
127
163
|
kind
|
128
164
|
end
|
129
165
|
end
|
166
|
+
|
167
|
+
# Run only the callbacks for the target location (before, after, around)
|
168
|
+
# and kind (save, update, create).
|
169
|
+
#
|
170
|
+
# @example Run the targeted callbacks.
|
171
|
+
# model.run_targeted_callbacks(:before, :save)
|
172
|
+
#
|
173
|
+
# @param [ Symbol ] place The time to run, :before, :after, :around.
|
174
|
+
# @param [ Symbol ] kind The type of callback, :save, :create, :update.
|
175
|
+
#
|
176
|
+
# @return [ Object ] The result of the chain execution.
|
177
|
+
#
|
178
|
+
# @since 3.0.0
|
179
|
+
def run_targeted_callbacks(place, kind)
|
180
|
+
name = "_run__#{place}__#{kind}__callbacks"
|
181
|
+
unless respond_to?(name)
|
182
|
+
chain = ActiveSupport::Callbacks::CallbackChain.new(name, {})
|
183
|
+
send("_#{kind}_callbacks").each do |callback|
|
184
|
+
chain.push(callback) if callback.kind == place
|
185
|
+
end
|
186
|
+
class_eval <<-EOM
|
187
|
+
def #{name}() #{chain.compile} end
|
188
|
+
protected :#{name}
|
189
|
+
EOM
|
190
|
+
end
|
191
|
+
send(name)
|
192
|
+
end
|
130
193
|
end
|
131
194
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Collections
|
2
|
+
module Mongoid
|
3
|
+
module Collections
|
4
4
|
|
5
5
|
# Provides behaviour for retrying commands on connection failure.
|
6
6
|
module Retry
|
@@ -32,7 +32,6 @@ module Mongoid #:nodoc:
|
|
32
32
|
retry
|
33
33
|
rescue Mongo::OperationFailure => ex
|
34
34
|
if ex.message =~ /not master/
|
35
|
-
Mongoid.reconnect!
|
36
35
|
retries = increase_retry_attempts(retries, ex)
|
37
36
|
retry
|
38
37
|
else
|
data/lib/mongoid/components.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Mongoid
|
3
|
-
module Components
|
2
|
+
module Mongoid
|
3
|
+
module Components
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
# All modules that a +Document+ is composed of are defined in this
|
@@ -21,22 +21,19 @@ module Mongoid #:nodoc
|
|
21
21
|
include Mongoid::Atomic
|
22
22
|
include Mongoid::Dirty
|
23
23
|
include Mongoid::Attributes
|
24
|
-
include Mongoid::
|
25
|
-
include Mongoid::DefaultScope
|
26
|
-
include Mongoid::Extras
|
24
|
+
include Mongoid::Copyable
|
27
25
|
include Mongoid::Fields
|
28
26
|
include Mongoid::Hierarchy
|
29
27
|
include Mongoid::Indexes
|
30
28
|
include Mongoid::Inspection
|
31
29
|
include Mongoid::JSON
|
32
|
-
include Mongoid::Keys
|
33
30
|
include Mongoid::Matchers
|
34
|
-
include Mongoid::NamedScope
|
35
31
|
include Mongoid::NestedAttributes
|
36
32
|
include Mongoid::Persistence
|
37
33
|
include Mongoid::Relations
|
38
34
|
include Mongoid::Reloading
|
39
|
-
include Mongoid::
|
35
|
+
include Mongoid::Scoping
|
36
|
+
include Mongoid::Sessions
|
40
37
|
include Mongoid::Serialization
|
41
38
|
include Mongoid::Sharding
|
42
39
|
include Mongoid::State
|
@@ -44,36 +41,29 @@ module Mongoid #:nodoc
|
|
44
41
|
include Mongoid::Timestamps::Timeless
|
45
42
|
include Mongoid::Validations
|
46
43
|
include Mongoid::Callbacks
|
47
|
-
include Mongoid::Copyable
|
48
|
-
include Mongoid::MultiDatabase
|
49
44
|
|
50
45
|
MODULES = [
|
51
46
|
Mongoid::Atomic,
|
52
47
|
Mongoid::Attributes,
|
53
|
-
Mongoid::Collections,
|
54
48
|
Mongoid::Copyable,
|
55
|
-
Mongoid::DefaultScope,
|
56
49
|
Mongoid::Dirty,
|
57
|
-
Mongoid::Extras,
|
58
50
|
Mongoid::Fields,
|
59
51
|
Mongoid::Hierarchy,
|
60
52
|
Mongoid::Indexes,
|
61
53
|
Mongoid::Inspection,
|
62
54
|
Mongoid::JSON,
|
63
|
-
Mongoid::
|
55
|
+
Mongoid::Loggable,
|
64
56
|
Mongoid::Matchers,
|
65
|
-
Mongoid::NamedScope,
|
66
57
|
Mongoid::NestedAttributes,
|
67
58
|
Mongoid::Persistence,
|
68
59
|
Mongoid::Relations,
|
69
60
|
Mongoid::Relations::Proxy,
|
70
|
-
Mongoid::
|
61
|
+
Mongoid::Scoping,
|
71
62
|
Mongoid::Serialization,
|
72
63
|
Mongoid::Sharding,
|
73
64
|
Mongoid::State,
|
74
65
|
Mongoid::Validations,
|
75
|
-
Mongoid::Callbacks
|
76
|
-
Mongoid::MultiDatabase,
|
66
|
+
Mongoid::Callbacks
|
77
67
|
]
|
78
68
|
|
79
69
|
class << self
|
@@ -88,11 +78,9 @@ module Mongoid #:nodoc
|
|
88
78
|
#
|
89
79
|
# @since 2.1.8
|
90
80
|
def prohibited_methods
|
91
|
-
@prohibited_methods ||= MODULES.
|
92
|
-
|
93
|
-
|
94
|
-
end
|
95
|
-
end.flatten
|
81
|
+
@prohibited_methods ||= MODULES.flat_map do |mod|
|
82
|
+
mod.instance_methods.map{ |m| m.to_sym }
|
83
|
+
end
|
96
84
|
end
|
97
85
|
end
|
98
86
|
end
|
data/lib/mongoid/config.rb
CHANGED
@@ -1,90 +1,29 @@
|
|
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
|
-
|
21
|
-
|
22
|
-
option :
|
23
|
-
option :
|
24
|
-
option :
|
25
|
-
option :
|
26
|
-
option :
|
27
|
-
option :
|
28
|
-
option :
|
29
|
-
option :
|
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.
|
62
|
-
#
|
63
|
-
# @example Get the blacklisted options.
|
64
|
-
# Config.blacklisted_options
|
65
|
-
#
|
66
|
-
# @return [ Array<String> ] The blacklist.
|
67
|
-
#
|
68
|
-
# @since 2.4.7
|
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
|
77
|
-
#
|
78
|
-
# @return [ Hash ] A hash of secondary databases.
|
79
|
-
def databases
|
80
|
-
configure_extras(@settings["databases"]) unless @databases || !@settings
|
81
|
-
@databases || {}
|
82
|
-
end
|
83
|
-
|
84
|
-
# @todo Durran: There were no tests around the databases setter, not sure
|
85
|
-
# what the exact expectation was. Set with a hash?
|
86
|
-
def databases=(databases)
|
87
|
-
end
|
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
|
88
27
|
|
89
28
|
# Return field names that could cause destructive things to happen if
|
90
29
|
# defined in a Mongoid::Document.
|
@@ -97,21 +36,6 @@ module Mongoid #:nodoc
|
|
97
36
|
Components.prohibited_methods
|
98
37
|
end
|
99
38
|
|
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
39
|
# Load the settings from a compliant mongoid.yml file. This can be used for
|
116
40
|
# easy setup with frameworks other than Rails.
|
117
41
|
#
|
@@ -119,48 +43,32 @@ module Mongoid #:nodoc
|
|
119
43
|
# Mongoid.load!("/path/to/mongoid.yml")
|
120
44
|
#
|
121
45
|
# @param [ String ] path The path to the file.
|
46
|
+
# @param [ String, Symbol ] environment The environment to load.
|
122
47
|
#
|
123
48
|
# @since 2.0.1
|
124
|
-
def load!(path)
|
125
|
-
Environment.load_yaml(path)
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
# Returns the default logger, which is either a Rails logger of stdout logger
|
131
|
-
#
|
132
|
-
# @example Get the default logger
|
133
|
-
# config.default_logger
|
134
|
-
#
|
135
|
-
# @return [ Logger ] The default Logger instance.
|
136
|
-
def default_logger
|
137
|
-
defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new($stdout)
|
49
|
+
def load!(path, environment = nil)
|
50
|
+
settings = Environment.load_yaml(path, environment)
|
51
|
+
load_configuration(settings) if settings.present?
|
52
|
+
settings
|
138
53
|
end
|
139
54
|
|
140
|
-
#
|
55
|
+
# Connect to the provided database name on the default session.
|
141
56
|
#
|
142
|
-
# @
|
143
|
-
# config.logger
|
57
|
+
# @note Use only in development or test environments for convenience.
|
144
58
|
#
|
145
|
-
# @
|
146
|
-
|
147
|
-
@logger = default_logger unless defined?(@logger)
|
148
|
-
@logger
|
149
|
-
end
|
150
|
-
|
151
|
-
# Sets the logger for Mongoid to use.
|
59
|
+
# @example Set the database to connect to.
|
60
|
+
# config.connect_to("mongoid_test")
|
152
61
|
#
|
153
|
-
# @
|
154
|
-
# config.logger = Logger.new($stdout, :warn)
|
62
|
+
# @param [ String ] name The database name.
|
155
63
|
#
|
156
|
-
# @
|
157
|
-
def
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
64
|
+
# @since 3.0.0
|
65
|
+
def connect_to(name)
|
66
|
+
self.sessions = {
|
67
|
+
default: {
|
68
|
+
database: name,
|
69
|
+
hosts: [ "localhost:27017" ]
|
70
|
+
}
|
71
|
+
}
|
164
72
|
end
|
165
73
|
|
166
74
|
# Purge all data in all collections, including indexes.
|
@@ -168,143 +76,89 @@ module Mongoid #:nodoc
|
|
168
76
|
# @example Purge all data.
|
169
77
|
# Mongoid::Config.purge!
|
170
78
|
#
|
79
|
+
# @return [ true ] true.
|
80
|
+
#
|
171
81
|
# @since 2.0.2
|
172
82
|
def purge!
|
173
|
-
|
174
|
-
|
175
|
-
|
83
|
+
session = Sessions.default
|
84
|
+
collections = session["system.namespaces"].find(name: { "$not" => /system|\$/ }).to_a
|
85
|
+
collections.each do |collection|
|
86
|
+
_, name = collection["name"].split(".", 2)
|
87
|
+
session[name].drop
|
88
|
+
end and true
|
176
89
|
end
|
177
90
|
|
178
|
-
#
|
179
|
-
# the ActiveSupport time zone.
|
91
|
+
# Set the configuration options. Will validate each one individually.
|
180
92
|
#
|
181
|
-
# @
|
93
|
+
# @example Set the options.
|
94
|
+
# config.options = { raise_not_found_error: true }
|
182
95
|
#
|
183
|
-
# @
|
184
|
-
# Config.use_activesupport_time_zone = true
|
96
|
+
# @param [ Hash ] options The configuration options.
|
185
97
|
#
|
186
|
-
# @
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
98
|
+
# @since 3.0.0
|
99
|
+
def options=(options)
|
100
|
+
if options
|
101
|
+
options.each_pair do |option, value|
|
102
|
+
Validators::Option.validate(option)
|
103
|
+
send("#{option}=", value)
|
104
|
+
end
|
105
|
+
end
|
191
106
|
end
|
192
|
-
attr_reader :use_activesupport_time_zone
|
193
|
-
alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone
|
194
107
|
|
195
|
-
#
|
196
|
-
# set is not a valid +Mongo::DB+, then an error will be raised.
|
197
|
-
#
|
198
|
-
# @example Set the master database.
|
199
|
-
# config.master = Mongo::Connection.new.db("test")
|
108
|
+
# Get the session configuration or an empty hash.
|
200
109
|
#
|
201
|
-
# @
|
110
|
+
# @example Get the sessions configuration.
|
111
|
+
# config.sessions
|
202
112
|
#
|
203
|
-
# @
|
113
|
+
# @return [ Hash ] The sessions configuration.
|
204
114
|
#
|
205
|
-
# @
|
206
|
-
def
|
207
|
-
|
208
|
-
@master = db
|
115
|
+
# @since 3.0.0
|
116
|
+
def sessions
|
117
|
+
@sessions ||= {}
|
209
118
|
end
|
210
|
-
alias :database= :master=
|
211
119
|
|
212
|
-
#
|
213
|
-
# error.
|
120
|
+
# Set the session configuration options.
|
214
121
|
#
|
215
|
-
# @example
|
216
|
-
# config.
|
217
|
-
#
|
218
|
-
# @raise [ Errors::InvalidDatabase ] If the database was not set.
|
219
|
-
#
|
220
|
-
# @return [ Mongo::DB ] The master database.
|
221
|
-
def master
|
222
|
-
unless @master
|
223
|
-
@master, @slaves = configure_databases(@settings) unless @settings.blank?
|
224
|
-
raise Errors::InvalidDatabase.new(nil) unless @master
|
225
|
-
end
|
226
|
-
if @reconnect
|
227
|
-
@reconnect = false
|
228
|
-
reconnect!
|
229
|
-
end
|
230
|
-
@master
|
231
|
-
end
|
232
|
-
alias :database :master
|
233
|
-
|
234
|
-
# Convenience method for connecting to the master database after forking a
|
235
|
-
# new process.
|
122
|
+
# @example Set the session configuration options.
|
123
|
+
# config.sessions = { default: { hosts: [ "localhost:27017" ] }}
|
236
124
|
#
|
237
|
-
# @
|
238
|
-
# Mongoid.reconnect!
|
125
|
+
# @param [ Hash ] sessions The configuration options.
|
239
126
|
#
|
240
|
-
# @
|
241
|
-
def
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
@reconnect = true
|
248
|
-
end
|
127
|
+
# @since 3.0.0
|
128
|
+
def sessions=(sessions)
|
129
|
+
raise Errors::NoSessionsConfig.new unless sessions
|
130
|
+
sess = sessions.with_indifferent_access
|
131
|
+
Validators::Session.validate(sess)
|
132
|
+
@sessions = sess
|
133
|
+
sess
|
249
134
|
end
|
250
135
|
|
251
|
-
|
252
|
-
|
253
|
-
# Check if the database is valid and the correct version.
|
136
|
+
# Get the time zone to use.
|
254
137
|
#
|
255
|
-
# @example
|
256
|
-
#
|
138
|
+
# @example Get the time zone.
|
139
|
+
# Config.time_zone
|
257
140
|
#
|
258
|
-
# @
|
141
|
+
# @return [ String ] The time zone.
|
259
142
|
#
|
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
|
143
|
+
# @since 3.0.0
|
144
|
+
def time_zone
|
145
|
+
use_utc? ? "UTC" : ::Time.zone
|
268
146
|
end
|
269
147
|
|
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
|
148
|
+
private
|
293
149
|
|
294
|
-
#
|
150
|
+
# From a hash of settings, load all the configuration.
|
295
151
|
#
|
296
|
-
# @example
|
297
|
-
# config.
|
152
|
+
# @example Load the configuration.
|
153
|
+
# config.load_configuration(settings)
|
298
154
|
#
|
299
|
-
# @param [ Hash ]
|
155
|
+
# @param [ Hash ] settings The configuration settings.
|
300
156
|
#
|
301
|
-
# @since
|
302
|
-
def
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
end
|
307
|
-
end
|
157
|
+
# @since 3.0.0
|
158
|
+
def load_configuration(settings)
|
159
|
+
configuration = settings.with_indifferent_access
|
160
|
+
self.options = configuration[:options]
|
161
|
+
self.sessions = configuration[:sessions]
|
308
162
|
end
|
309
163
|
end
|
310
164
|
end
|