mongoid 2.8.1 → 3.0.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- 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 Validations
|
2
|
+
module Mongoid
|
3
|
+
module Validations
|
4
4
|
|
5
5
|
# Validates whether or not a field is unique against the documents in the
|
6
6
|
# database.
|
@@ -66,7 +66,7 @@ module Mongoid #:nodoc:
|
|
66
66
|
# @since 2.4.10
|
67
67
|
def add_error(document, attribute, value)
|
68
68
|
document.errors.add(
|
69
|
-
attribute, :taken, options.except(:case_sensitive, :scope).merge(:
|
69
|
+
attribute, :taken, options.except(:case_sensitive, :scope).merge(value: value)
|
70
70
|
)
|
71
71
|
end
|
72
72
|
|
@@ -101,11 +101,9 @@ module Mongoid #:nodoc:
|
|
101
101
|
# @since 2.4.10
|
102
102
|
def create_criteria(base, document, attribute, value)
|
103
103
|
field = document.fields[attribute.to_s]
|
104
|
-
criteria = base.
|
104
|
+
criteria = base.unscoped
|
105
105
|
if field.try(:localized?)
|
106
|
-
criterion(document, attribute, value)
|
107
|
-
criteria.selector.store(key, value)
|
108
|
-
end
|
106
|
+
criteria.selector.update(criterion(document, attribute, value))
|
109
107
|
else
|
110
108
|
criteria = criteria.where(criterion(document, attribute, value))
|
111
109
|
end
|
@@ -127,11 +125,11 @@ module Mongoid #:nodoc:
|
|
127
125
|
#
|
128
126
|
# @since 2.3.0
|
129
127
|
def criterion(document, attribute, value)
|
130
|
-
{ attribute => filter(value) }
|
131
|
-
|
132
|
-
|
133
|
-
end
|
128
|
+
selector = { attribute => filter(value) }
|
129
|
+
if document.persisted? && !document.embedded?
|
130
|
+
selector.merge!(_id: { "$ne" => document.id })
|
134
131
|
end
|
132
|
+
selector
|
135
133
|
end
|
136
134
|
|
137
135
|
# Filter the value based on whether the check is case sensitive or not.
|
@@ -167,7 +165,6 @@ module Mongoid #:nodoc:
|
|
167
165
|
Array.wrap(options[:scope]).each do |item|
|
168
166
|
criteria = criteria.where(item => document.attributes[item.to_s])
|
169
167
|
end
|
170
|
-
criteria = criteria.where(:deleted_at => nil) if document.paranoid?
|
171
168
|
criteria
|
172
169
|
end
|
173
170
|
|
data/lib/mongoid/version.rb
CHANGED
data/lib/mongoid/versioning.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Mongoid
|
2
|
+
module Mongoid
|
3
3
|
|
4
4
|
# Include this module to get automatic versioning of root level documents.
|
5
5
|
# This will add a version field to the +Document+ and a has_many association
|
@@ -8,17 +8,17 @@ module Mongoid #:nodoc:
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
10
|
included do
|
11
|
-
field :version, :
|
11
|
+
field :version, type: Integer, default: 1
|
12
12
|
|
13
13
|
embeds_many \
|
14
14
|
:versions,
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
15
|
+
class_name: self.name,
|
16
|
+
validate: false,
|
17
|
+
cyclic: true,
|
18
|
+
inverse_of: nil,
|
19
|
+
versioned: true
|
20
20
|
|
21
|
-
set_callback :save, :before, :revise, :
|
21
|
+
set_callback :save, :before, :revise, if: :revisable?
|
22
22
|
|
23
23
|
class_attribute :version_max
|
24
24
|
self.cyclic = true
|
@@ -36,17 +36,16 @@ module Mongoid #:nodoc:
|
|
36
36
|
def revise
|
37
37
|
previous = previous_revision
|
38
38
|
if previous && versioned_attributes_changed?
|
39
|
-
versions.build(
|
40
|
-
previous.versioned_attributes, :
|
41
|
-
)
|
39
|
+
new_version = versions.build(
|
40
|
+
previous.versioned_attributes, without_protection: true
|
41
|
+
)
|
42
|
+
new_version._id = nil
|
42
43
|
if version_max.present? && versions.length > version_max
|
43
44
|
deleted = versions.first
|
44
45
|
if deleted.paranoid?
|
45
46
|
versions.delete_one(deleted)
|
46
|
-
collection.
|
47
|
-
|
48
|
-
{ "$pull" => { "versions" => { "version" => deleted.version }}}
|
49
|
-
)
|
47
|
+
collection.find(atomic_selector).
|
48
|
+
update({ "$pull" => { "versions" => { "version" => deleted.version }}})
|
50
49
|
else
|
51
50
|
versions.delete(deleted)
|
52
51
|
end
|
@@ -63,8 +62,8 @@ module Mongoid #:nodoc:
|
|
63
62
|
#
|
64
63
|
# @since 2.2.1
|
65
64
|
def revise!
|
66
|
-
|
67
|
-
(previous_revision || self).versioned_attributes, :
|
65
|
+
versions.build(
|
66
|
+
(previous_revision || self).versioned_attributes, without_protection: true
|
68
67
|
)
|
69
68
|
versions.shift if version_max.present? && versions.length > version_max
|
70
69
|
self.version = (version || 1 ) + 1
|
@@ -132,8 +131,8 @@ module Mongoid #:nodoc:
|
|
132
131
|
def previous_revision
|
133
132
|
_loading_revision do
|
134
133
|
self.class.unscoped.
|
135
|
-
where(:
|
136
|
-
any_of({ :
|
134
|
+
where(_id: id).
|
135
|
+
any_of({ version: version }, { version: nil }).first
|
137
136
|
end
|
138
137
|
end
|
139
138
|
|
@@ -160,7 +159,7 @@ module Mongoid #:nodoc:
|
|
160
159
|
#
|
161
160
|
# @since 2.0.0
|
162
161
|
def versionless?
|
163
|
-
|
162
|
+
@versionless ||= false
|
164
163
|
end
|
165
164
|
|
166
165
|
# Filters fields that should not be versioned out of an attributes hash.
|
@@ -172,15 +171,15 @@ module Mongoid #:nodoc:
|
|
172
171
|
#
|
173
172
|
# @since 2.1.0
|
174
173
|
def only_versioned_attributes(hash)
|
175
|
-
{}
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
end
|
174
|
+
versioned = {}
|
175
|
+
hash.except("versions").each_pair do |name, value|
|
176
|
+
field = fields[name]
|
177
|
+
versioned[name] = value if !field || field.versioned?
|
180
178
|
end
|
179
|
+
versioned
|
181
180
|
end
|
182
181
|
|
183
|
-
module ClassMethods
|
182
|
+
module ClassMethods
|
184
183
|
|
185
184
|
# Sets the maximum number of versions to store.
|
186
185
|
#
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Rack
|
3
|
-
module Mongoid
|
4
|
-
module Middleware
|
2
|
+
module Rack
|
3
|
+
module Mongoid
|
4
|
+
module Middleware
|
5
5
|
|
6
6
|
# This middleware contains the behaviour needed to properly use the
|
7
7
|
# identity map in Rack based applications.
|
@@ -6,7 +6,7 @@ module Mongoid
|
|
6
6
|
class ConfigGenerator < Rails::Generators::Base
|
7
7
|
desc "Creates a Mongoid configuration file at config/mongoid.yml"
|
8
8
|
|
9
|
-
argument :database_name, :
|
9
|
+
argument :database_name, type: :string, optional: true
|
10
10
|
|
11
11
|
def self.source_root
|
12
12
|
@_mongoid_source_root ||= File.expand_path("../templates", __FILE__)
|
@@ -1,20 +1,60 @@
|
|
1
1
|
development:
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
2
|
+
# Configure available database sessions. (required)
|
3
|
+
sessions:
|
4
|
+
# Defines the default session. (required)
|
5
|
+
default:
|
6
|
+
# Defines the name of the default database that Mongoid can connect to.
|
7
|
+
# (required).
|
8
|
+
database: <%= database_name || app_name %>_development
|
9
|
+
# Provides the hosts the default session can connect to. Must be an array
|
10
|
+
# of host:port pairs. (required)
|
11
|
+
hosts:
|
12
|
+
- localhost:27017
|
13
|
+
options:
|
14
|
+
# Change whether the session persists in safe mode by default.
|
15
|
+
# (default: false)
|
16
|
+
# safe: false
|
17
|
+
|
18
|
+
# Change the default consistency model to :eventual or :strong.
|
19
|
+
# :eventual will send reads to secondaries, :strong sends everything
|
20
|
+
# to master. (default: :eventual)
|
21
|
+
consistency: :strong
|
22
|
+
# Configure Mongoid specific options. (optional)
|
23
|
+
options:
|
24
|
+
# Configuration for whether or not to allow access to fields that do
|
25
|
+
# not have a field definition on the model. (default: true)
|
26
|
+
# allow_dynamic_fields: true
|
27
|
+
|
28
|
+
# Enable the identity map, needed for eager loading. (default: false)
|
29
|
+
# identity_map_enabled: false
|
30
|
+
|
31
|
+
# Includes the root model name in json serialization. (default: false)
|
32
|
+
# include_root_in_json: false
|
33
|
+
|
34
|
+
# Include the _type field in serializaion. (default: false)
|
35
|
+
# include_type_for_serialization: false
|
36
|
+
|
37
|
+
# Preload all models in development, needed when models use
|
38
|
+
# inheritance. (default: false)
|
39
|
+
# preload_models: false
|
40
|
+
|
41
|
+
# Protect id and type from mass assignment. (default: true)
|
42
|
+
# protect_sensitive_fields: true
|
43
|
+
|
44
|
+
# Raise an error when performing a #find and the document is not found.
|
45
|
+
# (default: true)
|
46
|
+
# raise_not_found_error: true
|
47
|
+
|
48
|
+
# Raise an error when defining a scope with the same name as an
|
49
|
+
# existing method. (default: false)
|
50
|
+
# scope_overwrite_exception: false
|
51
|
+
|
52
|
+
# Skip the database version check, used when connecting to a db without
|
53
|
+
# admin access. (default: false)
|
54
|
+
# skip_version_check: false
|
55
|
+
|
56
|
+
# User Active Support's time zone in conversions. (default: true)
|
57
|
+
# use_activesupport_time_zone: true
|
58
|
+
|
59
|
+
# Ensure all times are UTC in the app side. (default: false)
|
60
|
+
# use_utc: false
|
@@ -1,18 +1,18 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require "rails/generators/mongoid_generator"
|
3
3
|
|
4
|
-
module Mongoid
|
5
|
-
module Generators
|
6
|
-
class ModelGenerator < Base
|
4
|
+
module Mongoid
|
5
|
+
module Generators
|
6
|
+
class ModelGenerator < Base
|
7
7
|
|
8
8
|
desc "Creates a Mongoid model"
|
9
|
-
argument :attributes, :
|
9
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
10
10
|
|
11
11
|
check_class_collision
|
12
12
|
|
13
|
-
class_option :timestamps, :
|
14
|
-
class_option :parent, :
|
15
|
-
class_option :versioning, :
|
13
|
+
class_option :timestamps, type: :boolean
|
14
|
+
class_option :parent, type: :string, desc: "The parent class for the generated model"
|
15
|
+
class_option :versioning, type: :boolean, default: false, desc: "Enable mongoid versioning"
|
16
16
|
|
17
17
|
def create_model_file
|
18
18
|
template "model.rb.tt", File.join("app/models", class_path, "#{file_name}.rb")
|
@@ -10,10 +10,10 @@ class <%= class_name %><%= " < #{options[:parent].classify}" if options[:parent]
|
|
10
10
|
include Mongoid::Versioning
|
11
11
|
<% end -%>
|
12
12
|
<% attributes.reject{|attr| attr.reference?}.each do |attribute| -%>
|
13
|
-
field :<%= attribute.name %>, :
|
13
|
+
field :<%= attribute.name %>, type: <%= attribute.type_class %>
|
14
14
|
<% end -%>
|
15
15
|
<% attributes.select{|attr| attr.reference? }.each do |attribute| -%>
|
16
|
-
embedded_in :<%= attribute.name
|
16
|
+
embedded_in :<%= attribute.name%>
|
17
17
|
<% end -%>
|
18
18
|
end
|
19
19
|
<% end -%>
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require "rails/generators/mongoid_generator"
|
3
3
|
|
4
|
-
module Mongoid
|
5
|
-
module Generators
|
6
|
-
class ObserverGenerator < Base
|
4
|
+
module Mongoid
|
5
|
+
module Generators
|
6
|
+
class ObserverGenerator < Base
|
7
7
|
|
8
|
-
check_class_collision :
|
8
|
+
check_class_collision suffix: "Observer"
|
9
9
|
|
10
10
|
def create_observer_file
|
11
11
|
template 'observer.rb.tt', File.join('app/models', class_path, "#{file_name}_observer.rb")
|
@@ -2,9 +2,9 @@
|
|
2
2
|
require "rails/generators/named_base"
|
3
3
|
require "rails/generators/active_model"
|
4
4
|
|
5
|
-
module Mongoid
|
6
|
-
module Generators
|
7
|
-
class Base < ::Rails::Generators::NamedBase
|
5
|
+
module Mongoid
|
6
|
+
module Generators
|
7
|
+
class Base < ::Rails::Generators::NamedBase
|
8
8
|
|
9
9
|
def self.source_root
|
10
10
|
@_mongoid_source_root ||=
|
@@ -21,7 +21,7 @@ module Mongoid #:nodoc:
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
class ActiveModel < ::Rails::Generators::ActiveModel
|
24
|
+
class ActiveModel < ::Rails::Generators::ActiveModel
|
25
25
|
def self.all(klass)
|
26
26
|
"#{klass}.all"
|
27
27
|
end
|
@@ -59,7 +59,7 @@ end
|
|
59
59
|
|
60
60
|
module Rails
|
61
61
|
module Generators
|
62
|
-
class GeneratedAttribute
|
62
|
+
class GeneratedAttribute
|
63
63
|
def type_class
|
64
64
|
return "Time" if type.to_s == "datetime"
|
65
65
|
return "String" if type.to_s == "text"
|
data/lib/rails/mongoid.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
module Rails
|
3
|
-
module Mongoid
|
2
|
+
module Rails
|
3
|
+
module Mongoid
|
4
4
|
extend self
|
5
5
|
|
6
6
|
# Create indexes for each model given the provided pattern and the class is
|
@@ -15,23 +15,61 @@ module Rails #:nodoc:
|
|
15
15
|
#
|
16
16
|
# @since 2.1.0
|
17
17
|
def create_indexes(pattern)
|
18
|
-
|
18
|
+
logger = Logger.new($stdout)
|
19
|
+
models(pattern).each do |model|
|
20
|
+
next if model.index_options.empty?
|
21
|
+
unless model.embedded?
|
22
|
+
model.create_indexes
|
23
|
+
logger.info("Creating indexes on: #{model} for: #{model.index_options.keys.join(", ")}.")
|
24
|
+
else
|
25
|
+
logger.info("Index ignored on: #{model}, please define in the root model.")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Remove indexes for each model given the provided pattern and the class is
|
31
|
+
# not embedded.
|
32
|
+
#
|
33
|
+
# @example Remove all the indexes.
|
34
|
+
# Rails::Mongoid.create_indexes("app/models/**/*.rb")
|
35
|
+
#
|
36
|
+
# @param [ String ] pattern The file matching pattern.
|
37
|
+
#
|
38
|
+
# @return [ Array<String> ] The file names.
|
39
|
+
#
|
40
|
+
def remove_indexes(pattern)
|
41
|
+
logger = Logger.new($stdout)
|
42
|
+
models(pattern).each do |model|
|
43
|
+
next if model.embedded?
|
44
|
+
indexes = model.collection.indexes.map{ |doc| doc["name"] }
|
45
|
+
indexes.delete_one("_id_")
|
46
|
+
model.remove_indexes
|
47
|
+
logger.info("Removing indexes on: #{model} for: #{indexes.join(', ')}.")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return all models matching the pattern.
|
52
|
+
#
|
53
|
+
# @example Return all models.
|
54
|
+
# Rails::Mongoid.models("app/models/**/*.rb")
|
55
|
+
#
|
56
|
+
# @param [ String ] pattern The file matching pattern.
|
57
|
+
#
|
58
|
+
# @return [ Array<Class> ] The models.
|
59
|
+
#
|
60
|
+
def models(pattern)
|
61
|
+
Dir.glob(pattern).map do |file|
|
19
62
|
logger = Logger.new($stdout)
|
20
63
|
begin
|
21
|
-
|
64
|
+
determine_model(file, logger)
|
22
65
|
rescue => e
|
23
66
|
logger.error(%Q{Failed to determine model from #{file}:
|
24
67
|
#{e.class}:#{e.message}
|
25
68
|
#{e.backtrace.join("\n")}
|
26
69
|
})
|
70
|
+
nil
|
27
71
|
end
|
28
|
-
|
29
|
-
model.create_indexes
|
30
|
-
logger.info("Generated indexes for #{model}")
|
31
|
-
else
|
32
|
-
logger.info("Not a Mongoid parent model: #{file}")
|
33
|
-
end
|
34
|
-
end
|
72
|
+
end.flatten.compact
|
35
73
|
end
|
36
74
|
|
37
75
|
# Use the application configuration to get every model and require it, so
|
@@ -44,7 +82,14 @@ module Rails #:nodoc:
|
|
44
82
|
# @param [ Application ] app The rails application.
|
45
83
|
def load_models(app)
|
46
84
|
app.config.paths["app/models"].each do |path|
|
47
|
-
|
85
|
+
preload = ::Mongoid.preload_models
|
86
|
+
if preload.resizable?
|
87
|
+
files = preload.map { |model| "#{path}/#{model}.rb" }
|
88
|
+
else
|
89
|
+
files = Dir.glob("#{path}/**/*.rb")
|
90
|
+
end
|
91
|
+
|
92
|
+
files.sort.each do |file|
|
48
93
|
load_model(file.gsub("#{path}/" , "").gsub(".rb", ""))
|
49
94
|
end
|
50
95
|
end
|
@@ -84,20 +129,19 @@ module Rails #:nodoc:
|
|
84
129
|
#
|
85
130
|
# @since 2.1.0
|
86
131
|
def determine_model(file, logger)
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
return klass
|
99
|
-
end
|
132
|
+
return nil unless file =~ /app\/models\/(.*).rb$/
|
133
|
+
return nil unless logger
|
134
|
+
|
135
|
+
model_path = $1.split('/')
|
136
|
+
begin
|
137
|
+
parts = model_path.map { |path| path.camelize }
|
138
|
+
name = parts.join("::")
|
139
|
+
klass = name.constantize
|
140
|
+
rescue NameError, LoadError
|
141
|
+
logger.info("Attempted to constantize #{name}, trying without namespacing.")
|
142
|
+
klass = parts.last.constantize
|
100
143
|
end
|
144
|
+
klass if klass.ancestors.include?(::Mongoid::Document)
|
101
145
|
end
|
102
146
|
end
|
103
147
|
end
|