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,12 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Mongoid::
|
3
|
+
describe Mongoid::Association::Nested::Many do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
|
7
|
-
name: :addresses,
|
8
|
-
relation: Mongoid::Relations::Embedded::Many
|
9
|
-
)
|
5
|
+
let(:association) do
|
6
|
+
Person.relations['addresses']
|
10
7
|
end
|
11
8
|
|
12
9
|
let(:person) do
|
@@ -18,7 +15,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
18
15
|
context "when the option is provided" do
|
19
16
|
|
20
17
|
let(:builder) do
|
21
|
-
described_class.new(
|
18
|
+
described_class.new(association, {}, allow_destroy: true)
|
22
19
|
end
|
23
20
|
|
24
21
|
it "returns the option" do
|
@@ -29,7 +26,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
29
26
|
context "when the option is not provided" do
|
30
27
|
|
31
28
|
let(:builder) do
|
32
|
-
described_class.new(
|
29
|
+
described_class.new(association, {}, {})
|
33
30
|
end
|
34
31
|
|
35
32
|
it "returns false" do
|
@@ -51,7 +48,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
51
48
|
context "when attributes are over limit" do
|
52
49
|
|
53
50
|
let(:builder) do
|
54
|
-
described_class.new(
|
51
|
+
described_class.new(association, attributes, limit: 2)
|
55
52
|
end
|
56
53
|
|
57
54
|
it "raises an error" do
|
@@ -65,7 +62,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
65
62
|
|
66
63
|
let(:builder) do
|
67
64
|
described_class.new(
|
68
|
-
|
65
|
+
association,
|
69
66
|
attributes,
|
70
67
|
reject_if: ->(attrs){ attrs[:city].blank? }
|
71
68
|
)
|
@@ -85,7 +82,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
85
82
|
|
86
83
|
let(:builder) do
|
87
84
|
described_class.new(
|
88
|
-
|
85
|
+
association,
|
89
86
|
attributes,
|
90
87
|
reject_if: :reject_if_city_is_empty
|
91
88
|
)
|
@@ -112,7 +109,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
112
109
|
end
|
113
110
|
|
114
111
|
let(:builder) do
|
115
|
-
described_class.new(
|
112
|
+
described_class.new(association, attributes)
|
116
113
|
end
|
117
114
|
|
118
115
|
before do
|
@@ -131,7 +128,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
131
128
|
end
|
132
129
|
|
133
130
|
let(:builder) do
|
134
|
-
described_class.new(
|
131
|
+
described_class.new(association, attributes)
|
135
132
|
end
|
136
133
|
|
137
134
|
before do
|
@@ -155,7 +152,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
155
152
|
end
|
156
153
|
|
157
154
|
let(:builder) do
|
158
|
-
described_class.new(
|
155
|
+
described_class.new(association, attributes, {})
|
159
156
|
end
|
160
157
|
|
161
158
|
it "sorts the attributes" do
|
@@ -174,7 +171,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
174
171
|
context "when the proc matches" do
|
175
172
|
|
176
173
|
let(:builder) do
|
177
|
-
described_class.new(
|
174
|
+
described_class.new(association, {}, options)
|
178
175
|
end
|
179
176
|
|
180
177
|
it "returns true" do
|
@@ -185,7 +182,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
185
182
|
context "when the proc does not match" do
|
186
183
|
|
187
184
|
let(:builder) do
|
188
|
-
described_class.new(
|
185
|
+
described_class.new(association, {}, options)
|
189
186
|
end
|
190
187
|
|
191
188
|
it "returns false" do
|
@@ -197,7 +194,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
197
194
|
context "when the proc is not provided" do
|
198
195
|
|
199
196
|
let(:builder) do
|
200
|
-
described_class.new(
|
197
|
+
described_class.new(association, {}, {})
|
201
198
|
end
|
202
199
|
|
203
200
|
it "returns false" do
|
@@ -211,7 +208,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
211
208
|
context "when the option is provided" do
|
212
209
|
|
213
210
|
let(:builder) do
|
214
|
-
described_class.new(
|
211
|
+
described_class.new(association, {}, update_only: true)
|
215
212
|
end
|
216
213
|
|
217
214
|
it "returns the option" do
|
@@ -222,7 +219,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::Many do
|
|
222
219
|
context "when the option is not provided" do
|
223
220
|
|
224
221
|
let(:builder) do
|
225
|
-
described_class.new(
|
222
|
+
described_class.new(association, {}, {})
|
226
223
|
end
|
227
224
|
|
228
225
|
it "returns false" do
|
@@ -1,12 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Mongoid::
|
3
|
+
describe Mongoid::Association::Nested::One do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
|
7
|
-
name: :name,
|
8
|
-
relation: Mongoid::Relations::Embedded::One
|
9
|
-
)
|
5
|
+
let(:association) do
|
6
|
+
Person.relations['name']
|
10
7
|
end
|
11
8
|
|
12
9
|
describe "#allow_destroy?" do
|
@@ -14,7 +11,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
14
11
|
context "when the option is provided" do
|
15
12
|
|
16
13
|
let(:builder) do
|
17
|
-
described_class.new(
|
14
|
+
described_class.new(association, {}, allow_destroy: true)
|
18
15
|
end
|
19
16
|
|
20
17
|
it "returns the option" do
|
@@ -25,7 +22,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
25
22
|
context "when the option is not provided" do
|
26
23
|
|
27
24
|
let(:builder) do
|
28
|
-
described_class.new(
|
25
|
+
described_class.new(association, {}, {})
|
29
26
|
end
|
30
27
|
|
31
28
|
it "returns false" do
|
@@ -47,7 +44,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
47
44
|
end
|
48
45
|
|
49
46
|
let(:builder) do
|
50
|
-
described_class.new(
|
47
|
+
described_class.new(association, { last_name: "Lang" }, options)
|
51
48
|
end
|
52
49
|
|
53
50
|
before do
|
@@ -66,7 +63,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
66
63
|
end
|
67
64
|
|
68
65
|
let(:builder) do
|
69
|
-
described_class.new(
|
66
|
+
described_class.new(association, { last_name: "Lang" }, options)
|
70
67
|
end
|
71
68
|
|
72
69
|
before do
|
@@ -91,7 +88,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
91
88
|
end
|
92
89
|
|
93
90
|
let(:builder) do
|
94
|
-
described_class.new(
|
91
|
+
described_class.new(association, {
|
95
92
|
_id: name.id,
|
96
93
|
last_name: "Lang"
|
97
94
|
}, options)
|
@@ -113,7 +110,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
113
110
|
end
|
114
111
|
|
115
112
|
let(:builder) do
|
116
|
-
described_class.new(
|
113
|
+
described_class.new(association, {
|
117
114
|
last_name: "Lang"
|
118
115
|
}, options)
|
119
116
|
end
|
@@ -138,7 +135,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
138
135
|
end
|
139
136
|
|
140
137
|
let(:builder) do
|
141
|
-
described_class.new(
|
138
|
+
described_class.new(association, {
|
142
139
|
id: name.id,
|
143
140
|
last_name: "Lang",
|
144
141
|
_destroy: true
|
@@ -160,7 +157,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
160
157
|
context "when the attribute exists" do
|
161
158
|
|
162
159
|
let(:builder) do
|
163
|
-
described_class.new(
|
160
|
+
described_class.new(association, { _destroy: true }, {})
|
164
161
|
end
|
165
162
|
|
166
163
|
it "returns the value" do
|
@@ -171,7 +168,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
171
168
|
context "when the attribute does not exist" do
|
172
169
|
|
173
170
|
let(:builder) do
|
174
|
-
described_class.new(
|
171
|
+
described_class.new(association, {}, {})
|
175
172
|
end
|
176
173
|
|
177
174
|
it "returns nil" do
|
@@ -191,7 +188,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
191
188
|
context "when the proc matches" do
|
192
189
|
|
193
190
|
let(:builder) do
|
194
|
-
described_class.new(
|
191
|
+
described_class.new(association, {}, options)
|
195
192
|
end
|
196
193
|
|
197
194
|
it "returns true" do
|
@@ -202,7 +199,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
202
199
|
context "when the proc does not match" do
|
203
200
|
|
204
201
|
let(:builder) do
|
205
|
-
described_class.new(
|
202
|
+
described_class.new(association, {}, options)
|
206
203
|
end
|
207
204
|
|
208
205
|
it "returns false" do
|
@@ -214,7 +211,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
214
211
|
context "when the proc is not provided" do
|
215
212
|
|
216
213
|
let(:builder) do
|
217
|
-
described_class.new(
|
214
|
+
described_class.new(association, {}, {})
|
218
215
|
end
|
219
216
|
|
220
217
|
it "returns false" do
|
@@ -228,7 +225,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
228
225
|
context "when the option is provided" do
|
229
226
|
|
230
227
|
let(:builder) do
|
231
|
-
described_class.new(
|
228
|
+
described_class.new(association, {}, update_only: true)
|
232
229
|
end
|
233
230
|
|
234
231
|
it "returns the option" do
|
@@ -239,7 +236,7 @@ describe Mongoid::Relations::Builders::NestedAttributes::One do
|
|
239
236
|
context "when the option is not provided" do
|
240
237
|
|
241
238
|
let(:builder) do
|
242
|
-
described_class.new(
|
239
|
+
described_class.new(association, {}, {})
|
243
240
|
end
|
244
241
|
|
245
242
|
it "returns false" do
|
@@ -0,0 +1,1321 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Association::Options do
|
4
|
+
|
5
|
+
STORES_FOREIGN_KEY =
|
6
|
+
[
|
7
|
+
Mongoid::Association::Referenced::HasAndBelongsToMany,
|
8
|
+
Mongoid::Association::Referenced::BelongsTo
|
9
|
+
]
|
10
|
+
|
11
|
+
[
|
12
|
+
Mongoid::Association::Embedded::EmbeddedIn,
|
13
|
+
Mongoid::Association::Embedded::EmbedsMany,
|
14
|
+
Mongoid::Association::Embedded::EmbedsOne,
|
15
|
+
Mongoid::Association::Referenced::BelongsTo,
|
16
|
+
Mongoid::Association::Referenced::HasMany,
|
17
|
+
Mongoid::Association::Referenced::HasOne,
|
18
|
+
Mongoid::Association::Referenced::HasAndBelongsToMany
|
19
|
+
].each do |association_class|
|
20
|
+
|
21
|
+
context "when the association type is #{association_class}" do
|
22
|
+
|
23
|
+
let(:class_left) do
|
24
|
+
class ClassLeft; include Mongoid::Document; end
|
25
|
+
ClassLeft
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:class_right) do
|
29
|
+
class ClassRight; include Mongoid::Document; end
|
30
|
+
ClassRight
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:association) do
|
34
|
+
association_class.new(class_left, :name, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:options) do
|
38
|
+
{}
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'the :as option' do
|
42
|
+
|
43
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:as) do
|
44
|
+
|
45
|
+
context 'when :as is in the options' do
|
46
|
+
|
47
|
+
let(:options) do
|
48
|
+
{
|
49
|
+
as: :nameable
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns the :as value' do
|
54
|
+
expect(association.as).to eq(:nameable)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when :as is not in the options' do
|
59
|
+
|
60
|
+
it 'returns nil' do
|
61
|
+
expect(association.as).to be_nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:as) do
|
67
|
+
|
68
|
+
it 'returns nil' do
|
69
|
+
expect(association.as).to be_nil
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when the option is provided' do
|
73
|
+
|
74
|
+
let(:options) do
|
75
|
+
{
|
76
|
+
as: :nameable
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
81
|
+
expect {
|
82
|
+
association
|
83
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#autobuilding?' do
|
90
|
+
|
91
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:autobuild) do
|
92
|
+
|
93
|
+
context 'when :autobuild is in the options' do
|
94
|
+
|
95
|
+
context 'when :autobuild is true' do
|
96
|
+
|
97
|
+
let(:options) do
|
98
|
+
{
|
99
|
+
autobuild: true
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'returns true' do
|
104
|
+
expect(association.autobuilding?).to be(true)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'when :autobuild is false' do
|
109
|
+
|
110
|
+
let(:options) do
|
111
|
+
{
|
112
|
+
autobuild: false
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'returns false' do
|
117
|
+
expect(association.autobuilding?).to be(false)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when :autobuild is nil' do
|
122
|
+
|
123
|
+
let(:options) do
|
124
|
+
{
|
125
|
+
autobuild: nil
|
126
|
+
}
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'returns false' do
|
130
|
+
expect(association.autobuilding?).to be(false)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'when :autobuild is not in the options' do
|
136
|
+
|
137
|
+
it 'returns false' do
|
138
|
+
expect(association.autobuilding?).to be(false)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:autobuild) do
|
144
|
+
|
145
|
+
it 'returns false' do
|
146
|
+
expect(association.autobuilding?).to be(false)
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when the option is provided' do
|
150
|
+
|
151
|
+
let(:options) do
|
152
|
+
{
|
153
|
+
autobuild: false
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
158
|
+
expect {
|
159
|
+
association
|
160
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#autosave?' do
|
167
|
+
|
168
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:autosave) do
|
169
|
+
|
170
|
+
context 'when :autosave is in the options' do
|
171
|
+
|
172
|
+
context 'when :autosave is true' do
|
173
|
+
|
174
|
+
let(:options) do
|
175
|
+
{
|
176
|
+
autosave: true
|
177
|
+
}
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'returns true' do
|
181
|
+
expect(association.autosave?).to be(true)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'when :autosave is false' do
|
186
|
+
|
187
|
+
let(:options) do
|
188
|
+
{
|
189
|
+
autosave: false
|
190
|
+
}
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'returns false' do
|
194
|
+
expect(association.autosave?).to be(false)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context 'when :autosave is nil' do
|
199
|
+
|
200
|
+
let(:options) do
|
201
|
+
{
|
202
|
+
autosave: nil
|
203
|
+
}
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'returns false' do
|
207
|
+
expect(association.autosave?).to be(false)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context 'when :autosave is not in the options' do
|
213
|
+
|
214
|
+
it 'returns false' do
|
215
|
+
expect(association.autosave?).to be(false)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:autosave) do
|
221
|
+
|
222
|
+
it 'returns false' do
|
223
|
+
expect(association.autosave?).to be(false)
|
224
|
+
end
|
225
|
+
|
226
|
+
context 'when the option is provided' do
|
227
|
+
|
228
|
+
let(:options) do
|
229
|
+
{
|
230
|
+
autosave: false
|
231
|
+
}
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
235
|
+
expect {
|
236
|
+
association
|
237
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
describe 'the :dependent option' do
|
244
|
+
|
245
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:dependent) do
|
246
|
+
|
247
|
+
context 'when :dependent is in the options' do
|
248
|
+
|
249
|
+
let(:options) do
|
250
|
+
{
|
251
|
+
dependent: :delete_all
|
252
|
+
}
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'returns the :dependent value' do
|
256
|
+
expect(association.dependent).to eq(options[:dependent])
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
context 'when :dependent is not in the options' do
|
261
|
+
|
262
|
+
it 'returns nil' do
|
263
|
+
expect(association.dependent).to be_nil
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:dependent) do
|
269
|
+
|
270
|
+
it 'returns nil' do
|
271
|
+
expect(association.dependent).to be_nil
|
272
|
+
end
|
273
|
+
|
274
|
+
context 'when the option is provided' do
|
275
|
+
|
276
|
+
let(:options) do
|
277
|
+
{
|
278
|
+
dependent: :delete_all
|
279
|
+
}
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
283
|
+
expect {
|
284
|
+
association
|
285
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
describe 'the :foreign_key option' do
|
292
|
+
|
293
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:foreign_key) do
|
294
|
+
|
295
|
+
context 'when the :foreign_key option is specified' do
|
296
|
+
|
297
|
+
let(:options) do
|
298
|
+
{
|
299
|
+
foreign_key: :some_field
|
300
|
+
}
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'returns the foreign_key as a String' do
|
304
|
+
expect(association.foreign_key).to eq(options[:foreign_key].to_s)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
context 'when the association stores the foreign key', if: association_class::VALID_OPTIONS.include?(:foreign_key) &&
|
309
|
+
STORES_FOREIGN_KEY.include?(association_class) do
|
310
|
+
|
311
|
+
context 'when :foreign_key option is not specified' do
|
312
|
+
|
313
|
+
it 'returns the name followed by the foreign_key_suffix' do
|
314
|
+
expect(association.foreign_key).to eq("name#{association.class::FOREIGN_KEY_SUFFIX}")
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
context 'when the association does not store the foreign key', if: association_class::VALID_OPTIONS.include?(:foreign_key) &&
|
320
|
+
!STORES_FOREIGN_KEY.include?(association_class) do
|
321
|
+
|
322
|
+
context 'when :foreign_key option is not specified' do
|
323
|
+
|
324
|
+
before do
|
325
|
+
allow(association).to receive(:inverse).and_return(:other)
|
326
|
+
end
|
327
|
+
|
328
|
+
it 'returns the inverse name followed by the foreign_key_suffix' do
|
329
|
+
expect(association.foreign_key).to eq("other#{association.class::FOREIGN_KEY_SUFFIX}")
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:foreign_key) do
|
336
|
+
|
337
|
+
it 'returns nil' do
|
338
|
+
expect(association.dependent).to be_nil
|
339
|
+
end
|
340
|
+
|
341
|
+
context 'when the option is provided' do
|
342
|
+
|
343
|
+
let(:options) do
|
344
|
+
{
|
345
|
+
foreign_key: :some_field
|
346
|
+
}
|
347
|
+
end
|
348
|
+
|
349
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
350
|
+
expect {
|
351
|
+
association
|
352
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
353
|
+
end
|
354
|
+
end
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
describe 'the :primary_key option' do
|
359
|
+
|
360
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:primary_key) do
|
361
|
+
|
362
|
+
context 'when the option is specified' do
|
363
|
+
|
364
|
+
let(:options) do
|
365
|
+
{
|
366
|
+
primary_key: :other_id
|
367
|
+
}
|
368
|
+
end
|
369
|
+
|
370
|
+
it 'returns the primary_key from the options as a String' do
|
371
|
+
expect(association.primary_key).to eq(options[:primary_key].to_s)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
context 'when the option is not specified' do
|
376
|
+
|
377
|
+
it 'returns the default primary key' do
|
378
|
+
expect(association.primary_key).to eq(Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT)
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:primary_key) do
|
384
|
+
|
385
|
+
it 'returns nil' do
|
386
|
+
expect(association.primary_key).to be_nil
|
387
|
+
end
|
388
|
+
|
389
|
+
context 'when the option is provided' do
|
390
|
+
|
391
|
+
let(:options) do
|
392
|
+
{
|
393
|
+
primary_key: :other_id
|
394
|
+
}
|
395
|
+
end
|
396
|
+
|
397
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
398
|
+
expect {
|
399
|
+
association
|
400
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
401
|
+
end
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
describe 'the :after_add option' do
|
407
|
+
|
408
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:after_add) do
|
409
|
+
|
410
|
+
context 'when the :after_add option is specified' do
|
411
|
+
|
412
|
+
let(:options) do
|
413
|
+
{
|
414
|
+
after_add: :method_name
|
415
|
+
}
|
416
|
+
end
|
417
|
+
|
418
|
+
it 'retrieves the after_add method name from the association' do
|
419
|
+
expect(association.get_callbacks(:after_add)).to eq(Array(options[:after_add]))
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
context 'when the :after_add option is not specified' do
|
424
|
+
|
425
|
+
it 'returns nil' do
|
426
|
+
expect(association.get_callbacks(:after_add)).to be_empty
|
427
|
+
end
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:after_add) do
|
432
|
+
|
433
|
+
|
434
|
+
it 'returns nil' do
|
435
|
+
expect(association.get_callbacks(:after_add)).to be_empty
|
436
|
+
end
|
437
|
+
|
438
|
+
context 'when the option is provided' do
|
439
|
+
|
440
|
+
let(:options) do
|
441
|
+
{
|
442
|
+
after_add: :method_name
|
443
|
+
}
|
444
|
+
end
|
445
|
+
|
446
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
447
|
+
expect {
|
448
|
+
association
|
449
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
450
|
+
end
|
451
|
+
end
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
describe 'the :after_remove option' do
|
456
|
+
|
457
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:after_remove) do
|
458
|
+
|
459
|
+
context 'when the :after_remove option is specified' do
|
460
|
+
|
461
|
+
let(:options) do
|
462
|
+
{
|
463
|
+
after_remove: :method_name
|
464
|
+
}
|
465
|
+
end
|
466
|
+
|
467
|
+
it 'retrieves the after_remove method name from the association' do
|
468
|
+
expect(association.get_callbacks(:after_remove)).to eq(Array(options[:after_remove]))
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
context 'when the :after_remove option is not specified' do
|
473
|
+
|
474
|
+
it 'returns nil' do
|
475
|
+
expect(association.get_callbacks(:after_remove)).to be_empty
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:after_remove) do
|
481
|
+
|
482
|
+
|
483
|
+
it 'returns nil' do
|
484
|
+
expect(association.get_callbacks(:after_remove)).to be_empty
|
485
|
+
end
|
486
|
+
|
487
|
+
context 'when the option is provided' do
|
488
|
+
|
489
|
+
let(:options) do
|
490
|
+
{
|
491
|
+
after_remove: :method_name
|
492
|
+
}
|
493
|
+
end
|
494
|
+
|
495
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
496
|
+
expect {
|
497
|
+
association
|
498
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
499
|
+
end
|
500
|
+
end
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
describe 'the :before_add option' do
|
505
|
+
|
506
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:before_add) do
|
507
|
+
|
508
|
+
context 'when the :before_add option is specified' do
|
509
|
+
|
510
|
+
let(:options) do
|
511
|
+
{
|
512
|
+
before_add: :method_name
|
513
|
+
}
|
514
|
+
end
|
515
|
+
|
516
|
+
it 'retrieves the before_add method name from the association' do
|
517
|
+
expect(association.get_callbacks(:before_add)).to eq(Array(options[:before_add]))
|
518
|
+
end
|
519
|
+
end
|
520
|
+
|
521
|
+
context 'when the :before_add option is not specified' do
|
522
|
+
|
523
|
+
it 'returns nil' do
|
524
|
+
expect(association.get_callbacks(:before_add)).to be_empty
|
525
|
+
end
|
526
|
+
end
|
527
|
+
end
|
528
|
+
|
529
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:before_add) do
|
530
|
+
|
531
|
+
|
532
|
+
it 'returns nil' do
|
533
|
+
expect(association.get_callbacks(:before_add)).to be_empty
|
534
|
+
end
|
535
|
+
|
536
|
+
context 'when the option is provided' do
|
537
|
+
|
538
|
+
let(:options) do
|
539
|
+
{
|
540
|
+
before_add: :method_name
|
541
|
+
}
|
542
|
+
end
|
543
|
+
|
544
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
545
|
+
expect {
|
546
|
+
association
|
547
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
548
|
+
end
|
549
|
+
end
|
550
|
+
end
|
551
|
+
end
|
552
|
+
|
553
|
+
describe 'the :before_remove option' do
|
554
|
+
|
555
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:before_remove) do
|
556
|
+
|
557
|
+
context 'when the :before_remove option is specified' do
|
558
|
+
|
559
|
+
let(:options) do
|
560
|
+
{
|
561
|
+
before_remove: :method_name
|
562
|
+
}
|
563
|
+
end
|
564
|
+
|
565
|
+
it 'retrieves the before_remove method name from the association' do
|
566
|
+
expect(association.get_callbacks(:before_remove)).to eq(Array(options[:before_remove]))
|
567
|
+
end
|
568
|
+
end
|
569
|
+
|
570
|
+
context 'when the :before_remove option is not specified' do
|
571
|
+
|
572
|
+
it 'returns nil' do
|
573
|
+
expect(association.get_callbacks(:before_remove)).to be_empty
|
574
|
+
end
|
575
|
+
end
|
576
|
+
end
|
577
|
+
|
578
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:before_remove) do
|
579
|
+
|
580
|
+
|
581
|
+
it 'returns nil' do
|
582
|
+
expect(association.get_callbacks(:before_remove)).to be_empty
|
583
|
+
end
|
584
|
+
|
585
|
+
context 'when the option is provided' do
|
586
|
+
|
587
|
+
let(:options) do
|
588
|
+
{
|
589
|
+
before_remove: :method_name
|
590
|
+
}
|
591
|
+
end
|
592
|
+
|
593
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
594
|
+
expect {
|
595
|
+
association
|
596
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
597
|
+
end
|
598
|
+
end
|
599
|
+
end
|
600
|
+
end
|
601
|
+
|
602
|
+
|
603
|
+
describe '#indexed?' do
|
604
|
+
|
605
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:index) do
|
606
|
+
|
607
|
+
context 'when :index is in the options' do
|
608
|
+
|
609
|
+
context 'when :index is true' do
|
610
|
+
|
611
|
+
let(:options) do
|
612
|
+
{
|
613
|
+
index: true
|
614
|
+
}
|
615
|
+
end
|
616
|
+
|
617
|
+
it 'returns true' do
|
618
|
+
expect(association.indexed?).to be(true)
|
619
|
+
end
|
620
|
+
end
|
621
|
+
|
622
|
+
context 'when :index is false' do
|
623
|
+
|
624
|
+
let(:options) do
|
625
|
+
{
|
626
|
+
index: false
|
627
|
+
}
|
628
|
+
end
|
629
|
+
|
630
|
+
it 'returns false' do
|
631
|
+
expect(association.indexed?).to be(false)
|
632
|
+
end
|
633
|
+
end
|
634
|
+
|
635
|
+
context 'when :index is nil' do
|
636
|
+
|
637
|
+
let(:options) do
|
638
|
+
{
|
639
|
+
index: nil
|
640
|
+
}
|
641
|
+
end
|
642
|
+
|
643
|
+
it 'returns false' do
|
644
|
+
expect(association.indexed?).to be(false)
|
645
|
+
end
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
context 'when :index is not in the options' do
|
650
|
+
|
651
|
+
it 'returns false' do
|
652
|
+
expect(association.indexed?).to be(false)
|
653
|
+
end
|
654
|
+
end
|
655
|
+
end
|
656
|
+
|
657
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:index) do
|
658
|
+
|
659
|
+
it 'returns false' do
|
660
|
+
expect(association.indexed?).to be(false)
|
661
|
+
end
|
662
|
+
|
663
|
+
context 'when the option is provided' do
|
664
|
+
|
665
|
+
let(:options) do
|
666
|
+
{
|
667
|
+
index: true
|
668
|
+
}
|
669
|
+
end
|
670
|
+
|
671
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
672
|
+
expect {
|
673
|
+
association
|
674
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
675
|
+
end
|
676
|
+
end
|
677
|
+
end
|
678
|
+
end
|
679
|
+
|
680
|
+
describe 'the :order option' do
|
681
|
+
|
682
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:order) do
|
683
|
+
|
684
|
+
context 'when :order is in the options' do
|
685
|
+
|
686
|
+
let(:options) do
|
687
|
+
{
|
688
|
+
order: :rating.desc
|
689
|
+
}
|
690
|
+
end
|
691
|
+
|
692
|
+
it 'returns a Mongoid::Criteria::Queryable::Key' do
|
693
|
+
expect(association.order).to be_a(Mongoid::Criteria::Queryable::Key)
|
694
|
+
end
|
695
|
+
end
|
696
|
+
|
697
|
+
context 'when :order is not in the options' do
|
698
|
+
|
699
|
+
it 'returns nil' do
|
700
|
+
expect(association.order).to be_nil
|
701
|
+
end
|
702
|
+
end
|
703
|
+
end
|
704
|
+
|
705
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:order) do
|
706
|
+
|
707
|
+
it 'returns nil' do
|
708
|
+
expect(association.order).to be_nil
|
709
|
+
end
|
710
|
+
|
711
|
+
context 'when the option is provided' do
|
712
|
+
|
713
|
+
let(:options) do
|
714
|
+
{
|
715
|
+
order: :rating.desc
|
716
|
+
}
|
717
|
+
end
|
718
|
+
|
719
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
720
|
+
expect {
|
721
|
+
association
|
722
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
723
|
+
end
|
724
|
+
end
|
725
|
+
end
|
726
|
+
end
|
727
|
+
|
728
|
+
describe '#polymorphic' do
|
729
|
+
|
730
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:polymorphic) do
|
731
|
+
|
732
|
+
context 'when :polymorphic is in the options' do
|
733
|
+
|
734
|
+
context 'when :polymorphic is true' do
|
735
|
+
|
736
|
+
let(:options) do
|
737
|
+
{
|
738
|
+
polymorphic: true
|
739
|
+
}
|
740
|
+
end
|
741
|
+
|
742
|
+
it 'returns true' do
|
743
|
+
expect(association.polymorphic?).to be(true)
|
744
|
+
end
|
745
|
+
end
|
746
|
+
|
747
|
+
context 'when :polymorphic is false' do
|
748
|
+
|
749
|
+
let(:options) do
|
750
|
+
{
|
751
|
+
polymorphic: false
|
752
|
+
}
|
753
|
+
end
|
754
|
+
|
755
|
+
it 'returns false' do
|
756
|
+
expect(association.polymorphic?).to be(false)
|
757
|
+
end
|
758
|
+
end
|
759
|
+
|
760
|
+
context 'when :polymorphic is nil' do
|
761
|
+
|
762
|
+
let(:options) do
|
763
|
+
{
|
764
|
+
polymorphic: nil
|
765
|
+
}
|
766
|
+
end
|
767
|
+
|
768
|
+
it 'returns false' do
|
769
|
+
expect(association.polymorphic?).to be(false)
|
770
|
+
end
|
771
|
+
end
|
772
|
+
end
|
773
|
+
|
774
|
+
context 'when :polymorphic is not in the options' do
|
775
|
+
|
776
|
+
it 'returns false' do
|
777
|
+
expect(association.polymorphic?).to be(false)
|
778
|
+
end
|
779
|
+
end
|
780
|
+
end
|
781
|
+
|
782
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:polymorphic) do
|
783
|
+
|
784
|
+
it 'returns false' do
|
785
|
+
expect(association.polymorphic?).to be(false)
|
786
|
+
end
|
787
|
+
|
788
|
+
context 'when the option is provided' do
|
789
|
+
|
790
|
+
let(:options) do
|
791
|
+
{
|
792
|
+
polymorphic: true
|
793
|
+
}
|
794
|
+
end
|
795
|
+
|
796
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
797
|
+
expect {
|
798
|
+
association
|
799
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
800
|
+
end
|
801
|
+
end
|
802
|
+
end
|
803
|
+
end
|
804
|
+
|
805
|
+
describe '#cascading_callbacks?' do
|
806
|
+
|
807
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:cascade_callbacks) do
|
808
|
+
|
809
|
+
context 'when :cascade_callbacks is in the options' do
|
810
|
+
|
811
|
+
context 'when :cascade_callbacks is true' do
|
812
|
+
|
813
|
+
let(:options) do
|
814
|
+
{
|
815
|
+
cascade_callbacks: true
|
816
|
+
}
|
817
|
+
end
|
818
|
+
|
819
|
+
it 'returns true' do
|
820
|
+
expect(association.cascading_callbacks?).to be(true)
|
821
|
+
end
|
822
|
+
end
|
823
|
+
|
824
|
+
context 'when :cascade_callbacks is false' do
|
825
|
+
|
826
|
+
let(:options) do
|
827
|
+
{
|
828
|
+
cascade_callbacks: false
|
829
|
+
}
|
830
|
+
end
|
831
|
+
|
832
|
+
it 'returns false' do
|
833
|
+
expect(association.cascading_callbacks?).to be(false)
|
834
|
+
end
|
835
|
+
end
|
836
|
+
|
837
|
+
context 'when :cascade_callbacks is nil' do
|
838
|
+
|
839
|
+
let(:options) do
|
840
|
+
{
|
841
|
+
cascade_callbacks: nil
|
842
|
+
}
|
843
|
+
end
|
844
|
+
|
845
|
+
it 'returns false' do
|
846
|
+
expect(association.cascading_callbacks?).to be(false)
|
847
|
+
end
|
848
|
+
end
|
849
|
+
end
|
850
|
+
|
851
|
+
context 'when :cascade_callbacks is not in the options' do
|
852
|
+
|
853
|
+
it 'returns false' do
|
854
|
+
expect(association.cascading_callbacks?).to be(false)
|
855
|
+
end
|
856
|
+
end
|
857
|
+
end
|
858
|
+
|
859
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:cascade_callbacks) do
|
860
|
+
|
861
|
+
it 'returns false' do
|
862
|
+
expect(association.cascading_callbacks?).to be(false)
|
863
|
+
end
|
864
|
+
|
865
|
+
context 'when the option is provided' do
|
866
|
+
|
867
|
+
let(:options) do
|
868
|
+
{
|
869
|
+
cascade_callbacks: true
|
870
|
+
}
|
871
|
+
end
|
872
|
+
|
873
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
874
|
+
expect {
|
875
|
+
association
|
876
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
877
|
+
end
|
878
|
+
end
|
879
|
+
end
|
880
|
+
end
|
881
|
+
|
882
|
+
describe '#cyclic?' do
|
883
|
+
|
884
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:cyclic) do
|
885
|
+
|
886
|
+
context 'when :cyclic is in the options' do
|
887
|
+
|
888
|
+
context 'when :cyclic is true' do
|
889
|
+
|
890
|
+
let(:options) do
|
891
|
+
{
|
892
|
+
cyclic: true
|
893
|
+
}
|
894
|
+
end
|
895
|
+
|
896
|
+
it 'returns true' do
|
897
|
+
expect(association.cyclic?).to be(true)
|
898
|
+
end
|
899
|
+
end
|
900
|
+
|
901
|
+
context 'when :cyclic is false' do
|
902
|
+
|
903
|
+
let(:options) do
|
904
|
+
{
|
905
|
+
cyclic: false
|
906
|
+
}
|
907
|
+
end
|
908
|
+
|
909
|
+
it 'returns false' do
|
910
|
+
expect(association.cyclic?).to be(false)
|
911
|
+
end
|
912
|
+
end
|
913
|
+
|
914
|
+
context 'when :cyclic is nil' do
|
915
|
+
|
916
|
+
let(:options) do
|
917
|
+
{
|
918
|
+
cyclic: nil
|
919
|
+
}
|
920
|
+
end
|
921
|
+
|
922
|
+
it 'returns false' do
|
923
|
+
expect(association.cyclic?).to be(false)
|
924
|
+
end
|
925
|
+
end
|
926
|
+
end
|
927
|
+
|
928
|
+
context 'when :cyclic is not in the options' do
|
929
|
+
|
930
|
+
it 'returns false' do
|
931
|
+
expect(association.cyclic?).to be(false)
|
932
|
+
end
|
933
|
+
end
|
934
|
+
end
|
935
|
+
|
936
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:cyclic) do
|
937
|
+
|
938
|
+
it 'returns false' do
|
939
|
+
expect(association.cyclic?).to be(false)
|
940
|
+
end
|
941
|
+
|
942
|
+
context 'when the option is provided' do
|
943
|
+
|
944
|
+
let(:options) do
|
945
|
+
{
|
946
|
+
cyclic: true
|
947
|
+
}
|
948
|
+
end
|
949
|
+
|
950
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
951
|
+
expect {
|
952
|
+
association
|
953
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
954
|
+
end
|
955
|
+
end
|
956
|
+
end
|
957
|
+
end
|
958
|
+
|
959
|
+
describe 'the :store_as option' do
|
960
|
+
|
961
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:store_as) do
|
962
|
+
|
963
|
+
context 'when :store_as is in the options' do
|
964
|
+
|
965
|
+
let(:options) do
|
966
|
+
{
|
967
|
+
store_as: :another_name
|
968
|
+
}
|
969
|
+
end
|
970
|
+
|
971
|
+
it 'returns the :store_as value' do
|
972
|
+
expect(association.store_as).to eq(options[:store_as].to_s)
|
973
|
+
end
|
974
|
+
end
|
975
|
+
|
976
|
+
context 'when :store_as is not in the options' do
|
977
|
+
|
978
|
+
it 'returns the name as a string' do
|
979
|
+
expect(association.store_as).to eq("name")
|
980
|
+
end
|
981
|
+
end
|
982
|
+
end
|
983
|
+
|
984
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:store_as) do
|
985
|
+
|
986
|
+
it 'returns nil' do
|
987
|
+
expect(association.store_as).to be_nil
|
988
|
+
end
|
989
|
+
|
990
|
+
context 'when the option is provided' do
|
991
|
+
|
992
|
+
let(:options) do
|
993
|
+
{
|
994
|
+
store_as: :another_name
|
995
|
+
}
|
996
|
+
end
|
997
|
+
|
998
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
999
|
+
expect {
|
1000
|
+
association
|
1001
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
1002
|
+
end
|
1003
|
+
end
|
1004
|
+
end
|
1005
|
+
end
|
1006
|
+
|
1007
|
+
describe 'the :class_name option' do
|
1008
|
+
|
1009
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:class_name) do
|
1010
|
+
|
1011
|
+
context 'when :class_name is in the options' do
|
1012
|
+
|
1013
|
+
let(:options) do
|
1014
|
+
{
|
1015
|
+
class_name: :a_class
|
1016
|
+
}
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
it 'returns the :class_name value' do
|
1020
|
+
expect(association.class_name).to eq(:a_class)
|
1021
|
+
end
|
1022
|
+
end
|
1023
|
+
|
1024
|
+
context 'when :class_name is not in the options' do
|
1025
|
+
|
1026
|
+
it 'returns the name deduced from the association name' do
|
1027
|
+
expect(association.class_name).to eq(ActiveSupport::Inflector.classify(:name))
|
1028
|
+
end
|
1029
|
+
end
|
1030
|
+
end
|
1031
|
+
|
1032
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:class_name) do
|
1033
|
+
|
1034
|
+
it 'returns the name deduced from the association name' do
|
1035
|
+
expect(association.class_name).to eq(ActiveSupport::Inflector.classify(:name))
|
1036
|
+
end
|
1037
|
+
|
1038
|
+
context 'when the option is provided' do
|
1039
|
+
|
1040
|
+
let(:options) do
|
1041
|
+
{
|
1042
|
+
class_name: :a_class
|
1043
|
+
}
|
1044
|
+
end
|
1045
|
+
|
1046
|
+
it 'should support the option' do
|
1047
|
+
fail('All association types should support this option')
|
1048
|
+
end
|
1049
|
+
end
|
1050
|
+
end
|
1051
|
+
end
|
1052
|
+
|
1053
|
+
describe '#counter_cached?' do
|
1054
|
+
|
1055
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:counter_cache) do
|
1056
|
+
|
1057
|
+
context 'when :counter_cache is in the options' do
|
1058
|
+
|
1059
|
+
context 'when :counter_cache is true' do
|
1060
|
+
|
1061
|
+
let(:options) do
|
1062
|
+
{
|
1063
|
+
counter_cache: true
|
1064
|
+
}
|
1065
|
+
end
|
1066
|
+
|
1067
|
+
it 'returns true' do
|
1068
|
+
expect(association.counter_cached?).to be(true)
|
1069
|
+
end
|
1070
|
+
end
|
1071
|
+
|
1072
|
+
context 'when :counter_cache is false' do
|
1073
|
+
|
1074
|
+
let(:options) do
|
1075
|
+
{
|
1076
|
+
counter_cache: false
|
1077
|
+
}
|
1078
|
+
end
|
1079
|
+
|
1080
|
+
it 'returns false' do
|
1081
|
+
expect(association.counter_cached?).to be(false)
|
1082
|
+
end
|
1083
|
+
end
|
1084
|
+
|
1085
|
+
context 'when :counter_cache is nil' do
|
1086
|
+
|
1087
|
+
let(:options) do
|
1088
|
+
{
|
1089
|
+
counter_cache: nil
|
1090
|
+
}
|
1091
|
+
end
|
1092
|
+
|
1093
|
+
it 'returns false' do
|
1094
|
+
expect(association.counter_cached?).to be(false)
|
1095
|
+
end
|
1096
|
+
end
|
1097
|
+
end
|
1098
|
+
|
1099
|
+
context 'when :counter_cache is not in the options' do
|
1100
|
+
|
1101
|
+
it 'returns false' do
|
1102
|
+
expect(association.counter_cached?).to be(false)
|
1103
|
+
end
|
1104
|
+
end
|
1105
|
+
end
|
1106
|
+
|
1107
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:counter_cache) do
|
1108
|
+
|
1109
|
+
it 'returns false' do
|
1110
|
+
expect(association.counter_cached?).to be(false)
|
1111
|
+
end
|
1112
|
+
|
1113
|
+
context 'when the option is provided' do
|
1114
|
+
|
1115
|
+
let(:options) do
|
1116
|
+
{
|
1117
|
+
counter_cache: :column_name
|
1118
|
+
}
|
1119
|
+
end
|
1120
|
+
|
1121
|
+
it 'raises a Mongoid::Errors::InvalidRelationOption error' do
|
1122
|
+
expect {
|
1123
|
+
association
|
1124
|
+
}.to raise_exception(Mongoid::Errors::InvalidRelationOption)
|
1125
|
+
end
|
1126
|
+
end
|
1127
|
+
end
|
1128
|
+
end
|
1129
|
+
|
1130
|
+
describe '#extension' do
|
1131
|
+
|
1132
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:extend) do
|
1133
|
+
|
1134
|
+
context 'when a block is passed' do
|
1135
|
+
|
1136
|
+
let!(:association) do
|
1137
|
+
association_class.new(class_left, :name, options) do; end
|
1138
|
+
end
|
1139
|
+
|
1140
|
+
after do
|
1141
|
+
Object.send(:remove_const, :ClassLeft)
|
1142
|
+
end
|
1143
|
+
|
1144
|
+
it 'defines an extension module' do
|
1145
|
+
expect(ClassLeft::ClassLeftNameRelationExtension).to be_a(Module)
|
1146
|
+
end
|
1147
|
+
|
1148
|
+
it 'returns the extension' do
|
1149
|
+
expect(association.extension).to eq(ClassLeft::ClassLeftNameRelationExtension)
|
1150
|
+
end
|
1151
|
+
end
|
1152
|
+
|
1153
|
+
context 'when a module name is passed' do
|
1154
|
+
|
1155
|
+
before do
|
1156
|
+
class ClassLeft; end
|
1157
|
+
module ClassLeft::Handle; end
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
let(:options) do
|
1161
|
+
{
|
1162
|
+
extend: ClassLeft::Handle
|
1163
|
+
}
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
let!(:association) do
|
1167
|
+
association_class.new(class_left, :name, options)
|
1168
|
+
end
|
1169
|
+
|
1170
|
+
it 'returns the extension' do
|
1171
|
+
expect(association.extension).to eq(ClassLeft::Handle)
|
1172
|
+
end
|
1173
|
+
end
|
1174
|
+
|
1175
|
+
context 'when a block is not passed' do
|
1176
|
+
|
1177
|
+
it 'does not define an extension module' do
|
1178
|
+
expect(defined?(ClassLeft::ClassLeftNameRelationExtension)).to be_nil
|
1179
|
+
end
|
1180
|
+
|
1181
|
+
it 'returns nil' do
|
1182
|
+
expect(association.extension).to be_nil
|
1183
|
+
end
|
1184
|
+
end
|
1185
|
+
|
1186
|
+
|
1187
|
+
end
|
1188
|
+
|
1189
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:extend) do
|
1190
|
+
|
1191
|
+
it 'should support the option' do
|
1192
|
+
fail('All association types should support this option')
|
1193
|
+
end
|
1194
|
+
end
|
1195
|
+
end
|
1196
|
+
|
1197
|
+
describe 'the :inverse_of option' do
|
1198
|
+
|
1199
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:inverse_of) do
|
1200
|
+
|
1201
|
+
context 'when :inverse_of is in the options' do
|
1202
|
+
|
1203
|
+
let(:options) do
|
1204
|
+
{
|
1205
|
+
inverse_of: :inverse_name
|
1206
|
+
}
|
1207
|
+
end
|
1208
|
+
|
1209
|
+
it 'returns the :inverse_of value' do
|
1210
|
+
expect(association.inverse_of).to eq(:inverse_name)
|
1211
|
+
end
|
1212
|
+
end
|
1213
|
+
|
1214
|
+
context 'when :inverse_of is not in the options' do
|
1215
|
+
|
1216
|
+
it 'returns nil' do
|
1217
|
+
expect(association.inverse_of).to be_nil
|
1218
|
+
end
|
1219
|
+
end
|
1220
|
+
end
|
1221
|
+
|
1222
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:inverse_of) do
|
1223
|
+
|
1224
|
+
it 'returns nil' do
|
1225
|
+
expect(association.inverse_of).to be_nil
|
1226
|
+
end
|
1227
|
+
|
1228
|
+
|
1229
|
+
context 'when the option is provided' do
|
1230
|
+
|
1231
|
+
let(:options) do
|
1232
|
+
{
|
1233
|
+
inverse_of: :inverse_name
|
1234
|
+
}
|
1235
|
+
end
|
1236
|
+
|
1237
|
+
it 'should support the option' do
|
1238
|
+
fail('All association types should support this option')
|
1239
|
+
end
|
1240
|
+
end
|
1241
|
+
end
|
1242
|
+
end
|
1243
|
+
|
1244
|
+
describe '#validate?' do
|
1245
|
+
|
1246
|
+
context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:validate) do
|
1247
|
+
|
1248
|
+
context 'when :validate is in the options' do
|
1249
|
+
|
1250
|
+
context 'when :validate is true' do
|
1251
|
+
|
1252
|
+
let(:options) do
|
1253
|
+
{
|
1254
|
+
validate: true
|
1255
|
+
}
|
1256
|
+
end
|
1257
|
+
|
1258
|
+
it 'returns true' do
|
1259
|
+
expect(association.send(:validate?)).to be(true)
|
1260
|
+
end
|
1261
|
+
end
|
1262
|
+
|
1263
|
+
context 'when :validate is false' do
|
1264
|
+
|
1265
|
+
let(:options) do
|
1266
|
+
{
|
1267
|
+
validate: false
|
1268
|
+
}
|
1269
|
+
end
|
1270
|
+
|
1271
|
+
it 'returns false' do
|
1272
|
+
expect(association.send(:validate?)).to be(false)
|
1273
|
+
end
|
1274
|
+
end
|
1275
|
+
|
1276
|
+
context 'when :validate is nil' do
|
1277
|
+
|
1278
|
+
let(:options) do
|
1279
|
+
{
|
1280
|
+
validate: nil
|
1281
|
+
}
|
1282
|
+
end
|
1283
|
+
|
1284
|
+
it 'returns the validation default' do
|
1285
|
+
expect(association.send(:validate?)).to be(association.validation_default)
|
1286
|
+
end
|
1287
|
+
end
|
1288
|
+
end
|
1289
|
+
|
1290
|
+
context 'when :validate is not in the options' do
|
1291
|
+
|
1292
|
+
it 'returns the validation default' do
|
1293
|
+
expect(association.send(:validate?)).to be(association.validation_default)
|
1294
|
+
end
|
1295
|
+
end
|
1296
|
+
end
|
1297
|
+
|
1298
|
+
context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:validate) do
|
1299
|
+
|
1300
|
+
it 'returns false' do
|
1301
|
+
expect(association.send(:validate?)).to be(false)
|
1302
|
+
end
|
1303
|
+
|
1304
|
+
|
1305
|
+
context 'when the option is provided' do
|
1306
|
+
|
1307
|
+
let(:options) do
|
1308
|
+
{
|
1309
|
+
validate: true
|
1310
|
+
}
|
1311
|
+
end
|
1312
|
+
|
1313
|
+
it 'should support the option' do
|
1314
|
+
fail('All association types should support this option')
|
1315
|
+
end
|
1316
|
+
end
|
1317
|
+
end
|
1318
|
+
end
|
1319
|
+
end
|
1320
|
+
end
|
1321
|
+
end
|