mongoid 7.6.2 → 8.0.1
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
- checksums.yaml.gz.sig +2 -0
- data/README.md +3 -3
- data/Rakefile +21 -85
- data/lib/config/locales/en.yml +46 -56
- data/lib/mongoid/association/accessors.rb +32 -3
- data/lib/mongoid/association/bindable.rb +48 -0
- data/lib/mongoid/association/builders.rb +4 -2
- data/lib/mongoid/association/depending.rb +4 -8
- data/lib/mongoid/association/eager_loadable.rb +29 -7
- data/lib/mongoid/association/embedded/batchable.rb +28 -5
- data/lib/mongoid/association/embedded/embedded_in/binding.rb +24 -2
- data/lib/mongoid/association/embedded/embedded_in.rb +2 -1
- data/lib/mongoid/association/embedded/embeds_many/binding.rb +1 -0
- data/lib/mongoid/association/embedded/embeds_many/buildable.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +40 -18
- data/lib/mongoid/association/embedded/embeds_one/buildable.rb +18 -4
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +21 -2
- data/lib/mongoid/association/macros.rb +2 -1
- data/lib/mongoid/association/many.rb +5 -0
- data/lib/mongoid/association/nested/many.rb +5 -39
- data/lib/mongoid/association/nested/nested_buildable.rb +0 -14
- data/lib/mongoid/association/proxy.rb +12 -0
- data/lib/mongoid/association/referenced/auto_save.rb +3 -2
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -0
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +1 -1
- data/lib/mongoid/association/referenced/belongs_to.rb +1 -1
- data/lib/mongoid/association/referenced/counter_cache.rb +8 -8
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +64 -11
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +4 -1
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +10 -18
- data/lib/mongoid/association/referenced/has_many/proxy.rb +11 -107
- data/lib/mongoid/association/referenced/has_one/buildable.rb +1 -1
- data/lib/mongoid/association/referenced/has_one/proxy.rb +8 -11
- data/lib/mongoid/association/referenced/syncable.rb +2 -2
- data/lib/mongoid/association/relatable.rb +38 -4
- data/lib/mongoid/attributes/processing.rb +9 -2
- data/lib/mongoid/attributes.rb +30 -27
- data/lib/mongoid/cacheable.rb +2 -2
- data/lib/mongoid/changeable.rb +37 -2
- data/lib/mongoid/clients/factory.rb +0 -4
- data/lib/mongoid/clients/options.rb +4 -0
- data/lib/mongoid/clients/sessions.rb +2 -14
- data/lib/mongoid/config.rb +15 -73
- data/lib/mongoid/contextual/aggregable/memory.rb +22 -20
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/map_reduce.rb +2 -2
- data/lib/mongoid/contextual/memory.rb +63 -47
- data/lib/mongoid/contextual/mongo.rb +173 -287
- data/lib/mongoid/contextual/none.rb +33 -15
- data/lib/mongoid/copyable.rb +32 -8
- data/lib/mongoid/criteria/includable.rb +24 -20
- data/lib/mongoid/criteria/marshalable.rb +10 -2
- data/lib/mongoid/criteria/queryable/extensions/array.rb +2 -15
- data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +25 -4
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/date.rb +6 -1
- data/lib/mongoid/criteria/queryable/extensions/date_time.rb +6 -1
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +0 -16
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/object.rb +2 -1
- data/lib/mongoid/criteria/queryable/extensions/range.rb +13 -5
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -1
- data/lib/mongoid/criteria/queryable/extensions/time.rb +6 -1
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +6 -1
- data/lib/mongoid/criteria/queryable/mergeable.rb +0 -12
- data/lib/mongoid/criteria/queryable/optional.rb +3 -9
- data/lib/mongoid/criteria/queryable/options.rb +1 -1
- data/lib/mongoid/criteria/queryable/selectable.rb +2 -133
- data/lib/mongoid/criteria/queryable/selector.rb +90 -5
- data/lib/mongoid/criteria/queryable/smash.rb +39 -6
- data/lib/mongoid/criteria/queryable/storable.rb +1 -1
- data/lib/mongoid/criteria/queryable.rb +11 -6
- data/lib/mongoid/criteria.rb +1 -28
- data/lib/mongoid/deprecable.rb +36 -0
- data/lib/mongoid/deprecation.rb +25 -0
- data/lib/mongoid/document.rb +88 -33
- data/lib/mongoid/equality.rb +4 -4
- data/lib/mongoid/errors/document_not_found.rb +6 -2
- data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +23 -0
- data/lib/mongoid/errors/invalid_field.rb +5 -1
- data/lib/mongoid/errors/invalid_field_type.rb +26 -0
- data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -1
- data/lib/mongoid/errors.rb +2 -3
- data/lib/mongoid/extensions/array.rb +8 -6
- data/lib/mongoid/extensions/big_decimal.rb +29 -10
- data/lib/mongoid/extensions/binary.rb +42 -0
- data/lib/mongoid/extensions/boolean.rb +8 -2
- data/lib/mongoid/extensions/date.rb +26 -20
- data/lib/mongoid/extensions/date_time.rb +1 -1
- data/lib/mongoid/extensions/float.rb +4 -5
- data/lib/mongoid/extensions/hash.rb +13 -32
- data/lib/mongoid/extensions/integer.rb +4 -5
- data/lib/mongoid/extensions/object.rb +2 -0
- data/lib/mongoid/extensions/range.rb +41 -10
- data/lib/mongoid/extensions/regexp.rb +11 -4
- data/lib/mongoid/extensions/set.rb +11 -4
- data/lib/mongoid/extensions/string.rb +2 -13
- data/lib/mongoid/extensions/symbol.rb +3 -14
- data/lib/mongoid/extensions/time.rb +27 -16
- data/lib/mongoid/extensions/time_with_zone.rb +1 -2
- data/lib/mongoid/extensions.rb +1 -0
- data/lib/mongoid/factory.rb +42 -7
- data/lib/mongoid/fields/foreign_key.rb +7 -0
- data/lib/mongoid/fields/validators/macro.rb +3 -9
- data/lib/mongoid/fields.rb +49 -7
- data/lib/mongoid/findable.rb +21 -16
- data/lib/mongoid/indexable/specification.rb +1 -1
- data/lib/mongoid/indexable/validators/options.rb +4 -1
- data/lib/mongoid/interceptable.rb +69 -9
- data/lib/mongoid/matchable.rb +1 -6
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +5 -3
- data/lib/mongoid/matcher/regex.rb +9 -8
- data/lib/mongoid/matcher.rb +0 -1
- data/lib/mongoid/persistable/creatable.rb +14 -5
- data/lib/mongoid/persistable/updatable.rb +12 -5
- data/lib/mongoid/persistence_context.rb +8 -42
- data/lib/mongoid/query_cache.rb +6 -258
- data/lib/mongoid/railties/controller_runtime.rb +1 -1
- data/lib/mongoid/reloadable.rb +7 -3
- data/lib/mongoid/scopable.rb +9 -11
- data/lib/mongoid/selectable.rb +1 -2
- data/lib/mongoid/shardable.rb +11 -35
- data/lib/mongoid/stateful.rb +27 -1
- data/lib/mongoid/threaded.rb +0 -3
- data/lib/mongoid/timestamps/created.rb +1 -1
- data/lib/mongoid/timestamps/updated.rb +1 -1
- data/lib/mongoid/touchable.rb +2 -3
- data/lib/mongoid/traversable.rb +1 -0
- data/lib/mongoid/validatable/uniqueness.rb +2 -1
- data/lib/mongoid/version.rb +1 -5
- data/lib/mongoid/warnings.rb +3 -5
- data/lib/mongoid.rb +1 -0
- data/spec/config/mongoid.yml +16 -0
- data/spec/integration/app_spec.rb +8 -20
- data/spec/integration/associations/belongs_to_spec.rb +18 -0
- data/spec/integration/associations/embedded_spec.rb +15 -0
- data/spec/integration/associations/embeds_many_spec.rb +15 -2
- data/spec/integration/associations/embeds_one_spec.rb +18 -0
- data/spec/integration/associations/foreign_key_spec.rb +9 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +21 -0
- data/spec/integration/associations/has_one_spec.rb +97 -1
- data/spec/integration/associations/scope_option_spec.rb +1 -1
- data/spec/integration/callbacks_models.rb +95 -1
- data/spec/integration/callbacks_spec.rb +226 -4
- data/spec/integration/criteria/range_spec.rb +95 -1
- data/spec/integration/discriminator_key_spec.rb +115 -76
- data/spec/integration/dots_and_dollars_spec.rb +277 -0
- data/spec/integration/i18n_fallbacks_spec.rb +1 -15
- data/spec/integration/matcher_examples_spec.rb +20 -13
- data/spec/integration/matcher_operator_data/regex.yml +0 -21
- data/spec/integration/matcher_operator_data/type_decimal.yml +3 -2
- data/spec/integration/matcher_operator_spec.rb +3 -5
- data/spec/integration/persistence/range_field_spec.rb +350 -0
- data/spec/mongoid/association/counter_cache_spec.rb +1 -1
- data/spec/mongoid/association/depending_spec.rb +9 -9
- data/spec/mongoid/association/eager_spec.rb +2 -1
- data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +2 -1
- data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +54 -0
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +69 -9
- data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +112 -0
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +198 -8
- data/spec/mongoid/association/embedded/embeds_many_models.rb +36 -0
- data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +12 -0
- data/spec/mongoid/association/embedded/embeds_many_spec.rb +68 -0
- data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +25 -0
- data/spec/mongoid/association/embedded/embeds_one_models.rb +19 -0
- data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -0
- data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +2 -1
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +54 -0
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +15 -0
- data/spec/mongoid/association/referenced/belongs_to_models.rb +11 -0
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -2
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +36 -51
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +25 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +35 -2
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +109 -0
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +2 -56
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +60 -156
- data/spec/mongoid/association/referenced/has_many_models.rb +3 -1
- data/spec/mongoid/association/referenced/has_many_spec.rb +25 -0
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +2 -2
- data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +107 -1
- data/spec/mongoid/association/referenced/has_one_models.rb +16 -0
- data/spec/mongoid/association/syncable_spec.rb +14 -0
- data/spec/mongoid/atomic/paths_spec.rb +0 -14
- data/spec/mongoid/attributes/nested_spec.rb +73 -198
- data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
- data/spec/mongoid/attributes/projector_spec.rb +1 -5
- data/spec/mongoid/attributes_spec.rb +480 -27
- data/spec/mongoid/cacheable_spec.rb +3 -3
- data/spec/mongoid/changeable_spec.rb +130 -13
- data/spec/mongoid/clients/factory_spec.rb +22 -60
- data/spec/mongoid/clients/sessions_spec.rb +0 -38
- data/spec/mongoid/clients_spec.rb +2 -2
- data/spec/mongoid/config_spec.rb +52 -15
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +371 -231
- data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
- data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +62 -0
- data/spec/mongoid/contextual/map_reduce_spec.rb +2 -16
- data/spec/mongoid/contextual/memory_spec.rb +467 -156
- data/spec/mongoid/contextual/mongo_spec.rb +566 -426
- data/spec/mongoid/contextual/none_spec.rb +11 -19
- data/spec/mongoid/copyable_spec.rb +451 -2
- data/spec/mongoid/criteria/findable_spec.rb +86 -210
- data/spec/mongoid/criteria/includable_spec.rb +1492 -0
- data/spec/mongoid/criteria/includable_spec_models.rb +54 -0
- data/spec/mongoid/criteria/marshalable_spec.rb +18 -1
- data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +7 -19
- data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +134 -26
- data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +1 -2
- data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +11 -0
- data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +11 -0
- data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +1 -2
- data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +0 -15
- data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +73 -7
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +11 -0
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +11 -0
- data/spec/mongoid/criteria/queryable/optional_spec.rb +0 -484
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +50 -2
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +77 -85
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +0 -200
- data/spec/mongoid/criteria/queryable/selector_spec.rb +16 -77
- data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
- data/spec/mongoid/criteria_spec.rb +469 -1203
- data/spec/mongoid/document_fields_spec.rb +173 -24
- data/spec/mongoid/document_spec.rb +32 -41
- data/spec/mongoid/equality_spec.rb +12 -12
- data/spec/mongoid/errors/document_not_found_spec.rb +29 -2
- data/spec/mongoid/errors/invalid_field_spec.rb +1 -1
- data/spec/mongoid/errors/invalid_field_type_spec.rb +55 -0
- data/spec/mongoid/errors/mongoid_error_spec.rb +3 -1
- data/spec/mongoid/errors/no_environment_spec.rb +3 -3
- data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +1 -1
- data/spec/mongoid/extensions/array_spec.rb +16 -2
- data/spec/mongoid/extensions/big_decimal_spec.rb +697 -212
- data/spec/mongoid/extensions/binary_spec.rb +44 -9
- data/spec/mongoid/extensions/boolean_spec.rb +68 -82
- data/spec/mongoid/extensions/date_class_mongoize_spec.rb +7 -3
- data/spec/mongoid/extensions/date_spec.rb +71 -1
- data/spec/mongoid/extensions/date_time_spec.rb +15 -9
- data/spec/mongoid/extensions/float_spec.rb +48 -76
- data/spec/mongoid/extensions/hash_spec.rb +30 -236
- data/spec/mongoid/extensions/integer_spec.rb +45 -66
- data/spec/mongoid/extensions/range_spec.rb +255 -54
- data/spec/mongoid/extensions/regexp_spec.rb +58 -33
- data/spec/mongoid/extensions/set_spec.rb +106 -0
- data/spec/mongoid/extensions/string_spec.rb +53 -25
- data/spec/mongoid/extensions/symbol_spec.rb +18 -25
- data/spec/mongoid/extensions/time_spec.rb +634 -66
- data/spec/mongoid/extensions/time_with_zone_spec.rb +17 -31
- data/spec/mongoid/factory_spec.rb +61 -1
- data/spec/mongoid/fields_spec.rb +321 -50
- data/spec/mongoid/findable_spec.rb +64 -29
- data/spec/mongoid/indexable/specification_spec.rb +2 -2
- data/spec/mongoid/indexable_spec.rb +16 -19
- data/spec/mongoid/interceptable_spec.rb +584 -5
- data/spec/mongoid/interceptable_spec_models.rb +235 -4
- data/spec/mongoid/matcher/extract_attribute_spec.rb +1 -5
- data/spec/mongoid/mongoizable_spec.rb +285 -0
- data/spec/mongoid/persistable/creatable_spec.rb +2 -2
- data/spec/mongoid/persistable/deletable_spec.rb +2 -2
- data/spec/mongoid/persistable/destroyable_spec.rb +2 -2
- data/spec/mongoid/persistable/upsertable_spec.rb +14 -0
- data/spec/mongoid/persistence_context_spec.rb +24 -0
- data/spec/mongoid/query_cache_middleware_spec.rb +0 -18
- data/spec/mongoid/query_cache_spec.rb +1 -156
- data/spec/mongoid/reloadable_spec.rb +35 -2
- data/spec/mongoid/scopable_spec.rb +36 -34
- data/spec/mongoid/serializable_spec.rb +14 -7
- data/spec/mongoid/shardable_models.rb +0 -14
- data/spec/mongoid/shardable_spec.rb +61 -153
- data/spec/mongoid/stateful_spec.rb +28 -0
- data/spec/mongoid/timestamps_spec.rb +390 -0
- data/spec/mongoid/timestamps_spec_models.rb +67 -0
- data/spec/mongoid/touchable_spec.rb +116 -0
- data/spec/mongoid/touchable_spec_models.rb +12 -8
- data/spec/mongoid/traversable_spec.rb +4 -11
- data/spec/mongoid/validatable/presence_spec.rb +1 -1
- data/spec/mongoid/validatable/uniqueness_spec.rb +60 -31
- data/spec/mongoid/warnings_spec.rb +35 -0
- data/spec/mongoid_spec.rb +1 -7
- data/spec/rails/controller_extension/controller_runtime_spec.rb +2 -2
- data/spec/rails/mongoid_spec.rb +4 -16
- data/spec/shared/lib/mrss/cluster_config.rb +40 -5
- data/spec/shared/lib/mrss/constraints.rb +46 -7
- data/spec/shared/lib/mrss/docker_runner.rb +21 -17
- data/spec/shared/lib/mrss/eg_config_utils.rb +31 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +5 -15
- data/spec/shared/lib/mrss/lite_constraints.rb +2 -10
- data/spec/shared/lib/mrss/server_version_registry.rb +29 -18
- data/spec/shared/lib/mrss/spec_organizer.rb +3 -32
- data/spec/shared/lib/mrss/utils.rb +6 -28
- data/spec/shared/share/Dockerfile.erb +114 -38
- data/spec/shared/shlib/distro.sh +0 -10
- data/spec/shared/shlib/server.sh +83 -64
- data/spec/shared/shlib/set_env.sh +81 -16
- data/spec/support/constraints.rb +24 -0
- data/spec/support/expectations.rb +17 -20
- data/spec/support/macros.rb +30 -0
- data/spec/support/models/augmentation.rb +12 -0
- data/spec/support/models/band.rb +3 -0
- data/spec/support/models/catalog.rb +24 -0
- data/spec/support/models/circus.rb +3 -0
- data/spec/support/models/fanatic.rb +8 -0
- data/spec/support/models/implant.rb +9 -0
- data/spec/support/models/label.rb +2 -0
- data/spec/support/models/passport.rb +9 -0
- data/spec/support/models/person.rb +1 -0
- data/spec/support/models/player.rb +2 -0
- data/spec/support/models/powerup.rb +12 -0
- data/spec/support/models/registry.rb +1 -0
- data/spec/support/models/school.rb +14 -0
- data/spec/support/models/shield.rb +18 -0
- data/spec/support/models/student.rb +14 -0
- data/spec/support/models/weapon.rb +12 -0
- data.tar.gz.sig +0 -0
- metadata +695 -650
- metadata.gz.sig +0 -0
- data/lib/mongoid/errors/eager_load.rb +0 -23
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +0 -26
- data/lib/mongoid/errors/invalid_value.rb +0 -17
- data/lib/mongoid/field_readable.rb +0 -70
- data/lib/mongoid/matcher/regexp_budget.rb +0 -389
- data/spec/integration/matcher_regexp_timeout_spec.rb +0 -215
- data/spec/integration/query_operator_guard_spec.rb +0 -89
- data/spec/mongoid/errors/eager_load_spec.rb +0 -31
- data/spec/mongoid/matcher/regexp_budget_spec.rb +0 -570
- data/spec/shared/CANDIDATE.md +0 -28
- data/spec/shared/lib/mrss/release/candidate.rb +0 -287
- data/spec/shared/lib/mrss/release/product_data.rb +0 -144
- data/spec/shared/lib/tasks/candidate.rake +0 -64
|
@@ -19,12 +19,14 @@ module Mongoid
|
|
|
19
19
|
# name.person.bind(:continue => true)
|
|
20
20
|
# name.person = Person.new
|
|
21
21
|
def bind_one
|
|
22
|
-
_base._association = _association.inverse_association(_target) unless _base._association
|
|
23
|
-
_base.parentize(_target)
|
|
24
22
|
binding do
|
|
23
|
+
check_polymorphic_inverses!(_target)
|
|
24
|
+
_base._association = _association.inverse_association(_target) unless _base._association
|
|
25
|
+
_base.parentize(_target)
|
|
25
26
|
if _base.embedded_many?
|
|
26
27
|
_target.do_or_do_not(_association.inverse(_target)).push(_base)
|
|
27
28
|
else
|
|
29
|
+
remove_associated(_target)
|
|
28
30
|
_target.do_or_do_not(_association.inverse_setter(_target), _base)
|
|
29
31
|
end
|
|
30
32
|
end
|
|
@@ -45,6 +47,26 @@ module Mongoid
|
|
|
45
47
|
end
|
|
46
48
|
end
|
|
47
49
|
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# Check for problems with multiple inverse definitions.
|
|
54
|
+
#
|
|
55
|
+
# @api private
|
|
56
|
+
#
|
|
57
|
+
# @example Check for inverses errors.
|
|
58
|
+
# binding.check_inverses!(doc)
|
|
59
|
+
#
|
|
60
|
+
# @param [ Document ] doc The document to check.
|
|
61
|
+
def check_polymorphic_inverses!(doc)
|
|
62
|
+
if inverses = _association.inverses(doc)
|
|
63
|
+
if inverses.length > 1
|
|
64
|
+
raise Errors::InvalidSetPolymorphicRelation.new(
|
|
65
|
+
_association.name, _base.class.name, _target.class.name
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
48
70
|
end
|
|
49
71
|
end
|
|
50
72
|
end
|
|
@@ -31,7 +31,7 @@ module Mongoid
|
|
|
31
31
|
docs = []
|
|
32
32
|
object.each do |attrs|
|
|
33
33
|
if _loading? && base.persisted?
|
|
34
|
-
docs.push(Factory.
|
|
34
|
+
docs.push(Factory.execute_from_db(klass, attrs, nil, selected_fields, execute_callbacks: false))
|
|
35
35
|
else
|
|
36
36
|
docs.push(Factory.build(klass, attrs))
|
|
37
37
|
end
|
|
@@ -67,10 +67,11 @@ module Mongoid
|
|
|
67
67
|
#
|
|
68
68
|
# @return [ Document ] The new document.
|
|
69
69
|
def build(attributes = {}, type = nil)
|
|
70
|
-
doc = Factory.
|
|
70
|
+
doc = Factory.execute_build(type || _association.klass, attributes, execute_callbacks: false)
|
|
71
71
|
append(doc)
|
|
72
72
|
doc.apply_post_processed_defaults
|
|
73
73
|
yield(doc) if block_given?
|
|
74
|
+
doc.run_pending_callbacks
|
|
74
75
|
doc.run_callbacks(:build) { doc }
|
|
75
76
|
_base._reset_memoized_descendants!
|
|
76
77
|
doc
|
|
@@ -94,6 +95,7 @@ module Mongoid
|
|
|
94
95
|
# @return [ self ] The empty association.
|
|
95
96
|
def clear
|
|
96
97
|
batch_clear(_target.dup)
|
|
98
|
+
update_attributes_hash
|
|
97
99
|
self
|
|
98
100
|
end
|
|
99
101
|
|
|
@@ -135,20 +137,21 @@ module Mongoid
|
|
|
135
137
|
#
|
|
136
138
|
# @return [ Document, nil ] The deleted document or nil if nothing deleted.
|
|
137
139
|
def delete(document)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
execute_callbacks_around(:remove, document) do
|
|
141
|
+
doc = _target.delete_one(document)
|
|
142
|
+
if doc && !_binding?
|
|
143
|
+
_unscoped.delete_one(doc)
|
|
144
|
+
if _assigning?
|
|
145
|
+
_base.add_atomic_pull(doc)
|
|
146
|
+
else
|
|
147
|
+
doc.delete(suppress: true)
|
|
148
|
+
unbind_one(doc)
|
|
149
|
+
end
|
|
150
|
+
update_attributes_hash
|
|
147
151
|
end
|
|
152
|
+
reindex
|
|
153
|
+
doc
|
|
148
154
|
end
|
|
149
|
-
reindex
|
|
150
|
-
execute_callback :after_remove, document
|
|
151
|
-
doc
|
|
152
155
|
end
|
|
153
156
|
|
|
154
157
|
# Delete all the documents in the association without running callbacks.
|
|
@@ -209,7 +212,7 @@ module Mongoid
|
|
|
209
212
|
#
|
|
210
213
|
# @return [ true, false ] True is persisted documents exist, false if not.
|
|
211
214
|
def exists?
|
|
212
|
-
|
|
215
|
+
_target.any? { |doc| doc.persisted? }
|
|
213
216
|
end
|
|
214
217
|
|
|
215
218
|
# Finds a document in this association through several different
|
|
@@ -256,6 +259,7 @@ module Mongoid
|
|
|
256
259
|
integrate(doc)
|
|
257
260
|
doc._index = index
|
|
258
261
|
end
|
|
262
|
+
update_attributes_hash
|
|
259
263
|
@_unscoped = _target.dup
|
|
260
264
|
@_target = scope(_target)
|
|
261
265
|
end
|
|
@@ -291,6 +295,8 @@ module Mongoid
|
|
|
291
295
|
end
|
|
292
296
|
else
|
|
293
297
|
delete(_target[-1])
|
|
298
|
+
end.tap do
|
|
299
|
+
update_attributes_hash
|
|
294
300
|
end
|
|
295
301
|
end
|
|
296
302
|
|
|
@@ -314,6 +320,8 @@ module Mongoid
|
|
|
314
320
|
end
|
|
315
321
|
else
|
|
316
322
|
delete(_target[0])
|
|
323
|
+
end.tap do
|
|
324
|
+
update_attributes_hash
|
|
317
325
|
end
|
|
318
326
|
end
|
|
319
327
|
|
|
@@ -328,6 +336,7 @@ module Mongoid
|
|
|
328
336
|
# @return [ Many ] The proxied association.
|
|
329
337
|
def substitute(docs)
|
|
330
338
|
batch_replace(docs)
|
|
339
|
+
update_attributes_hash
|
|
331
340
|
self
|
|
332
341
|
end
|
|
333
342
|
|
|
@@ -365,6 +374,7 @@ module Mongoid
|
|
|
365
374
|
end
|
|
366
375
|
_unscoped.push(document)
|
|
367
376
|
integrate(document)
|
|
377
|
+
update_attributes_hash
|
|
368
378
|
document._index = _unscoped.size - 1
|
|
369
379
|
execute_callback :after_add, document
|
|
370
380
|
end
|
|
@@ -398,6 +408,7 @@ module Mongoid
|
|
|
398
408
|
def delete_one(document)
|
|
399
409
|
_target.delete_one(document)
|
|
400
410
|
_unscoped.delete_one(document)
|
|
411
|
+
update_attributes_hash
|
|
401
412
|
reindex
|
|
402
413
|
end
|
|
403
414
|
|
|
@@ -486,6 +497,7 @@ module Mongoid
|
|
|
486
497
|
criteria = where(conditions || {})
|
|
487
498
|
removed = criteria.size
|
|
488
499
|
batch_remove(criteria, method)
|
|
500
|
+
update_attributes_hash
|
|
489
501
|
removed
|
|
490
502
|
end
|
|
491
503
|
|
|
@@ -511,12 +523,22 @@ module Mongoid
|
|
|
511
523
|
@_unscoped = docs
|
|
512
524
|
end
|
|
513
525
|
|
|
526
|
+
# Returns a list of attributes hashes for each document.
|
|
527
|
+
#
|
|
528
|
+
# @return [ Array<Hash> ] The list of attributes hashes
|
|
514
529
|
def as_attributes
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
530
|
+
_unscoped.map { |doc| doc.send(:as_attributes) }
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
# Update the _base's attributes hash with the _target's attributes
|
|
534
|
+
#
|
|
535
|
+
# @api private
|
|
536
|
+
def update_attributes_hash
|
|
537
|
+
if !_target.empty?
|
|
538
|
+
_base.attributes.merge!(_association.store_as => _target.map(&:attributes))
|
|
539
|
+
else
|
|
540
|
+
_base.attributes.delete(_association.store_as)
|
|
518
541
|
end
|
|
519
|
-
attributes
|
|
520
542
|
end
|
|
521
543
|
|
|
522
544
|
class << self
|
|
@@ -25,11 +25,25 @@ module Mongoid
|
|
|
25
25
|
#
|
|
26
26
|
# @return [ Document ] A single document.
|
|
27
27
|
def build(base, object, _type = nil, selected_fields = nil)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
if object.is_a?(Hash)
|
|
29
|
+
if _loading? && base.persisted?
|
|
30
|
+
Factory.execute_from_db(klass, object, nil, selected_fields, execute_callbacks: false)
|
|
31
|
+
else
|
|
32
|
+
Factory.build(klass, object)
|
|
33
|
+
end
|
|
31
34
|
else
|
|
32
|
-
|
|
35
|
+
clear_associated(object)
|
|
36
|
+
object
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def clear_associated(doc)
|
|
43
|
+
if doc && (inv = inverse(doc))
|
|
44
|
+
if associated = doc.ivar(inv)
|
|
45
|
+
associated.substitute(nil)
|
|
46
|
+
end
|
|
33
47
|
end
|
|
34
48
|
end
|
|
35
49
|
end
|
|
@@ -31,6 +31,7 @@ module Mongoid
|
|
|
31
31
|
characterize_one(_target)
|
|
32
32
|
bind_one
|
|
33
33
|
characterize_one(_target)
|
|
34
|
+
update_attributes_hash(_target)
|
|
34
35
|
_base._reset_memoized_descendants!
|
|
35
36
|
_target.save if persistable?
|
|
36
37
|
end
|
|
@@ -80,11 +81,15 @@ module Mongoid
|
|
|
80
81
|
end
|
|
81
82
|
end
|
|
82
83
|
unbind_one
|
|
83
|
-
|
|
84
|
+
unless replacement
|
|
85
|
+
update_attributes_hash(replacement)
|
|
86
|
+
return nil
|
|
87
|
+
end
|
|
84
88
|
replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash)
|
|
85
89
|
self._target = replacement
|
|
86
|
-
bind_one
|
|
87
90
|
characterize_one(_target)
|
|
91
|
+
bind_one
|
|
92
|
+
update_attributes_hash(_target)
|
|
88
93
|
_target.save if persistable?
|
|
89
94
|
end
|
|
90
95
|
self
|
|
@@ -112,6 +117,20 @@ module Mongoid
|
|
|
112
117
|
_base.persisted? && !_binding? && !_building? && !_assigning?
|
|
113
118
|
end
|
|
114
119
|
|
|
120
|
+
# Update the _base's attributes hash with the _target's attributes
|
|
121
|
+
#
|
|
122
|
+
# @param replacement [ Document | nil ] The doc to use to update the
|
|
123
|
+
# attributes hash.
|
|
124
|
+
#
|
|
125
|
+
# @api private
|
|
126
|
+
def update_attributes_hash(replacement)
|
|
127
|
+
if replacement
|
|
128
|
+
_base.attributes.merge!(_association.store_as => replacement.attributes)
|
|
129
|
+
else
|
|
130
|
+
_base.attributes.delete(_association.store_as)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
115
134
|
class << self
|
|
116
135
|
|
|
117
136
|
# Returns true if the association is an embedded one. In this case
|
|
@@ -34,6 +34,7 @@ module Mongoid
|
|
|
34
34
|
#
|
|
35
35
|
# @api private
|
|
36
36
|
class_attribute :aliased_associations
|
|
37
|
+
|
|
37
38
|
self.embedded = false
|
|
38
39
|
self.embedded_relations = BSON::Document.new
|
|
39
40
|
self.relations = BSON::Document.new
|
|
@@ -216,7 +217,7 @@ module Mongoid
|
|
|
216
217
|
Association::MACRO_MAPPING[macro_name].new(self, name, options, &block).tap do |assoc|
|
|
217
218
|
assoc.setup!
|
|
218
219
|
self.relations = self.relations.merge(name => assoc)
|
|
219
|
-
if assoc.respond_to?(:store_as) && assoc.store_as != name
|
|
220
|
+
if assoc.embedded? && assoc.respond_to?(:store_as) && assoc.store_as != name
|
|
220
221
|
self.aliased_associations[assoc.store_as] = name
|
|
221
222
|
end
|
|
222
223
|
end
|
|
@@ -61,6 +61,11 @@ module Mongoid
|
|
|
61
61
|
attributes.map { |attrs| create!(attrs, type, &block) }
|
|
62
62
|
else
|
|
63
63
|
doc = build(attributes, type, &block)
|
|
64
|
+
|
|
65
|
+
Array(doc).each do |doc|
|
|
66
|
+
doc.try(:run_pending_callbacks)
|
|
67
|
+
end
|
|
68
|
+
|
|
64
69
|
_base.persisted? ? doc.save! : raise_unsaved(doc)
|
|
65
70
|
doc
|
|
66
71
|
end
|
|
@@ -134,8 +134,9 @@ module Mongoid
|
|
|
134
134
|
# @param [ Proxy ] relation The association proxy.
|
|
135
135
|
# @param [ Document ] doc The document to delete.
|
|
136
136
|
def destroy_document(relation, doc)
|
|
137
|
+
res = doc.destroy unless doc.embedded? || doc.destroyed?
|
|
137
138
|
relation.delete(doc)
|
|
138
|
-
|
|
139
|
+
res
|
|
139
140
|
end
|
|
140
141
|
|
|
141
142
|
# Update the document.
|
|
@@ -168,7 +169,7 @@ module Mongoid
|
|
|
168
169
|
# @param [ Hash ] attrs The single document attributes to process.
|
|
169
170
|
def update_nested_relation(parent, id, attrs)
|
|
170
171
|
first = existing.first
|
|
171
|
-
converted =
|
|
172
|
+
converted = first ? convert_id(first.class, id) : id
|
|
172
173
|
|
|
173
174
|
if existing.where(_id: converted).exists?
|
|
174
175
|
# document exists in association
|
|
@@ -178,46 +179,11 @@ module Mongoid
|
|
|
178
179
|
else
|
|
179
180
|
update_document(doc, attrs)
|
|
180
181
|
end
|
|
181
|
-
|
|
182
|
-
raise Errors::DocumentNotFound.new(association.klass, id)
|
|
183
|
-
elsif destroyable?(attrs)
|
|
184
|
-
# A destroy of a document that is not in the association is
|
|
185
|
-
# ignored, rather than reaching for it outside the association.
|
|
186
|
-
nil
|
|
187
|
-
elsif Mongoid.allow_reparenting_via_nested_attributes?
|
|
188
|
-
Mongoid::Warnings.warn_reparenting_via_nested_attributes
|
|
189
|
-
|
|
182
|
+
else
|
|
190
183
|
# push existing document to association
|
|
191
184
|
doc = association.klass.unscoped.find(converted)
|
|
192
|
-
# find returns nil instead of raising when
|
|
193
|
-
# Mongoid.raise_not_found_error is false; a missing document
|
|
194
|
-
# must still be an error here, consistent with the other
|
|
195
|
-
# not-found branches.
|
|
196
|
-
raise Errors::DocumentNotFound.new(association.klass, id) if doc.nil?
|
|
197
|
-
|
|
198
185
|
update_document(doc, attrs)
|
|
199
|
-
existing.push(doc)
|
|
200
|
-
else
|
|
201
|
-
raise Errors::DocumentNotFound.new(association.klass, not_found_params(parent, id))
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
# The params to report when an id in the nested attributes could not
|
|
206
|
-
# be resolved within the association.
|
|
207
|
-
#
|
|
208
|
-
# @api private
|
|
209
|
-
#
|
|
210
|
-
# @param [ Document ] parent The parent document.
|
|
211
|
-
# @param [ String | BSON::ObjectId ] id of the related document.
|
|
212
|
-
#
|
|
213
|
-
# @return [ Hash | Object ] The params for the not found error.
|
|
214
|
-
def not_found_params(parent, id)
|
|
215
|
-
if association.is_a?(Association::Referenced::HasAndBelongsToMany)
|
|
216
|
-
# A many-to-many association has no foreign key on the child, so
|
|
217
|
-
# the id is only ever resolved within the association itself.
|
|
218
|
-
id
|
|
219
|
-
else
|
|
220
|
-
{ _id: id, association.foreign_key => parent.id }
|
|
186
|
+
existing.push(doc) unless destroyable?(attrs)
|
|
221
187
|
end
|
|
222
188
|
end
|
|
223
189
|
end
|
|
@@ -53,26 +53,12 @@ module Mongoid
|
|
|
53
53
|
# @example Convert the id.
|
|
54
54
|
# builder.convert_id(Person, "4d371b444835d98b8b000010")
|
|
55
55
|
#
|
|
56
|
-
# Ids arriving from a form are always scalars. A Hash or an Array
|
|
57
|
-
# here means the parameters were crafted, and letting one through
|
|
58
|
-
# would turn the id into a query operator, so they are rejected.
|
|
59
|
-
#
|
|
60
56
|
# @param [ Class ] klass The class we're trying to convert for.
|
|
61
57
|
# @param [ String ] id The id, usually coming from the form.
|
|
62
58
|
#
|
|
63
59
|
# @return [ BSON::ObjectId, String, Object ] The converted id.
|
|
64
|
-
#
|
|
65
|
-
# @raise [ Errors::DocumentNotFound ] if the id is not a scalar, or
|
|
66
|
-
# cannot be converted to the type the class uses for its ids.
|
|
67
|
-
# The BSON::Error rescue is defensive: a value that reaches
|
|
68
|
-
# BSON::ObjectId.mongoize and raises there must not surface as an
|
|
69
|
-
# unhandled BSON error in the caller.
|
|
70
60
|
def convert_id(klass, id)
|
|
71
|
-
raise Errors::DocumentNotFound.new(klass, id) if id.is_a?(::Hash) || id.is_a?(::Array)
|
|
72
|
-
|
|
73
61
|
klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
|
|
74
|
-
rescue BSON::Error
|
|
75
|
-
raise Errors::DocumentNotFound.new(klass, id)
|
|
76
62
|
end
|
|
77
63
|
end
|
|
78
64
|
end
|
|
@@ -169,6 +169,18 @@ module Mongoid
|
|
|
169
169
|
end
|
|
170
170
|
end
|
|
171
171
|
|
|
172
|
+
# Execute the before and after callbacks for the given method.
|
|
173
|
+
#
|
|
174
|
+
# @param [ Symbol ] name The name of the callbacks to execute.
|
|
175
|
+
#
|
|
176
|
+
# @return [ Object ] The result of the given block
|
|
177
|
+
def execute_callbacks_around(name, doc)
|
|
178
|
+
execute_callback :"before_#{name.to_s}", doc
|
|
179
|
+
yield.tap do
|
|
180
|
+
execute_callback :"after_#{name.to_s}", doc
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
172
184
|
class << self
|
|
173
185
|
|
|
174
186
|
# Apply ordering to the criteria if it was defined on the association.
|
|
@@ -56,7 +56,8 @@ module Mongoid
|
|
|
56
56
|
__autosaving__ do
|
|
57
57
|
if assoc_value = ivar(association.name)
|
|
58
58
|
Array(assoc_value).each do |doc|
|
|
59
|
-
doc.
|
|
59
|
+
pc = doc.persistence_context? ? doc.persistence_context : persistence_context
|
|
60
|
+
doc.with(pc) do |d|
|
|
60
61
|
d.save
|
|
61
62
|
end
|
|
62
63
|
end
|
|
@@ -64,7 +65,7 @@ module Mongoid
|
|
|
64
65
|
end
|
|
65
66
|
end
|
|
66
67
|
end
|
|
67
|
-
klass.
|
|
68
|
+
klass.after_persist_parent save_method, unless: :autosaved?
|
|
68
69
|
end
|
|
69
70
|
end
|
|
70
71
|
end
|
|
@@ -153,7 +153,7 @@ module Mongoid
|
|
|
153
153
|
create_foreign_key_field!
|
|
154
154
|
setup_index!
|
|
155
155
|
define_touchable!
|
|
156
|
-
@owner_class.validates_associated(name) if validate?
|
|
156
|
+
@owner_class.validates_associated(name) if validate? || require_association?
|
|
157
157
|
@owner_class.validates(name, presence: true) if require_association?
|
|
158
158
|
end
|
|
159
159
|
|
|
@@ -97,18 +97,18 @@ module Mongoid
|
|
|
97
97
|
|
|
98
98
|
association.inverse_class.tap do |klass|
|
|
99
99
|
klass.after_update do
|
|
100
|
-
|
|
101
|
-
foreign_key = association.foreign_key
|
|
100
|
+
foreign_key = association.foreign_key
|
|
102
101
|
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
if send("#{foreign_key}_previously_changed?")
|
|
103
|
+
original, current = send("#{foreign_key}_previous_change")
|
|
105
104
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
end
|
|
105
|
+
unless original.nil?
|
|
106
|
+
association.klass.with(persistence_context) do |_class|
|
|
107
|
+
_class.decrement_counter(cache_column, original)
|
|
110
108
|
end
|
|
109
|
+
end
|
|
111
110
|
|
|
111
|
+
if record = __send__(name)
|
|
112
112
|
unless current.nil?
|
|
113
113
|
record[cache_column] = (record[cache_column] || 0) + 1
|
|
114
114
|
record.class.with(record.persistence_context) do |_class|
|
|
@@ -28,10 +28,34 @@ module Mongoid
|
|
|
28
28
|
docs = args.flatten
|
|
29
29
|
return concat(docs) if docs.size > 1
|
|
30
30
|
if doc = docs.first
|
|
31
|
-
append(doc)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
append(doc) do
|
|
32
|
+
# We ignore the changes to the value for the foreign key in the
|
|
33
|
+
# changed_attributes hash in this block of code for two reasons:
|
|
34
|
+
#
|
|
35
|
+
# 1) The add_to_set method deletes the value for the foreign
|
|
36
|
+
# key in the changed_attributes hash, but if we enter this
|
|
37
|
+
# method with a value for the foreign key in the
|
|
38
|
+
# changed_attributes hash, then we want it to exist outside
|
|
39
|
+
# this method as well. It's used later on in the Syncable
|
|
40
|
+
# module to set the inverse foreign keys.
|
|
41
|
+
# 2) The reset_unloaded method accesses the value for the foreign
|
|
42
|
+
# key on _base, which causes it to get added to the
|
|
43
|
+
# changed_attributes hash. This happens because when reading
|
|
44
|
+
# a "resizable" attribute, it is automatically added to the
|
|
45
|
+
# changed_attributes hash. This is true only for the foreign
|
|
46
|
+
# key value for HABTM associations as the other associations
|
|
47
|
+
# use strings for their foreign key values. For consistency
|
|
48
|
+
# with the other associations, we ignore this addition to
|
|
49
|
+
# the changed_attributes hash.
|
|
50
|
+
# See MONGOID-4843 for a longer discussion about this.
|
|
51
|
+
reset_foreign_key_changes do
|
|
52
|
+
_base.add_to_set(foreign_key => doc.public_send(_association.primary_key))
|
|
53
|
+
|
|
54
|
+
if child_persistable?(doc)
|
|
55
|
+
doc.save
|
|
56
|
+
end
|
|
57
|
+
reset_unloaded
|
|
58
|
+
end
|
|
35
59
|
end
|
|
36
60
|
end
|
|
37
61
|
unsynced(_base, foreign_key) and self
|
|
@@ -81,12 +105,13 @@ module Mongoid
|
|
|
81
105
|
#
|
|
82
106
|
# @return [ Document ] The new document.
|
|
83
107
|
def build(attributes = {}, type = nil)
|
|
84
|
-
doc = Factory.
|
|
85
|
-
_base.public_send(foreign_key).push(doc.public_send(_association.primary_key))
|
|
108
|
+
doc = Factory.execute_build(type || klass, attributes, execute_callbacks: false)
|
|
86
109
|
append(doc)
|
|
87
110
|
doc.apply_post_processed_defaults
|
|
111
|
+
_base.public_send(foreign_key).push(doc.public_send(_association.primary_key))
|
|
88
112
|
unsynced(doc, inverse_foreign_key)
|
|
89
113
|
yield(doc) if block_given?
|
|
114
|
+
doc.run_pending_callbacks
|
|
90
115
|
doc
|
|
91
116
|
end
|
|
92
117
|
|
|
@@ -178,6 +203,7 @@ module Mongoid
|
|
|
178
203
|
push(replacement.compact.uniq)
|
|
179
204
|
else
|
|
180
205
|
reset_unloaded
|
|
206
|
+
clear_foreign_key_changes
|
|
181
207
|
end
|
|
182
208
|
self
|
|
183
209
|
end
|
|
@@ -195,6 +221,32 @@ module Mongoid
|
|
|
195
221
|
|
|
196
222
|
private
|
|
197
223
|
|
|
224
|
+
# Clears the foreign key from the changed_attributes hash.
|
|
225
|
+
#
|
|
226
|
+
# This is, in general, used to clear the foreign key from the
|
|
227
|
+
# changed_attributes hash for consistency with the other referenced
|
|
228
|
+
# associations.
|
|
229
|
+
#
|
|
230
|
+
# @api private
|
|
231
|
+
def clear_foreign_key_changes
|
|
232
|
+
_base.changed_attributes.delete(foreign_key)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Reset the value in the changed_attributes hash for the foreign key
|
|
236
|
+
# to its value before executing the given block.
|
|
237
|
+
#
|
|
238
|
+
# @api private
|
|
239
|
+
def reset_foreign_key_changes
|
|
240
|
+
if _base.changed_attributes.key?(foreign_key)
|
|
241
|
+
fk = _base.changed_attributes[foreign_key].dup
|
|
242
|
+
yield if block_given?
|
|
243
|
+
_base.changed_attributes[foreign_key] = fk
|
|
244
|
+
else
|
|
245
|
+
yield if block_given?
|
|
246
|
+
clear_foreign_key_changes
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
198
250
|
# Appends the document to the target array, updating the index on the
|
|
199
251
|
# document at the same time.
|
|
200
252
|
#
|
|
@@ -203,11 +255,12 @@ module Mongoid
|
|
|
203
255
|
#
|
|
204
256
|
# @param [ Document ] document The document to append to the target.
|
|
205
257
|
def append(document)
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
258
|
+
execute_callbacks_around(:add, document) do
|
|
259
|
+
_target.push(document)
|
|
260
|
+
characterize_one(document)
|
|
261
|
+
bind_one(document)
|
|
262
|
+
yield if block_given?
|
|
263
|
+
end
|
|
211
264
|
end
|
|
212
265
|
|
|
213
266
|
# Instantiate the binding associated with this association.
|
|
@@ -119,6 +119,8 @@ module Mongoid
|
|
|
119
119
|
@options[:inverse_foreign_key]
|
|
120
120
|
elsif @options.key?(:inverse_of)
|
|
121
121
|
inverse_of ? "#{inverse_of.to_s.singularize}#{FOREIGN_KEY_SUFFIX}" : nil
|
|
122
|
+
elsif inv = inverse_association&.foreign_key
|
|
123
|
+
inv
|
|
122
124
|
else
|
|
123
125
|
"#{inverse_class_name.demodulize.underscore}#{FOREIGN_KEY_SUFFIX}"
|
|
124
126
|
end
|
|
@@ -219,7 +221,7 @@ module Mongoid
|
|
|
219
221
|
def synced_save
|
|
220
222
|
assoc = self
|
|
221
223
|
inverse_class.set_callback(
|
|
222
|
-
:
|
|
224
|
+
:persist_parent,
|
|
223
225
|
:after,
|
|
224
226
|
if: ->(doc){ doc._syncable?(assoc) }
|
|
225
227
|
) do |doc|
|
|
@@ -228,6 +230,7 @@ module Mongoid
|
|
|
228
230
|
end
|
|
229
231
|
|
|
230
232
|
def create_foreign_key_field!
|
|
233
|
+
inverse_class.aliased_associations[foreign_key] = name.to_s
|
|
231
234
|
@owner_class.field(
|
|
232
235
|
foreign_key,
|
|
233
236
|
type: FOREIGN_KEY_FIELD_TYPE,
|