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
data/CHANGELOG.md
CHANGED
@@ -3,88 +3,830 @@
|
|
3
3
|
For instructions on upgrading to newer versions, visit
|
4
4
|
[mongoid.org](http://mongoid.org/docs/upgrading.html).
|
5
5
|
|
6
|
-
##
|
6
|
+
## 3.0.0 (branch: master)
|
7
7
|
|
8
|
-
|
8
|
+
### New Features
|
9
9
|
|
10
|
-
|
10
|
+
* \#1989 Criteria `count`, `size` and `length` now behave as Active Record
|
11
|
+
with regards to database access.
|
11
12
|
|
12
|
-
|
13
|
+
`Criteria#count` will always hit the database to get the count.
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
`Criteria#length` and `Criteria#size` will hit the database once if the
|
16
|
+
criteria has not been loaded, and subsequent calls will return the
|
17
|
+
cached value.
|
16
18
|
|
17
|
-
|
19
|
+
If the criteria has been iterated over or loaded, `length` and `size`
|
20
|
+
will never hit the db.
|
18
21
|
|
19
|
-
|
22
|
+
* \#1976 Eager loading no longer produces queries when the base query returns
|
23
|
+
zero results.
|
20
24
|
|
21
|
-
*
|
22
|
-
|
25
|
+
* `Model.find_by` now accepts a block and will yield to the found document if
|
26
|
+
it is not nil.
|
23
27
|
|
24
|
-
|
28
|
+
Band.find_by(name: "Depeche Mode") do |band|
|
29
|
+
band.likes = 100
|
30
|
+
end
|
25
31
|
|
26
|
-
|
32
|
+
* \#1958/\#1798 Documents and `belongs_to` relations now support touch.
|
27
33
|
|
28
|
-
|
34
|
+
class Band
|
35
|
+
include Mongoid::Document
|
36
|
+
include Mongoid::Timestamps::Updated
|
37
|
+
belongs_to :label, touch: true
|
38
|
+
end
|
29
39
|
|
30
|
-
|
40
|
+
Update the document's updated_at timestamp to the current time. This
|
41
|
+
will also update any touchable relation's timestamp as well.
|
31
42
|
|
32
|
-
|
43
|
+
Band.first.touch
|
33
44
|
|
34
|
-
|
35
|
-
include `Mongoid::Timestamps::Updated`. (Arthur Nogueira Neves)
|
45
|
+
Update a specific time field along with the udpated_at.
|
36
46
|
|
37
|
-
|
47
|
+
Band.first.touch(:founded)
|
38
48
|
|
39
|
-
|
49
|
+
This fires no validations or callbacks.
|
40
50
|
|
41
|
-
*
|
42
|
-
3.0.0-stable.
|
51
|
+
* Mongoid now supports MongoDB's $findAndModify command.
|
43
52
|
|
44
|
-
|
45
|
-
memory when updating attributes. (Chris Thompson)
|
53
|
+
Band.find_and_modify("$inc" => { likes: 1 })
|
46
54
|
|
47
|
-
|
55
|
+
Band.desc(:name).only(:name).find_and_modify(
|
56
|
+
{ "$inc" => { likes: 1 }}, new: true
|
57
|
+
)
|
48
58
|
|
49
|
-
|
59
|
+
* \#1906 Mongoid will retrieve documents from the identity map when
|
60
|
+
providing multiple ids to find. (Hans Hasselberg)
|
50
61
|
|
51
|
-
*
|
52
|
-
|
62
|
+
* \#1903 Mongoid raises an error if provided a javascript expression
|
63
|
+
to a where clause on an embedded collection. (Sebastien Azimi)
|
53
64
|
|
54
|
-
|
65
|
+
* Aggregations now adhere to both a Mongoid API and their enumerable
|
66
|
+
counterparts where applicable.
|
55
67
|
|
56
|
-
|
68
|
+
Band.min(:likes)
|
69
|
+
Band.min do |a, b|
|
70
|
+
a.likes <=> b.likes
|
71
|
+
end
|
57
72
|
|
58
|
-
|
59
|
-
|
73
|
+
Band.max(:likes)
|
74
|
+
Band.max do |a, b|
|
75
|
+
a.likes <=> b.likes
|
76
|
+
end
|
60
77
|
|
61
|
-
|
62
|
-
|
78
|
+
Note that when providing a field name and no block, a single numeric
|
79
|
+
value will be returned, but when providing a block, a document will
|
80
|
+
be returned which has the min/max value. This is since Ruby's
|
81
|
+
Enumerable API dictates when providing a block, the matching element
|
82
|
+
is returned.
|
63
83
|
|
64
|
-
|
84
|
+
When providing a block, all documents will be loaded into memory.
|
85
|
+
When providing a symbol, the execution is handled via map/reduce on
|
86
|
+
the server.
|
65
87
|
|
66
|
-
|
88
|
+
* A kitchen sink aggregation method is now provided, to get everything in
|
89
|
+
in a single call for a field.
|
90
|
+
|
91
|
+
Band.aggregates(:likes)
|
92
|
+
# =>
|
93
|
+
# {
|
94
|
+
# "count" => 2.0,
|
95
|
+
# "max" => 1000.0,
|
96
|
+
# "min" => 500.0,
|
97
|
+
# "sum" => 1500.0,
|
98
|
+
# "avg" => 750.0
|
99
|
+
# }
|
100
|
+
|
101
|
+
* A DSL off the criteria API is now provided for map/reduce operations
|
102
|
+
as a convenience.
|
103
|
+
|
104
|
+
Band.where(name: "Tool").map_reduce(map, reduce).out(inline: 1)
|
105
|
+
Band.map_reduce(map, reduce).out(replace: "coll-name")
|
106
|
+
Band.map_reduce(map, reduce).out(inline: 1).finalize(finalize)
|
107
|
+
|
108
|
+
* Mongoid now uses Origin for its Criteria API. See the Origin repo
|
109
|
+
and API docs for the documentation.
|
110
|
+
|
111
|
+
* \#1861 Mongoid now raises an `AmbiguousRelationship` error when it
|
112
|
+
cannot determine the inverse of a relation and there are multiple
|
113
|
+
potential candidates. (Hans Hasselberg)
|
114
|
+
|
115
|
+
* You can now perform an explain directly from criteria.
|
116
|
+
|
117
|
+
Band.where(name: "Depeche Mode").explain
|
118
|
+
|
119
|
+
* \#1856 Push on one to many relations can now be chained.
|
120
|
+
|
121
|
+
band.albums.push(undertow).push(aenima)
|
122
|
+
|
123
|
+
* \#1842 MultiParameterAttributes now supported aliased fields.
|
124
|
+
(Anton Orel)
|
125
|
+
|
126
|
+
* \#1833 If an embedded document is attempted to be saved with no
|
127
|
+
parent defined, Mongoid now will raise a `Mongoid::Errors::NoParent`
|
128
|
+
exception.
|
129
|
+
|
130
|
+
* Added an ORM-agnostic way to get the field names
|
131
|
+
|
132
|
+
class Band
|
133
|
+
include Mongoid::Document
|
134
|
+
field :name, type: String
|
135
|
+
end
|
136
|
+
|
137
|
+
Band.attribute_names
|
138
|
+
|
139
|
+
* \#1831 `find_or_create_by` on relations now takes mass assignment
|
140
|
+
and type options. (Tatsuya Ono)
|
141
|
+
|
142
|
+
class Band
|
143
|
+
include Mongoid::Document
|
144
|
+
embeds_many :albums
|
145
|
+
end
|
146
|
+
|
147
|
+
band.albums.find_or_create_by({ name: "101" }, LP)
|
148
|
+
|
149
|
+
* \#1818 Add capability to choose the key where your `embeds_many` relation
|
150
|
+
is stores. (Cyril Mougel)
|
151
|
+
|
152
|
+
class User
|
153
|
+
include Mongoid::Document
|
154
|
+
field :name, type: String
|
155
|
+
embeds_many :prefs, class_name: "Preference", store_as: 'my_preferences'
|
156
|
+
end
|
157
|
+
|
158
|
+
user.prefs.build(value: "ok")
|
159
|
+
user.save
|
160
|
+
# document saves in MongoDB as:
|
161
|
+
# { "name" => "foo", "my_preferences" => [{ "value" => "ok" }]}
|
162
|
+
|
163
|
+
* \#1806 `Model.find_or_create_by` and `Model.find_or_initialize_by` can now
|
164
|
+
take documents as paramters for belongs_to relations.
|
165
|
+
|
166
|
+
person = Person.first
|
167
|
+
Game.find_or_create_by(person: person)
|
168
|
+
|
169
|
+
* \#1774 Relations now have a :restrict option for dependent relations
|
170
|
+
which will raise an error when attempting to delete a parent that
|
171
|
+
still has children on it. (Hans Hasselberg)
|
172
|
+
|
173
|
+
class Band
|
174
|
+
include Mongoid::Document
|
175
|
+
has_many :albums, dependent: :restrict
|
176
|
+
end
|
177
|
+
|
178
|
+
band = Band.first
|
179
|
+
band.albums << Albums.first
|
180
|
+
band.delete # Raises DeleteRestriction error.
|
181
|
+
|
182
|
+
* \#1764 Add method to check if field differs from the default value.
|
183
|
+
|
184
|
+
class Band
|
185
|
+
include Mongoid::Document
|
186
|
+
field :name, type: String, default: "New"
|
187
|
+
end
|
188
|
+
|
189
|
+
band = Band.first
|
190
|
+
band.name_changed_from_default?
|
191
|
+
|
192
|
+
* \#1759 Invalid fields error messages have been updated to show the
|
193
|
+
source and location of the original method. The new message is:
|
194
|
+
|
195
|
+
Problem:
|
196
|
+
Defining a field named 'crazy_method' is not allowed.
|
197
|
+
Summary:
|
198
|
+
Defining this field would override the method 'crazy_method',
|
199
|
+
which would cause issues with expectations around the original
|
200
|
+
method and cause extremely hard to debug issues.
|
201
|
+
The original method was defined in:
|
202
|
+
Object: MyModule
|
203
|
+
File: /path/to/my/module.rb
|
204
|
+
Line: 8
|
205
|
+
Resolution:
|
206
|
+
Use Mongoid.destructive_fields to see what names are
|
207
|
+
not allowed, and don't use these names. These include names
|
208
|
+
that also conflict with core Ruby methods on Object, Module,
|
209
|
+
Enumerable, or included gems that inject methods into these
|
210
|
+
or Mongoid internals.
|
211
|
+
|
212
|
+
* \#1753/#1649 A setter and getter for has_many relations to set it's
|
213
|
+
children is now provided. (Piotr Jakubowski)
|
214
|
+
|
215
|
+
class Album
|
216
|
+
include Mongoid::Document
|
217
|
+
has_many :engineers
|
218
|
+
end
|
219
|
+
|
220
|
+
class Engineer
|
221
|
+
include Mongoid::Document
|
222
|
+
belongs_to :album
|
223
|
+
end
|
224
|
+
|
225
|
+
album = Album.first
|
226
|
+
engineer = Engineer.first
|
227
|
+
album.engineer_ids = [ engineer.id ]
|
228
|
+
album.engineer_ids # Returns the ids of the engineers.
|
229
|
+
|
230
|
+
* \#1741 Mongoid now provides a rake task to force remove indexes for
|
231
|
+
environments where Mongoid manages the index definitions and the
|
232
|
+
removal should be automated. (Hans Hasselberg)
|
233
|
+
|
234
|
+
rake db:force_remove_indexes
|
235
|
+
rake db:mongoid:force_remove_indexes
|
236
|
+
|
237
|
+
* \#1726 `Mongoid.load!` now accepts an optional second argument for the
|
238
|
+
environment to load. This takes precedence over any environment variable
|
239
|
+
that is set if provided.
|
240
|
+
|
241
|
+
Mongoid.load!("/path/to/mongoid.yml", :development)
|
242
|
+
|
243
|
+
* \#1724 Mongoid now supports regex fields.
|
244
|
+
|
245
|
+
class Rule
|
246
|
+
include Mongoid::Document
|
247
|
+
field :pattern, type: Regexp, default: /[^abc]/
|
248
|
+
end
|
249
|
+
|
250
|
+
* \#1714/\#1706 Added better logging on index creation. (Hans Hasselberg)
|
251
|
+
|
252
|
+
When an index is present on a root document model:
|
253
|
+
|
254
|
+
Creating indexes on: Model for: name, dob.
|
255
|
+
|
256
|
+
When an index is defined on an embedded model:
|
257
|
+
|
258
|
+
Index ignored on: Address, please define in the root model.
|
259
|
+
|
260
|
+
When no index is defined, nothing is logged, and if a bad index is
|
261
|
+
defined an error is raised.
|
262
|
+
|
263
|
+
* \#1710 For cases when you don't want Mongoid to auto-protect the id
|
264
|
+
and type attributes, you can set a configuration option to turn this
|
265
|
+
off.
|
266
|
+
|
267
|
+
Mongoid.protect_sensitive_fields = false
|
268
|
+
|
269
|
+
* \#1685 Belongs to relations now have build_ and create_ methods.
|
270
|
+
|
271
|
+
class Comment
|
272
|
+
include Mongoid::Document
|
273
|
+
belongs_to :user
|
274
|
+
end
|
275
|
+
|
276
|
+
comment = Comment.new
|
277
|
+
comment.build_user # Build a new user object
|
278
|
+
comment.create_user # Create a new user object
|
279
|
+
|
280
|
+
* \#1684 Raise a `Mongoid::Errors::InverseNotFound` when attempting to
|
281
|
+
set a child on a relation without the proper inverse_of definitions
|
282
|
+
due to Mongoid not being able to determine it.
|
283
|
+
|
284
|
+
class Lush
|
285
|
+
include Mongoid::Document
|
286
|
+
embeds_one :whiskey, class_name: "Drink"
|
287
|
+
end
|
288
|
+
|
289
|
+
class Drink
|
290
|
+
include Mongoid::Document
|
291
|
+
embedded_in :alcoholic, class_name: "Lush"
|
292
|
+
end
|
293
|
+
|
294
|
+
lush = Lush.new
|
295
|
+
lush.whiskey = Drink.new # raises an InverseNotFound error.
|
296
|
+
|
297
|
+
* \#1680 Polymorphic relations now use `*_type` keys in lookup queries.
|
298
|
+
|
299
|
+
class User
|
300
|
+
include Mongoid::Document
|
301
|
+
has_many :comments, as: :commentable
|
302
|
+
end
|
303
|
+
|
304
|
+
class Comment
|
305
|
+
include Mongoid::Document
|
306
|
+
belongs_to :commentable, polymorphic: true
|
307
|
+
end
|
308
|
+
|
309
|
+
user = User.find(id)
|
310
|
+
user.comments # Uses user.id and type "User" in the query.
|
311
|
+
|
312
|
+
* \#1677 Support for parent separable polymorphic relations to the same
|
313
|
+
parent class is now available. This only works if set from the parent
|
314
|
+
side in order to know which relation the children belong to.
|
315
|
+
(Douwe Maan)
|
316
|
+
|
317
|
+
class Face
|
318
|
+
include Mongoid::Document
|
319
|
+
has_one :left_eye, class_name: "Eye", as: :visible
|
320
|
+
has_one :right_eye, class_name: "Eye", as: :visible
|
321
|
+
end
|
322
|
+
|
323
|
+
class Eye
|
324
|
+
include Mongoid::Document
|
325
|
+
belongs_to :visible, polymorphic: true
|
326
|
+
end
|
327
|
+
|
328
|
+
face = Face.new
|
329
|
+
right_eye = Eye.new
|
330
|
+
left_eye = Eye.new
|
331
|
+
face.right_eye = right_eye
|
332
|
+
face.left_eye = left_eye
|
333
|
+
right_eye.visible = face # Will raise an error.
|
334
|
+
|
335
|
+
* \#1650 Objects that respond to `to_criteria` can now be merged into
|
336
|
+
existing criteria objects.
|
337
|
+
|
338
|
+
class Filter
|
339
|
+
def to_criteria
|
340
|
+
# return a Criteria object.
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
criteria = Person.where(title: "Sir")
|
345
|
+
criteria.merge(filter)
|
346
|
+
|
347
|
+
* \#1635 All exceptions now provide more comprehensive errors, including
|
348
|
+
the problem that occured, a detail summary of why it happened, and
|
349
|
+
potential resolutions. Example:
|
350
|
+
|
351
|
+
(Mongoid::Errors::DocumentNotFound)
|
352
|
+
Problem:
|
353
|
+
Document not found for class Town with
|
354
|
+
id(s) [BSON::ObjectId('4f35781b8ad54812e1000001')].
|
355
|
+
Summary:
|
356
|
+
When calling Town.find with an id or array of ids,
|
357
|
+
each parameter must match a document in the database
|
358
|
+
or this error will be raised.
|
359
|
+
Resolution:
|
360
|
+
Search for an id that is in the database or set the
|
361
|
+
Mongoid.raise_not_found_error configuration option to
|
362
|
+
false, which will cause a nil to be returned instead
|
363
|
+
of raising this error.
|
364
|
+
|
365
|
+
* \#1616 `Model.find_by` added which takes a hash of arugments to search
|
366
|
+
for in the database. If no single document is returned a DocumentNotFound
|
367
|
+
error is raised. (Piotr Jakubowski)
|
368
|
+
|
369
|
+
Band.find_by(name: "Depeche Mode")
|
370
|
+
|
371
|
+
* \#1606 Mongoid now enables autosave, like Active Record, when adding
|
372
|
+
an accepts_nested_attributes_for to a relation.
|
373
|
+
|
374
|
+
class Band
|
375
|
+
include Mongoid::Document
|
376
|
+
has_many :albums
|
377
|
+
accepts_nested_attributes_for :albums # This enables the autosave.
|
378
|
+
end
|
379
|
+
|
380
|
+
* \#1477 Mongoid now automatically protects the id and type attributes
|
381
|
+
from mass assignment. You can override this (not recommended) by redefining
|
382
|
+
them as accessible.
|
383
|
+
|
384
|
+
class Band
|
385
|
+
include Mongoid::Document
|
386
|
+
attr_accessible :id, :_id, :_type
|
387
|
+
end
|
388
|
+
|
389
|
+
* \#1459 The identity map can be disabled now for specific code execution
|
390
|
+
by passing options to the unit of work.
|
391
|
+
|
392
|
+
Mongoid.unit_of_work(disable: :all) do
|
393
|
+
# Disables the identity map on all threads for the block.
|
394
|
+
end
|
395
|
+
|
396
|
+
Mongoid.unit_of_work(disable: :current) do
|
397
|
+
# Disables the identity map on the current thread for the block.
|
398
|
+
end
|
399
|
+
|
400
|
+
Mongoid.unit_of_work do
|
401
|
+
# Business as usual.
|
402
|
+
end
|
403
|
+
|
404
|
+
* \#1355 Associations now can have safety options provided to them on single
|
405
|
+
document persistence operations.
|
406
|
+
|
407
|
+
band.albums.with(safe: true).push(album)
|
408
|
+
band.albums.with(safe: true).create(name: "Smiths")
|
409
|
+
|
410
|
+
album.with(safe: true).create_producer(name: "Flood")
|
411
|
+
|
412
|
+
* \#1348 Eager loading is now supported on many-to-many relations.
|
413
|
+
|
414
|
+
* \#1292 Remove attribute now unsets the attribute when the document is
|
415
|
+
saved instead of setting to nil.
|
416
|
+
|
417
|
+
band = Band.find(id)
|
418
|
+
band.remove_attribute(:label) # Uses $unset when the document is saved.
|
419
|
+
|
420
|
+
* \#1291 Mongoid database sessions are now connected to lazily, and are
|
421
|
+
completely thread safe. If a new thread is created, then a new database
|
422
|
+
session will be created for it.
|
423
|
+
|
424
|
+
* \#1291 Mongoid now supports any number of database connections as defined in
|
425
|
+
the mongoid.yml. For example you could have a local single server db, a
|
426
|
+
multi availablity zone replica set, and a shard cluster all in the same
|
427
|
+
application environment. Mongoid can connect to any session at any point in
|
428
|
+
time.
|
429
|
+
|
430
|
+
* \#1291 Mongoid now allows you to persist to whatever database or collection
|
431
|
+
you like at runtime, on a per-query or persistence operation basis by using
|
432
|
+
`with`.
|
433
|
+
|
434
|
+
Band.with(collection: "artists").create(name: "Depeche Mode")
|
435
|
+
band.with(database: "secondary).save!
|
436
|
+
Band.with(collection: "artists").where(name: "Depeche Mode")
|
437
|
+
|
438
|
+
* \#1291 You can now configure on a per-model basis where it's documents are
|
439
|
+
stored with the new and improved `store_in` macro.
|
440
|
+
|
441
|
+
class Band
|
442
|
+
include Mongoid::Document
|
443
|
+
store_in collection: "artists", database: "secondary", session: "replica"
|
444
|
+
end
|
445
|
+
|
446
|
+
This can be overridden, of course, at runtime via the `with` method.
|
447
|
+
|
448
|
+
* \#1212 Embedded documents can now be popped off a relation with persistence.
|
449
|
+
|
450
|
+
band.albums.pop # Pop 1 document and persist the removal.
|
451
|
+
band.albums.pop(3) # Pop 3 documents and persist the removal.
|
452
|
+
|
453
|
+
* \#1188 Relations now have existence predicates for simplified checking if the
|
454
|
+
relation is blank or not. (Andy Morris)
|
455
|
+
|
456
|
+
class Band
|
457
|
+
include Mongoid::Document
|
458
|
+
embeds_many :albums
|
459
|
+
embeds_one :label
|
460
|
+
end
|
461
|
+
|
462
|
+
band = Band.new
|
463
|
+
band.albums?
|
464
|
+
band.has_albums?
|
465
|
+
band.label?
|
466
|
+
band.has_label?
|
467
|
+
|
468
|
+
* \#1188 1-1 relations now have an :autobuild option to indicate if the
|
469
|
+
relation should automatically be build with empty attributes upon access
|
470
|
+
where the relation currently does not exist. Works on embeds_one,
|
471
|
+
embedded_in, has_one, belongs_to. (Andy Morris)
|
472
|
+
|
473
|
+
class Band
|
474
|
+
include Mongoid::Document
|
475
|
+
has_one :label, autobuild: true
|
476
|
+
end
|
477
|
+
|
478
|
+
band = Band.new
|
479
|
+
band.label # Returns a new label with empty attributes.
|
480
|
+
|
481
|
+
When using existence checks, autobuilding will not execute.
|
482
|
+
|
483
|
+
band = Band.new
|
484
|
+
band.label? # Returns false, does not autobuild on a check.
|
485
|
+
band.has_label? # Returns false, does not autobuild on a check.
|
486
|
+
|
487
|
+
* \#1081 Mongoid indexes both id and type as a compound index when providing
|
488
|
+
`index: true` to a polymorphic belongs_to.
|
489
|
+
|
490
|
+
class Comment
|
491
|
+
include Mongoid::Document
|
492
|
+
|
493
|
+
# Indexes commentable_id and commentable_type as a compound index.
|
494
|
+
belongs_to :commentable, polymorphic: true, index: true
|
495
|
+
end
|
496
|
+
|
497
|
+
* \#1053 Raise a `Mongoid::Errors::UnknownAttribute` instead of no method
|
498
|
+
when attempting to set a field that is not defined and allow dynamic
|
499
|
+
fields is false. (Cyril Mougel)
|
67
500
|
|
68
|
-
|
69
|
-
relation.
|
501
|
+
Mongoid.allow_dynamic_fields = false
|
70
502
|
|
71
|
-
|
72
|
-
|
503
|
+
class Person
|
504
|
+
include Mongoid::Document
|
505
|
+
field :title, type: String
|
506
|
+
end
|
73
507
|
|
74
|
-
|
508
|
+
Person.new.age = 50 # raises the UnknownAttribute error.
|
75
509
|
|
76
|
-
* \#
|
77
|
-
|
510
|
+
* \#772 Fields can now be flagged as readonly, which will only let their
|
511
|
+
values be set when the document is new.
|
78
512
|
|
79
|
-
|
513
|
+
class Band
|
514
|
+
include Mongoid::Document
|
515
|
+
field :name, type: String
|
516
|
+
field :genre, type: String
|
517
|
+
|
518
|
+
attr_readonly :name, :genre
|
519
|
+
end
|
520
|
+
|
521
|
+
Readonly values are ignored when attempting to set them on persisted
|
522
|
+
documents, with the exception of update_attribute and remove_attribute,
|
523
|
+
where errors will get raised.
|
524
|
+
|
525
|
+
band = Band.create(name: "Depeche Mode")
|
526
|
+
band.update_attribute(:name, "Smiths") # Raises ReadonlyAttribute error.
|
527
|
+
band.remove_attribute(:name) # Raises ReadonlyAttribute error.
|
528
|
+
|
529
|
+
|
530
|
+
### Major Changes (Backwards Incompatible)
|
531
|
+
|
532
|
+
* \#2039 Validating presence of a relation now checks both the relation and
|
533
|
+
the foreign key value.
|
534
|
+
|
535
|
+
* Indexing syntax has changed. The first parameter is now a hash of
|
536
|
+
name/direction pairs with an optional second hash parameter for
|
537
|
+
additional options.
|
538
|
+
|
539
|
+
Normal indexing with options, directions are either 1 or -1:
|
540
|
+
|
541
|
+
class Band
|
542
|
+
include Mongoid::Document
|
543
|
+
field :name, type: String
|
544
|
+
|
545
|
+
index({ name: 1 }, { unique: true, background: true })
|
546
|
+
end
|
547
|
+
|
548
|
+
Geospacial indexing needs "2d" as it's direction.
|
549
|
+
|
550
|
+
class Venue
|
551
|
+
include Mongoid::Document
|
552
|
+
field :location, type: Array
|
553
|
+
|
554
|
+
index location: "2d"
|
555
|
+
end
|
556
|
+
|
557
|
+
* Custom serializable fields have revamped. Your object no longer should
|
558
|
+
include `Mongoid::Fields::Serializable` - instead it only needs to
|
559
|
+
implement 3 methods: `#mongoize`, `.demongoize` and `.evolve`.
|
560
|
+
|
561
|
+
`#mongoize` is an instance method that transforms your object into
|
562
|
+
a mongo-friendly value.
|
563
|
+
|
564
|
+
`.demongoize` is a class method, that can take some data from mongo
|
565
|
+
and instantiate and object of your custom type.
|
566
|
+
|
567
|
+
`.evolve` is a class method, that can take any object, and
|
568
|
+
transform it for use in a `Mongoid::Criteria`.
|
569
|
+
|
570
|
+
An example of an implementation of this for `Range`:
|
571
|
+
|
572
|
+
class Range
|
573
|
+
|
574
|
+
def mongoize
|
575
|
+
{ "min" => first, "max" => last }
|
576
|
+
end
|
577
|
+
|
578
|
+
class << self
|
579
|
+
|
580
|
+
def demongoize(object)
|
581
|
+
Range.new(object["min"], object["max"])
|
582
|
+
end
|
583
|
+
|
584
|
+
def evolve(object)
|
585
|
+
{ "$gte" => object.first, "$lte" => object.last }
|
586
|
+
end
|
587
|
+
end
|
588
|
+
end
|
589
|
+
|
590
|
+
* `Document#changes` is no longer a hash with indifferent access.
|
591
|
+
|
592
|
+
* `after_initialize` callbacks no longer cascade to children if the option
|
593
|
+
is set.
|
594
|
+
|
595
|
+
* \#1865 `count` on the memory and mongo contexts now behave exactly the
|
596
|
+
same as Ruby's `count` on enumerable, and can take an object or a block.
|
597
|
+
This is optimized on the mongo context not to load everything in memory
|
598
|
+
with the exception of passing a block.
|
599
|
+
|
600
|
+
Band.where(name: "Tool").count
|
601
|
+
Band.where(name: "Tool").count(tool) # redundant.
|
602
|
+
Band.where(name: "Tool") do |doc|
|
603
|
+
doc.likes > 0
|
604
|
+
end
|
605
|
+
|
606
|
+
Note that although the signatures are the same for both the memory and
|
607
|
+
mongo contexts, it's recommended you only use the block syntax for the
|
608
|
+
memory context since the embedded documents are already loaded into
|
609
|
+
memory.
|
610
|
+
|
611
|
+
Also note that passing a boolean to take skip and limit into account
|
612
|
+
is no longer supported, as this is not necessarily a useful feature.
|
613
|
+
|
614
|
+
* The `autocreate_indexes` configuration option has been removed.
|
615
|
+
|
616
|
+
* `Model.defaults` no longer exists. You may get all defaults with a
|
617
|
+
combination of `Model.pre_processed_defaults` and
|
618
|
+
`Model.post_processed_defaults`.
|
619
|
+
|
620
|
+
Band.pre_processed_defaults
|
621
|
+
Band.post_processed_defaults
|
622
|
+
|
623
|
+
* `Model.identity` and `Model.key` have been removed. For custom ids,
|
624
|
+
users must now override the _id field.
|
625
|
+
|
626
|
+
When the default value is a proc, the default is applied *after* all
|
627
|
+
other attributes are set.
|
628
|
+
|
629
|
+
class Band
|
630
|
+
include Mongoid::Document
|
631
|
+
field :_id, type: String, default: ->{ name }
|
632
|
+
end
|
633
|
+
|
634
|
+
To have the default applied *before* other attributes, set `:pre_processed`
|
635
|
+
to true.
|
636
|
+
|
637
|
+
class Band
|
638
|
+
include Mongoid::Document
|
639
|
+
field :_id,
|
640
|
+
type: String,
|
641
|
+
pre_processed: true,
|
642
|
+
default: ->{ BSON::ObjectId.new.to_s }
|
643
|
+
end
|
644
|
+
|
645
|
+
* Custom application exceptions in various languages has been removed,
|
646
|
+
along with the `Mongoid.add_language` feature.
|
647
|
+
|
648
|
+
* Mongoid no longer supports 1.8 syntax. 1.9.x or other vms running in
|
649
|
+
1.9 mode is now only supported.
|
650
|
+
|
651
|
+
* \#1734 When searching for documents via `Model.find` with multiple ids,
|
652
|
+
Mongoid will raise an error if not *all* ids are found, and tell you
|
653
|
+
what the missing ones were. Previously the error only got raised if
|
654
|
+
nothing was returned.
|
655
|
+
|
656
|
+
* \#1675 Adding presence validation on a relation now enables autosave.
|
657
|
+
This is to ensure that when a new parent object is saved with a new
|
658
|
+
child and marked is valid, both are persisted to ensure a correct
|
659
|
+
state in the database.
|
660
|
+
|
661
|
+
* \#1491 Ensure empty translations returns an empty hash on access.
|
662
|
+
|
663
|
+
* \#1484 `Model#has_attribute?` now behaves the same as Active Record.
|
664
|
+
|
665
|
+
* \#1471 Mongoid no longer strips any level of precision off of times.
|
666
|
+
|
667
|
+
* \#1475 Active support's time zone is now used by default in time
|
668
|
+
serialization if it is defined.
|
669
|
+
|
670
|
+
* \#1342 `Model.find` and `model.relation.find` now only take a single or
|
671
|
+
multiple ids. The first/last/all with a conditions hash has been removed.
|
672
|
+
|
673
|
+
* \#1291 The mongoid.yml has been revamped completely, and upgrading
|
674
|
+
existing applications will greet you with some lovely Mongoid specific
|
675
|
+
configuration errors. You can re-generate a new mongoid.yml via the
|
676
|
+
existing rake task, which is commented to an insane degree to help you
|
677
|
+
with all the configuration possibilities.
|
678
|
+
|
679
|
+
* \#1291 The `persist_in_safe_mode` configuration option has been removed.
|
680
|
+
You must now tell a database session in the mongoid.yml whether or not
|
681
|
+
it should persist in safe mode by default.
|
682
|
+
|
683
|
+
production:
|
684
|
+
sessions:
|
685
|
+
default:
|
686
|
+
database: my_app_prod
|
687
|
+
hosts:
|
688
|
+
- db.app.com:27018
|
689
|
+
- db.app.com:27019
|
690
|
+
options:
|
691
|
+
consistency: :eventual
|
692
|
+
safe: true
|
693
|
+
|
694
|
+
* \#1291 `safely` and `unsafely` have been removed. Please now use `with`
|
695
|
+
to provide safe mode options at runtime.
|
696
|
+
|
697
|
+
Band.with(safe: true).create
|
698
|
+
band.with(safe: { w: 3 }).save!
|
699
|
+
Band.with(safe: false).create!
|
700
|
+
|
701
|
+
* \#1270 Relation macros have been changed to match their AR counterparts:
|
702
|
+
only :has_one, :has_many, :has_and_belongs_to_many, and :belongs_to
|
703
|
+
exist now.
|
704
|
+
|
705
|
+
* \#1268 `Model#new?` has been removed, developers must now always use
|
706
|
+
`Model#new_record?`.
|
707
|
+
|
708
|
+
* \#1182 A reload is no longer required to refresh a relation after setting
|
709
|
+
the value of the foreign key field for it. Note this behaves exactly as
|
710
|
+
Active Record.
|
711
|
+
|
712
|
+
If the id is set, but the document for it has not been persisted, accessing
|
713
|
+
the relation will return empty results.
|
714
|
+
|
715
|
+
If the id is set and it's document is persisted, accessing the relation
|
716
|
+
will return the document.
|
717
|
+
|
718
|
+
If the id is set, but the base document is not saved afterwards, then
|
719
|
+
reloading will return the document to it's original state.
|
720
|
+
|
721
|
+
* \#1093 Field serialization strategies have changed on Array, Hash, Integer
|
722
|
+
and Boolean to be more consistent and match AR where appropriate.
|
723
|
+
|
724
|
+
Serialization of arrays calls `Array.wrap(object)`
|
725
|
+
Serialization of hashes calls `Hash[object]` (to_hash on the object)
|
726
|
+
Serialization of integers always returns an int via `to_i`
|
727
|
+
Serialization of booleans defaults to false instead of nil.
|
728
|
+
|
729
|
+
* \#933 `:field.size` has been renamed to `:field.count` in criteria for
|
730
|
+
$size not to conflict with Symbol's size method.
|
731
|
+
|
732
|
+
* \#829/\#797 Mongoid scoping code has been completely rewritten, and now
|
733
|
+
matches the Active Record API. With this backwards incompatible change,
|
734
|
+
some methods have been removed or renamed.
|
735
|
+
|
736
|
+
Criteria#as_conditions and Criteria#fuse no longer exist.
|
737
|
+
|
738
|
+
Criteria#merge now only accepts another object that responds to
|
739
|
+
`to_criteria`.
|
740
|
+
|
741
|
+
Criteria#merge! now merges in another object without creating a new
|
742
|
+
criteria object.
|
743
|
+
|
744
|
+
Band.where(name: "Tool").merge!(criteria)
|
745
|
+
|
746
|
+
Named scopes and default scopes no longer take hashes as parameters.
|
747
|
+
From now on only criteria and procs wrapping criteria will be
|
748
|
+
accepted, and an error will be raised if the arguments are incorrect.
|
749
|
+
|
750
|
+
class Band
|
751
|
+
include Mongoid::Document
|
752
|
+
|
753
|
+
default_scope ->{ where(active: true) }
|
754
|
+
scope :inactive, where(active: false)
|
755
|
+
scope :invalid, where: { valid: false } # This will raise an error.
|
756
|
+
end
|
757
|
+
|
758
|
+
The 'named_scope' macro has been removed, from now on only use 'scope'.
|
759
|
+
|
760
|
+
Model.unscoped now accepts a block which will not allow default scoping
|
761
|
+
to be applied for any calls inside the block.
|
762
|
+
|
763
|
+
Band.unscoped do
|
764
|
+
Band.scoped.where(name: "Ministry")
|
765
|
+
end
|
766
|
+
|
767
|
+
Model.scoped now takes options that will be set directly on the criteria
|
768
|
+
options hash.
|
769
|
+
|
770
|
+
Band.scoped(skip: 10, limit: 20)
|
80
771
|
|
81
772
|
### Resolved Issues
|
82
773
|
|
83
|
-
*
|
84
|
-
|
85
|
-
|
774
|
+
* \#1997 Cascading callbacks should be able to halt the callback chain when
|
775
|
+
terminating.
|
776
|
+
|
777
|
+
* \#1972 `added`, `loaded`, and `unloaded` can now be valid scope names on a
|
778
|
+
document that is part of a 1-n relation.
|
86
779
|
|
87
|
-
* \#
|
780
|
+
* \#1952/#1950 `#all_in` behaviour on embedded documents now properly matches
|
781
|
+
root documents when passing an empty array. (Hans Hasselberg)
|
782
|
+
|
783
|
+
* \#1941/#1939 `Model.find_by` now returns nil if raise not found error is
|
784
|
+
set to false. (Hans Hasselberg)
|
785
|
+
|
786
|
+
* \#1859/#1860 `Model#remove_attribute` now properly unsets on embedded
|
787
|
+
documents. (Anton Onyshchenko)
|
788
|
+
|
789
|
+
* \#1852 Ensure no infinite recursion on cascading callbacks. (Ara Howard)
|
790
|
+
|
791
|
+
* \#1823 `Relation#includes?` now properly works with identity map enabled.
|
792
|
+
|
793
|
+
* \#1810 `Model#changed?` no longer returns true when hash and array fields
|
794
|
+
have only been accessed.
|
795
|
+
|
796
|
+
* \#1876/\#1782 Allow dot notation in embeds many criteria queries.
|
797
|
+
(Cyril Mougel)
|
798
|
+
|
799
|
+
* \#1745 Fixed batch clear to work within attribute setting.
|
800
|
+
|
801
|
+
* \#1718 Ensure consistency of #first/#last in relations - they now always
|
802
|
+
match first/last in the database, but opts for in memory first.
|
803
|
+
|
804
|
+
* \#1692/\#1376 `Model#updateattributes` and `Model#update_attributes!` now
|
805
|
+
accept assignment options. (Hans Hasselberg)
|
806
|
+
|
807
|
+
* \#1688/\#1207 Don't require namespacing when providing class name on
|
808
|
+
relation macros inside the namespace. (Hans Hasselberg)
|
809
|
+
|
810
|
+
* \#1665/\#1672 Expand complex criteria in nested criteria selectors, like
|
811
|
+
\#matches. (Hans Hasselberg)
|
812
|
+
|
813
|
+
* \#1335 Don't add id sorting criteria to first/last is there is already
|
814
|
+
sorting options on the criteria.
|
815
|
+
|
816
|
+
* \#1321 Referenced many enumerable targets are now hash-backed, preventing
|
817
|
+
duplicates in a more efficient manner.
|
818
|
+
|
819
|
+
* \#1135 DateTimes now properly get time zones on deserialization.
|
820
|
+
|
821
|
+
* \#1031 Mongoid now serializes values in Array fields to their proper
|
822
|
+
Mongo-friendly values when possible.
|
823
|
+
|
824
|
+
* \#685 Attempting to use versioning with embedded documents will now
|
825
|
+
raise a proper error alerting the developer this is not allowed.
|
826
|
+
|
827
|
+
## 2.4.11 (branch: 2.4.0-stable)
|
828
|
+
|
829
|
+
### Resolved Issues
|
88
830
|
|
89
831
|
* \#2038 Allow inverse relations to be determined by foreign keys alone
|
90
832
|
if defined on both sides, not just an inverse_of declaration.
|
@@ -95,8 +837,6 @@ For instructions on upgrading to newer versions, visit
|
|
95
837
|
* \#2008 Presence validation should hit the db to check validity if the
|
96
838
|
relation in memory is blank.
|
97
839
|
|
98
|
-
* \#2006 Allow excluding only the _id field post execution of an #only call.
|
99
|
-
|
100
840
|
## 2.4.10
|
101
841
|
|
102
842
|
### Resolved Issues
|
@@ -113,8 +853,6 @@ For instructions on upgrading to newer versions, visit
|
|
113
853
|
* \#1987 Don't double-insert documents into identity map when eager loading
|
114
854
|
twice inside the unit of work.
|
115
855
|
|
116
|
-
* \#1976 Don't execute eager load queries when base query is empty.
|
117
|
-
|
118
856
|
* \#1953 Uniqueness validation now works on localized fields.
|
119
857
|
|
120
858
|
* \#1936 Allow setting n levels deep embedded documents atomically without
|
@@ -156,7 +894,7 @@ For instructions on upgrading to newer versions, visit
|
|
156
894
|
* \#1916/\#1913 Uniqueness validation no longer is affected by the default
|
157
895
|
scope. (Hans Hasselberg)
|
158
896
|
|
159
|
-
* \#
|
897
|
+
* \#1943 Ensure numericality validation works for big decimals.
|
160
898
|
|
161
899
|
## 2.4.8
|
162
900
|
|
@@ -267,8 +1005,8 @@ For instructions on upgrading to newer versions, visit
|
|
267
1005
|
|
268
1006
|
* \#1676 Allow eager loading to work as a default scope.
|
269
1007
|
|
270
|
-
* \#1665
|
271
|
-
|
1008
|
+
* \#1665/\#1672 Expand complex criteria in nested criteria selectors, like
|
1009
|
+
\#matches. (Hans Hasselberg)
|
272
1010
|
|
273
1011
|
* \#1668 Ensure Mongoid logger exists before calling warn. (Rémy Coutable)
|
274
1012
|
|
@@ -276,10 +1014,10 @@ For instructions on upgrading to newer versions, visit
|
|
276
1014
|
|
277
1015
|
* \#1659 Clear delayed atomic sets when resetting the same embedded relation.
|
278
1016
|
|
279
|
-
* \#1656
|
1017
|
+
* \#1656/\#1657 Don't hit database for uniqueness validation if BOTH scope
|
280
1018
|
and attribute hasn't changed. (priyaaank)
|
281
1019
|
|
282
|
-
* \#1205
|
1020
|
+
* \#1205/\#1642 When limiting fields returned from the database via
|
283
1021
|
`Criteria#only` and `Criteria#without` and then subsequently saving
|
284
1022
|
the document. Default values no longer override excluded fields.
|
285
1023
|
|
@@ -290,16 +1028,18 @@ For instructions on upgrading to newer versions, visit
|
|
290
1028
|
* \#1647 DateTime serialization when already in UTC does not convert to
|
291
1029
|
local time.
|
292
1030
|
|
1031
|
+
* \#1641/\#1639 Mongoid.observer.disable :all now behaves as AR does.
|
1032
|
+
|
293
1033
|
* \#1640 Update consumers should be tied to the name of the collection
|
294
1034
|
they persist to, not the name of the class.
|
295
1035
|
|
296
|
-
* \#1636 Scopes no longer modify parent class scopes when subclassing.
|
1036
|
+
* \#1637/\#1636 Scopes no longer modify parent class scopes when subclassing.
|
297
1037
|
(Hans Hasselberg)
|
298
1038
|
|
299
1039
|
* \#1629 $all and $in criteria on embedded many relations now properly
|
300
1040
|
handles regex searches and elements of varying length. (Douwe Maan)
|
301
1041
|
|
302
|
-
* \#1623 Default scopes no longer break Mongoid::Versioning.
|
1042
|
+
* \#1623/\#1634 Default scopes no longer break Mongoid::Versioning.
|
303
1043
|
(Hans Hasselberg)
|
304
1044
|
|
305
1045
|
* \#1605 Fix regression of rescue responses, Rails 3.2
|
@@ -308,6 +1048,10 @@ For instructions on upgrading to newer versions, visit
|
|
308
1048
|
|
309
1049
|
### Resolved Issues
|
310
1050
|
|
1051
|
+
* \#1628 _type field can once again be included in serialization to json
|
1052
|
+
or xml as a global option with `include_type_for_serialization`.
|
1053
|
+
(Roman Shterenzon)
|
1054
|
+
|
311
1055
|
* \#1627 Validating format now works properly with localized fields.
|
312
1056
|
(Douwe Maan)
|
313
1057
|
|