mongoid 6.4.8 → 7.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +0 -26
- data/lib/config/locales/en.yml +17 -21
- data/lib/mongoid.rb +2 -2
- data/lib/mongoid/association.rb +150 -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 +116 -0
- data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
- data/lib/mongoid/association/embedded.rb +4 -0
- data/lib/mongoid/{relations → association}/embedded/batchable.rb +27 -53
- data/lib/mongoid/association/embedded/cyclic.rb +109 -0
- data/lib/mongoid/association/embedded/embedded_in.rb +154 -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/embeds_many.rb +210 -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 +529 -0
- data/lib/mongoid/association/embedded/embeds_one.rb +173 -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/macros.rb +204 -0
- data/lib/mongoid/{relations → association}/many.rb +18 -52
- data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
- data/lib/mongoid/association/nested.rb +15 -0
- 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/{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.rb +7 -0
- data/lib/mongoid/association/referenced/auto_save.rb +79 -0
- data/lib/mongoid/association/referenced/belongs_to.rb +248 -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/counter_cache.rb +163 -0
- data/lib/mongoid/association/referenced/eager.rb +159 -0
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -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 +310 -0
- data/lib/mongoid/association/referenced/has_many.rb +273 -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 +479 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +577 -0
- data/lib/mongoid/association/referenced/has_one.rb +204 -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/syncable.rb +170 -0
- data/lib/mongoid/{relations → association}/reflections.rb +21 -17
- data/lib/mongoid/association/relatable.rb +415 -0
- data/lib/mongoid/association/touchable.rb +97 -0
- data/lib/mongoid/atomic.rb +6 -6
- data/lib/mongoid/atomic/modifiers.rb +8 -12
- data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
- data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
- data/lib/mongoid/attributes.rb +2 -1
- data/lib/mongoid/attributes/nested.rb +10 -10
- data/lib/mongoid/attributes/processing.rb +2 -2
- data/lib/mongoid/attributes/readonly.rb +2 -4
- data/lib/mongoid/clients.rb +0 -2
- data/lib/mongoid/clients/options.rb +1 -1
- data/lib/mongoid/clients/storage_options.rb +0 -1
- data/lib/mongoid/composable.rb +3 -4
- data/lib/mongoid/config.rb +1 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +3 -6
- data/lib/mongoid/contextual/map_reduce.rb +3 -7
- data/lib/mongoid/contextual/memory.rb +5 -10
- data/lib/mongoid/contextual/mongo.rb +10 -27
- data/lib/mongoid/copyable.rb +6 -6
- data/lib/mongoid/criteria.rb +1 -2
- data/lib/mongoid/criteria/includable.rb +14 -14
- data/lib/mongoid/criteria/modifiable.rb +8 -14
- data/lib/mongoid/criteria/queryable/mergeable.rb +1 -3
- data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
- data/lib/mongoid/criteria/queryable/selectable.rb +10 -34
- data/lib/mongoid/document.rb +6 -6
- data/lib/mongoid/errors.rb +3 -1
- 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/extensions/array.rb +5 -5
- data/lib/mongoid/extensions/big_decimal.rb +1 -1
- data/lib/mongoid/extensions/object.rb +4 -4
- data/lib/mongoid/extensions/range.rb +1 -0
- data/lib/mongoid/extensions/regexp.rb +0 -1
- data/lib/mongoid/extensions/string.rb +1 -3
- data/lib/mongoid/factory.rb +4 -3
- data/lib/mongoid/fields.rb +1 -1
- 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/indexable.rb +8 -5
- data/lib/mongoid/interceptable.rb +5 -5
- data/lib/mongoid/matchable.rb +0 -3
- data/lib/mongoid/persistable.rb +4 -5
- data/lib/mongoid/persistable/creatable.rb +2 -4
- data/lib/mongoid/persistable/deletable.rb +9 -10
- data/lib/mongoid/persistable/destroyable.rb +5 -1
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +5 -5
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -2
- data/lib/mongoid/persistence_context.rb +4 -9
- data/lib/mongoid/query_cache.rb +18 -65
- data/lib/mongoid/railtie.rb +0 -17
- data/lib/mongoid/reloadable.rb +1 -1
- data/lib/mongoid/scopable.rb +3 -3
- data/lib/mongoid/serializable.rb +3 -3
- data/lib/mongoid/tasks/database.rb +2 -3
- data/lib/mongoid/threaded.rb +0 -74
- data/lib/mongoid/traversable.rb +2 -2
- data/lib/mongoid/validatable.rb +8 -8
- data/lib/mongoid/validatable/presence.rb +2 -2
- data/lib/mongoid/validatable/uniqueness.rb +4 -4
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -4
- data/spec/app/models/animal.rb +2 -1
- data/spec/app/models/band.rb +0 -1
- data/spec/app/models/bomb.rb +1 -1
- data/spec/app/models/message.rb +1 -1
- data/spec/app/models/person.rb +5 -2
- data/spec/app/models/vertex.rb +6 -0
- data/spec/app/models/wiki_page.rb +1 -1
- data/spec/config/mongoid.yml +1 -0
- data/spec/mongoid/{relations → association}/accessors_spec.rb +1 -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 +613 -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 → association}/embedded/dirty_spec.rb +0 -0
- 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} +140 -428
- 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 -93
- 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 +7 -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} +9 -9
- 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 +1963 -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} +5 -5
- 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_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} +11 -11
- data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +1 -109
- data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
- 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_spec.rb +1244 -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 → association}/touchable_spec.rb +19 -1
- data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
- data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
- data/spec/mongoid/atomic_spec.rb +17 -17
- data/spec/mongoid/attributes/nested_spec.rb +14 -12
- data/spec/mongoid/attributes/readonly_spec.rb +80 -125
- data/spec/mongoid/clients/factory_spec.rb +28 -52
- data/spec/mongoid/clients/options_spec.rb +65 -69
- data/spec/mongoid/config_spec.rb +24 -0
- data/spec/mongoid/contextual/geo_near_spec.rb +0 -1
- data/spec/mongoid/contextual/mongo_spec.rb +4 -112
- 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/pipeline_spec.rb +12 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +6 -74
- data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
- data/spec/mongoid/criteria/scopable_spec.rb +0 -81
- data/spec/mongoid/criteria_spec.rb +16 -19
- data/spec/mongoid/document_spec.rb +2 -56
- data/spec/mongoid/extensions/array_spec.rb +11 -15
- data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
- 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 +0 -23
- data/spec/mongoid/extensions/string_spec.rb +7 -35
- data/spec/mongoid/factory_spec.rb +18 -11
- 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 +21 -2
- data/spec/mongoid/matchable_spec.rb +1 -26
- data/spec/mongoid/persistable/deletable_spec.rb +0 -19
- data/spec/mongoid/persistable/destroyable_spec.rb +0 -19
- data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
- data/spec/mongoid/persistable/savable_spec.rb +3 -3
- data/spec/mongoid/persistable/settable_spec.rb +1 -35
- data/spec/mongoid/persistable/updatable_spec.rb +2 -2
- data/spec/mongoid/persistable_spec.rb +16 -16
- data/spec/mongoid/persistence_context_spec.rb +0 -14
- data/spec/mongoid/positional_spec.rb +10 -10
- data/spec/mongoid/query_cache_spec.rb +18 -87
- data/spec/mongoid/relations/proxy_spec.rb +124 -124
- data/spec/mongoid/scopable_spec.rb +0 -13
- data/spec/mongoid/threaded_spec.rb +0 -68
- 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/spec_helper.rb +7 -83
- metadata +586 -611
- metadata.gz.sig +2 -5
- data/lib/mongoid/clients/sessions.rb +0 -113
- data/lib/mongoid/errors/invalid_session_use.rb +0 -24
- data/lib/mongoid/matchable/nor.rb +0 -37
- data/lib/mongoid/railties/controller_runtime.rb +0 -86
- data/lib/mongoid/relations.rb +0 -148
- data/lib/mongoid/relations/accessors.rb +0 -267
- data/lib/mongoid/relations/auto_save.rb +0 -94
- data/lib/mongoid/relations/bindings.rb +0 -9
- 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/builder.rb +0 -57
- data/lib/mongoid/relations/builders.rb +0 -106
- 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/cascading.rb +0 -56
- 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/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.rb +0 -2
- data/lib/mongoid/relations/targets/enumerable.rb +0 -493
- data/lib/mongoid/relations/touchable.rb +0 -97
- data/spec/app/models/array_field.rb +0 -7
- data/spec/app/models/delegating_patient.rb +0 -16
- data/spec/integration/document_spec.rb +0 -22
- data/spec/mongoid/clients/sessions_spec.rb +0 -334
- 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/matchable/nor_spec.rb +0 -209
- 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/spec/rails/controller_extension/controller_runtime_spec.rb +0 -110
- data/spec/support/cluster_config.rb +0 -158
- data/spec/support/constraints.rb +0 -101
- data/spec/support/macros.rb +0 -20
- data/spec/support/session_registry.rb +0 -50
- data/spec/support/spec_config.rb +0 -42
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Mongoid::
|
3
|
+
describe Mongoid::Association::Referenced::HasOne::Binding do
|
4
4
|
|
5
5
|
let(:person) do
|
6
6
|
Person.new
|
@@ -10,14 +10,14 @@ describe Mongoid::Relations::Bindings::Referenced::One do
|
|
10
10
|
Game.new
|
11
11
|
end
|
12
12
|
|
13
|
-
let(:
|
13
|
+
let(:association) do
|
14
14
|
Person.relations["game"]
|
15
15
|
end
|
16
16
|
|
17
17
|
describe "#bind_one" do
|
18
18
|
|
19
19
|
let(:binding) do
|
20
|
-
described_class.new(person, game,
|
20
|
+
described_class.new(person, game, association)
|
21
21
|
end
|
22
22
|
|
23
23
|
context "when the document is bindable" do
|
@@ -53,7 +53,7 @@ describe Mongoid::Relations::Bindings::Referenced::One do
|
|
53
53
|
describe "#unbind_one" do
|
54
54
|
|
55
55
|
let(:binding) do
|
56
|
-
described_class.new(person, game,
|
56
|
+
described_class.new(person, game, association)
|
57
57
|
end
|
58
58
|
|
59
59
|
context "when the document is unbindable" do
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Association::Referenced::HasOne::Buildable do
|
4
|
+
|
5
|
+
let(:base) do
|
6
|
+
double(new_record?: false)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#build" do
|
10
|
+
|
11
|
+
let(:document) do
|
12
|
+
association.build(base, object)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:association) do
|
16
|
+
Mongoid::Association::Referenced::HasOne.new(Person, :account)
|
17
|
+
end
|
18
|
+
|
19
|
+
context "when provided an id" do
|
20
|
+
|
21
|
+
let!(:account) do
|
22
|
+
Account.create!(person_id: object, name: 'banking')
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:object) do
|
26
|
+
BSON::ObjectId.new
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
expect(Account).to receive(:where).with(association.foreign_key => object).and_call_original
|
31
|
+
end
|
32
|
+
|
33
|
+
it "sets the document" do
|
34
|
+
expect(document).to eq(account)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "when provided a object" do
|
39
|
+
|
40
|
+
let(:object) do
|
41
|
+
Account.new
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns the object" do
|
45
|
+
expect(document).to eq(object)
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when the object is already related to another object' do
|
49
|
+
|
50
|
+
let(:original_person) do
|
51
|
+
Person.new
|
52
|
+
end
|
53
|
+
|
54
|
+
let(:object) do
|
55
|
+
Account.new(person: original_person)
|
56
|
+
end
|
57
|
+
|
58
|
+
let!(:document) do
|
59
|
+
association.build(Person.new, object)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'clears the object of its previous relation' do
|
63
|
+
expect(original_person.account).to be_nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'returns the object' do
|
67
|
+
expect(document).to eq(object)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when the document is not found" do
|
73
|
+
|
74
|
+
let(:object) do
|
75
|
+
BSON::ObjectId.new
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns nil" do
|
79
|
+
expect(document).to be_nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "when the document is persisted" do
|
84
|
+
|
85
|
+
let(:person) do
|
86
|
+
Person.create
|
87
|
+
end
|
88
|
+
|
89
|
+
let!(:game) do
|
90
|
+
Game.create(person: person)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "returns the document" do
|
94
|
+
expect(person.game).to eq(game)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when the document have a non standard pk" do
|
99
|
+
|
100
|
+
let(:person) do
|
101
|
+
Person.create
|
102
|
+
end
|
103
|
+
|
104
|
+
let!(:cat) do
|
105
|
+
Cat.create(person: person)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "returns the document" do
|
109
|
+
expect(person.cat).to eq(cat)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Mongoid::
|
3
|
+
describe Mongoid::Association::Referenced::HasOne::Eager do
|
4
4
|
|
5
5
|
describe ".grouped_doc" do
|
6
6
|
|
@@ -12,13 +12,13 @@ describe Mongoid::Relations::Eager::HasOne do
|
|
12
12
|
Person.all.to_a
|
13
13
|
end
|
14
14
|
|
15
|
-
let(:
|
15
|
+
let(:association) do
|
16
16
|
Person.reflect_on_association(:cat)
|
17
17
|
end
|
18
18
|
|
19
19
|
let(:eager) do
|
20
|
-
described_class.new([
|
21
|
-
b.
|
20
|
+
described_class.new([association], docs).tap do |b|
|
21
|
+
b.send(:shift_association)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -27,7 +27,7 @@ describe Mongoid::Relations::Eager::HasOne do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it "aggregates by the relation primary key" do
|
30
|
-
expect(eager.grouped_docs.keys).to eq([person.username])
|
30
|
+
expect(eager.send(:grouped_docs).keys).to eq([person.username])
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -41,13 +41,13 @@ describe Mongoid::Relations::Eager::HasOne do
|
|
41
41
|
Person.all.to_a
|
42
42
|
end
|
43
43
|
|
44
|
-
let(:
|
44
|
+
let(:association) do
|
45
45
|
Person.reflect_on_association(:cat)
|
46
46
|
end
|
47
47
|
|
48
48
|
let(:eager) do
|
49
|
-
described_class.new([
|
50
|
-
b.
|
49
|
+
described_class.new([association], docs).tap do |b|
|
50
|
+
b.send(:shift_association)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -60,12 +60,12 @@ describe Mongoid::Relations::Eager::HasOne do
|
|
60
60
|
docs.each do |doc|
|
61
61
|
expect(doc).to receive(:set_relation).once.with(:cat, :foo)
|
62
62
|
end
|
63
|
-
eager.set_on_parent
|
63
|
+
eager.send(:set_on_parent, person.username, :foo)
|
64
64
|
end
|
65
65
|
|
66
66
|
it "doesnt call an extra query" do
|
67
67
|
expect_query(0) do
|
68
|
-
eager.set_on_parent
|
68
|
+
eager.send(:set_on_parent, person.username, :foo)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Mongoid::
|
3
|
+
describe Mongoid::Association::Referenced::HasOne::Proxy do
|
4
4
|
|
5
5
|
describe "#=" do
|
6
6
|
|
@@ -52,17 +52,17 @@ describe Mongoid::Relations::Referenced::One do
|
|
52
52
|
Game.new
|
53
53
|
end
|
54
54
|
|
55
|
-
let(:
|
55
|
+
let(:association) do
|
56
56
|
Game.relations["person"]
|
57
57
|
end
|
58
58
|
|
59
59
|
before do
|
60
|
-
expect(
|
60
|
+
expect(association).to receive(:criteria).never
|
61
61
|
person.game = game
|
62
62
|
end
|
63
63
|
|
64
64
|
it "sets the target of the relation" do
|
65
|
-
expect(person.game.
|
65
|
+
expect(person.game._target).to eq(game)
|
66
66
|
end
|
67
67
|
|
68
68
|
it "sets the foreign key on the relation" do
|
@@ -97,7 +97,7 @@ describe Mongoid::Relations::Referenced::One do
|
|
97
97
|
end
|
98
98
|
|
99
99
|
it "sets the target of the relation" do
|
100
|
-
expect(person.game.
|
100
|
+
expect(person.game._target).to eq(game)
|
101
101
|
end
|
102
102
|
|
103
103
|
it "sets the foreign key of the relation" do
|
@@ -152,7 +152,7 @@ describe Mongoid::Relations::Referenced::One do
|
|
152
152
|
end
|
153
153
|
|
154
154
|
it "sets the target of the relation" do
|
155
|
-
expect(person.cat.
|
155
|
+
expect(person.cat._target).to eq(cat)
|
156
156
|
end
|
157
157
|
|
158
158
|
it "sets the foreign key of the relation" do
|
@@ -190,7 +190,7 @@ describe Mongoid::Relations::Referenced::One do
|
|
190
190
|
end
|
191
191
|
|
192
192
|
it "sets the target of the relation" do
|
193
|
-
expect(bar.rating.
|
193
|
+
expect(bar.rating._target).to eq(rating)
|
194
194
|
end
|
195
195
|
|
196
196
|
it "sets the foreign key on the relation" do
|
@@ -225,7 +225,7 @@ describe Mongoid::Relations::Referenced::One do
|
|
225
225
|
end
|
226
226
|
|
227
227
|
it "sets the target of the relation" do
|
228
|
-
expect(bar.rating.
|
228
|
+
expect(bar.rating._target).to eq(rating)
|
229
229
|
end
|
230
230
|
|
231
231
|
it "sets the foreign key of the relation" do
|
@@ -859,27 +859,6 @@ describe Mongoid::Relations::Referenced::One do
|
|
859
859
|
end
|
860
860
|
end
|
861
861
|
|
862
|
-
describe ".builder" do
|
863
|
-
|
864
|
-
let(:builder_klass) do
|
865
|
-
Mongoid::Relations::Builders::Referenced::One
|
866
|
-
end
|
867
|
-
|
868
|
-
let(:document) do
|
869
|
-
double
|
870
|
-
end
|
871
|
-
|
872
|
-
let(:metadata) do
|
873
|
-
double(extension?: false)
|
874
|
-
end
|
875
|
-
|
876
|
-
it "returns the embedded in builder" do
|
877
|
-
expect(
|
878
|
-
described_class.builder(nil, metadata, document)
|
879
|
-
).to be_a_kind_of(builder_klass)
|
880
|
-
end
|
881
|
-
end
|
882
|
-
|
883
862
|
describe "#create_#\{name}" do
|
884
863
|
|
885
864
|
context "when the relationship is an illegal embedded reference" do
|
@@ -996,48 +975,6 @@ describe Mongoid::Relations::Referenced::One do
|
|
996
975
|
end
|
997
976
|
end
|
998
977
|
|
999
|
-
describe ".criteria" do
|
1000
|
-
|
1001
|
-
let(:id) do
|
1002
|
-
BSON::ObjectId.new
|
1003
|
-
end
|
1004
|
-
|
1005
|
-
context "when the relation is polymorphic" do
|
1006
|
-
|
1007
|
-
let(:metadata) do
|
1008
|
-
Book.relations["rating"]
|
1009
|
-
end
|
1010
|
-
|
1011
|
-
let(:criteria) do
|
1012
|
-
described_class.criteria(metadata, id, Book)
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
it "includes the type in the criteria" do
|
1016
|
-
expect(criteria.selector).to eq(
|
1017
|
-
{
|
1018
|
-
"ratable_id" => id,
|
1019
|
-
"ratable_type" => "Book"
|
1020
|
-
}
|
1021
|
-
)
|
1022
|
-
end
|
1023
|
-
end
|
1024
|
-
|
1025
|
-
context "when the relation is not polymorphic" do
|
1026
|
-
|
1027
|
-
let(:metadata) do
|
1028
|
-
Person.relations["game"]
|
1029
|
-
end
|
1030
|
-
|
1031
|
-
let(:criteria) do
|
1032
|
-
described_class.criteria(metadata, id, Person)
|
1033
|
-
end
|
1034
|
-
|
1035
|
-
it "does not include the type in the criteria" do
|
1036
|
-
expect(criteria.selector).to eq({ "person_id" => id })
|
1037
|
-
end
|
1038
|
-
end
|
1039
|
-
end
|
1040
|
-
|
1041
978
|
describe ".embedded?" do
|
1042
979
|
|
1043
980
|
it "returns false" do
|
@@ -1045,20 +982,6 @@ describe Mongoid::Relations::Referenced::One do
|
|
1045
982
|
end
|
1046
983
|
end
|
1047
984
|
|
1048
|
-
describe ".foreign_key_suffix" do
|
1049
|
-
|
1050
|
-
it "returns _id" do
|
1051
|
-
expect(described_class.foreign_key_suffix).to eq("_id")
|
1052
|
-
end
|
1053
|
-
end
|
1054
|
-
|
1055
|
-
describe ".macro" do
|
1056
|
-
|
1057
|
-
it "returns has_one" do
|
1058
|
-
expect(described_class.macro).to eq(:has_one)
|
1059
|
-
end
|
1060
|
-
end
|
1061
|
-
|
1062
985
|
describe "#nullify" do
|
1063
986
|
|
1064
987
|
let(:person) do
|
@@ -1133,29 +1056,6 @@ describe Mongoid::Relations::Referenced::One do
|
|
1133
1056
|
end
|
1134
1057
|
end
|
1135
1058
|
|
1136
|
-
describe ".stores_foreign_key?" do
|
1137
|
-
|
1138
|
-
it "returns false" do
|
1139
|
-
expect(described_class.stores_foreign_key?).to be false
|
1140
|
-
end
|
1141
|
-
end
|
1142
|
-
|
1143
|
-
describe ".valid_options" do
|
1144
|
-
|
1145
|
-
it "returns the valid options" do
|
1146
|
-
expect(described_class.valid_options).to eq(
|
1147
|
-
[ :as, :autobuild, :autosave, :dependent, :foreign_key, :primary_key ]
|
1148
|
-
)
|
1149
|
-
end
|
1150
|
-
end
|
1151
|
-
|
1152
|
-
describe ".validation_default" do
|
1153
|
-
|
1154
|
-
it "returns true" do
|
1155
|
-
expect(described_class.validation_default).to be true
|
1156
|
-
end
|
1157
|
-
end
|
1158
|
-
|
1159
1059
|
context "when reloading the relation" do
|
1160
1060
|
|
1161
1061
|
let!(:person) do
|
@@ -1178,7 +1078,7 @@ describe Mongoid::Relations::Referenced::One do
|
|
1178
1078
|
|
1179
1079
|
before do
|
1180
1080
|
Game.collection.find({ _id: game_one.id }).
|
1181
|
-
|
1081
|
+
update_one({ "$set" => { name: "Diablo 2" }})
|
1182
1082
|
end
|
1183
1083
|
|
1184
1084
|
let(:reloaded) do
|
@@ -0,0 +1,1244 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Association::Referenced::HasOne do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class OwnerObject; include Mongoid::Document; end
|
7
|
+
class BelongingObject; include Mongoid::Document; end
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
Object.send(:remove_const, :BelongingObject)
|
12
|
+
Object.send(:remove_const, :OwnerObject)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:has_one_class) do
|
16
|
+
OwnerObject
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:name) do
|
20
|
+
:belonging_object
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:association) do
|
24
|
+
has_one_class.has_one name, options
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:options) do
|
28
|
+
{ }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#relation_complements' do
|
32
|
+
|
33
|
+
let(:expected_complements) do
|
34
|
+
[
|
35
|
+
Mongoid::Association::Referenced::BelongsTo,
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns the relation complements' do
|
40
|
+
expect(association.relation_complements).to eq(expected_complements)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#setup!' do
|
45
|
+
|
46
|
+
it 'sets up a getter for the relation' do
|
47
|
+
expect(Mongoid::Association::Accessors).to receive(:define_getter!).with(association)
|
48
|
+
association.setup!
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'sets up a setter for the relation' do
|
52
|
+
expect(Mongoid::Association::Accessors).to receive(:define_setter!).with(association)
|
53
|
+
association.setup!
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'sets up an existence check for the relation' do
|
57
|
+
expect(Mongoid::Association::Accessors).to receive(:define_existence_check!).with(association)
|
58
|
+
association.setup!
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'sets up the builder for the relation' do
|
62
|
+
expect(Mongoid::Association::Builders).to receive(:define_builder!).with(association)
|
63
|
+
association.setup!
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'sets up the creator for the relation' do
|
67
|
+
expect(Mongoid::Association::Builders).to receive(:define_creator!).with(association)
|
68
|
+
association.setup!
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'autosave' do
|
72
|
+
|
73
|
+
context 'when the :autosave option is true' do
|
74
|
+
|
75
|
+
let(:options) do
|
76
|
+
{
|
77
|
+
autosave: true
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
let(:association) do
|
82
|
+
# Note that it is necessary to create the association directly, otherwise the
|
83
|
+
# setup! method will be called by the :has_one macro
|
84
|
+
described_class.new(has_one_class, name, options)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'sets up autosave' do
|
88
|
+
expect(Mongoid::Association::Referenced::AutoSave).to receive(:define_autosave!).with(association)
|
89
|
+
association.setup!
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'when the :autosave option is false' do
|
94
|
+
|
95
|
+
let(:options) do
|
96
|
+
{
|
97
|
+
autosave: false
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'does not set up autosave' do
|
102
|
+
expect(Mongoid::Association::Referenced::AutoSave).not_to receive(:define_autosave!)
|
103
|
+
association.setup!
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'when the :autosave option is not provided' do
|
108
|
+
|
109
|
+
let(:association) do
|
110
|
+
# Note that it is necessary to create the association directly, otherwise the
|
111
|
+
# setup! method will be called by the :has_one macro
|
112
|
+
described_class.new(has_one_class, name, options)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'does not set up autosave' do
|
116
|
+
expect(Mongoid::Association::Referenced::AutoSave).not_to receive(:define_autosave!)
|
117
|
+
association.setup!
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'when the :validate option is true' do
|
123
|
+
|
124
|
+
let(:options) do
|
125
|
+
{
|
126
|
+
validate: true
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
let(:association) do
|
131
|
+
# Note that it is necessary to create the association directly, otherwise the
|
132
|
+
# setup! method will be called by the :has_one macro
|
133
|
+
described_class.new(has_one_class, name, options)
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'sets up validation' do
|
137
|
+
expect(has_one_class).to receive(:validates_associated).with(name).and_call_original
|
138
|
+
association.setup!
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context 'when the :validate option is false' do
|
143
|
+
|
144
|
+
let(:options) do
|
145
|
+
{
|
146
|
+
validate: false
|
147
|
+
}
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'does not set up validation' do
|
151
|
+
expect(has_one_class).not_to receive(:validates_associated)
|
152
|
+
association.setup!
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'when the :validate option is not provided' do
|
157
|
+
|
158
|
+
let(:association) do
|
159
|
+
# Note that it is necessary to create the association directly, otherwise the
|
160
|
+
# setup! method will be called by the :embeds_one macro
|
161
|
+
described_class.new(has_one_class, name, options)
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'sets up the validation because it uses the validation default (true)' do
|
165
|
+
expect(has_one_class).to receive(:validates_associated).with(name).and_call_original
|
166
|
+
association.setup!
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'polymorphic' do
|
171
|
+
|
172
|
+
context 'when the as option is provided' do
|
173
|
+
|
174
|
+
|
175
|
+
let(:options) do
|
176
|
+
{
|
177
|
+
as: :containable
|
178
|
+
}
|
179
|
+
end
|
180
|
+
|
181
|
+
before do
|
182
|
+
association
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'set the polymorphic attribute on the owner class' do
|
186
|
+
expect(has_one_class.polymorphic).to be(true)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'when the as option is not provided' do
|
191
|
+
|
192
|
+
it 'does not set the polymorphic attribute on the owner class' do
|
193
|
+
expect(has_one_class.polymorphic).to be(false)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context 'dependent' do
|
199
|
+
|
200
|
+
context 'when the dependent option is provided' do
|
201
|
+
|
202
|
+
context 'when the dependent option is :delete_all' do
|
203
|
+
|
204
|
+
let(:options) do
|
205
|
+
{
|
206
|
+
dependent: :delete_all
|
207
|
+
}
|
208
|
+
end
|
209
|
+
|
210
|
+
let(:association) do
|
211
|
+
# Note that it is necessary to create the association directly, otherwise the
|
212
|
+
# setup! method will be called by the :belongs_to macro
|
213
|
+
described_class.new(has_one_class, name, options)
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'sets up the dependency' do
|
217
|
+
expect(Mongoid::Association::Depending).to receive(:define_dependency!)
|
218
|
+
association.setup!
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'when the dependent option is :destroy' do
|
223
|
+
|
224
|
+
let(:options) do
|
225
|
+
{
|
226
|
+
dependent: :destroy
|
227
|
+
}
|
228
|
+
end
|
229
|
+
|
230
|
+
let(:association) do
|
231
|
+
# Note that it is necessary to create the association directly, otherwise the
|
232
|
+
# setup! method will be called by the :belongs_to macro
|
233
|
+
described_class.new(has_one_class, name, options)
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'sets up the dependency' do
|
237
|
+
expect(Mongoid::Association::Depending).to receive(:define_dependency!)
|
238
|
+
association.setup!
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
context 'when the dependent option is :nullify' do
|
243
|
+
|
244
|
+
let(:options) do
|
245
|
+
{
|
246
|
+
dependent: :nullify
|
247
|
+
}
|
248
|
+
end
|
249
|
+
|
250
|
+
let(:association) do
|
251
|
+
# Note that it is necessary to create the association directly, otherwise the
|
252
|
+
# setup! method will be called by the :belongs_to macro
|
253
|
+
described_class.new(has_one_class, name, options)
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'sets up the dependency' do
|
257
|
+
expect(Mongoid::Association::Depending).to receive(:define_dependency!)
|
258
|
+
association.setup!
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
context 'when the dependent option is :restrict_with_exception' do
|
263
|
+
|
264
|
+
let(:options) do
|
265
|
+
{
|
266
|
+
dependent: :restrict_with_exception
|
267
|
+
}
|
268
|
+
end
|
269
|
+
|
270
|
+
let(:association) do
|
271
|
+
# Note that it is necessary to create the association directly, otherwise the
|
272
|
+
# setup! method will be called by the :belongs_to macro
|
273
|
+
described_class.new(has_one_class, name, options)
|
274
|
+
end
|
275
|
+
|
276
|
+
it 'sets up the dependency' do
|
277
|
+
expect(Mongoid::Association::Depending).to receive(:define_dependency!)
|
278
|
+
association.setup!
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
context 'when the dependent option is :restrict_with_error' do
|
283
|
+
|
284
|
+
let(:options) do
|
285
|
+
{
|
286
|
+
dependent: :restrict_with_error
|
287
|
+
}
|
288
|
+
end
|
289
|
+
|
290
|
+
let(:association) do
|
291
|
+
# Note that it is necessary to create the association directly, otherwise the
|
292
|
+
# setup! method will be called by the :belongs_to macro
|
293
|
+
described_class.new(has_one_class, name, options)
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'sets up the dependency' do
|
297
|
+
expect(Mongoid::Association::Depending).to receive(:define_dependency!)
|
298
|
+
association.setup!
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
context 'when the dependent option is not provided' do
|
304
|
+
|
305
|
+
it 'does not set up the dependency' do
|
306
|
+
expect(Mongoid::Association::Depending).not_to receive(:define_dependency!)
|
307
|
+
association.setup!
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
describe '#type' do
|
314
|
+
|
315
|
+
context 'when polymorphic' do
|
316
|
+
|
317
|
+
let(:options) do
|
318
|
+
{ as: :containable }
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'returns the as attribute followed by "_type"' do
|
322
|
+
expect(association.type).to eq("#{options[:as]}_type")
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
context 'when not polymorphic' do
|
327
|
+
|
328
|
+
it 'returns nil' do
|
329
|
+
expect(association.type).to be_nil
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
describe '#inverse_type' do
|
335
|
+
|
336
|
+
context 'when polymorphic' do
|
337
|
+
|
338
|
+
let(:options) do
|
339
|
+
{ as: :containable }
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'returns nil' do
|
343
|
+
expect(association.inverse_type).to be_nil
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
context 'when not polymorphic' do
|
348
|
+
|
349
|
+
it 'returns nil' do
|
350
|
+
expect(association.inverse_type).to be_nil
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
describe '#inverse_type_setter' do
|
356
|
+
|
357
|
+
context 'when polymorphic' do
|
358
|
+
|
359
|
+
let(:options) do
|
360
|
+
{ as: :containable }
|
361
|
+
end
|
362
|
+
|
363
|
+
it 'returns nil' do
|
364
|
+
expect(association.inverse_type).to be_nil
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
context 'when not polymorphic' do
|
369
|
+
|
370
|
+
it 'returns nil' do
|
371
|
+
expect(association.inverse_type).to be_nil
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
describe '#foreign_key' do
|
377
|
+
|
378
|
+
context 'when options has foreign_key specified' do
|
379
|
+
|
380
|
+
let(:options) do
|
381
|
+
{ foreign_key: :other_object_id }
|
382
|
+
end
|
383
|
+
|
384
|
+
it 'raises returns the foreign key as a String' do
|
385
|
+
expect(association.foreign_key).to eq(options[:foreign_key].to_s)
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
context 'when options does not have foreign_key specified' do
|
390
|
+
|
391
|
+
it 'returns the default foreign key, the name of the inverse followed by "_id"' do
|
392
|
+
expect(association.foreign_key).to eq("#{association.inverse}_id")
|
393
|
+
end
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
describe '#embedded?' do
|
398
|
+
|
399
|
+
it 'returns false' do
|
400
|
+
expect(association.embedded?).to be(false)
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
describe '#primary_key' do
|
405
|
+
|
406
|
+
context 'when the :primary_key option is specified' do
|
407
|
+
|
408
|
+
let(:options) do
|
409
|
+
{
|
410
|
+
primary_key: 'guid'
|
411
|
+
}
|
412
|
+
end
|
413
|
+
|
414
|
+
it 'returns the primary_key' do
|
415
|
+
expect(association.primary_key).to eq(options[:primary_key])
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
context 'when the :primary_key option is not specified' do
|
420
|
+
|
421
|
+
it 'returns the primary_key default' do
|
422
|
+
expect(association.primary_key).to eq(Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT)
|
423
|
+
end
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
describe '#indexed?' do
|
428
|
+
|
429
|
+
it 'returns false' do
|
430
|
+
expect(association.indexed?).to be(false)
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
describe '#relation' do
|
435
|
+
|
436
|
+
it 'returns Mongoid::Association::Referenced::HasOne::Proxy' do
|
437
|
+
expect(association.relation).to be(Mongoid::Association::Referenced::HasOne::Proxy)
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
describe '#validation_default' do
|
442
|
+
|
443
|
+
it 'returns true' do
|
444
|
+
expect(association.validation_default).to be(true)
|
445
|
+
end
|
446
|
+
end
|
447
|
+
|
448
|
+
describe '#name' do
|
449
|
+
|
450
|
+
it 'returns the name of the relation' do
|
451
|
+
expect(association.name).to be(name)
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
describe '#options' do
|
456
|
+
|
457
|
+
it 'returns the options' do
|
458
|
+
expect(association.options).to be(options)
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
describe '#merge!' do
|
463
|
+
|
464
|
+
end
|
465
|
+
|
466
|
+
describe '#store_as' do
|
467
|
+
it 'returns nil' do
|
468
|
+
expect(association.store_as).to be_nil
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
describe '#touchable?' do
|
473
|
+
|
474
|
+
it 'return false' do
|
475
|
+
expect(association.send(:touchable?)).to be(false)
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
describe '#order' do
|
480
|
+
|
481
|
+
it 'returns nil' do
|
482
|
+
expect(association.order).to be_nil
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
486
|
+
describe '#as' do
|
487
|
+
|
488
|
+
context 'when :as is specified in the options' do
|
489
|
+
|
490
|
+
let(:options) do
|
491
|
+
{
|
492
|
+
as: :containable
|
493
|
+
}
|
494
|
+
end
|
495
|
+
|
496
|
+
it 'returns the :as option' do
|
497
|
+
expect(association.as).to eq(options[:as])
|
498
|
+
end
|
499
|
+
end
|
500
|
+
|
501
|
+
context 'when :as is not specified in the options' do
|
502
|
+
|
503
|
+
it 'returns nil' do
|
504
|
+
expect(association.as).to be_nil
|
505
|
+
end
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
describe '#polymorphic?' do
|
510
|
+
|
511
|
+
context 'when :as is specified in the options' do
|
512
|
+
|
513
|
+
let(:options) do
|
514
|
+
{
|
515
|
+
as: :containable
|
516
|
+
}
|
517
|
+
end
|
518
|
+
|
519
|
+
it 'returns true' do
|
520
|
+
expect(association.polymorphic?).to be(true)
|
521
|
+
end
|
522
|
+
|
523
|
+
end
|
524
|
+
|
525
|
+
context 'when :as is not specified in the options' do
|
526
|
+
|
527
|
+
it 'returns false' do
|
528
|
+
expect(association.polymorphic?).to be(false)
|
529
|
+
end
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
describe '#type_setter' do
|
534
|
+
|
535
|
+
context 'when polymorphic' do
|
536
|
+
|
537
|
+
let(:options) do
|
538
|
+
{ as: :containable }
|
539
|
+
end
|
540
|
+
|
541
|
+
it 'returns the type followed by = as a String' do
|
542
|
+
expect(association.type_setter).to eq("containable_type=")
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
context 'when not polymorphic' do
|
547
|
+
|
548
|
+
it 'returns nil' do
|
549
|
+
expect(association.type).to be_nil
|
550
|
+
end
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
554
|
+
describe '#dependent' do
|
555
|
+
|
556
|
+
context 'when the dependent option is provided' do
|
557
|
+
|
558
|
+
context 'when the dependent option is :delete_all' do
|
559
|
+
|
560
|
+
let(:options) do
|
561
|
+
{
|
562
|
+
dependent: :delete_all
|
563
|
+
}
|
564
|
+
end
|
565
|
+
|
566
|
+
it 'returns :delete_all' do
|
567
|
+
expect(association.dependent).to eq(:delete_all)
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
context 'when the dependent option is :destroy' do
|
572
|
+
|
573
|
+
let(:options) do
|
574
|
+
{
|
575
|
+
dependent: :destroy
|
576
|
+
}
|
577
|
+
end
|
578
|
+
|
579
|
+
it 'returns :destroy' do
|
580
|
+
expect(association.dependent).to eq(:destroy)
|
581
|
+
end
|
582
|
+
end
|
583
|
+
|
584
|
+
context 'when the dependent option is :nullify' do
|
585
|
+
|
586
|
+
let(:options) do
|
587
|
+
{
|
588
|
+
dependent: :nullify
|
589
|
+
}
|
590
|
+
end
|
591
|
+
|
592
|
+
it 'returns :nullify' do
|
593
|
+
expect(association.dependent).to eq(:nullify)
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
context 'when the dependent option is :restrict_with_exception' do
|
598
|
+
|
599
|
+
let(:options) do
|
600
|
+
{
|
601
|
+
dependent: :restrict_with_exception
|
602
|
+
}
|
603
|
+
end
|
604
|
+
|
605
|
+
it 'returns :restrict_with_exception' do
|
606
|
+
expect(association.dependent).to eq(:restrict_with_exception)
|
607
|
+
end
|
608
|
+
end
|
609
|
+
|
610
|
+
context 'when the dependent option is :restrict_with_error' do
|
611
|
+
|
612
|
+
let(:options) do
|
613
|
+
{
|
614
|
+
dependent: :restrict_with_error
|
615
|
+
}
|
616
|
+
end
|
617
|
+
|
618
|
+
it 'returns :restrict_with_error' do
|
619
|
+
expect(association.dependent).to eq(:restrict_with_error)
|
620
|
+
end
|
621
|
+
end
|
622
|
+
end
|
623
|
+
|
624
|
+
context 'when the dependent option is not provided' do
|
625
|
+
|
626
|
+
it 'returns nil' do
|
627
|
+
expect(association.dependent).to be_nil
|
628
|
+
end
|
629
|
+
end
|
630
|
+
end
|
631
|
+
|
632
|
+
describe '#inverse_type' do
|
633
|
+
|
634
|
+
it 'returns nil' do
|
635
|
+
expect(association.inverse_type).to be_nil
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
describe '#bindable?' do
|
640
|
+
|
641
|
+
it 'returns false' do
|
642
|
+
expect(association.bindable?(Person.new)).to be(false)
|
643
|
+
end
|
644
|
+
end
|
645
|
+
|
646
|
+
describe '#inverses' do
|
647
|
+
|
648
|
+
context 'when polymorphic' do
|
649
|
+
|
650
|
+
before do
|
651
|
+
BelongingObject.belongs_to :containable, polymorphic: true
|
652
|
+
end
|
653
|
+
|
654
|
+
let(:options) do
|
655
|
+
{
|
656
|
+
as: :containable
|
657
|
+
}
|
658
|
+
end
|
659
|
+
|
660
|
+
context 'when another object is passed to the method' do
|
661
|
+
|
662
|
+
let(:instance_of_other_class) do
|
663
|
+
BelongingObject.new
|
664
|
+
end
|
665
|
+
|
666
|
+
context 'when the relation class has only one relation whose class matches the owning class' do
|
667
|
+
|
668
|
+
it 'returns the :as attribute of this association' do
|
669
|
+
expect(association.inverses(instance_of_other_class)).to match_array([ :containable ])
|
670
|
+
end
|
671
|
+
end
|
672
|
+
|
673
|
+
context 'when :inverse_of is specified' do
|
674
|
+
|
675
|
+
before do
|
676
|
+
options.merge!(inverse_of: :inverse_name)
|
677
|
+
end
|
678
|
+
|
679
|
+
it 'returns the :inverse_of value' do
|
680
|
+
expect(association.inverses(instance_of_other_class)).to eq([ :inverse_name ])
|
681
|
+
end
|
682
|
+
end
|
683
|
+
|
684
|
+
context 'when inverse_of is not specified' do
|
685
|
+
|
686
|
+
it 'returns the :as attribute of this association' do
|
687
|
+
expect(association.inverses(instance_of_other_class)).to match_array([ :containable ])
|
688
|
+
end
|
689
|
+
end
|
690
|
+
end
|
691
|
+
|
692
|
+
context 'when another object is not passed to the method' do
|
693
|
+
|
694
|
+
context 'when inverse_of is specified' do
|
695
|
+
|
696
|
+
before do
|
697
|
+
options.merge!(inverse_of: :inverse_name)
|
698
|
+
end
|
699
|
+
|
700
|
+
it 'returns the :inverse_of value' do
|
701
|
+
expect(association.inverses).to eq([ :inverse_name ])
|
702
|
+
end
|
703
|
+
end
|
704
|
+
|
705
|
+
context 'when inverse_of is not specified' do
|
706
|
+
|
707
|
+
it 'returns the :as attribute' do
|
708
|
+
expect(association.inverses).to eq([ :containable ])
|
709
|
+
end
|
710
|
+
end
|
711
|
+
end
|
712
|
+
end
|
713
|
+
|
714
|
+
context 'when not polymorphic' do
|
715
|
+
|
716
|
+
before do
|
717
|
+
BelongingObject.belongs_to :owner_object
|
718
|
+
end
|
719
|
+
|
720
|
+
context 'when inverse_of is specified' do
|
721
|
+
|
722
|
+
before do
|
723
|
+
options.merge!(inverse_of: :inverse_name)
|
724
|
+
end
|
725
|
+
|
726
|
+
it 'returns the :inverse_of value' do
|
727
|
+
expect(association.inverses).to eq([ :inverse_name ])
|
728
|
+
end
|
729
|
+
end
|
730
|
+
|
731
|
+
context 'when inverse_of is not specified' do
|
732
|
+
|
733
|
+
it 'uses the inverse class to find the inverse name' do
|
734
|
+
expect(association.inverses).to eq([ :owner_object ])
|
735
|
+
end
|
736
|
+
end
|
737
|
+
|
738
|
+
context 'when :cyclic is specified' do
|
739
|
+
|
740
|
+
it 'returns the cyclic inverse name' do
|
741
|
+
|
742
|
+
end
|
743
|
+
end
|
744
|
+
end
|
745
|
+
end
|
746
|
+
|
747
|
+
describe '##inverse' do
|
748
|
+
|
749
|
+
context 'when polymorphic' do
|
750
|
+
|
751
|
+
before do
|
752
|
+
BelongingObject.belongs_to :containable, polymorphic: true
|
753
|
+
end
|
754
|
+
|
755
|
+
let(:options) do
|
756
|
+
{
|
757
|
+
as: :containable
|
758
|
+
}
|
759
|
+
end
|
760
|
+
|
761
|
+
context 'when another object is passed to the method' do
|
762
|
+
|
763
|
+
let(:instance_of_other_class) do
|
764
|
+
BelongingObject.new
|
765
|
+
end
|
766
|
+
|
767
|
+
context 'when the relation class has only one relation whose class matches the owning class' do
|
768
|
+
|
769
|
+
it 'returns the :as attribute of this association' do
|
770
|
+
expect(association.inverse(instance_of_other_class)).to eq(:containable)
|
771
|
+
end
|
772
|
+
end
|
773
|
+
|
774
|
+
context 'when :inverse_of is specified' do
|
775
|
+
|
776
|
+
before do
|
777
|
+
options.merge!(inverse_of: :inverse_name)
|
778
|
+
end
|
779
|
+
|
780
|
+
it 'returns the :inverse_of value' do
|
781
|
+
expect(association.inverse(instance_of_other_class)).to eq(:inverse_name)
|
782
|
+
end
|
783
|
+
end
|
784
|
+
|
785
|
+
context 'when inverse_of is not specified' do
|
786
|
+
|
787
|
+
it 'returns the :as attribute of this association' do
|
788
|
+
expect(association.inverse(instance_of_other_class)).to eq(:containable)
|
789
|
+
end
|
790
|
+
end
|
791
|
+
end
|
792
|
+
|
793
|
+
context 'when another object is not passed to the method' do
|
794
|
+
|
795
|
+
context 'when inverse_of is specified' do
|
796
|
+
|
797
|
+
before do
|
798
|
+
options.merge!(inverse_of: :inverse_name)
|
799
|
+
end
|
800
|
+
|
801
|
+
it 'returns the :inverse_of value' do
|
802
|
+
expect(association.inverse).to eq(:inverse_name)
|
803
|
+
end
|
804
|
+
end
|
805
|
+
|
806
|
+
context 'when inverse_of is not specified' do
|
807
|
+
|
808
|
+
it 'returns the :as attribute' do
|
809
|
+
expect(association.inverse).to eq(:containable)
|
810
|
+
end
|
811
|
+
end
|
812
|
+
end
|
813
|
+
end
|
814
|
+
|
815
|
+
context 'when not polymorphic' do
|
816
|
+
|
817
|
+
before do
|
818
|
+
BelongingObject.belongs_to :owner_object
|
819
|
+
end
|
820
|
+
|
821
|
+
context 'when inverse_of is specified' do
|
822
|
+
|
823
|
+
before do
|
824
|
+
options.merge!(inverse_of: :inverse_name)
|
825
|
+
end
|
826
|
+
|
827
|
+
it 'returns the :inverse_of value' do
|
828
|
+
expect(association.inverse).to eq(:inverse_name)
|
829
|
+
end
|
830
|
+
end
|
831
|
+
|
832
|
+
context 'when inverse_of is not specified' do
|
833
|
+
|
834
|
+
it 'uses the inverse class to find the inverse name' do
|
835
|
+
expect(association.inverse).to eq(:owner_object)
|
836
|
+
end
|
837
|
+
end
|
838
|
+
|
839
|
+
context 'when :cyclic is specified' do
|
840
|
+
|
841
|
+
it 'returns the cyclic inverse name' do
|
842
|
+
|
843
|
+
end
|
844
|
+
end
|
845
|
+
end
|
846
|
+
end
|
847
|
+
|
848
|
+
describe '#inverse_association' do
|
849
|
+
|
850
|
+
end
|
851
|
+
|
852
|
+
describe '#autosave' do
|
853
|
+
|
854
|
+
context 'when the autosave option is specified' do
|
855
|
+
|
856
|
+
context 'when the autosave option is true' do
|
857
|
+
|
858
|
+
let(:options) do
|
859
|
+
{
|
860
|
+
autosave: true
|
861
|
+
}
|
862
|
+
end
|
863
|
+
|
864
|
+
it 'returns true' do
|
865
|
+
expect(association.autosave).to be(true)
|
866
|
+
end
|
867
|
+
end
|
868
|
+
|
869
|
+
context 'when the autosave option is false' do
|
870
|
+
|
871
|
+
let(:options) do
|
872
|
+
{
|
873
|
+
autosave: false
|
874
|
+
}
|
875
|
+
end
|
876
|
+
|
877
|
+
it 'returns false' do
|
878
|
+
expect(association.autosave).to be(false)
|
879
|
+
end
|
880
|
+
end
|
881
|
+
end
|
882
|
+
|
883
|
+
context 'when the autosave option is not specified' do
|
884
|
+
|
885
|
+
it 'returns false' do
|
886
|
+
expect(association.autosave).to be(false)
|
887
|
+
end
|
888
|
+
end
|
889
|
+
end
|
890
|
+
|
891
|
+
describe '#relation_class_name' do
|
892
|
+
|
893
|
+
context 'when the :class_name option is specified' do
|
894
|
+
|
895
|
+
let(:options) do
|
896
|
+
{ class_name: 'OtherBelongingObject' }
|
897
|
+
end
|
898
|
+
|
899
|
+
it 'returns the class name option' do
|
900
|
+
expect(association.relation_class_name).to eq('OtherBelongingObject')
|
901
|
+
end
|
902
|
+
end
|
903
|
+
|
904
|
+
context 'when the class_name option is not specified' do
|
905
|
+
|
906
|
+
it 'uses the name of the relation to deduce the class name' do
|
907
|
+
expect(association.relation_class_name).to eq('BelongingObject')
|
908
|
+
end
|
909
|
+
end
|
910
|
+
end
|
911
|
+
|
912
|
+
describe '#klass' do
|
913
|
+
|
914
|
+
context 'when the :class_name option is specified' do
|
915
|
+
|
916
|
+
let!(:_class) do
|
917
|
+
class OtherBelongingObject; end
|
918
|
+
OtherBelongingObject
|
919
|
+
end
|
920
|
+
|
921
|
+
let(:options) do
|
922
|
+
{ class_name: 'OtherBelongingObject' }
|
923
|
+
end
|
924
|
+
|
925
|
+
it 'returns the class name option' do
|
926
|
+
expect(association.klass).to eq(_class)
|
927
|
+
end
|
928
|
+
end
|
929
|
+
|
930
|
+
context 'when the class_name option is not specified' do
|
931
|
+
|
932
|
+
it 'uses the name of the relation to deduce the class name' do
|
933
|
+
expect(association.klass).to eq(BelongingObject)
|
934
|
+
end
|
935
|
+
end
|
936
|
+
end
|
937
|
+
|
938
|
+
describe '#inverse_class_name' do
|
939
|
+
|
940
|
+
it 'returns the name of the owner class' do
|
941
|
+
expect(association.inverse_class_name).to eq(OwnerObject.name)
|
942
|
+
end
|
943
|
+
end
|
944
|
+
|
945
|
+
describe '#inverse_class' do
|
946
|
+
|
947
|
+
it 'returns the owner class' do
|
948
|
+
expect(association.inverse_class).to be(OwnerObject)
|
949
|
+
end
|
950
|
+
end
|
951
|
+
|
952
|
+
describe '#inverse_of' do
|
953
|
+
|
954
|
+
context 'when :inverse_of is specified in the options' do
|
955
|
+
|
956
|
+
let(:options) do
|
957
|
+
{ inverse_of: :a_belonging_object }
|
958
|
+
end
|
959
|
+
|
960
|
+
it 'returns the inverse_of value' do
|
961
|
+
expect(association.inverse_of).to eq(options[:inverse_of])
|
962
|
+
end
|
963
|
+
end
|
964
|
+
|
965
|
+
context 'when :inverse_of is not specified in the options' do
|
966
|
+
|
967
|
+
it 'returns nil' do
|
968
|
+
expect(association.inverse_of).to be_nil
|
969
|
+
end
|
970
|
+
end
|
971
|
+
end
|
972
|
+
|
973
|
+
describe '#key' do
|
974
|
+
|
975
|
+
it 'returns the primary key' do
|
976
|
+
expect(association.key).to eq(association.primary_key)
|
977
|
+
end
|
978
|
+
end
|
979
|
+
|
980
|
+
describe '#setter' do
|
981
|
+
|
982
|
+
it 'returns a string of the name followed by =' do
|
983
|
+
expect(association.setter).to eq("#{name}=")
|
984
|
+
end
|
985
|
+
end
|
986
|
+
|
987
|
+
describe '#validate?' do
|
988
|
+
|
989
|
+
context 'when :validate is specified in the options' do
|
990
|
+
|
991
|
+
context 'when validate is true' do
|
992
|
+
|
993
|
+
let(:options) do
|
994
|
+
{ validate: true }
|
995
|
+
end
|
996
|
+
|
997
|
+
it 'returns true' do
|
998
|
+
expect(association.send(:validate?)).to be(true)
|
999
|
+
end
|
1000
|
+
end
|
1001
|
+
|
1002
|
+
context 'when validate is false' do
|
1003
|
+
|
1004
|
+
let(:options) do
|
1005
|
+
{ validate: false }
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
it 'returns false' do
|
1009
|
+
expect(association.send(:validate?)).to be(false)
|
1010
|
+
end
|
1011
|
+
end
|
1012
|
+
end
|
1013
|
+
|
1014
|
+
context 'when :validate is not specified in the options' do
|
1015
|
+
|
1016
|
+
it 'returns the validation_default' do
|
1017
|
+
expect(association.send(:validate?)).to eq(association.validation_default)
|
1018
|
+
end
|
1019
|
+
end
|
1020
|
+
end
|
1021
|
+
|
1022
|
+
describe '#autobuilding?' do
|
1023
|
+
|
1024
|
+
context 'when :autobuild is specified in the options' do
|
1025
|
+
|
1026
|
+
context 'when autobuild is true' do
|
1027
|
+
|
1028
|
+
let(:options) do
|
1029
|
+
{ autobuild: true }
|
1030
|
+
end
|
1031
|
+
|
1032
|
+
it 'returns true' do
|
1033
|
+
expect(association.autobuilding?).to be(true)
|
1034
|
+
end
|
1035
|
+
end
|
1036
|
+
|
1037
|
+
context 'when autobuild is false' do
|
1038
|
+
|
1039
|
+
let(:options) do
|
1040
|
+
{ autobuild: false }
|
1041
|
+
end
|
1042
|
+
|
1043
|
+
it 'returns true' do
|
1044
|
+
expect(association.autobuilding?).to be(false)
|
1045
|
+
end
|
1046
|
+
end
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
context 'when :validate is not specified in the options' do
|
1050
|
+
|
1051
|
+
it 'returns false' do
|
1052
|
+
expect(association.autobuilding?).to be(false)
|
1053
|
+
end
|
1054
|
+
end
|
1055
|
+
end
|
1056
|
+
|
1057
|
+
describe '#forced_nil_inverse?' do
|
1058
|
+
|
1059
|
+
it 'returns false' do
|
1060
|
+
expect(association.forced_nil_inverse?).to be(false)
|
1061
|
+
end
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
describe '#stores_foreign_key?' do
|
1065
|
+
|
1066
|
+
it 'returns false' do
|
1067
|
+
expect(association.stores_foreign_key?).to be(false)
|
1068
|
+
end
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
describe '#inverse_setter' do
|
1072
|
+
|
1073
|
+
context 'when an inverse can be determined' do
|
1074
|
+
|
1075
|
+
before do
|
1076
|
+
BelongingObject.belongs_to :owner_object
|
1077
|
+
end
|
1078
|
+
|
1079
|
+
it 'returns the name of the inverse followed by =' do
|
1080
|
+
expect(association.inverse_setter).to eq('owner_object=')
|
1081
|
+
end
|
1082
|
+
end
|
1083
|
+
|
1084
|
+
context 'when an inverse cannot be determined' do
|
1085
|
+
|
1086
|
+
it 'returns nil' do
|
1087
|
+
expect(association.inverse_setter).to be_nil
|
1088
|
+
end
|
1089
|
+
end
|
1090
|
+
end
|
1091
|
+
|
1092
|
+
describe '#extension' do
|
1093
|
+
|
1094
|
+
context 'when a block is passed' do
|
1095
|
+
|
1096
|
+
let(:association) do
|
1097
|
+
has_one_class.embeds_one name, options do; end
|
1098
|
+
end
|
1099
|
+
|
1100
|
+
it 'defines an extension module' do
|
1101
|
+
expect(association.extension).to be_a(Module)
|
1102
|
+
end
|
1103
|
+
|
1104
|
+
it 'returns the extension' do
|
1105
|
+
expect(association.extension).to eq(
|
1106
|
+
"#{has_one_class.name}::#{has_one_class.name}#{name.to_s.camelize}RelationExtension".constantize)
|
1107
|
+
end
|
1108
|
+
end
|
1109
|
+
|
1110
|
+
context 'when an :extension is not specified in the options' do
|
1111
|
+
|
1112
|
+
it 'returns false' do
|
1113
|
+
expect(association.extension).to be_nil
|
1114
|
+
end
|
1115
|
+
end
|
1116
|
+
end
|
1117
|
+
|
1118
|
+
describe '#foreign_key_setter' do
|
1119
|
+
|
1120
|
+
it 'returns the foreign key followed by "="' do
|
1121
|
+
expect(association.foreign_key_setter).to eq("#{association.foreign_key}=")
|
1122
|
+
end
|
1123
|
+
end
|
1124
|
+
|
1125
|
+
describe '#destructive?' do
|
1126
|
+
|
1127
|
+
context 'when the dependent option is provided' do
|
1128
|
+
|
1129
|
+
context 'when the dependent option is :delete_all' do
|
1130
|
+
|
1131
|
+
let(:options) do
|
1132
|
+
{
|
1133
|
+
dependent: :delete_all
|
1134
|
+
}
|
1135
|
+
end
|
1136
|
+
|
1137
|
+
it 'returns true' do
|
1138
|
+
expect(association.destructive?).to be(true)
|
1139
|
+
end
|
1140
|
+
end
|
1141
|
+
|
1142
|
+
context 'when the dependent option is :destroy' do
|
1143
|
+
|
1144
|
+
let(:options) do
|
1145
|
+
{
|
1146
|
+
dependent: :destroy
|
1147
|
+
}
|
1148
|
+
end
|
1149
|
+
|
1150
|
+
it 'returns true' do
|
1151
|
+
expect(association.destructive?).to be(true)
|
1152
|
+
end
|
1153
|
+
end
|
1154
|
+
|
1155
|
+
context 'when the dependent option is :nullify' do
|
1156
|
+
|
1157
|
+
let(:options) do
|
1158
|
+
{
|
1159
|
+
dependent: :nullify
|
1160
|
+
}
|
1161
|
+
end
|
1162
|
+
|
1163
|
+
it 'returns false' do
|
1164
|
+
expect(association.destructive?).to be(false)
|
1165
|
+
end
|
1166
|
+
end
|
1167
|
+
|
1168
|
+
context 'when the dependent option is :restrict_with_exception' do
|
1169
|
+
|
1170
|
+
let(:options) do
|
1171
|
+
{
|
1172
|
+
dependent: :restrict_with_exception
|
1173
|
+
}
|
1174
|
+
end
|
1175
|
+
|
1176
|
+
it 'returns false' do
|
1177
|
+
expect(association.destructive?).to be(false)
|
1178
|
+
end
|
1179
|
+
end
|
1180
|
+
|
1181
|
+
context 'when the dependent option is :restrict_with_error' do
|
1182
|
+
|
1183
|
+
let(:options) do
|
1184
|
+
{
|
1185
|
+
dependent: :restrict_with_error
|
1186
|
+
}
|
1187
|
+
end
|
1188
|
+
|
1189
|
+
it 'returns false' do
|
1190
|
+
expect(association.destructive?).to be(false)
|
1191
|
+
end
|
1192
|
+
end
|
1193
|
+
end
|
1194
|
+
|
1195
|
+
context 'when the dependent option is not provided' do
|
1196
|
+
|
1197
|
+
it 'returns false' do
|
1198
|
+
expect(association.destructive?).to be(false)
|
1199
|
+
end
|
1200
|
+
end
|
1201
|
+
end
|
1202
|
+
|
1203
|
+
describe '#nested_builder' do
|
1204
|
+
|
1205
|
+
it 'returns an instance of Association::Nested::One' do
|
1206
|
+
expect(association.nested_builder({}, {})).to be_a(Mongoid::Association::Nested::One)
|
1207
|
+
end
|
1208
|
+
end
|
1209
|
+
|
1210
|
+
describe '#path' do
|
1211
|
+
|
1212
|
+
it 'returns an instance of Mongoid::Atomic::Paths::Root' do
|
1213
|
+
expect(association.path(double( :_parent => true))).to be_a(Mongoid::Atomic::Paths::Root)
|
1214
|
+
end
|
1215
|
+
end
|
1216
|
+
|
1217
|
+
describe '#foreign_key_check' do
|
1218
|
+
|
1219
|
+
it 'returns the nil' do
|
1220
|
+
expect(association.foreign_key_check).to be_nil
|
1221
|
+
end
|
1222
|
+
end
|
1223
|
+
|
1224
|
+
describe '#create_relation' do
|
1225
|
+
|
1226
|
+
let(:owner) do
|
1227
|
+
OwnerObject.new
|
1228
|
+
end
|
1229
|
+
|
1230
|
+
let(:target) do
|
1231
|
+
BelongingObject.new
|
1232
|
+
end
|
1233
|
+
|
1234
|
+
before do
|
1235
|
+
association
|
1236
|
+
BelongingObject.belongs_to :owner_object
|
1237
|
+
end
|
1238
|
+
|
1239
|
+
it 'returns an the target (EmbeddedObject)' do
|
1240
|
+
expect(Mongoid::Association::Referenced::HasOne::Proxy).to receive(:new).and_call_original
|
1241
|
+
expect(association.create_relation(owner, target)).to be_a(BelongingObject)
|
1242
|
+
end
|
1243
|
+
end
|
1244
|
+
end
|