mongoid 6.4.2 → 7.0.3
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/config/locales/en.yml +17 -0
- data/lib/mongoid/association/accessors.rb +339 -0
- data/lib/mongoid/{relations/binding.rb → association/bindable.rb} +32 -52
- data/lib/mongoid/association/builders.rb +92 -0
- data/lib/mongoid/{relations/constraint.rb → association/constrainable.rb} +11 -22
- data/lib/mongoid/association/depending.rb +137 -0
- data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
- data/lib/mongoid/{relations → association}/embedded/batchable.rb +19 -19
- data/lib/mongoid/association/embedded/cyclic.rb +109 -0
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +56 -0
- data/lib/mongoid/{relations/builders/embedded/in.rb → association/embedded/embedded_in/buildable.rb} +12 -6
- data/lib/mongoid/association/embedded/embedded_in/proxy.rb +121 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +154 -0
- data/lib/mongoid/{relations/bindings/embedded/many.rb → association/embedded/embeds_many/binding.rb} +11 -9
- data/lib/mongoid/{relations/builders/embedded/many.rb → association/embedded/embeds_many/buildable.rb} +13 -7
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +552 -0
- data/lib/mongoid/association/embedded/embeds_many.rb +210 -0
- data/lib/mongoid/{relations/bindings/embedded/one.rb → association/embedded/embeds_one/binding.rb} +12 -10
- data/lib/mongoid/{relations/builders/embedded/one.rb → association/embedded/embeds_one/buildable.rb} +13 -7
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +130 -0
- data/lib/mongoid/association/embedded/embeds_one.rb +173 -0
- data/lib/mongoid/association/embedded.rb +4 -0
- data/lib/mongoid/association/macros.rb +204 -0
- data/lib/mongoid/{relations → association}/many.rb +19 -49
- data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
- data/lib/mongoid/association/nested/many.rb +200 -0
- data/lib/mongoid/association/nested/nested_buildable.rb +72 -0
- data/lib/mongoid/association/nested/one.rb +127 -0
- data/lib/mongoid/association/nested.rb +15 -0
- data/lib/mongoid/{relations → association}/one.rb +6 -6
- data/lib/mongoid/association/options.rb +152 -0
- data/lib/mongoid/{relations → association}/proxy.rb +31 -58
- data/lib/mongoid/association/referenced/auto_save.rb +79 -0
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +87 -0
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +46 -0
- data/lib/mongoid/association/referenced/belongs_to/eager.rb +36 -0
- data/lib/mongoid/association/referenced/belongs_to/proxy.rb +136 -0
- data/lib/mongoid/association/referenced/belongs_to.rb +248 -0
- data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
- data/lib/mongoid/association/referenced/eager.rb +162 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +71 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +52 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +312 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -0
- data/lib/mongoid/{relations/bindings/referenced/many.rb → association/referenced/has_many/binding.rb} +6 -5
- data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
- data/lib/mongoid/association/referenced/has_many/eager.rb +43 -0
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +510 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +578 -0
- data/lib/mongoid/association/referenced/has_many.rb +275 -0
- data/lib/mongoid/{relations/bindings/referenced/one.rb → association/referenced/has_one/binding.rb} +11 -8
- data/lib/mongoid/association/referenced/has_one/buildable.rb +60 -0
- data/lib/mongoid/association/referenced/has_one/eager.rb +35 -0
- data/lib/mongoid/{relations/builders/nested_attributes/one.rb → association/referenced/has_one/nested_builder.rb} +9 -9
- data/lib/mongoid/association/referenced/has_one/proxy.rb +113 -0
- data/lib/mongoid/association/referenced/has_one.rb +204 -0
- data/lib/mongoid/association/referenced/syncable.rb +170 -0
- data/lib/mongoid/association/referenced.rb +7 -0
- data/lib/mongoid/{relations → association}/reflections.rb +21 -17
- data/lib/mongoid/association/relatable.rb +509 -0
- data/lib/mongoid/{relations.rb → association.rb} +57 -56
- data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
- data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
- data/lib/mongoid/atomic.rb +4 -4
- data/lib/mongoid/attributes/dynamic.rb +2 -2
- data/lib/mongoid/attributes/nested.rb +22 -11
- data/lib/mongoid/attributes/processing.rb +2 -2
- data/lib/mongoid/attributes/readonly.rb +2 -4
- data/lib/mongoid/attributes.rb +22 -13
- data/lib/mongoid/changeable.rb +1 -1
- data/lib/mongoid/clients/options.rb +7 -5
- data/lib/mongoid/composable.rb +4 -4
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/geo_near.rb +1 -1
- data/lib/mongoid/contextual/memory.rb +21 -3
- data/lib/mongoid/contextual/mongo.rb +10 -8
- data/lib/mongoid/copyable.rb +7 -6
- data/lib/mongoid/criteria/includable.rb +14 -14
- data/lib/mongoid/criteria/modifiable.rb +8 -14
- data/lib/mongoid/criteria/options.rb +2 -2
- data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -1
- data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
- data/lib/mongoid/criteria/queryable/selectable.rb +37 -7
- data/lib/mongoid/criteria.rb +2 -2
- data/lib/mongoid/document.rb +15 -6
- data/lib/mongoid/errors/invalid_dependent_strategy.rb +32 -0
- data/lib/mongoid/errors/invalid_relation_option.rb +29 -0
- data/lib/mongoid/errors/unknown_model.rb +25 -0
- data/lib/mongoid/errors.rb +3 -0
- data/lib/mongoid/extensions/array.rb +5 -5
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +5 -2
- data/lib/mongoid/extensions/object.rb +4 -4
- data/lib/mongoid/extensions/range.rb +1 -0
- data/lib/mongoid/extensions/regexp.rb +1 -0
- data/lib/mongoid/extensions.rb +0 -4
- data/lib/mongoid/factory.rb +13 -3
- data/lib/mongoid/fields/foreign_key.rb +5 -5
- data/lib/mongoid/fields/standard.rb +2 -14
- data/lib/mongoid/fields/validators/macro.rb +1 -1
- data/lib/mongoid/fields.rb +3 -3
- data/lib/mongoid/indexable.rb +4 -1
- data/lib/mongoid/interceptable.rb +5 -5
- data/lib/mongoid/matchable/and.rb +1 -1
- data/lib/mongoid/matchable/elem_match.rb +9 -3
- data/lib/mongoid/matchable/eq.rb +22 -0
- data/lib/mongoid/matchable/ne.rb +1 -1
- data/lib/mongoid/matchable.rb +3 -1
- data/lib/mongoid/persistable/deletable.rb +7 -6
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +57 -12
- data/lib/mongoid/persistable.rb +4 -5
- data/lib/mongoid/persistence_context.rb +20 -5
- data/lib/mongoid/query_cache.rb +8 -4
- data/lib/mongoid/railtie.rb +17 -0
- data/lib/mongoid/railties/controller_runtime.rb +86 -0
- data/lib/mongoid/scopable.rb +3 -3
- data/lib/mongoid/serializable.rb +4 -4
- data/lib/mongoid/shardable.rb +1 -1
- data/lib/mongoid/threaded.rb +36 -0
- data/lib/mongoid/touchable.rb +102 -0
- data/lib/mongoid/traversable.rb +3 -3
- data/lib/mongoid/validatable/presence.rb +2 -2
- data/lib/mongoid/validatable/uniqueness.rb +4 -4
- data/lib/mongoid/validatable.rb +8 -8
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -0
- data/spec/app/models/animal.rb +2 -1
- data/spec/app/models/bomb.rb +1 -1
- data/spec/app/models/message.rb +1 -1
- data/spec/app/models/minim.rb +7 -0
- data/spec/app/models/person.rb +5 -2
- data/spec/app/models/shipment_address.rb +1 -0
- data/spec/app/models/store_as_dup_test3.rb +7 -0
- data/spec/app/models/store_as_dup_test4.rb +7 -0
- data/spec/app/models/updatable.rb +7 -0
- data/spec/app/models/vertex.rb +6 -0
- data/spec/app/models/wiki_page.rb +1 -1
- data/spec/config/mongoid.yml +13 -3
- data/spec/integration/associations/belongs_to_spec.rb +13 -0
- data/spec/lite_spec_helper.rb +56 -0
- data/spec/mongoid/{relations → association}/accessors_spec.rb +40 -1
- data/spec/mongoid/{relations → association}/auto_save_spec.rb +60 -12
- data/spec/mongoid/{relations → association}/builders_spec.rb +1 -1
- data/spec/mongoid/association/constrainable_spec.rb +115 -0
- data/spec/mongoid/{relations → association}/counter_cache_spec.rb +1 -1
- data/spec/mongoid/association/depending_spec.rb +866 -0
- data/spec/mongoid/{relations → association}/eager_spec.rb +12 -12
- data/spec/mongoid/{relations → association/embedded}/cyclic_spec.rb +1 -1
- data/spec/mongoid/{relations/bindings/embedded/in_spec.rb → association/embedded/embedded_in/binding_spec.rb} +13 -13
- data/spec/mongoid/{relations/builders/embedded/in_spec.rb → association/embedded/embedded_in/buildable_spec.rb} +9 -9
- data/spec/mongoid/{relations/embedded/in_spec.rb → association/embedded/embedded_in/proxy_spec.rb} +5 -77
- data/spec/mongoid/association/embedded/embedded_in_spec.rb +843 -0
- data/spec/mongoid/{relations/bindings/embedded/many_spec.rb → association/embedded/embeds_many/binding_spec.rb} +3 -3
- data/spec/mongoid/{relations/builders/embedded/many_spec.rb → association/embedded/embeds_many/buildable_spec.rb} +17 -45
- data/spec/mongoid/{relations/embedded/many_spec.rb → association/embedded/embeds_many/proxy_spec.rb} +124 -182
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +852 -0
- data/spec/mongoid/{relations/bindings/embedded/one_spec.rb → association/embedded/embeds_one/binding_spec.rb} +4 -4
- data/spec/mongoid/{relations/builders/embedded/one_spec.rb → association/embedded/embeds_one/buildable_spec.rb} +14 -34
- data/spec/mongoid/{relations/embedded/one_spec.rb → association/embedded/embeds_one/proxy_spec.rb} +39 -84
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +908 -0
- data/spec/mongoid/{relations → association}/macros_spec.rb +148 -73
- data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/many_spec.rb +16 -19
- data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/one_spec.rb +17 -20
- data/spec/mongoid/association/options_spec.rb +1321 -0
- data/spec/mongoid/{relations → association}/polymorphic_spec.rb +66 -34
- data/spec/mongoid/{relations/bindings/referenced/in_spec.rb → association/referenced/belongs_to/binding_spec.rb} +7 -7
- data/spec/mongoid/{relations/builders/referenced/in_spec.rb → association/referenced/belongs_to/buildable_spec.rb} +46 -79
- data/spec/mongoid/{relations/eager/belongs_to_spec.rb → association/referenced/belongs_to/eager_spec.rb} +33 -14
- data/spec/mongoid/{relations/referenced/in_spec.rb → association/referenced/belongs_to/proxy_spec.rb} +57 -91
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +2006 -0
- data/spec/mongoid/{relations/bindings/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/binding_spec.rb} +5 -5
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +121 -0
- data/spec/mongoid/{relations/eager/has_and_belongs_to_many_spec.rb → association/referenced/has_and_belongs_to_many/eager_spec.rb} +26 -7
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +56 -0
- data/spec/mongoid/{relations/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/proxy_spec.rb} +107 -98
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +26 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1027 -0
- data/spec/mongoid/{relations/bindings/referenced/many_spec.rb → association/referenced/has_many/binding_spec.rb} +5 -5
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +119 -0
- data/spec/mongoid/{relations/eager/has_many_spec.rb → association/referenced/has_many/eager_spec.rb} +26 -11
- data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +122 -1
- data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
- data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
- data/spec/mongoid/association/referenced/has_many_models.rb +37 -0
- data/spec/mongoid/association/referenced/has_many_spec.rb +1225 -0
- data/spec/mongoid/{relations/bindings/referenced/one_spec.rb → association/referenced/has_one/binding_spec.rb} +4 -4
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +113 -0
- data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb} +10 -10
- data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb} +9 -109
- data/spec/mongoid/association/referenced/has_one_models.rb +48 -0
- data/spec/mongoid/association/referenced/has_one_spec.rb +1350 -0
- data/spec/mongoid/{relations → association}/reflections_spec.rb +1 -12
- data/spec/mongoid/{relations/synchronization_spec.rb → association/syncable_spec.rb} +4 -2
- data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
- data/spec/mongoid/atomic/modifiers_spec.rb +2 -2
- data/spec/mongoid/atomic_spec.rb +4 -4
- data/spec/mongoid/attributes/nested_spec.rb +29 -11
- data/spec/mongoid/attributes/readonly_spec.rb +80 -125
- data/spec/mongoid/attributes_spec.rb +38 -2
- data/spec/mongoid/clients/factory_spec.rb +24 -18
- data/spec/mongoid/clients/options_spec.rb +58 -44
- data/spec/mongoid/clients/sessions_spec.rb +1 -1
- data/spec/mongoid/clients/transactions_spec.rb +369 -0
- data/spec/mongoid/clients_spec.rb +68 -8
- data/spec/mongoid/config_spec.rb +27 -1
- data/spec/mongoid/contextual/memory_spec.rb +19 -0
- data/spec/mongoid/contextual/mongo_spec.rb +33 -5
- data/spec/mongoid/copyable_spec.rb +90 -6
- data/spec/mongoid/copyable_spec_models.rb +17 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +183 -60
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +43 -0
- data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +42 -3
- data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
- data/spec/mongoid/criteria/scopable_spec.rb +81 -0
- data/spec/mongoid/criteria_spec.rb +33 -18
- data/spec/mongoid/document_spec.rb +83 -4
- data/spec/mongoid/extensions/array_spec.rb +11 -15
- data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
- data/spec/mongoid/extensions/hash_spec.rb +18 -1
- data/spec/mongoid/extensions/object_spec.rb +7 -11
- data/spec/mongoid/extensions/range_spec.rb +7 -0
- data/spec/mongoid/extensions/regexp_spec.rb +23 -0
- data/spec/mongoid/factory_spec.rb +19 -1
- data/spec/mongoid/fields/foreign_key_spec.rb +24 -32
- data/spec/mongoid/fields_spec.rb +2 -2
- data/spec/mongoid/findable_spec.rb +1 -1
- data/spec/mongoid/indexable_spec.rb +18 -8
- data/spec/mongoid/interceptable_spec.rb +22 -1
- data/spec/mongoid/matchable/elem_match_spec.rb +20 -0
- data/spec/mongoid/matchable/eq_spec.rb +48 -0
- data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
- data/spec/mongoid/persistable/savable_spec.rb +2 -2
- data/spec/mongoid/persistable/settable_spec.rb +95 -10
- data/spec/mongoid/persistable_spec.rb +21 -6
- data/spec/mongoid/persistence_context_spec.rb +1 -1
- data/spec/mongoid/query_cache_spec.rb +61 -22
- data/spec/mongoid/relations/proxy_spec.rb +124 -124
- data/spec/mongoid/scopable_spec.rb +13 -0
- data/spec/mongoid/shardable_spec.rb +32 -12
- data/spec/mongoid/threaded_spec.rb +68 -0
- data/spec/mongoid/{relations/touchable_spec.rb → touchable_spec.rb} +40 -1
- data/spec/mongoid/validatable/associated_spec.rb +1 -1
- data/spec/mongoid/validatable/presence_spec.rb +7 -6
- data/spec/mongoid/validatable_spec.rb +1 -1
- data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
- data/spec/spec_helper.rb +51 -25
- data/spec/support/constraints.rb +101 -0
- data/spec/support/macros.rb +20 -0
- data/spec/support/spec_config.rb +39 -0
- metadata +602 -582
- checksums.yaml.gz.sig +0 -2
- data/lib/mongoid/relations/accessors.rb +0 -267
- data/lib/mongoid/relations/auto_save.rb +0 -94
- data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
- data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
- data/lib/mongoid/relations/bindings.rb +0 -9
- data/lib/mongoid/relations/builder.rb +0 -57
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
- data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
- data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
- data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
- data/lib/mongoid/relations/builders.rb +0 -106
- data/lib/mongoid/relations/cascading/delete.rb +0 -44
- data/lib/mongoid/relations/cascading/destroy.rb +0 -43
- data/lib/mongoid/relations/cascading/nullify.rb +0 -35
- data/lib/mongoid/relations/cascading/restrict.rb +0 -39
- data/lib/mongoid/relations/cascading.rb +0 -56
- data/lib/mongoid/relations/conversions.rb +0 -34
- data/lib/mongoid/relations/counter_cache.rb +0 -160
- data/lib/mongoid/relations/cyclic.rb +0 -107
- data/lib/mongoid/relations/eager/base.rb +0 -153
- data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
- data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
- data/lib/mongoid/relations/eager/has_many.rb +0 -38
- data/lib/mongoid/relations/eager/has_one.rb +0 -30
- data/lib/mongoid/relations/embedded/in.rb +0 -241
- data/lib/mongoid/relations/embedded/many.rb +0 -683
- data/lib/mongoid/relations/embedded/one.rb +0 -235
- data/lib/mongoid/relations/macros.rb +0 -367
- data/lib/mongoid/relations/metadata.rb +0 -1179
- data/lib/mongoid/relations/nested_builder.rb +0 -74
- data/lib/mongoid/relations/options.rb +0 -49
- data/lib/mongoid/relations/polymorphic.rb +0 -39
- data/lib/mongoid/relations/referenced/in.rb +0 -304
- data/lib/mongoid/relations/referenced/many.rb +0 -812
- data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
- data/lib/mongoid/relations/referenced/one.rb +0 -290
- data/lib/mongoid/relations/synchronization.rb +0 -169
- data/lib/mongoid/relations/targets/enumerable.rb +0 -493
- data/lib/mongoid/relations/targets.rb +0 -2
- data/lib/mongoid/relations/touchable.rb +0 -97
- data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
- data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
- data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
- data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
- data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
- data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
- data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
- data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
- data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
- data/spec/mongoid/relations/cascading_spec.rb +0 -355
- data/spec/mongoid/relations/constraint_spec.rb +0 -75
- data/spec/mongoid/relations/conversions_spec.rb +0 -128
- data/spec/mongoid/relations/metadata_spec.rb +0 -1985
- data/spec/mongoid/relations/options_spec.rb +0 -35
- data.tar.gz.sig +0 -0
- metadata.gz.sig +0 -0
- /data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
data/lib/mongoid/atomic.rb
CHANGED
|
@@ -36,7 +36,7 @@ module Mongoid
|
|
|
36
36
|
# @since 2.2.0
|
|
37
37
|
def add_atomic_pull(document)
|
|
38
38
|
document.flagged_for_destroy = true
|
|
39
|
-
(delayed_atomic_pulls[document.
|
|
39
|
+
(delayed_atomic_pulls[document.association_name.to_s] ||= []).push(document)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# Add an atomic unset for the document.
|
|
@@ -51,7 +51,7 @@ module Mongoid
|
|
|
51
51
|
# @since 3.0.0
|
|
52
52
|
def add_atomic_unset(document)
|
|
53
53
|
document.flagged_for_destroy = true
|
|
54
|
-
(delayed_atomic_unsets[document.
|
|
54
|
+
(delayed_atomic_unsets[document.association_name.to_s] ||= []).push(document)
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
# Returns path of the attribute for modification
|
|
@@ -189,7 +189,7 @@ module Mongoid
|
|
|
189
189
|
#
|
|
190
190
|
# @since 2.1.0
|
|
191
191
|
def atomic_paths
|
|
192
|
-
@atomic_paths ||=
|
|
192
|
+
@atomic_paths ||= _association ? _association.path(self) : Atomic::Paths::Root.new(self)
|
|
193
193
|
end
|
|
194
194
|
|
|
195
195
|
# Get all the attributes that need to be pulled.
|
|
@@ -218,7 +218,7 @@ module Mongoid
|
|
|
218
218
|
# @example Get the pushes.
|
|
219
219
|
# person.atomic_pushes
|
|
220
220
|
#
|
|
221
|
-
# @return [ Hash ] The
|
|
221
|
+
# @return [ Hash ] The $push and $each operations.
|
|
222
222
|
#
|
|
223
223
|
# @since 2.1.0
|
|
224
224
|
def atomic_pushes
|
|
@@ -41,7 +41,7 @@ module Mongoid
|
|
|
41
41
|
class_eval <<-READER, __FILE__, __LINE__ + 1
|
|
42
42
|
def #{name}
|
|
43
43
|
attribute_will_change!(#{name.inspect})
|
|
44
|
-
|
|
44
|
+
read_raw_attribute(#{name.inspect})
|
|
45
45
|
end
|
|
46
46
|
READER
|
|
47
47
|
end
|
|
@@ -147,7 +147,7 @@ module Mongoid
|
|
|
147
147
|
getter = attr.reader
|
|
148
148
|
define_dynamic_reader(getter)
|
|
149
149
|
attribute_will_change!(attr.reader)
|
|
150
|
-
|
|
150
|
+
read_raw_attribute(getter)
|
|
151
151
|
end
|
|
152
152
|
end
|
|
153
153
|
end
|
|
@@ -42,21 +42,27 @@ module Mongoid
|
|
|
42
42
|
# to a class method to reject documents with.
|
|
43
43
|
# @option *args [ Integer ] :limit The max number to create.
|
|
44
44
|
# @option *args [ true, false ] :update_only Only update existing docs.
|
|
45
|
+
# @options *args [ true, false ] :autosave Whether autosave should be enabled on the
|
|
46
|
+
# association. Note that since the default is true, setting autosave to nil will still
|
|
47
|
+
# enable it.
|
|
45
48
|
def accepts_nested_attributes_for(*args)
|
|
46
|
-
options = args.extract_options
|
|
49
|
+
options = args.extract_options!.dup
|
|
50
|
+
options[:autosave] = true if options[:autosave].nil?
|
|
51
|
+
|
|
47
52
|
options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
|
|
48
53
|
args.each do |name|
|
|
49
54
|
meth = "#{name}_attributes="
|
|
50
55
|
self.nested_attributes["#{name}_attributes"] = meth
|
|
51
|
-
|
|
52
|
-
raise Errors::NestedAttributesMetadataNotFound.new(self, name) unless
|
|
53
|
-
autosave_nested_attributes(
|
|
56
|
+
association = relations[name.to_s]
|
|
57
|
+
raise Errors::NestedAttributesMetadataNotFound.new(self, name) unless association
|
|
58
|
+
autosave_nested_attributes(association) if options[:autosave]
|
|
59
|
+
|
|
54
60
|
re_define_method(meth) do |attrs|
|
|
55
61
|
_assigning do
|
|
56
|
-
if
|
|
57
|
-
options = options.merge!(:class_name => self.send(
|
|
62
|
+
if association.polymorphic? and association.inverse_type
|
|
63
|
+
options = options.merge!(:class_name => self.send(association.inverse_type))
|
|
58
64
|
end
|
|
59
|
-
|
|
65
|
+
association.nested_builder(attrs, options).build(self)
|
|
60
66
|
end
|
|
61
67
|
end
|
|
62
68
|
end
|
|
@@ -71,12 +77,17 @@ module Mongoid
|
|
|
71
77
|
# @example Add the autosave if appropriate.
|
|
72
78
|
# Person.autosave_nested_attributes(metadata)
|
|
73
79
|
#
|
|
74
|
-
# @param [
|
|
80
|
+
# @param [ Association ] association The existing association metadata.
|
|
75
81
|
#
|
|
76
82
|
# @since 3.1.4
|
|
77
|
-
def autosave_nested_attributes(
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
def autosave_nested_attributes(association)
|
|
84
|
+
# In order for the autosave functionality to work properly, the association needs to be
|
|
85
|
+
# marked as autosave despite the fact that the option isn't present. Because the method
|
|
86
|
+
# Association#autosave? is implemented by checking the autosave option, this is the most
|
|
87
|
+
# straightforward way to mark it.
|
|
88
|
+
if association.autosave? || (association.options[:autosave].nil? && !association.embedded?)
|
|
89
|
+
association.options[:autosave] = true
|
|
90
|
+
Association::Referenced::AutoSave.define_autosave!(association)
|
|
80
91
|
end
|
|
81
92
|
end
|
|
82
93
|
end
|
|
@@ -133,9 +133,9 @@ module Mongoid
|
|
|
133
133
|
# @since 2.0.0.rc.7
|
|
134
134
|
def process_relations
|
|
135
135
|
pending_relations.each_pair do |name, value|
|
|
136
|
-
|
|
136
|
+
association = relations[name]
|
|
137
137
|
if value.is_a?(Hash)
|
|
138
|
-
|
|
138
|
+
association.nested_builder(value, {}).build(self)
|
|
139
139
|
else
|
|
140
140
|
send("#{name}=", value)
|
|
141
141
|
end
|
|
@@ -22,17 +22,15 @@ module Mongoid
|
|
|
22
22
|
# readonly.
|
|
23
23
|
#
|
|
24
24
|
# @since 3.0.0
|
|
25
|
-
#
|
|
26
|
-
# @deprecated Use #as_writable_attribute! instead.
|
|
27
25
|
def attribute_writable?(name)
|
|
28
|
-
new_record? || !readonly_attributes.include?(
|
|
26
|
+
new_record? || (!readonly_attributes.include?(name) && _loaded?(name))
|
|
29
27
|
end
|
|
30
28
|
|
|
31
29
|
private
|
|
32
30
|
|
|
33
31
|
def as_writable_attribute!(name, value = :nil)
|
|
34
32
|
normalized_name = database_field_name(name)
|
|
35
|
-
if
|
|
33
|
+
if attribute_writable?(normalized_name)
|
|
36
34
|
yield(normalized_name)
|
|
37
35
|
else
|
|
38
36
|
raise Errors::ReadonlyAttribute.new(name, value)
|
data/lib/mongoid/attributes.rb
CHANGED
|
@@ -29,7 +29,7 @@ module Mongoid
|
|
|
29
29
|
#
|
|
30
30
|
# @since 1.0.0
|
|
31
31
|
def attribute_present?(name)
|
|
32
|
-
attribute =
|
|
32
|
+
attribute = read_raw_attribute(name)
|
|
33
33
|
!attribute.blank? || attribute == false
|
|
34
34
|
rescue ActiveModel::MissingAttributeError
|
|
35
35
|
false
|
|
@@ -92,21 +92,15 @@ module Mongoid
|
|
|
92
92
|
#
|
|
93
93
|
# @since 1.0.0
|
|
94
94
|
def read_attribute(name)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
end
|
|
99
|
-
if hash_dot_syntax?(normalized)
|
|
100
|
-
attributes.__nested__(normalized)
|
|
101
|
-
else
|
|
102
|
-
attributes[normalized]
|
|
103
|
-
end
|
|
95
|
+
field = fields[name.to_s]
|
|
96
|
+
raw = read_raw_attribute(name)
|
|
97
|
+
field ? field.demongoize(raw) : raw
|
|
104
98
|
end
|
|
105
99
|
alias :[] :read_attribute
|
|
106
100
|
|
|
107
101
|
# Read a value from the attributes before type cast. If the value has not
|
|
108
102
|
# yet been assigned then this will return the attribute's existing value
|
|
109
|
-
# using
|
|
103
|
+
# using read_raw_attribute.
|
|
110
104
|
#
|
|
111
105
|
# @example Read an attribute before type cast.
|
|
112
106
|
# person.read_attribute_before_type_cast(:price)
|
|
@@ -122,7 +116,7 @@ module Mongoid
|
|
|
122
116
|
if attributes_before_type_cast.key?(attr)
|
|
123
117
|
attributes_before_type_cast[attr]
|
|
124
118
|
else
|
|
125
|
-
|
|
119
|
+
read_raw_attribute(attr)
|
|
126
120
|
end
|
|
127
121
|
end
|
|
128
122
|
|
|
@@ -163,7 +157,8 @@ module Mongoid
|
|
|
163
157
|
#
|
|
164
158
|
# @since 1.0.0
|
|
165
159
|
def write_attribute(name, value)
|
|
166
|
-
|
|
160
|
+
access = database_field_name(name)
|
|
161
|
+
if attribute_writable?(access)
|
|
167
162
|
_assigning do
|
|
168
163
|
validate_attribute_value(access, value)
|
|
169
164
|
localized = fields[access].try(:localized?)
|
|
@@ -292,6 +287,20 @@ module Mongoid
|
|
|
292
287
|
fields.key?(key) ? fields[key].mongoize(value) : value.mongoize
|
|
293
288
|
end
|
|
294
289
|
|
|
290
|
+
private
|
|
291
|
+
|
|
292
|
+
def read_raw_attribute(name)
|
|
293
|
+
normalized = database_field_name(name.to_s)
|
|
294
|
+
if attribute_missing?(normalized)
|
|
295
|
+
raise ActiveModel::MissingAttributeError, "Missing attribute: '#{name}'."
|
|
296
|
+
end
|
|
297
|
+
if hash_dot_syntax?(normalized)
|
|
298
|
+
attributes.__nested__(normalized)
|
|
299
|
+
else
|
|
300
|
+
attributes[normalized]
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
295
304
|
module ClassMethods
|
|
296
305
|
|
|
297
306
|
# Alias the provided name to the original field. This will provide an
|
data/lib/mongoid/changeable.rb
CHANGED
|
@@ -227,7 +227,7 @@ module Mongoid
|
|
|
227
227
|
# @since 2.3.0
|
|
228
228
|
def attribute_will_change!(attr)
|
|
229
229
|
unless changed_attributes.key?(attr)
|
|
230
|
-
changed_attributes[attr] =
|
|
230
|
+
changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
|
|
231
231
|
end
|
|
232
232
|
end
|
|
233
233
|
|
|
@@ -20,11 +20,12 @@ module Mongoid
|
|
|
20
20
|
#
|
|
21
21
|
# @since 6.0.0
|
|
22
22
|
def with(options_or_context, &block)
|
|
23
|
+
original_context = PersistenceContext.get(self)
|
|
23
24
|
original_cluster = persistence_context.cluster
|
|
24
25
|
set_persistence_context(options_or_context)
|
|
25
26
|
yield self
|
|
26
27
|
ensure
|
|
27
|
-
clear_persistence_context(original_cluster)
|
|
28
|
+
clear_persistence_context(original_cluster, original_context)
|
|
28
29
|
end
|
|
29
30
|
|
|
30
31
|
def collection(parent = nil)
|
|
@@ -38,7 +39,7 @@ module Mongoid
|
|
|
38
39
|
def mongo_client
|
|
39
40
|
persistence_context.client
|
|
40
41
|
end
|
|
41
|
-
|
|
42
|
+
|
|
42
43
|
def persistence_context
|
|
43
44
|
PersistenceContext.get(self) ||
|
|
44
45
|
PersistenceContext.get(self.class) ||
|
|
@@ -51,8 +52,8 @@ module Mongoid
|
|
|
51
52
|
PersistenceContext.set(self, options_or_context)
|
|
52
53
|
end
|
|
53
54
|
|
|
54
|
-
def clear_persistence_context(original_cluster = nil)
|
|
55
|
-
PersistenceContext.clear(self, original_cluster)
|
|
55
|
+
def clear_persistence_context(original_cluster = nil, context = nil)
|
|
56
|
+
PersistenceContext.clear(self, original_cluster, context)
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
module ClassMethods
|
|
@@ -92,11 +93,12 @@ module Mongoid
|
|
|
92
93
|
#
|
|
93
94
|
# @since 6.0.0
|
|
94
95
|
def with(options, &block)
|
|
96
|
+
original_context = PersistenceContext.get(self)
|
|
95
97
|
original_cluster = persistence_context.cluster
|
|
96
98
|
PersistenceContext.set(self, options)
|
|
97
99
|
yield self
|
|
98
100
|
ensure
|
|
99
|
-
PersistenceContext.clear(self, original_cluster)
|
|
101
|
+
PersistenceContext.clear(self, original_cluster, original_context)
|
|
100
102
|
end
|
|
101
103
|
|
|
102
104
|
def persistence_context
|
data/lib/mongoid/composable.rb
CHANGED
|
@@ -43,7 +43,7 @@ module Mongoid
|
|
|
43
43
|
include Inspectable
|
|
44
44
|
include Matchable
|
|
45
45
|
include Persistable
|
|
46
|
-
include
|
|
46
|
+
include Association
|
|
47
47
|
include Reloadable
|
|
48
48
|
include Scopable
|
|
49
49
|
include Selectable
|
|
@@ -70,7 +70,7 @@ module Mongoid
|
|
|
70
70
|
Interceptable,
|
|
71
71
|
Matchable,
|
|
72
72
|
Persistable,
|
|
73
|
-
|
|
73
|
+
Association,
|
|
74
74
|
Reloadable,
|
|
75
75
|
Scopable,
|
|
76
76
|
Serializable,
|
|
@@ -83,8 +83,8 @@ module Mongoid
|
|
|
83
83
|
Traversable,
|
|
84
84
|
Validatable,
|
|
85
85
|
Equality,
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
Association::Referenced::Syncable,
|
|
87
|
+
Association::Macros,
|
|
88
88
|
ActiveModel::Model,
|
|
89
89
|
ActiveModel::Validations
|
|
90
90
|
]
|
data/lib/mongoid/config.rb
CHANGED
|
@@ -27,6 +27,7 @@ module Mongoid
|
|
|
27
27
|
option :log_level, default: :info
|
|
28
28
|
option :belongs_to_required_by_default, default: true
|
|
29
29
|
option :app_name, default: nil
|
|
30
|
+
option :background_indexing, default: false
|
|
30
31
|
|
|
31
32
|
# Has Mongoid been configured? This is checking that at least a valid
|
|
32
33
|
# client config exists.
|
|
@@ -118,7 +118,7 @@ module Mongoid
|
|
|
118
118
|
# @since 3.0.0
|
|
119
119
|
def push_all(pushes)
|
|
120
120
|
push_each_updates = collect_operations(pushes).each.inject({}) do |ops, (field, elements)|
|
|
121
|
-
ops.merge!(field => {'$each' => elements})
|
|
121
|
+
ops.merge!(field => { '$each' => elements })
|
|
122
122
|
end
|
|
123
123
|
view.update_many("$push" => push_each_updates)
|
|
124
124
|
end
|
|
@@ -242,7 +242,7 @@ module Mongoid
|
|
|
242
242
|
# @since 3.0.0
|
|
243
243
|
def documents
|
|
244
244
|
results["results"].map do |attributes|
|
|
245
|
-
doc = Factory.from_db(criteria.klass, attributes["obj"], criteria
|
|
245
|
+
doc = Factory.from_db(criteria.klass, attributes["obj"], criteria)
|
|
246
246
|
doc.attributes["geo_near_distance"] = attributes["dis"]
|
|
247
247
|
doc
|
|
248
248
|
end
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
require "mongoid/contextual/aggregable/memory"
|
|
3
|
-
require "mongoid/
|
|
3
|
+
require "mongoid/association/eager_loadable"
|
|
4
4
|
|
|
5
5
|
module Mongoid
|
|
6
6
|
module Contextual
|
|
7
7
|
class Memory
|
|
8
8
|
include Enumerable
|
|
9
9
|
include Aggregable::Memory
|
|
10
|
-
include
|
|
10
|
+
include Association::EagerLoadable
|
|
11
11
|
include Queryable
|
|
12
12
|
include Positional
|
|
13
13
|
|
|
@@ -130,7 +130,7 @@ module Mongoid
|
|
|
130
130
|
# @return [ Document ] The first document.
|
|
131
131
|
#
|
|
132
132
|
# @since 3.0.0
|
|
133
|
-
def first
|
|
133
|
+
def first(*args)
|
|
134
134
|
eager_load([documents.first]).first
|
|
135
135
|
end
|
|
136
136
|
alias :one :first
|
|
@@ -155,6 +155,22 @@ module Mongoid
|
|
|
155
155
|
apply_options
|
|
156
156
|
end
|
|
157
157
|
|
|
158
|
+
# Increment a value on all documents.
|
|
159
|
+
#
|
|
160
|
+
# @example Perform the increment.
|
|
161
|
+
# context.inc(likes: 10)
|
|
162
|
+
#
|
|
163
|
+
# @param [ Hash ] incs The operations.
|
|
164
|
+
#
|
|
165
|
+
# @return [ Enumerator ] The enumerator.
|
|
166
|
+
#
|
|
167
|
+
# @since 7.0.0
|
|
168
|
+
def inc(*args)
|
|
169
|
+
each do |document|
|
|
170
|
+
document.inc *args
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
158
174
|
# Get the last document in the database for the criteria's selector.
|
|
159
175
|
#
|
|
160
176
|
# @example Get the last document.
|
|
@@ -446,6 +462,8 @@ module Mongoid
|
|
|
446
462
|
doc.destroyed = true
|
|
447
463
|
end
|
|
448
464
|
|
|
465
|
+
private
|
|
466
|
+
|
|
449
467
|
def _session
|
|
450
468
|
@criteria.send(:_session)
|
|
451
469
|
end
|
|
@@ -4,7 +4,7 @@ require "mongoid/contextual/aggregable/mongo"
|
|
|
4
4
|
require "mongoid/contextual/command"
|
|
5
5
|
require "mongoid/contextual/geo_near"
|
|
6
6
|
require "mongoid/contextual/map_reduce"
|
|
7
|
-
require "mongoid/
|
|
7
|
+
require "mongoid/association/eager_loadable"
|
|
8
8
|
|
|
9
9
|
module Mongoid
|
|
10
10
|
module Contextual
|
|
@@ -12,7 +12,7 @@ module Mongoid
|
|
|
12
12
|
include Enumerable
|
|
13
13
|
include Aggregable::Mongo
|
|
14
14
|
include Atomic
|
|
15
|
-
include
|
|
15
|
+
include Association::EagerLoadable
|
|
16
16
|
include Queryable
|
|
17
17
|
|
|
18
18
|
# Options constant.
|
|
@@ -256,12 +256,12 @@ module Mongoid
|
|
|
256
256
|
try_cache(:first) do
|
|
257
257
|
if sort = view.sort || ({ _id: 1 } unless opts[:id_sort] == :none)
|
|
258
258
|
if raw_doc = view.sort(sort).limit(-1).first
|
|
259
|
-
doc = Factory.from_db(klass, raw_doc, criteria
|
|
259
|
+
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
260
260
|
eager_load([doc]).first
|
|
261
261
|
end
|
|
262
262
|
else
|
|
263
263
|
if raw_doc = view.limit(-1).first
|
|
264
|
-
doc = Factory.from_db(klass, raw_doc, criteria
|
|
264
|
+
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
265
265
|
eager_load([doc]).first
|
|
266
266
|
end
|
|
267
267
|
end
|
|
@@ -277,7 +277,7 @@ module Mongoid
|
|
|
277
277
|
def find_first
|
|
278
278
|
return documents.first if cached? && cache_loaded?
|
|
279
279
|
if raw_doc = view.first
|
|
280
|
-
doc = Factory.from_db(klass, raw_doc, criteria
|
|
280
|
+
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
281
281
|
eager_load([doc]).first
|
|
282
282
|
end
|
|
283
283
|
end
|
|
@@ -368,7 +368,7 @@ module Mongoid
|
|
|
368
368
|
try_cache(:last) do
|
|
369
369
|
with_inverse_sorting(opts) do
|
|
370
370
|
if raw_doc = view.limit(-1).first
|
|
371
|
-
doc = Factory.from_db(klass, raw_doc, criteria
|
|
371
|
+
doc = Factory.from_db(klass, raw_doc, criteria)
|
|
372
372
|
eager_load([doc]).first
|
|
373
373
|
end
|
|
374
374
|
end
|
|
@@ -683,7 +683,7 @@ module Mongoid
|
|
|
683
683
|
def documents_for_iteration
|
|
684
684
|
return documents if cached? && !documents.empty?
|
|
685
685
|
return view unless eager_loadable?
|
|
686
|
-
docs = view.map{ |doc| Factory.from_db(klass, doc, criteria
|
|
686
|
+
docs = view.map{ |doc| Factory.from_db(klass, doc, criteria) }
|
|
687
687
|
eager_load(docs)
|
|
688
688
|
end
|
|
689
689
|
|
|
@@ -701,11 +701,13 @@ module Mongoid
|
|
|
701
701
|
# @since 3.0.0
|
|
702
702
|
def yield_document(document, &block)
|
|
703
703
|
doc = document.respond_to?(:_id) ?
|
|
704
|
-
document : Factory.from_db(klass, document, criteria
|
|
704
|
+
document : Factory.from_db(klass, document, criteria)
|
|
705
705
|
yield(doc)
|
|
706
706
|
documents.push(doc) if cacheable?
|
|
707
707
|
end
|
|
708
708
|
|
|
709
|
+
private
|
|
710
|
+
|
|
709
711
|
def _session
|
|
710
712
|
@criteria.send(:_session)
|
|
711
713
|
end
|
data/lib/mongoid/copyable.rb
CHANGED
|
@@ -69,15 +69,16 @@ module Mongoid
|
|
|
69
69
|
attrs["#{name}_translations"] = value
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
|
-
klass.embedded_relations.each do |_,
|
|
73
|
-
next unless attrs.present? && attrs[
|
|
72
|
+
klass.embedded_relations.each do |_, association|
|
|
73
|
+
next unless attrs.present? && attrs[association.key].present?
|
|
74
74
|
|
|
75
|
-
if
|
|
76
|
-
attrs[
|
|
77
|
-
|
|
75
|
+
if association.is_a?(Association::Embedded::EmbedsMany)
|
|
76
|
+
attrs[association.key].each do |attr|
|
|
77
|
+
embedded_klass = attr.fetch('_type', association.relation_class_name).constantize
|
|
78
|
+
process_localized_attributes(embedded_klass, attr)
|
|
78
79
|
end
|
|
79
80
|
else
|
|
80
|
-
process_localized_attributes(
|
|
81
|
+
process_localized_attributes(association.klass, attrs[association.key])
|
|
81
82
|
end
|
|
82
83
|
end
|
|
83
84
|
end
|
|
@@ -36,7 +36,7 @@ module Mongoid
|
|
|
36
36
|
# @example Get the eager loading inclusions.
|
|
37
37
|
# Person.includes(:game).inclusions
|
|
38
38
|
#
|
|
39
|
-
# @return [ Array<
|
|
39
|
+
# @return [ Array<Association> ] The inclusions.
|
|
40
40
|
#
|
|
41
41
|
# @since 2.2.0
|
|
42
42
|
def inclusions
|
|
@@ -46,11 +46,11 @@ module Mongoid
|
|
|
46
46
|
# Set the inclusions for the criteria.
|
|
47
47
|
#
|
|
48
48
|
# @example Set the inclusions.
|
|
49
|
-
# criteria.inclusions = [
|
|
49
|
+
# criteria.inclusions = [ association ]
|
|
50
50
|
#
|
|
51
|
-
# @param [ Array<
|
|
51
|
+
# @param [ Array<Association> ] value The inclusions.
|
|
52
52
|
#
|
|
53
|
-
# @return [ Array<
|
|
53
|
+
# @return [ Array<Association> ] The new inclusions.
|
|
54
54
|
#
|
|
55
55
|
# @since 3.0.0
|
|
56
56
|
def inclusions=(value)
|
|
@@ -65,28 +65,28 @@ module Mongoid
|
|
|
65
65
|
# criteria.add_inclusion(Person, :posts)
|
|
66
66
|
#
|
|
67
67
|
# @param [ Class, String, Symbol ] _klass The class or string/symbol of the class name.
|
|
68
|
-
# @param [ Symbol ]
|
|
68
|
+
# @param [ Symbol ] association The relation.
|
|
69
69
|
#
|
|
70
70
|
# @raise [ Errors::InvalidIncludes ] If no relation is found.
|
|
71
71
|
#
|
|
72
72
|
# @since 5.1.0
|
|
73
|
-
def add_inclusion(_klass,
|
|
74
|
-
inclusions.push(
|
|
73
|
+
def add_inclusion(_klass, association)
|
|
74
|
+
inclusions.push(association) unless inclusions.include?(association)
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
def extract_includes_list(_parent_class, *relations_list)
|
|
78
78
|
relations_list.flatten.each do |relation_object|
|
|
79
79
|
if relation_object.is_a?(Hash)
|
|
80
80
|
relation_object.each do |relation, _includes|
|
|
81
|
-
|
|
82
|
-
raise Errors::InvalidIncludes.new(_klass, [ relation ]) unless
|
|
83
|
-
add_inclusion(_parent_class,
|
|
84
|
-
extract_includes_list(
|
|
81
|
+
association = _parent_class.reflect_on_association(relation)
|
|
82
|
+
raise Errors::InvalidIncludes.new(_klass, [ relation ]) unless association
|
|
83
|
+
add_inclusion(_parent_class, association)
|
|
84
|
+
extract_includes_list(association.klass, _includes)
|
|
85
85
|
end
|
|
86
86
|
else
|
|
87
|
-
|
|
88
|
-
raise Errors::InvalidIncludes.new(_parent_class, [ relation_object ]) unless
|
|
89
|
-
add_inclusion(_parent_class,
|
|
87
|
+
association = _parent_class.reflect_on_association(relation_object)
|
|
88
|
+
raise Errors::InvalidIncludes.new(_parent_class, [ relation_object ]) unless association
|
|
89
|
+
add_inclusion(_parent_class, association)
|
|
90
90
|
end
|
|
91
91
|
end
|
|
92
92
|
end
|
|
@@ -3,10 +3,6 @@ module Mongoid
|
|
|
3
3
|
class Criteria
|
|
4
4
|
module Modifiable
|
|
5
5
|
|
|
6
|
-
# @attribute [r] create_attrs Additional attributes to add to the Document upon creation.
|
|
7
|
-
# @api private
|
|
8
|
-
attr_reader :create_attrs
|
|
9
|
-
|
|
10
6
|
# Build a document given the selector and return it.
|
|
11
7
|
# Complex criteria, such as $in and $or operations will get ignored.
|
|
12
8
|
#
|
|
@@ -61,9 +57,6 @@ module Mongoid
|
|
|
61
57
|
|
|
62
58
|
# Define attributes with which new documents will be created.
|
|
63
59
|
#
|
|
64
|
-
# Note that if `find_or_create_by` is called after this in a method chain, the attributes in
|
|
65
|
-
# the query will override those from this method.
|
|
66
|
-
#
|
|
67
60
|
# @example Define attributes to be used when a new document is created.
|
|
68
61
|
# Person.create_with(job: 'Engineer').find_or_create_by(employer: 'MongoDB')
|
|
69
62
|
#
|
|
@@ -71,9 +64,7 @@ module Mongoid
|
|
|
71
64
|
#
|
|
72
65
|
# @since 5.1.0
|
|
73
66
|
def create_with(attrs = {})
|
|
74
|
-
|
|
75
|
-
(@create_attrs ||= {}).merge!(attrs)
|
|
76
|
-
end
|
|
67
|
+
where(selector.merge(attrs))
|
|
77
68
|
end
|
|
78
69
|
|
|
79
70
|
# Find the first +Document+ given the conditions, or creates a new document
|
|
@@ -181,8 +172,7 @@ module Mongoid
|
|
|
181
172
|
#
|
|
182
173
|
# @since 3.0.0
|
|
183
174
|
def create_document(method, attrs = nil, &block)
|
|
184
|
-
|
|
185
|
-
attributes = selector.reduce(attrs) do |hash, (key, value)|
|
|
175
|
+
attributes = selector.reduce(attrs ? attrs.dup : {}) do |hash, (key, value)|
|
|
186
176
|
unless invalid_key?(hash, key) || invalid_embedded_doc?(value)
|
|
187
177
|
hash[key] = value
|
|
188
178
|
end
|
|
@@ -190,9 +180,13 @@ module Mongoid
|
|
|
190
180
|
end
|
|
191
181
|
if embedded?
|
|
192
182
|
attributes[:_parent] = parent_document
|
|
193
|
-
attributes[:
|
|
183
|
+
attributes[:_association] = association
|
|
184
|
+
end
|
|
185
|
+
if polymorphic? && @criterion
|
|
186
|
+
klass.__send__(method, attributes.merge(@criterion), &block)
|
|
187
|
+
else
|
|
188
|
+
klass.__send__(method, attributes, &block)
|
|
194
189
|
end
|
|
195
|
-
klass.__send__(method, attributes, &block)
|
|
196
190
|
end
|
|
197
191
|
|
|
198
192
|
# Find the first object or create/initialize it.
|
|
@@ -17,8 +17,8 @@ module Mongoid
|
|
|
17
17
|
PersistenceContext.set(klass, options)
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
def clear_persistence_context(original_cluster)
|
|
21
|
-
PersistenceContext.clear(klass, original_cluster)
|
|
20
|
+
def clear_persistence_context(original_cluster, original_context)
|
|
21
|
+
PersistenceContext.clear(klass, original_cluster, original_context)
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
end
|
|
@@ -72,16 +72,21 @@ module Mongoid
|
|
|
72
72
|
#
|
|
73
73
|
# @example Add the unwind.
|
|
74
74
|
# pipeline.unwind(:field)
|
|
75
|
+
# pipeline.unwind(document)
|
|
75
76
|
#
|
|
76
|
-
# @param [ String, Symbol ]
|
|
77
|
+
# @param [ String, Symbol, Hash ] field_or_doc The name of the field or a document.
|
|
77
78
|
#
|
|
78
79
|
# @return [ Pipeline ] The pipeline.
|
|
79
80
|
#
|
|
80
81
|
# @since 2.0.0
|
|
81
|
-
def unwind(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
def unwind(field_or_doc)
|
|
83
|
+
unless field_or_doc.respond_to? :keys
|
|
84
|
+
normalized = field_or_doc.to_s
|
|
85
|
+
name = aliases[normalized] || normalized
|
|
86
|
+
push("$unwind" => name.__mongo_expression__)
|
|
87
|
+
else
|
|
88
|
+
push("$unwind" => field_or_doc)
|
|
89
|
+
end
|
|
85
90
|
end
|
|
86
91
|
|
|
87
92
|
private
|