mongoid 6.4.8 → 7.0.0.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +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,75 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe Mongoid::Relations::Constraint do
|
|
4
|
-
|
|
5
|
-
describe "#convert" do
|
|
6
|
-
|
|
7
|
-
context "when the id's class stores object ids" do
|
|
8
|
-
|
|
9
|
-
before(:all) do
|
|
10
|
-
Person.field(
|
|
11
|
-
:_id,
|
|
12
|
-
type: BSON::ObjectId,
|
|
13
|
-
pre_processed: true,
|
|
14
|
-
default: ->{ BSON::ObjectId.new },
|
|
15
|
-
overwrite: true
|
|
16
|
-
)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
let(:metadata) do
|
|
20
|
-
Mongoid::Relations::Metadata.new(
|
|
21
|
-
class_name: "Person",
|
|
22
|
-
name: :person,
|
|
23
|
-
inverse_class_name: "Post",
|
|
24
|
-
relation: Mongoid::Relations::Referenced::In
|
|
25
|
-
)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
let(:constraint) do
|
|
29
|
-
described_class.new(metadata)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
context "when provided an object id" do
|
|
33
|
-
|
|
34
|
-
let(:object) do
|
|
35
|
-
BSON::ObjectId.new
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it "returns the object id" do
|
|
39
|
-
expect(constraint.convert(object)).to eq(object)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
context "when provided a string" do
|
|
44
|
-
|
|
45
|
-
let(:object) do
|
|
46
|
-
BSON::ObjectId.new
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
it "returns an object id from the string" do
|
|
50
|
-
expect(constraint.convert(object.to_s)).to eq(object)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
context "when the id's class does not store object ids" do
|
|
56
|
-
|
|
57
|
-
let(:metadata) do
|
|
58
|
-
Mongoid::Relations::Metadata.new(
|
|
59
|
-
class_name: "Account",
|
|
60
|
-
name: :account,
|
|
61
|
-
inverse_class_name: "Alert",
|
|
62
|
-
relation: Mongoid::Relations::Referenced::In
|
|
63
|
-
)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
let(:constraint) do
|
|
67
|
-
described_class.new(metadata)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
it "returns the object" do
|
|
71
|
-
expect(constraint.convert("testing")).to eq("testing")
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe Mongoid::Relations::Conversions do
|
|
4
|
-
|
|
5
|
-
describe ".flag" do
|
|
6
|
-
|
|
7
|
-
context "when the inverse uses object ids" do
|
|
8
|
-
|
|
9
|
-
let(:metadata) do
|
|
10
|
-
Person.relations["game"]
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
let(:object) do
|
|
14
|
-
BSON::ObjectId.new
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
let(:flagged) do
|
|
18
|
-
described_class.flag(object, metadata)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
it "returns the object" do
|
|
22
|
-
expect(flagged).to eq(object)
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
context "when the inverse uses string ids" do
|
|
27
|
-
|
|
28
|
-
before do
|
|
29
|
-
Person.field(
|
|
30
|
-
:_id,
|
|
31
|
-
type: String,
|
|
32
|
-
pre_processed: true,
|
|
33
|
-
overwrite: true,
|
|
34
|
-
default: ->{ BSON::ObjectId.new.to_s }
|
|
35
|
-
)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
after do
|
|
39
|
-
Person.field(
|
|
40
|
-
:_id,
|
|
41
|
-
type: BSON::ObjectId,
|
|
42
|
-
pre_processed: true,
|
|
43
|
-
overwrite: true,
|
|
44
|
-
default: ->{ BSON::ObjectId.new }
|
|
45
|
-
)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
let(:metadata) do
|
|
49
|
-
Person.relations["game"]
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context "when provided an object id" do
|
|
53
|
-
|
|
54
|
-
let(:object) do
|
|
55
|
-
BSON::ObjectId.new
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
let(:flagged) do
|
|
59
|
-
described_class.flag(object, metadata)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it "returns the object id" do
|
|
63
|
-
expect(flagged).to eq(object)
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
context "when provided a string" do
|
|
68
|
-
|
|
69
|
-
let(:object) do
|
|
70
|
-
BSON::ObjectId.new.to_s
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
let(:flagged) do
|
|
74
|
-
described_class.flag(object, metadata)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
it "returns the string" do
|
|
78
|
-
expect(flagged).to eq(object)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it "marks the string as unconvertable" do
|
|
82
|
-
expect(flagged).to be_unconvertable_to_bson
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
context "when the inverse uses integer ids" do
|
|
88
|
-
|
|
89
|
-
let(:metadata) do
|
|
90
|
-
Jar.relations["cookies"]
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
context "when provided an object id" do
|
|
94
|
-
|
|
95
|
-
let(:object) do
|
|
96
|
-
BSON::ObjectId.new
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
let(:flagged) do
|
|
100
|
-
described_class.flag(object, metadata)
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
it "returns the object id" do
|
|
104
|
-
expect(flagged).to eq(object)
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
context "when provided an integer" do
|
|
109
|
-
|
|
110
|
-
let(:object) do
|
|
111
|
-
15
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
let(:flagged) do
|
|
115
|
-
described_class.flag(object, metadata)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
it "returns the integer" do
|
|
119
|
-
expect(flagged).to eq(15)
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
it "marks the integer as unconvertable" do
|
|
123
|
-
expect(flagged).to be_unconvertable_to_bson
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|
|
@@ -1,1985 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe Mongoid::Relations::Metadata do
|
|
4
|
-
|
|
5
|
-
describe "#counter_cached?" do
|
|
6
|
-
|
|
7
|
-
context "when counter_cache is false" do
|
|
8
|
-
|
|
9
|
-
let(:metadata) do
|
|
10
|
-
described_class.new(
|
|
11
|
-
name: :car,
|
|
12
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
13
|
-
counter_cache: false
|
|
14
|
-
)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it "returns false" do
|
|
18
|
-
expect(metadata).to_not be_counter_cached
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
context "when counter_cache is true" do
|
|
23
|
-
|
|
24
|
-
let(:metadata) do
|
|
25
|
-
described_class.new(
|
|
26
|
-
name: :car,
|
|
27
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
28
|
-
counter_cache: true
|
|
29
|
-
)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
it "returns true" do
|
|
33
|
-
expect(metadata).to be_counter_cached
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
context "when counter_cache is name for column" do
|
|
38
|
-
|
|
39
|
-
let(:metadata) do
|
|
40
|
-
described_class.new(
|
|
41
|
-
name: :car,
|
|
42
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
43
|
-
counter_cache: 'counter'
|
|
44
|
-
)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
it "returns true" do
|
|
48
|
-
expect(metadata).to be_counter_cached
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context "when counter_cache is nil" do
|
|
53
|
-
|
|
54
|
-
let(:metadata) do
|
|
55
|
-
described_class.new(
|
|
56
|
-
name: :car,
|
|
57
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
58
|
-
)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it "returns false" do
|
|
62
|
-
expect(metadata).to_not be_counter_cached
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
describe "#counter_cache_column_name" do
|
|
68
|
-
|
|
69
|
-
let(:inverse_class_name) do
|
|
70
|
-
"Wheel"
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
context "when the counter_cache is true" do
|
|
74
|
-
|
|
75
|
-
let(:metadata) do
|
|
76
|
-
described_class.new(
|
|
77
|
-
name: :car,
|
|
78
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
79
|
-
inverse_class_name: inverse_class_name,
|
|
80
|
-
counter_cache: true
|
|
81
|
-
)
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
before do
|
|
85
|
-
expect(metadata).to receive(:inverse).and_return(:wheels)
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
it "returns inverse name + _count" do
|
|
89
|
-
expect(metadata.counter_cache_column_name).to eq(
|
|
90
|
-
"#{inverse_class_name.demodulize.underscore.pluralize}_count"
|
|
91
|
-
)
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
context "when given a custom name for counter cache" do
|
|
96
|
-
let(:counter_cache_name) { 'counte_cache_for_wheels' }
|
|
97
|
-
let(:metadata) do
|
|
98
|
-
described_class.new(
|
|
99
|
-
name: :car,
|
|
100
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
101
|
-
inverse_class_name: inverse_class_name,
|
|
102
|
-
counter_cache: counter_cache_name
|
|
103
|
-
)
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
it "returns the name given" do
|
|
107
|
-
expect(metadata.counter_cache_column_name).to eq(counter_cache_name)
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
describe "#autobuilding?" do
|
|
113
|
-
|
|
114
|
-
context "when the option is true" do
|
|
115
|
-
|
|
116
|
-
let(:metadata) do
|
|
117
|
-
described_class.new(
|
|
118
|
-
relation: Mongoid::Relations::Embedded::One,
|
|
119
|
-
autobuild: true
|
|
120
|
-
)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
it "returns true" do
|
|
124
|
-
expect(metadata).to be_autobuilding
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
context "when the option is false" do
|
|
129
|
-
|
|
130
|
-
let(:metadata) do
|
|
131
|
-
described_class.new(
|
|
132
|
-
relation: Mongoid::Relations::Embedded::One,
|
|
133
|
-
autobuild: false
|
|
134
|
-
)
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
it "returns false" do
|
|
138
|
-
expect(metadata).to_not be_autobuilding
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
context "when the option is nil" do
|
|
143
|
-
|
|
144
|
-
let(:metadata) do
|
|
145
|
-
described_class.new(
|
|
146
|
-
relation: Mongoid::Relations::Embedded::One
|
|
147
|
-
)
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
it "returns false" do
|
|
151
|
-
expect(metadata).to_not be_autobuilding
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
describe "#builder" do
|
|
157
|
-
|
|
158
|
-
let(:metadata) do
|
|
159
|
-
described_class.new(relation: Mongoid::Relations::Embedded::One)
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
let(:object) do
|
|
163
|
-
double
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
let(:base) do
|
|
167
|
-
double
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
it "returns the builder from the relation" do
|
|
171
|
-
expect(
|
|
172
|
-
metadata.builder(base, object)
|
|
173
|
-
).to be_a_kind_of(Mongoid::Relations::Builders::Embedded::One)
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
describe "#cascading_callbacks?" do
|
|
178
|
-
|
|
179
|
-
context "when the option is true" do
|
|
180
|
-
|
|
181
|
-
let(:metadata) do
|
|
182
|
-
described_class.new(
|
|
183
|
-
relation: Mongoid::Relations::Embedded::Many,
|
|
184
|
-
cascade_callbacks: true
|
|
185
|
-
)
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
it "returns true" do
|
|
189
|
-
expect(metadata).to be_cascading_callbacks
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
context "when the option is false" do
|
|
194
|
-
|
|
195
|
-
let(:metadata) do
|
|
196
|
-
described_class.new(
|
|
197
|
-
relation: Mongoid::Relations::Embedded::Many,
|
|
198
|
-
cascade_callbacks: false
|
|
199
|
-
)
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
it "returns false" do
|
|
203
|
-
expect(metadata).to_not be_cascading_callbacks
|
|
204
|
-
end
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
context "when the option is nil" do
|
|
208
|
-
|
|
209
|
-
let(:metadata) do
|
|
210
|
-
described_class.new(
|
|
211
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
212
|
-
)
|
|
213
|
-
end
|
|
214
|
-
|
|
215
|
-
it "returns false" do
|
|
216
|
-
expect(metadata).to_not be_cascading_callbacks
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
describe "#cascade_strategy" do
|
|
222
|
-
|
|
223
|
-
context "when no dependent option is set" do
|
|
224
|
-
|
|
225
|
-
let(:metadata) do
|
|
226
|
-
described_class.new(
|
|
227
|
-
name: :posts,
|
|
228
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
229
|
-
)
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
it "returns nil" do
|
|
233
|
-
expect(metadata.cascade_strategy).to be_nil
|
|
234
|
-
end
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
context "when dependent is delete" do
|
|
238
|
-
|
|
239
|
-
let(:metadata) do
|
|
240
|
-
described_class.new(
|
|
241
|
-
name: :posts,
|
|
242
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
243
|
-
dependent: :delete
|
|
244
|
-
)
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
it "returns the delete strategy" do
|
|
248
|
-
expect(metadata.cascade_strategy).to eq(
|
|
249
|
-
Mongoid::Relations::Cascading::Delete
|
|
250
|
-
)
|
|
251
|
-
end
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
context "when dependent is destroy" do
|
|
255
|
-
|
|
256
|
-
let(:metadata) do
|
|
257
|
-
described_class.new(
|
|
258
|
-
name: :posts,
|
|
259
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
260
|
-
dependent: :destroy
|
|
261
|
-
)
|
|
262
|
-
end
|
|
263
|
-
|
|
264
|
-
it "returns the destroy strategy" do
|
|
265
|
-
expect(metadata.cascade_strategy).to eq(
|
|
266
|
-
Mongoid::Relations::Cascading::Destroy
|
|
267
|
-
)
|
|
268
|
-
end
|
|
269
|
-
end
|
|
270
|
-
|
|
271
|
-
context "when dependent is nullify" do
|
|
272
|
-
|
|
273
|
-
let(:metadata) do
|
|
274
|
-
described_class.new(
|
|
275
|
-
name: :posts,
|
|
276
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
277
|
-
dependent: :nullify
|
|
278
|
-
)
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
it "returns the nullify strategy" do
|
|
282
|
-
expect(metadata.cascade_strategy).to eq(
|
|
283
|
-
Mongoid::Relations::Cascading::Nullify
|
|
284
|
-
)
|
|
285
|
-
end
|
|
286
|
-
end
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
describe "#constraint" do
|
|
290
|
-
|
|
291
|
-
let(:metadata) do
|
|
292
|
-
described_class.new(
|
|
293
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
294
|
-
class_name: "Person"
|
|
295
|
-
)
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
it "returns the constraint object" do
|
|
299
|
-
expect(metadata.constraint).to be_a(Mongoid::Relations::Constraint)
|
|
300
|
-
end
|
|
301
|
-
end
|
|
302
|
-
|
|
303
|
-
describe "#classify" do
|
|
304
|
-
|
|
305
|
-
let(:name) do
|
|
306
|
-
"name"
|
|
307
|
-
end
|
|
308
|
-
|
|
309
|
-
let(:metadata) do
|
|
310
|
-
described_class.new(name: name)
|
|
311
|
-
end
|
|
312
|
-
|
|
313
|
-
let(:classified) do
|
|
314
|
-
metadata.send(:classify)
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
before do
|
|
318
|
-
expect(Mongoid::Relations::Options).to receive(:validate!).at_least(:once)
|
|
319
|
-
end
|
|
320
|
-
|
|
321
|
-
it "concatenates the result from #find_module and name.classify" do
|
|
322
|
-
expect(metadata).to receive(:find_module).once.and_return("Fruit")
|
|
323
|
-
expect(classified).to eq("Fruit::Name")
|
|
324
|
-
end
|
|
325
|
-
end
|
|
326
|
-
|
|
327
|
-
describe "#find_module" do
|
|
328
|
-
|
|
329
|
-
let(:name) do
|
|
330
|
-
"name"
|
|
331
|
-
end
|
|
332
|
-
|
|
333
|
-
let(:metadata) do
|
|
334
|
-
described_class.new(name: name, inverse_class_name: inverse_class_name)
|
|
335
|
-
end
|
|
336
|
-
|
|
337
|
-
let(:mod) do
|
|
338
|
-
metadata.send(:find_module)
|
|
339
|
-
end
|
|
340
|
-
|
|
341
|
-
before do
|
|
342
|
-
expect(Mongoid::Relations::Options).to receive(:validate!).at_least(:once)
|
|
343
|
-
end
|
|
344
|
-
|
|
345
|
-
context "when inverse_class_name is nil" do
|
|
346
|
-
|
|
347
|
-
let(:inverse_class_name) do
|
|
348
|
-
nil
|
|
349
|
-
end
|
|
350
|
-
|
|
351
|
-
it "returns nil" do
|
|
352
|
-
expect(mod).to be_nil
|
|
353
|
-
end
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
context "when inverse_class_name is empty" do
|
|
357
|
-
|
|
358
|
-
let(:inverse_class_name) do
|
|
359
|
-
""
|
|
360
|
-
end
|
|
361
|
-
|
|
362
|
-
it "returns nil" do
|
|
363
|
-
expect(mod).to be_nil
|
|
364
|
-
end
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
context "when inverse_class_name is defined" do
|
|
368
|
-
|
|
369
|
-
context "when inverse_class_name is in root namespace" do
|
|
370
|
-
|
|
371
|
-
let(:inverse_class_name) do
|
|
372
|
-
"Person"
|
|
373
|
-
end
|
|
374
|
-
|
|
375
|
-
context "when name isn't defined" do
|
|
376
|
-
|
|
377
|
-
let(:name) do
|
|
378
|
-
"undefined"
|
|
379
|
-
end
|
|
380
|
-
|
|
381
|
-
it "returns nil" do
|
|
382
|
-
expect(mod).to be_nil
|
|
383
|
-
end
|
|
384
|
-
end
|
|
385
|
-
|
|
386
|
-
context "when name is defined in root namespace" do
|
|
387
|
-
|
|
388
|
-
let(:name) do
|
|
389
|
-
"account"
|
|
390
|
-
end
|
|
391
|
-
|
|
392
|
-
it "returns root namespace" do
|
|
393
|
-
expect(mod).to be_nil
|
|
394
|
-
end
|
|
395
|
-
end
|
|
396
|
-
|
|
397
|
-
context "when name is defined in module Fruits" do
|
|
398
|
-
|
|
399
|
-
context "when inverse_class_name is defined in the same module" do
|
|
400
|
-
|
|
401
|
-
let(:inverse_class_name) do
|
|
402
|
-
"Fruits::Apple"
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
let(:name) do
|
|
406
|
-
"banana"
|
|
407
|
-
end
|
|
408
|
-
|
|
409
|
-
it "returns Fruits" do
|
|
410
|
-
expect(mod).to eq("Fruits")
|
|
411
|
-
end
|
|
412
|
-
end
|
|
413
|
-
|
|
414
|
-
context "when inverse_class_name is defined in a nested module" do
|
|
415
|
-
|
|
416
|
-
let(:inverse_class_name) do
|
|
417
|
-
"Fruits::Big::Ananas"
|
|
418
|
-
end
|
|
419
|
-
|
|
420
|
-
let(:name) do
|
|
421
|
-
"banana"
|
|
422
|
-
end
|
|
423
|
-
|
|
424
|
-
it "returns Fruits" do
|
|
425
|
-
expect(mod).to eq("Fruits")
|
|
426
|
-
end
|
|
427
|
-
end
|
|
428
|
-
|
|
429
|
-
context "when the inverse_class_name is defined in the root namespace" do
|
|
430
|
-
|
|
431
|
-
let(:inverse_class_name) do
|
|
432
|
-
"Person"
|
|
433
|
-
end
|
|
434
|
-
|
|
435
|
-
let(:name) do
|
|
436
|
-
"banana"
|
|
437
|
-
end
|
|
438
|
-
|
|
439
|
-
it "returns nil" do
|
|
440
|
-
expect(mod).to be_nil
|
|
441
|
-
end
|
|
442
|
-
end
|
|
443
|
-
end
|
|
444
|
-
end
|
|
445
|
-
end
|
|
446
|
-
end
|
|
447
|
-
|
|
448
|
-
describe "#class_name" do
|
|
449
|
-
|
|
450
|
-
context "when class_name provided" do
|
|
451
|
-
|
|
452
|
-
context "when the class name contains leading ::" do
|
|
453
|
-
|
|
454
|
-
let(:metadata) do
|
|
455
|
-
described_class.new(
|
|
456
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
457
|
-
class_name: "::Person"
|
|
458
|
-
)
|
|
459
|
-
end
|
|
460
|
-
|
|
461
|
-
it "returns the stripped class name" do
|
|
462
|
-
expect(metadata.class_name).to eq("Person")
|
|
463
|
-
end
|
|
464
|
-
end
|
|
465
|
-
|
|
466
|
-
context "when the class name has no prefix" do
|
|
467
|
-
|
|
468
|
-
let(:metadata) do
|
|
469
|
-
described_class.new(
|
|
470
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
471
|
-
class_name: "Person"
|
|
472
|
-
)
|
|
473
|
-
end
|
|
474
|
-
|
|
475
|
-
it "constantizes the class name" do
|
|
476
|
-
expect(metadata.class_name).to eq("Person")
|
|
477
|
-
end
|
|
478
|
-
end
|
|
479
|
-
end
|
|
480
|
-
|
|
481
|
-
context "when no class_name provided" do
|
|
482
|
-
|
|
483
|
-
context "when inverse_class_name is provided" do
|
|
484
|
-
|
|
485
|
-
context "when inverse_class_name is a simple class name" do
|
|
486
|
-
|
|
487
|
-
context "when association name is singular" do
|
|
488
|
-
|
|
489
|
-
let(:relation) do
|
|
490
|
-
Mongoid::Relations::Embedded::One
|
|
491
|
-
end
|
|
492
|
-
|
|
493
|
-
let(:metadata) do
|
|
494
|
-
described_class.new(name: :name, relation: relation, inverse_class_name: "Person")
|
|
495
|
-
end
|
|
496
|
-
|
|
497
|
-
it "classifies and constantizes the association name and adds the module" do
|
|
498
|
-
expect(metadata.class_name).to eq("Name")
|
|
499
|
-
end
|
|
500
|
-
end
|
|
501
|
-
|
|
502
|
-
context "when association name is plural" do
|
|
503
|
-
|
|
504
|
-
let(:relation) do
|
|
505
|
-
Mongoid::Relations::Embedded::Many
|
|
506
|
-
end
|
|
507
|
-
|
|
508
|
-
let(:metadata) do
|
|
509
|
-
described_class.new(name: :addresses, relation: relation, inverse_class_name: "Person")
|
|
510
|
-
end
|
|
511
|
-
|
|
512
|
-
it "classifies and constantizes the association name and adds the module" do
|
|
513
|
-
expect(metadata.class_name).to eq("Address")
|
|
514
|
-
end
|
|
515
|
-
end
|
|
516
|
-
|
|
517
|
-
end
|
|
518
|
-
|
|
519
|
-
context "when inverse_class_name is a class name in a module" do
|
|
520
|
-
|
|
521
|
-
context "when association name is singular" do
|
|
522
|
-
|
|
523
|
-
let(:relation) do
|
|
524
|
-
Mongoid::Relations::Embedded::One
|
|
525
|
-
end
|
|
526
|
-
|
|
527
|
-
let(:metadata) do
|
|
528
|
-
described_class.new(name: :apple, relation: relation, inverse_class_name: "Fruits::Banana")
|
|
529
|
-
end
|
|
530
|
-
|
|
531
|
-
it "classifies and constantizes the association name and adds the module" do
|
|
532
|
-
expect(metadata.class_name).to eq("Fruits::Apple")
|
|
533
|
-
end
|
|
534
|
-
end
|
|
535
|
-
|
|
536
|
-
context "when association name is plural" do
|
|
537
|
-
|
|
538
|
-
let(:relation) do
|
|
539
|
-
Mongoid::Relations::Embedded::Many
|
|
540
|
-
end
|
|
541
|
-
|
|
542
|
-
let(:metadata) do
|
|
543
|
-
described_class.new(name: :apples, relation: relation, inverse_class_name: "Fruits::Banana")
|
|
544
|
-
end
|
|
545
|
-
|
|
546
|
-
it "classifies and constantizes the association name and adds the module" do
|
|
547
|
-
expect(metadata.class_name).to eq("Fruits::Apple")
|
|
548
|
-
end
|
|
549
|
-
end
|
|
550
|
-
|
|
551
|
-
end
|
|
552
|
-
end
|
|
553
|
-
|
|
554
|
-
context "when no inverse_class_name is provided" do
|
|
555
|
-
|
|
556
|
-
context "when association name is singular" do
|
|
557
|
-
|
|
558
|
-
let(:relation) do
|
|
559
|
-
Mongoid::Relations::Embedded::One
|
|
560
|
-
end
|
|
561
|
-
|
|
562
|
-
let(:metadata) do
|
|
563
|
-
described_class.new(name: :name, relation: relation)
|
|
564
|
-
end
|
|
565
|
-
|
|
566
|
-
it "classifies and constantizes the association name" do
|
|
567
|
-
expect(metadata.class_name).to eq("Name")
|
|
568
|
-
end
|
|
569
|
-
end
|
|
570
|
-
|
|
571
|
-
context "when association name is plural" do
|
|
572
|
-
|
|
573
|
-
let(:relation) do
|
|
574
|
-
Mongoid::Relations::Embedded::Many
|
|
575
|
-
end
|
|
576
|
-
|
|
577
|
-
let(:metadata) do
|
|
578
|
-
described_class.new(name: :addresses, relation: relation)
|
|
579
|
-
end
|
|
580
|
-
|
|
581
|
-
it "classifies and constantizes the association name" do
|
|
582
|
-
expect(metadata.class_name).to eq("Address")
|
|
583
|
-
end
|
|
584
|
-
end
|
|
585
|
-
end
|
|
586
|
-
end
|
|
587
|
-
end
|
|
588
|
-
|
|
589
|
-
context "when the association is polymorphic" do
|
|
590
|
-
|
|
591
|
-
let(:metadata) do
|
|
592
|
-
described_class.new(
|
|
593
|
-
name: :ratable,
|
|
594
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
595
|
-
polymorphic: true,
|
|
596
|
-
inverse_class_name: "Rating"
|
|
597
|
-
)
|
|
598
|
-
end
|
|
599
|
-
|
|
600
|
-
it "returns the polymorphic class name" do
|
|
601
|
-
expect(metadata.class_name).to eq("Ratable")
|
|
602
|
-
end
|
|
603
|
-
end
|
|
604
|
-
|
|
605
|
-
describe "#destructive?" do
|
|
606
|
-
|
|
607
|
-
context "when the relation has a destructive dependent option" do
|
|
608
|
-
|
|
609
|
-
let(:metadata) do
|
|
610
|
-
described_class.new(
|
|
611
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
612
|
-
dependent: :destroy
|
|
613
|
-
)
|
|
614
|
-
end
|
|
615
|
-
|
|
616
|
-
it "returns true" do
|
|
617
|
-
expect(metadata).to be_destructive
|
|
618
|
-
end
|
|
619
|
-
end
|
|
620
|
-
|
|
621
|
-
context "when no dependent option" do
|
|
622
|
-
|
|
623
|
-
let(:metadata) do
|
|
624
|
-
described_class.new(
|
|
625
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
626
|
-
)
|
|
627
|
-
end
|
|
628
|
-
|
|
629
|
-
it "returns false" do
|
|
630
|
-
expect(metadata).to_not be_destructive
|
|
631
|
-
end
|
|
632
|
-
end
|
|
633
|
-
end
|
|
634
|
-
|
|
635
|
-
describe "#embedded?" do
|
|
636
|
-
|
|
637
|
-
context "when the relation is embedded" do
|
|
638
|
-
|
|
639
|
-
let(:metadata) do
|
|
640
|
-
described_class.new(
|
|
641
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
642
|
-
)
|
|
643
|
-
end
|
|
644
|
-
|
|
645
|
-
it "returns true" do
|
|
646
|
-
expect(metadata).to be_embedded
|
|
647
|
-
end
|
|
648
|
-
end
|
|
649
|
-
|
|
650
|
-
context "when the relation is not embedded" do
|
|
651
|
-
|
|
652
|
-
let(:metadata) do
|
|
653
|
-
described_class.new(
|
|
654
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
655
|
-
)
|
|
656
|
-
end
|
|
657
|
-
|
|
658
|
-
it "returns false" do
|
|
659
|
-
expect(metadata).to_not be_embedded
|
|
660
|
-
end
|
|
661
|
-
end
|
|
662
|
-
end
|
|
663
|
-
|
|
664
|
-
describe "#extension" do
|
|
665
|
-
|
|
666
|
-
let(:metadata) do
|
|
667
|
-
described_class.new(
|
|
668
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
669
|
-
extend: :value
|
|
670
|
-
)
|
|
671
|
-
end
|
|
672
|
-
|
|
673
|
-
it "returns the extend property" do
|
|
674
|
-
expect(metadata.extension).to eq(:value)
|
|
675
|
-
end
|
|
676
|
-
end
|
|
677
|
-
|
|
678
|
-
describe "#extension?" do
|
|
679
|
-
|
|
680
|
-
context "when an extends property exists" do
|
|
681
|
-
|
|
682
|
-
let(:metadata) do
|
|
683
|
-
described_class.new(
|
|
684
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
685
|
-
extend: :value
|
|
686
|
-
)
|
|
687
|
-
end
|
|
688
|
-
|
|
689
|
-
it "returns true" do
|
|
690
|
-
expect(metadata.extension?).to be true
|
|
691
|
-
end
|
|
692
|
-
end
|
|
693
|
-
|
|
694
|
-
context "when the extend option is nil" do
|
|
695
|
-
|
|
696
|
-
let(:metadata) do
|
|
697
|
-
described_class.new(
|
|
698
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
699
|
-
)
|
|
700
|
-
end
|
|
701
|
-
|
|
702
|
-
it "returns false" do
|
|
703
|
-
expect(metadata.extension?).to be false
|
|
704
|
-
end
|
|
705
|
-
end
|
|
706
|
-
end
|
|
707
|
-
|
|
708
|
-
describe "#foreign_key" do
|
|
709
|
-
|
|
710
|
-
context "when no foreign key was explicitly defined" do
|
|
711
|
-
|
|
712
|
-
context "when the relation stores a foreign key" do
|
|
713
|
-
|
|
714
|
-
context "when referenced in" do
|
|
715
|
-
|
|
716
|
-
context "when not providing a class name" do
|
|
717
|
-
|
|
718
|
-
let(:metadata) do
|
|
719
|
-
described_class.new(
|
|
720
|
-
name: :person,
|
|
721
|
-
relation: Mongoid::Relations::Referenced::In
|
|
722
|
-
)
|
|
723
|
-
end
|
|
724
|
-
|
|
725
|
-
it "returns the foreign_key" do
|
|
726
|
-
expect(metadata.foreign_key).to eq("person_id")
|
|
727
|
-
end
|
|
728
|
-
end
|
|
729
|
-
|
|
730
|
-
context "when providing a class name" do
|
|
731
|
-
|
|
732
|
-
let(:metadata) do
|
|
733
|
-
described_class.new(
|
|
734
|
-
name: :person,
|
|
735
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
736
|
-
class_name: "TheseAreNotTheDriods"
|
|
737
|
-
)
|
|
738
|
-
end
|
|
739
|
-
|
|
740
|
-
it "returns the foreign_key" do
|
|
741
|
-
expect(metadata.foreign_key).to eq("person_id")
|
|
742
|
-
end
|
|
743
|
-
end
|
|
744
|
-
|
|
745
|
-
context "when the class is namespaces" do
|
|
746
|
-
|
|
747
|
-
let(:metadata) do
|
|
748
|
-
described_class.new(
|
|
749
|
-
name: :apple,
|
|
750
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
751
|
-
class_name: "Fruits::Apple"
|
|
752
|
-
)
|
|
753
|
-
end
|
|
754
|
-
|
|
755
|
-
it "returns the foreign_key without the module name" do
|
|
756
|
-
expect(metadata.foreign_key).to eq("apple_id")
|
|
757
|
-
end
|
|
758
|
-
end
|
|
759
|
-
end
|
|
760
|
-
|
|
761
|
-
context "when references and referenced in many" do
|
|
762
|
-
|
|
763
|
-
let(:metadata) do
|
|
764
|
-
described_class.new(
|
|
765
|
-
name: :people,
|
|
766
|
-
relation: Mongoid::Relations::Referenced::ManyToMany
|
|
767
|
-
)
|
|
768
|
-
end
|
|
769
|
-
|
|
770
|
-
it "returns the foreign_key" do
|
|
771
|
-
expect(metadata.foreign_key).to eq("person_ids")
|
|
772
|
-
end
|
|
773
|
-
|
|
774
|
-
context "given a specific foreign key" do
|
|
775
|
-
|
|
776
|
-
let(:metadata) do
|
|
777
|
-
described_class.new(
|
|
778
|
-
name: :follower,
|
|
779
|
-
foreign_key: :follower_list,
|
|
780
|
-
relation: Mongoid::Relations::Referenced::ManyToMany
|
|
781
|
-
)
|
|
782
|
-
end
|
|
783
|
-
|
|
784
|
-
it "returns the foreign_key" do
|
|
785
|
-
expect(metadata.foreign_key).to eq("follower_list")
|
|
786
|
-
end
|
|
787
|
-
end
|
|
788
|
-
|
|
789
|
-
context "using name as foreign key" do
|
|
790
|
-
|
|
791
|
-
let(:metadata) do
|
|
792
|
-
described_class.new(
|
|
793
|
-
name: :followers,
|
|
794
|
-
class_name: "Person",
|
|
795
|
-
relation: Mongoid::Relations::Referenced::ManyToMany
|
|
796
|
-
)
|
|
797
|
-
end
|
|
798
|
-
|
|
799
|
-
it "returns the foreign_key" do
|
|
800
|
-
expect(metadata.foreign_key).to eq("follower_ids")
|
|
801
|
-
end
|
|
802
|
-
end
|
|
803
|
-
|
|
804
|
-
context "when the class is namespaced" do
|
|
805
|
-
|
|
806
|
-
let(:metadata) do
|
|
807
|
-
described_class.new(
|
|
808
|
-
name: :bananas,
|
|
809
|
-
relation: Mongoid::Relations::Referenced::ManyToMany,
|
|
810
|
-
inverse_class_name: "Fruits::Apple",
|
|
811
|
-
class_name: "Fruits::Banana"
|
|
812
|
-
)
|
|
813
|
-
end
|
|
814
|
-
|
|
815
|
-
it "returns the foreign_key without the module name" do
|
|
816
|
-
expect(metadata.foreign_key).to eq("banana_ids")
|
|
817
|
-
end
|
|
818
|
-
|
|
819
|
-
it "returns the inverse_foreign_key without the module name" do
|
|
820
|
-
expect(metadata.inverse_foreign_key).to eq("apple_ids")
|
|
821
|
-
end
|
|
822
|
-
end
|
|
823
|
-
end
|
|
824
|
-
end
|
|
825
|
-
|
|
826
|
-
context "when the relation does not store a foreign key" do
|
|
827
|
-
|
|
828
|
-
context "when references one" do
|
|
829
|
-
|
|
830
|
-
let(:metadata) do
|
|
831
|
-
described_class.new(
|
|
832
|
-
name: :post,
|
|
833
|
-
relation: Mongoid::Relations::Referenced::One,
|
|
834
|
-
inverse_class_name: "Person"
|
|
835
|
-
)
|
|
836
|
-
end
|
|
837
|
-
|
|
838
|
-
it "returns the inverse foreign key" do
|
|
839
|
-
expect(metadata.foreign_key).to eq("person_id")
|
|
840
|
-
end
|
|
841
|
-
end
|
|
842
|
-
|
|
843
|
-
context "when references many" do
|
|
844
|
-
|
|
845
|
-
context "when an inverse_of is defined" do
|
|
846
|
-
|
|
847
|
-
let(:metadata) do
|
|
848
|
-
described_class.new(
|
|
849
|
-
name: :created_streets,
|
|
850
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
851
|
-
inverse_class_name: "House",
|
|
852
|
-
inverse_of: :creator
|
|
853
|
-
)
|
|
854
|
-
end
|
|
855
|
-
|
|
856
|
-
it "returns the inverse_of plus suffix" do
|
|
857
|
-
expect(metadata.foreign_key).to eq("creator_id")
|
|
858
|
-
end
|
|
859
|
-
end
|
|
860
|
-
|
|
861
|
-
context "when the class is not namespaced" do
|
|
862
|
-
|
|
863
|
-
let(:metadata) do
|
|
864
|
-
described_class.new(
|
|
865
|
-
name: :posts,
|
|
866
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
867
|
-
inverse_class_name: "Person"
|
|
868
|
-
)
|
|
869
|
-
end
|
|
870
|
-
|
|
871
|
-
it "returns the inverse foreign key" do
|
|
872
|
-
expect(metadata.foreign_key).to eq("person_id")
|
|
873
|
-
end
|
|
874
|
-
end
|
|
875
|
-
|
|
876
|
-
context "when the class is namespaced" do
|
|
877
|
-
|
|
878
|
-
context "when name doesnt include namespace" do
|
|
879
|
-
|
|
880
|
-
let(:metadata) do
|
|
881
|
-
described_class.new(
|
|
882
|
-
name: :bananas,
|
|
883
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
884
|
-
inverse_class_name: "Fruits::Apple",
|
|
885
|
-
class_name: "Fruits::Banana"
|
|
886
|
-
)
|
|
887
|
-
end
|
|
888
|
-
|
|
889
|
-
it "returns the foreign_key without the module name" do
|
|
890
|
-
expect(metadata.foreign_key).to eq("apple_id")
|
|
891
|
-
end
|
|
892
|
-
end
|
|
893
|
-
|
|
894
|
-
context "when name include namespace" do
|
|
895
|
-
|
|
896
|
-
let(:metadata) do
|
|
897
|
-
described_class.new(
|
|
898
|
-
name: :fruits_melons,
|
|
899
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
900
|
-
inverse_class_name: "Fruits::Apple",
|
|
901
|
-
class_name: "Fruits::Melon"
|
|
902
|
-
)
|
|
903
|
-
end
|
|
904
|
-
|
|
905
|
-
it "returns the foreign_key with the module name" do
|
|
906
|
-
expect(metadata.foreign_key).to eq("fruit_apple_id")
|
|
907
|
-
end
|
|
908
|
-
end
|
|
909
|
-
end
|
|
910
|
-
end
|
|
911
|
-
|
|
912
|
-
context "when embeds one" do
|
|
913
|
-
let(:metadata) do
|
|
914
|
-
described_class.new(
|
|
915
|
-
name: :post,
|
|
916
|
-
relation: Mongoid::Relations::Embedded::One,
|
|
917
|
-
inverse_class_name: "Person"
|
|
918
|
-
)
|
|
919
|
-
end
|
|
920
|
-
|
|
921
|
-
it "returns a nil foreign key" do
|
|
922
|
-
expect(metadata.foreign_key).to be_nil
|
|
923
|
-
end
|
|
924
|
-
end
|
|
925
|
-
end
|
|
926
|
-
end
|
|
927
|
-
|
|
928
|
-
context "when a foreign_key was defined" do
|
|
929
|
-
|
|
930
|
-
let(:metadata) do
|
|
931
|
-
described_class.new(
|
|
932
|
-
name: :person,
|
|
933
|
-
relation: Mongoid::Relations::Referenced::ManyToMany,
|
|
934
|
-
foreign_key: "blog_post_id"
|
|
935
|
-
)
|
|
936
|
-
end
|
|
937
|
-
|
|
938
|
-
it "returns the foreign_key" do
|
|
939
|
-
expect(metadata.foreign_key).to eq("blog_post_id")
|
|
940
|
-
end
|
|
941
|
-
end
|
|
942
|
-
end
|
|
943
|
-
|
|
944
|
-
describe "#foreign_key_default" do
|
|
945
|
-
|
|
946
|
-
context "when the relation stores keys in an array" do
|
|
947
|
-
|
|
948
|
-
let(:metadata) do
|
|
949
|
-
described_class.new(
|
|
950
|
-
name: :preferences,
|
|
951
|
-
relation: Mongoid::Relations::Referenced::ManyToMany,
|
|
952
|
-
foreign_key: "preference_ids"
|
|
953
|
-
)
|
|
954
|
-
end
|
|
955
|
-
|
|
956
|
-
it "returns an empty array" do
|
|
957
|
-
expect(metadata.foreign_key_default).to be_empty
|
|
958
|
-
end
|
|
959
|
-
end
|
|
960
|
-
|
|
961
|
-
context "when the relation stores a single key" do
|
|
962
|
-
|
|
963
|
-
let(:metadata) do
|
|
964
|
-
described_class.new(
|
|
965
|
-
name: :person,
|
|
966
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
967
|
-
foreign_key: "person_id"
|
|
968
|
-
)
|
|
969
|
-
end
|
|
970
|
-
|
|
971
|
-
it "returns an empty array" do
|
|
972
|
-
expect(metadata.foreign_key_default).to be_nil
|
|
973
|
-
end
|
|
974
|
-
end
|
|
975
|
-
end
|
|
976
|
-
|
|
977
|
-
describe "#foreign_key_setter" do
|
|
978
|
-
|
|
979
|
-
context "when the relation is not polymorphic" do
|
|
980
|
-
|
|
981
|
-
let(:metadata) do
|
|
982
|
-
described_class.new(
|
|
983
|
-
name: :person,
|
|
984
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
985
|
-
foreign_key: "person_id"
|
|
986
|
-
)
|
|
987
|
-
end
|
|
988
|
-
|
|
989
|
-
it "returns the foreign_key plus =" do
|
|
990
|
-
expect(metadata.foreign_key_setter).to eq("person_id=")
|
|
991
|
-
end
|
|
992
|
-
end
|
|
993
|
-
|
|
994
|
-
context "when the relation is polymorphic" do
|
|
995
|
-
|
|
996
|
-
let(:metadata) do
|
|
997
|
-
described_class.new(
|
|
998
|
-
name: :ratings,
|
|
999
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
1000
|
-
as: :ratable,
|
|
1001
|
-
inverse_class_name: "Movie"
|
|
1002
|
-
)
|
|
1003
|
-
end
|
|
1004
|
-
|
|
1005
|
-
it "returns the polymorphic foreign_key plus =" do
|
|
1006
|
-
expect(metadata.foreign_key_setter).to eq("ratable_id=")
|
|
1007
|
-
end
|
|
1008
|
-
end
|
|
1009
|
-
end
|
|
1010
|
-
|
|
1011
|
-
describe "#inverse_type" do
|
|
1012
|
-
|
|
1013
|
-
context "when the relation is not polymorphic" do
|
|
1014
|
-
|
|
1015
|
-
let(:metadata) do
|
|
1016
|
-
described_class.new(
|
|
1017
|
-
name: :person,
|
|
1018
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1019
|
-
foreign_key: "person_id"
|
|
1020
|
-
)
|
|
1021
|
-
end
|
|
1022
|
-
|
|
1023
|
-
it "returns nil" do
|
|
1024
|
-
expect(metadata.inverse_type).to be_nil
|
|
1025
|
-
end
|
|
1026
|
-
end
|
|
1027
|
-
|
|
1028
|
-
context "when the relation is polymorphic" do
|
|
1029
|
-
|
|
1030
|
-
let(:metadata) do
|
|
1031
|
-
described_class.new(
|
|
1032
|
-
name: :ratable,
|
|
1033
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1034
|
-
polymorphic: true,
|
|
1035
|
-
inverse_class_name: "Rating"
|
|
1036
|
-
)
|
|
1037
|
-
end
|
|
1038
|
-
|
|
1039
|
-
it "returns the polymorphic name plus type" do
|
|
1040
|
-
expect(metadata.inverse_type).to eq("ratable_type")
|
|
1041
|
-
end
|
|
1042
|
-
end
|
|
1043
|
-
end
|
|
1044
|
-
|
|
1045
|
-
describe "#inverse_type_setter" do
|
|
1046
|
-
|
|
1047
|
-
context "when the relation is not polymorphic" do
|
|
1048
|
-
|
|
1049
|
-
let(:metadata) do
|
|
1050
|
-
described_class.new(
|
|
1051
|
-
name: :person,
|
|
1052
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1053
|
-
foreign_key: "person_id"
|
|
1054
|
-
)
|
|
1055
|
-
end
|
|
1056
|
-
|
|
1057
|
-
it "returns nil" do
|
|
1058
|
-
expect(metadata.inverse_type_setter).to be_nil
|
|
1059
|
-
end
|
|
1060
|
-
end
|
|
1061
|
-
|
|
1062
|
-
context "when the relation is polymorphic" do
|
|
1063
|
-
|
|
1064
|
-
let(:metadata) do
|
|
1065
|
-
described_class.new(
|
|
1066
|
-
name: :ratable,
|
|
1067
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1068
|
-
polymorphic: true,
|
|
1069
|
-
inverse_class_name: "Rating"
|
|
1070
|
-
)
|
|
1071
|
-
end
|
|
1072
|
-
|
|
1073
|
-
it "returns the inverse type plus =" do
|
|
1074
|
-
expect(metadata.inverse_type_setter).to eq("ratable_type=")
|
|
1075
|
-
end
|
|
1076
|
-
end
|
|
1077
|
-
end
|
|
1078
|
-
|
|
1079
|
-
describe "#inverses" do
|
|
1080
|
-
|
|
1081
|
-
context "when the relation is polymorphic" do
|
|
1082
|
-
|
|
1083
|
-
context "when an inverse_of is defined" do
|
|
1084
|
-
|
|
1085
|
-
let(:metadata) do
|
|
1086
|
-
described_class.new(
|
|
1087
|
-
name: :ratable,
|
|
1088
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1089
|
-
polymorphic: true,
|
|
1090
|
-
inverse_of: "my_ratings"
|
|
1091
|
-
)
|
|
1092
|
-
end
|
|
1093
|
-
|
|
1094
|
-
it "returns only the inverse_of" do
|
|
1095
|
-
expect(metadata.inverses(nil)).to eq([ "my_ratings" ])
|
|
1096
|
-
end
|
|
1097
|
-
end
|
|
1098
|
-
end
|
|
1099
|
-
end
|
|
1100
|
-
|
|
1101
|
-
describe "#indexed?" do
|
|
1102
|
-
|
|
1103
|
-
context "when an index property exists" do
|
|
1104
|
-
|
|
1105
|
-
let(:metadata) do
|
|
1106
|
-
described_class.new(
|
|
1107
|
-
index: true,
|
|
1108
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1109
|
-
)
|
|
1110
|
-
end
|
|
1111
|
-
|
|
1112
|
-
it "returns true" do
|
|
1113
|
-
expect(metadata.indexed?).to be true
|
|
1114
|
-
end
|
|
1115
|
-
end
|
|
1116
|
-
|
|
1117
|
-
context "when the index option is nil" do
|
|
1118
|
-
|
|
1119
|
-
let(:metadata) do
|
|
1120
|
-
described_class.new(
|
|
1121
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1122
|
-
)
|
|
1123
|
-
end
|
|
1124
|
-
|
|
1125
|
-
it "returns false" do
|
|
1126
|
-
expect(metadata.indexed?).to be false
|
|
1127
|
-
end
|
|
1128
|
-
end
|
|
1129
|
-
|
|
1130
|
-
context "when the index option is false" do
|
|
1131
|
-
|
|
1132
|
-
let(:metadata) do
|
|
1133
|
-
described_class.new(
|
|
1134
|
-
index: false,
|
|
1135
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1136
|
-
)
|
|
1137
|
-
end
|
|
1138
|
-
|
|
1139
|
-
it "returns false" do
|
|
1140
|
-
expect(metadata.indexed?).to be false
|
|
1141
|
-
end
|
|
1142
|
-
end
|
|
1143
|
-
end
|
|
1144
|
-
|
|
1145
|
-
describe "#inverse" do
|
|
1146
|
-
|
|
1147
|
-
context "when an inverse relation exists" do
|
|
1148
|
-
|
|
1149
|
-
context "when the inverse has a foreign key match" do
|
|
1150
|
-
|
|
1151
|
-
let(:metadata) do
|
|
1152
|
-
User.reflect_on_association(:account)
|
|
1153
|
-
end
|
|
1154
|
-
|
|
1155
|
-
it "returns the name of the inverse with the matching foreign_key" do
|
|
1156
|
-
expect(metadata.inverse).to eq(:creator)
|
|
1157
|
-
end
|
|
1158
|
-
end
|
|
1159
|
-
|
|
1160
|
-
context "when multiple relations against the same class exist" do
|
|
1161
|
-
|
|
1162
|
-
context "when not polymorphic" do
|
|
1163
|
-
|
|
1164
|
-
let(:metadata) do
|
|
1165
|
-
described_class.new(
|
|
1166
|
-
inverse_class_name: "User",
|
|
1167
|
-
name: :shop,
|
|
1168
|
-
relation: Mongoid::Relations::Referenced::One
|
|
1169
|
-
)
|
|
1170
|
-
end
|
|
1171
|
-
|
|
1172
|
-
it "returns the name of the inverse with the matching inverse of" do
|
|
1173
|
-
expect(metadata.inverse).to eq(:user)
|
|
1174
|
-
end
|
|
1175
|
-
end
|
|
1176
|
-
|
|
1177
|
-
context "when polymorphic" do
|
|
1178
|
-
|
|
1179
|
-
let(:metadata) do
|
|
1180
|
-
described_class.new(
|
|
1181
|
-
name: :left_eye,
|
|
1182
|
-
as: :eyeable,
|
|
1183
|
-
inverse_class_name: "Face",
|
|
1184
|
-
class_name: "Eye",
|
|
1185
|
-
relation: Mongoid::Relations::Referenced::One
|
|
1186
|
-
)
|
|
1187
|
-
end
|
|
1188
|
-
|
|
1189
|
-
it "returns the name of the relation" do
|
|
1190
|
-
expect(metadata.inverse).to eq(:eyeable)
|
|
1191
|
-
end
|
|
1192
|
-
end
|
|
1193
|
-
|
|
1194
|
-
context "when polymorphic on the child" do
|
|
1195
|
-
|
|
1196
|
-
let(:metadata) do
|
|
1197
|
-
described_class.new(
|
|
1198
|
-
name: :eyeable,
|
|
1199
|
-
polymorphic: true,
|
|
1200
|
-
inverse_class_name: "Eye",
|
|
1201
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1202
|
-
)
|
|
1203
|
-
end
|
|
1204
|
-
|
|
1205
|
-
it "returns nil" do
|
|
1206
|
-
expect(metadata.inverse(Face.new)).to be_nil
|
|
1207
|
-
end
|
|
1208
|
-
end
|
|
1209
|
-
end
|
|
1210
|
-
|
|
1211
|
-
context "when inverse_of is defined" do
|
|
1212
|
-
|
|
1213
|
-
context "when inverse_of is nil" do
|
|
1214
|
-
|
|
1215
|
-
let(:metadata) do
|
|
1216
|
-
described_class.new(
|
|
1217
|
-
inverse_of: nil,
|
|
1218
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1219
|
-
)
|
|
1220
|
-
end
|
|
1221
|
-
|
|
1222
|
-
it "returns nil" do
|
|
1223
|
-
expect(metadata.inverse).to be_nil
|
|
1224
|
-
end
|
|
1225
|
-
end
|
|
1226
|
-
|
|
1227
|
-
context "when inverse_of is a symbol" do
|
|
1228
|
-
|
|
1229
|
-
let(:metadata) do
|
|
1230
|
-
described_class.new(
|
|
1231
|
-
inverse_of: :crazy_name,
|
|
1232
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1233
|
-
)
|
|
1234
|
-
end
|
|
1235
|
-
|
|
1236
|
-
it "returns the name of the inverse_of property" do
|
|
1237
|
-
expect(metadata.inverse).to eq(:crazy_name)
|
|
1238
|
-
end
|
|
1239
|
-
end
|
|
1240
|
-
end
|
|
1241
|
-
|
|
1242
|
-
context "when not polymorphic" do
|
|
1243
|
-
|
|
1244
|
-
let(:metadata) do
|
|
1245
|
-
described_class.new(
|
|
1246
|
-
name: :pet,
|
|
1247
|
-
class_name: "Animal",
|
|
1248
|
-
inverse_class_name: "Person",
|
|
1249
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1250
|
-
)
|
|
1251
|
-
end
|
|
1252
|
-
|
|
1253
|
-
it "returns the name of the relation" do
|
|
1254
|
-
expect(metadata.inverse).to eq(:person)
|
|
1255
|
-
end
|
|
1256
|
-
end
|
|
1257
|
-
|
|
1258
|
-
context "when polymorphic" do
|
|
1259
|
-
|
|
1260
|
-
let(:metadata) do
|
|
1261
|
-
described_class.new(
|
|
1262
|
-
name: :addresses,
|
|
1263
|
-
as: :addressable,
|
|
1264
|
-
inverse_class_name: "Person",
|
|
1265
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
1266
|
-
)
|
|
1267
|
-
end
|
|
1268
|
-
|
|
1269
|
-
it "returns the name of the relation" do
|
|
1270
|
-
expect(metadata.inverse).to eq(:addressable)
|
|
1271
|
-
end
|
|
1272
|
-
end
|
|
1273
|
-
|
|
1274
|
-
context "when polymorphic on the child" do
|
|
1275
|
-
|
|
1276
|
-
let(:metadata) do
|
|
1277
|
-
described_class.new(
|
|
1278
|
-
name: :addressable,
|
|
1279
|
-
polymorphic: true,
|
|
1280
|
-
inverse_class_name: "Address",
|
|
1281
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1282
|
-
)
|
|
1283
|
-
end
|
|
1284
|
-
|
|
1285
|
-
it "returns the name of the relation" do
|
|
1286
|
-
expect(metadata.inverse(Person.new)).to eq(:addresses)
|
|
1287
|
-
end
|
|
1288
|
-
end
|
|
1289
|
-
|
|
1290
|
-
context "when in a cyclic relation" do
|
|
1291
|
-
|
|
1292
|
-
context "when the base name is included in the plural form" do
|
|
1293
|
-
|
|
1294
|
-
let(:metadata) do
|
|
1295
|
-
described_class.new(
|
|
1296
|
-
name: :parent_role,
|
|
1297
|
-
class_name: "Role",
|
|
1298
|
-
inverse_class_name: "Role",
|
|
1299
|
-
relation: Mongoid::Relations::Embedded::In,
|
|
1300
|
-
cyclic: true
|
|
1301
|
-
)
|
|
1302
|
-
end
|
|
1303
|
-
|
|
1304
|
-
it "returns the name of the relation" do
|
|
1305
|
-
expect(metadata.inverse(Role.new)).to eq(:child_roles)
|
|
1306
|
-
end
|
|
1307
|
-
end
|
|
1308
|
-
|
|
1309
|
-
context "when the base name is not included in the plural form" do
|
|
1310
|
-
|
|
1311
|
-
let(:metadata) do
|
|
1312
|
-
described_class.new(
|
|
1313
|
-
name: :parent_entry,
|
|
1314
|
-
class_name: "Entry",
|
|
1315
|
-
inverse_class_name: "Entry",
|
|
1316
|
-
relation: Mongoid::Relations::Embedded::In,
|
|
1317
|
-
cyclic: true
|
|
1318
|
-
)
|
|
1319
|
-
end
|
|
1320
|
-
|
|
1321
|
-
it "returns the name of the relation" do
|
|
1322
|
-
expect(metadata.inverse(Entry.new)).to eq(:child_entries)
|
|
1323
|
-
end
|
|
1324
|
-
end
|
|
1325
|
-
end
|
|
1326
|
-
end
|
|
1327
|
-
end
|
|
1328
|
-
|
|
1329
|
-
context "#inverse_foreign_key" do
|
|
1330
|
-
|
|
1331
|
-
context "when the inverse foreign key is not defined" do
|
|
1332
|
-
|
|
1333
|
-
let(:metadata) do
|
|
1334
|
-
described_class.new(
|
|
1335
|
-
name: :preferences,
|
|
1336
|
-
index: true,
|
|
1337
|
-
dependent: :nullify,
|
|
1338
|
-
relation: Mongoid::Relations::Referenced::ManyToMany,
|
|
1339
|
-
inverse_class_name: "Person"
|
|
1340
|
-
)
|
|
1341
|
-
end
|
|
1342
|
-
|
|
1343
|
-
it "returns the inverse class name plus suffix" do
|
|
1344
|
-
expect(metadata.inverse_foreign_key).to eq("person_ids")
|
|
1345
|
-
end
|
|
1346
|
-
end
|
|
1347
|
-
|
|
1348
|
-
context "when the inverse_of is nil" do
|
|
1349
|
-
|
|
1350
|
-
let(:metadata) do
|
|
1351
|
-
described_class.new(
|
|
1352
|
-
name: :blogs,
|
|
1353
|
-
class_name: "Blog",
|
|
1354
|
-
relation: Mongoid::Relations::Referenced::ManyToMany,
|
|
1355
|
-
inverse_of: nil
|
|
1356
|
-
)
|
|
1357
|
-
end
|
|
1358
|
-
|
|
1359
|
-
it "returns nil" do
|
|
1360
|
-
expect(metadata.inverse_foreign_key).to be_nil
|
|
1361
|
-
end
|
|
1362
|
-
end
|
|
1363
|
-
end
|
|
1364
|
-
|
|
1365
|
-
context "#inverse_klass" do
|
|
1366
|
-
|
|
1367
|
-
let(:metadata) do
|
|
1368
|
-
described_class.new(
|
|
1369
|
-
inverse_class_name: "Person",
|
|
1370
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1371
|
-
)
|
|
1372
|
-
end
|
|
1373
|
-
|
|
1374
|
-
it "constantizes the inverse_class_name" do
|
|
1375
|
-
expect(metadata.inverse_klass).to eq(Person)
|
|
1376
|
-
end
|
|
1377
|
-
end
|
|
1378
|
-
|
|
1379
|
-
context "#inverse_setter" do
|
|
1380
|
-
|
|
1381
|
-
context "when the relation is not polymorphic" do
|
|
1382
|
-
|
|
1383
|
-
let(:metadata) do
|
|
1384
|
-
described_class.new(
|
|
1385
|
-
name: :pet,
|
|
1386
|
-
class_name: "Animal",
|
|
1387
|
-
inverse_class_name: "Person",
|
|
1388
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1389
|
-
)
|
|
1390
|
-
end
|
|
1391
|
-
|
|
1392
|
-
it "returns a string for the setter" do
|
|
1393
|
-
expect(metadata.inverse_setter).to eq("person=")
|
|
1394
|
-
end
|
|
1395
|
-
end
|
|
1396
|
-
|
|
1397
|
-
context "when the relation is polymorphic" do
|
|
1398
|
-
|
|
1399
|
-
context "when multiple relations against the same class exist" do
|
|
1400
|
-
|
|
1401
|
-
context "when a referenced in" do
|
|
1402
|
-
|
|
1403
|
-
let(:metadata) do
|
|
1404
|
-
described_class.new(
|
|
1405
|
-
name: :eyeable,
|
|
1406
|
-
polymorphic: true,
|
|
1407
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1408
|
-
)
|
|
1409
|
-
end
|
|
1410
|
-
|
|
1411
|
-
let(:other) do
|
|
1412
|
-
Face.new
|
|
1413
|
-
end
|
|
1414
|
-
|
|
1415
|
-
it "returns nil" do
|
|
1416
|
-
expect(metadata.inverse_setter(other)).to be_nil
|
|
1417
|
-
end
|
|
1418
|
-
end
|
|
1419
|
-
|
|
1420
|
-
context "when a references many" do
|
|
1421
|
-
|
|
1422
|
-
let(:metadata) do
|
|
1423
|
-
described_class.new(
|
|
1424
|
-
name: :blue_eyes,
|
|
1425
|
-
inverse_class_name: "EyeBowl",
|
|
1426
|
-
as: :eyeable,
|
|
1427
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
1428
|
-
)
|
|
1429
|
-
end
|
|
1430
|
-
|
|
1431
|
-
it "returns a string for the setter" do
|
|
1432
|
-
expect(metadata.inverse_setter).to eq("eyeable=")
|
|
1433
|
-
end
|
|
1434
|
-
end
|
|
1435
|
-
end
|
|
1436
|
-
|
|
1437
|
-
context "when a referenced in" do
|
|
1438
|
-
|
|
1439
|
-
let(:metadata) do
|
|
1440
|
-
described_class.new(
|
|
1441
|
-
name: :ratable,
|
|
1442
|
-
inverse_class_name: "Rating",
|
|
1443
|
-
polymorphic: true,
|
|
1444
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1445
|
-
)
|
|
1446
|
-
end
|
|
1447
|
-
|
|
1448
|
-
let(:other) do
|
|
1449
|
-
Movie.new
|
|
1450
|
-
end
|
|
1451
|
-
|
|
1452
|
-
it "returns a string for the setter" do
|
|
1453
|
-
expect(metadata.inverse_setter(other)).to eq("ratings=")
|
|
1454
|
-
end
|
|
1455
|
-
end
|
|
1456
|
-
|
|
1457
|
-
context "when a references many" do
|
|
1458
|
-
|
|
1459
|
-
let(:metadata) do
|
|
1460
|
-
described_class.new(
|
|
1461
|
-
name: :ratings,
|
|
1462
|
-
inverse_class_name: "Rating",
|
|
1463
|
-
as: :ratable,
|
|
1464
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
1465
|
-
)
|
|
1466
|
-
end
|
|
1467
|
-
|
|
1468
|
-
it "returns a string for the setter" do
|
|
1469
|
-
expect(metadata.inverse_setter).to eq("ratable=")
|
|
1470
|
-
end
|
|
1471
|
-
end
|
|
1472
|
-
end
|
|
1473
|
-
end
|
|
1474
|
-
|
|
1475
|
-
context "#key" do
|
|
1476
|
-
|
|
1477
|
-
context "when relation is embedded" do
|
|
1478
|
-
|
|
1479
|
-
let(:metadata) do
|
|
1480
|
-
described_class.new(
|
|
1481
|
-
name: :addresses,
|
|
1482
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
1483
|
-
)
|
|
1484
|
-
end
|
|
1485
|
-
|
|
1486
|
-
it "returns the name as a string" do
|
|
1487
|
-
expect(metadata.key).to eq("addresses")
|
|
1488
|
-
end
|
|
1489
|
-
|
|
1490
|
-
context "with a store_as option defined" do
|
|
1491
|
-
|
|
1492
|
-
let(:metadata) do
|
|
1493
|
-
described_class.new(
|
|
1494
|
-
name: :comment,
|
|
1495
|
-
relation: Mongoid::Relations::Embedded::Many,
|
|
1496
|
-
store_as: "user_comments"
|
|
1497
|
-
)
|
|
1498
|
-
end
|
|
1499
|
-
|
|
1500
|
-
it "return the name define by store_as option" do
|
|
1501
|
-
expect(metadata.key).to eq("user_comments")
|
|
1502
|
-
end
|
|
1503
|
-
end
|
|
1504
|
-
end
|
|
1505
|
-
|
|
1506
|
-
context "when relation is referenced" do
|
|
1507
|
-
|
|
1508
|
-
context "when relation stores foreign_key" do
|
|
1509
|
-
|
|
1510
|
-
context "when the relation is not polymorphic" do
|
|
1511
|
-
|
|
1512
|
-
let(:metadata) do
|
|
1513
|
-
described_class.new(
|
|
1514
|
-
name: :posts,
|
|
1515
|
-
relation: Mongoid::Relations::Referenced::ManyToMany
|
|
1516
|
-
)
|
|
1517
|
-
end
|
|
1518
|
-
|
|
1519
|
-
it "returns the foreign_key" do
|
|
1520
|
-
expect(metadata.key).to eq("post_ids")
|
|
1521
|
-
end
|
|
1522
|
-
end
|
|
1523
|
-
|
|
1524
|
-
context "when the relation is polymorphic" do
|
|
1525
|
-
|
|
1526
|
-
let(:metadata) do
|
|
1527
|
-
described_class.new(
|
|
1528
|
-
name: :ratable,
|
|
1529
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1530
|
-
polymorphic: true
|
|
1531
|
-
)
|
|
1532
|
-
end
|
|
1533
|
-
|
|
1534
|
-
it "returns the polymorphic foreign_key" do
|
|
1535
|
-
expect(metadata.key).to eq("ratable_id")
|
|
1536
|
-
end
|
|
1537
|
-
end
|
|
1538
|
-
end
|
|
1539
|
-
|
|
1540
|
-
context "when relation does not store a foreign_key" do
|
|
1541
|
-
|
|
1542
|
-
let(:metadata) do
|
|
1543
|
-
described_class.new(
|
|
1544
|
-
name: :addresses,
|
|
1545
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
1546
|
-
inverse_class_name: "Person"
|
|
1547
|
-
)
|
|
1548
|
-
end
|
|
1549
|
-
|
|
1550
|
-
it "returns _id" do
|
|
1551
|
-
expect(metadata.key).to eq("_id")
|
|
1552
|
-
end
|
|
1553
|
-
end
|
|
1554
|
-
end
|
|
1555
|
-
end
|
|
1556
|
-
|
|
1557
|
-
describe "#options" do
|
|
1558
|
-
|
|
1559
|
-
let(:metadata) do
|
|
1560
|
-
described_class.new(
|
|
1561
|
-
order: :rating.asc,
|
|
1562
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
1563
|
-
)
|
|
1564
|
-
end
|
|
1565
|
-
|
|
1566
|
-
it "returns self" do
|
|
1567
|
-
expect(metadata.options).to eq(metadata)
|
|
1568
|
-
end
|
|
1569
|
-
end
|
|
1570
|
-
|
|
1571
|
-
describe "#order" do
|
|
1572
|
-
|
|
1573
|
-
let(:metadata) do
|
|
1574
|
-
described_class.new(
|
|
1575
|
-
order: :rating.asc,
|
|
1576
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
1577
|
-
)
|
|
1578
|
-
end
|
|
1579
|
-
|
|
1580
|
-
it "returns order criteria" do
|
|
1581
|
-
expect(metadata.order).to eq(:rating.asc)
|
|
1582
|
-
end
|
|
1583
|
-
end
|
|
1584
|
-
|
|
1585
|
-
describe "#klass" do
|
|
1586
|
-
|
|
1587
|
-
context "when the class name is not namespaced" do
|
|
1588
|
-
|
|
1589
|
-
let(:metadata) do
|
|
1590
|
-
described_class.new(
|
|
1591
|
-
class_name: "Address",
|
|
1592
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
1593
|
-
)
|
|
1594
|
-
end
|
|
1595
|
-
|
|
1596
|
-
it "constantizes the class_name" do
|
|
1597
|
-
expect(metadata.klass).to eq(Address)
|
|
1598
|
-
end
|
|
1599
|
-
end
|
|
1600
|
-
|
|
1601
|
-
context "when the class name is prepended with ::" do
|
|
1602
|
-
|
|
1603
|
-
let(:metadata) do
|
|
1604
|
-
described_class.new(
|
|
1605
|
-
class_name: "::Address",
|
|
1606
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
1607
|
-
)
|
|
1608
|
-
end
|
|
1609
|
-
|
|
1610
|
-
it "returns the class" do
|
|
1611
|
-
expect(metadata.klass).to eq(Address)
|
|
1612
|
-
end
|
|
1613
|
-
end
|
|
1614
|
-
end
|
|
1615
|
-
|
|
1616
|
-
describe "#many?" do
|
|
1617
|
-
|
|
1618
|
-
context "when the relation is a many" do
|
|
1619
|
-
|
|
1620
|
-
let(:metadata) do
|
|
1621
|
-
described_class.new(relation: Mongoid::Relations::Embedded::Many)
|
|
1622
|
-
end
|
|
1623
|
-
|
|
1624
|
-
it "returns true" do
|
|
1625
|
-
expect(metadata).to be_many
|
|
1626
|
-
end
|
|
1627
|
-
end
|
|
1628
|
-
|
|
1629
|
-
context "when the relation is not a many" do
|
|
1630
|
-
|
|
1631
|
-
let(:metadata) do
|
|
1632
|
-
described_class.new(relation: Mongoid::Relations::Embedded::One)
|
|
1633
|
-
end
|
|
1634
|
-
|
|
1635
|
-
it "returns false" do
|
|
1636
|
-
expect(metadata).to_not be_many
|
|
1637
|
-
end
|
|
1638
|
-
end
|
|
1639
|
-
end
|
|
1640
|
-
|
|
1641
|
-
describe "#macro" do
|
|
1642
|
-
|
|
1643
|
-
let(:metadata) do
|
|
1644
|
-
described_class.new(relation: Mongoid::Relations::Embedded::One)
|
|
1645
|
-
end
|
|
1646
|
-
|
|
1647
|
-
it "returns the macro from the relation" do
|
|
1648
|
-
expect(metadata.macro).to eq(:embeds_one)
|
|
1649
|
-
end
|
|
1650
|
-
end
|
|
1651
|
-
|
|
1652
|
-
describe "#nested_builder" do
|
|
1653
|
-
|
|
1654
|
-
let(:metadata) do
|
|
1655
|
-
described_class.new(relation: Mongoid::Relations::Embedded::One)
|
|
1656
|
-
end
|
|
1657
|
-
|
|
1658
|
-
let(:attributes) do
|
|
1659
|
-
{}
|
|
1660
|
-
end
|
|
1661
|
-
|
|
1662
|
-
let(:options) do
|
|
1663
|
-
{}
|
|
1664
|
-
end
|
|
1665
|
-
|
|
1666
|
-
it "returns the nested builder from the relation" do
|
|
1667
|
-
expect(
|
|
1668
|
-
metadata.nested_builder(attributes, options)
|
|
1669
|
-
).to be_a_kind_of(Mongoid::Relations::Builders::NestedAttributes::One)
|
|
1670
|
-
end
|
|
1671
|
-
end
|
|
1672
|
-
|
|
1673
|
-
describe "#primary_key" do
|
|
1674
|
-
|
|
1675
|
-
context "when no primary key exists" do
|
|
1676
|
-
|
|
1677
|
-
let(:metadata) do
|
|
1678
|
-
described_class.new(
|
|
1679
|
-
name: :person,
|
|
1680
|
-
inverse_class_name: "Person",
|
|
1681
|
-
relation: Mongoid::Relations::Referenced::In
|
|
1682
|
-
)
|
|
1683
|
-
end
|
|
1684
|
-
|
|
1685
|
-
it "returns _id" do
|
|
1686
|
-
expect(metadata.primary_key).to eq("_id")
|
|
1687
|
-
end
|
|
1688
|
-
end
|
|
1689
|
-
|
|
1690
|
-
context "when a primary key exists" do
|
|
1691
|
-
|
|
1692
|
-
let(:metadata) do
|
|
1693
|
-
described_class.new(
|
|
1694
|
-
name: :person,
|
|
1695
|
-
inverse_class_name: "Person",
|
|
1696
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1697
|
-
primary_key: :something_id
|
|
1698
|
-
)
|
|
1699
|
-
end
|
|
1700
|
-
|
|
1701
|
-
it "returns the primary key" do
|
|
1702
|
-
expect(metadata.primary_key).to eq("something_id")
|
|
1703
|
-
end
|
|
1704
|
-
end
|
|
1705
|
-
end
|
|
1706
|
-
|
|
1707
|
-
describe "#validate?" do
|
|
1708
|
-
|
|
1709
|
-
context "when validate is provided" do
|
|
1710
|
-
|
|
1711
|
-
context "when validate is true" do
|
|
1712
|
-
|
|
1713
|
-
let(:metadata) do
|
|
1714
|
-
described_class.new(
|
|
1715
|
-
name: :posts,
|
|
1716
|
-
inverse_class_name: "Post",
|
|
1717
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
1718
|
-
validate: true
|
|
1719
|
-
)
|
|
1720
|
-
end
|
|
1721
|
-
|
|
1722
|
-
it "returns true" do
|
|
1723
|
-
expect(metadata.validate?).to be true
|
|
1724
|
-
end
|
|
1725
|
-
end
|
|
1726
|
-
|
|
1727
|
-
context "when validate is false" do
|
|
1728
|
-
|
|
1729
|
-
let(:metadata) do
|
|
1730
|
-
described_class.new(
|
|
1731
|
-
name: :posts,
|
|
1732
|
-
inverse_class_name: "Post",
|
|
1733
|
-
relation: Mongoid::Relations::Referenced::Many,
|
|
1734
|
-
validate: false
|
|
1735
|
-
)
|
|
1736
|
-
end
|
|
1737
|
-
|
|
1738
|
-
it "returns false" do
|
|
1739
|
-
expect(metadata.validate?).to be false
|
|
1740
|
-
end
|
|
1741
|
-
end
|
|
1742
|
-
end
|
|
1743
|
-
|
|
1744
|
-
context "when validate is not provided" do
|
|
1745
|
-
|
|
1746
|
-
let(:metadata) do
|
|
1747
|
-
described_class.new(
|
|
1748
|
-
name: :posts,
|
|
1749
|
-
inverse_class_name: "Post",
|
|
1750
|
-
relation: Mongoid::Relations::Referenced::Many
|
|
1751
|
-
)
|
|
1752
|
-
end
|
|
1753
|
-
|
|
1754
|
-
it "returns the relation default" do
|
|
1755
|
-
expect(metadata.validate?).to be true
|
|
1756
|
-
end
|
|
1757
|
-
end
|
|
1758
|
-
end
|
|
1759
|
-
|
|
1760
|
-
describe "#store_as" do
|
|
1761
|
-
|
|
1762
|
-
context "when store_as is define" do
|
|
1763
|
-
|
|
1764
|
-
let(:metadata) do
|
|
1765
|
-
described_class.new(
|
|
1766
|
-
name: :comment,
|
|
1767
|
-
relation: Mongoid::Relations::Embedded::Many,
|
|
1768
|
-
store_as: 'user_comments'
|
|
1769
|
-
)
|
|
1770
|
-
end
|
|
1771
|
-
|
|
1772
|
-
it "returns the value" do
|
|
1773
|
-
expect(metadata.store_as).to eq("user_comments")
|
|
1774
|
-
end
|
|
1775
|
-
end
|
|
1776
|
-
|
|
1777
|
-
context "when is not define" do
|
|
1778
|
-
|
|
1779
|
-
let(:metadata) do
|
|
1780
|
-
described_class.new(
|
|
1781
|
-
name: :comments,
|
|
1782
|
-
relation: Mongoid::Relations::Embedded::Many,
|
|
1783
|
-
)
|
|
1784
|
-
end
|
|
1785
|
-
|
|
1786
|
-
it "returns false" do
|
|
1787
|
-
expect(metadata.store_as).to eq("comments")
|
|
1788
|
-
end
|
|
1789
|
-
end
|
|
1790
|
-
end
|
|
1791
|
-
|
|
1792
|
-
describe "#determine_inverse_relation" do
|
|
1793
|
-
|
|
1794
|
-
let(:class_name) do
|
|
1795
|
-
"Person"
|
|
1796
|
-
end
|
|
1797
|
-
|
|
1798
|
-
let(:metadata) do
|
|
1799
|
-
described_class.new(
|
|
1800
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1801
|
-
inverse_class_name: "Drug",
|
|
1802
|
-
class_name: class_name,
|
|
1803
|
-
name: :person
|
|
1804
|
-
)
|
|
1805
|
-
end
|
|
1806
|
-
|
|
1807
|
-
let(:inverse_relation) do
|
|
1808
|
-
metadata.send(:determine_inverse_relation)
|
|
1809
|
-
end
|
|
1810
|
-
|
|
1811
|
-
context "when no match" do
|
|
1812
|
-
|
|
1813
|
-
let(:class_name) do
|
|
1814
|
-
"Slave"
|
|
1815
|
-
end
|
|
1816
|
-
|
|
1817
|
-
it "returns nil" do
|
|
1818
|
-
expect(inverse_relation).to be_nil
|
|
1819
|
-
end
|
|
1820
|
-
end
|
|
1821
|
-
|
|
1822
|
-
context "when one match" do
|
|
1823
|
-
|
|
1824
|
-
it "returns correct relation" do
|
|
1825
|
-
expect(inverse_relation).to eq(:drugs)
|
|
1826
|
-
end
|
|
1827
|
-
end
|
|
1828
|
-
|
|
1829
|
-
context "when multiple matches" do
|
|
1830
|
-
|
|
1831
|
-
context "when the inverse_of is not nil" do
|
|
1832
|
-
|
|
1833
|
-
before do
|
|
1834
|
-
class_name.constantize.has_many(:evil_drugs, class_name: "Drug")
|
|
1835
|
-
end
|
|
1836
|
-
|
|
1837
|
-
after do
|
|
1838
|
-
class_name.constantize.relations.delete("evil_drugs")
|
|
1839
|
-
Person.reset_callbacks(:validate)
|
|
1840
|
-
end
|
|
1841
|
-
|
|
1842
|
-
it "raises AmbiguousRelationship" do
|
|
1843
|
-
expect {
|
|
1844
|
-
inverse_relation
|
|
1845
|
-
}.to raise_error(Mongoid::Errors::AmbiguousRelationship)
|
|
1846
|
-
end
|
|
1847
|
-
end
|
|
1848
|
-
|
|
1849
|
-
context "when the inverse_of is nil" do
|
|
1850
|
-
|
|
1851
|
-
before do
|
|
1852
|
-
class_name.constantize.has_many(:evil_drugs, class_name: "Drug", inverse_of: nil)
|
|
1853
|
-
end
|
|
1854
|
-
|
|
1855
|
-
after do
|
|
1856
|
-
class_name.constantize.relations.delete("evil_drugs")
|
|
1857
|
-
Person.reset_callbacks(:validate)
|
|
1858
|
-
end
|
|
1859
|
-
|
|
1860
|
-
it "returns the non-nil inverses" do
|
|
1861
|
-
expect(inverse_relation).to eq(:drugs)
|
|
1862
|
-
end
|
|
1863
|
-
end
|
|
1864
|
-
end
|
|
1865
|
-
end
|
|
1866
|
-
|
|
1867
|
-
describe "touchable?" do
|
|
1868
|
-
|
|
1869
|
-
context "when touch is false" do
|
|
1870
|
-
|
|
1871
|
-
let(:metadata) do
|
|
1872
|
-
described_class.new(
|
|
1873
|
-
name: :versions,
|
|
1874
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1875
|
-
touch: false
|
|
1876
|
-
)
|
|
1877
|
-
end
|
|
1878
|
-
|
|
1879
|
-
it "returns false" do
|
|
1880
|
-
expect(metadata).to_not be_touchable
|
|
1881
|
-
end
|
|
1882
|
-
end
|
|
1883
|
-
|
|
1884
|
-
context "when touch is true" do
|
|
1885
|
-
|
|
1886
|
-
let(:metadata) do
|
|
1887
|
-
described_class.new(
|
|
1888
|
-
name: :versions,
|
|
1889
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1890
|
-
touch: true
|
|
1891
|
-
)
|
|
1892
|
-
end
|
|
1893
|
-
|
|
1894
|
-
it "returns true" do
|
|
1895
|
-
expect(metadata).to be_touchable
|
|
1896
|
-
end
|
|
1897
|
-
end
|
|
1898
|
-
|
|
1899
|
-
context "when touch specifies additional field" do
|
|
1900
|
-
|
|
1901
|
-
let(:metadata) do
|
|
1902
|
-
described_class.new(
|
|
1903
|
-
name: :versions,
|
|
1904
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1905
|
-
touch: :changed_at
|
|
1906
|
-
)
|
|
1907
|
-
end
|
|
1908
|
-
|
|
1909
|
-
it "returns true" do
|
|
1910
|
-
expect(metadata).to be_touchable
|
|
1911
|
-
end
|
|
1912
|
-
end
|
|
1913
|
-
|
|
1914
|
-
context "when touch is nil" do
|
|
1915
|
-
|
|
1916
|
-
let(:metadata) do
|
|
1917
|
-
described_class.new(
|
|
1918
|
-
name: :versions,
|
|
1919
|
-
relation: Mongoid::Relations::Referenced::In,
|
|
1920
|
-
)
|
|
1921
|
-
end
|
|
1922
|
-
|
|
1923
|
-
it "returns false" do
|
|
1924
|
-
expect(metadata).to_not be_touchable
|
|
1925
|
-
end
|
|
1926
|
-
end
|
|
1927
|
-
end
|
|
1928
|
-
|
|
1929
|
-
context "properties" do
|
|
1930
|
-
|
|
1931
|
-
PROPERTIES = [
|
|
1932
|
-
"as",
|
|
1933
|
-
"cyclic",
|
|
1934
|
-
"name",
|
|
1935
|
-
"order"
|
|
1936
|
-
]
|
|
1937
|
-
|
|
1938
|
-
PROPERTIES.each do |property|
|
|
1939
|
-
|
|
1940
|
-
describe "##{property}" do
|
|
1941
|
-
|
|
1942
|
-
let(:metadata) do
|
|
1943
|
-
described_class.new(
|
|
1944
|
-
property.to_sym => :value,
|
|
1945
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
1946
|
-
)
|
|
1947
|
-
end
|
|
1948
|
-
|
|
1949
|
-
it "returns the #{property} property" do
|
|
1950
|
-
expect(metadata.send(property)).to eq(:value)
|
|
1951
|
-
end
|
|
1952
|
-
end
|
|
1953
|
-
|
|
1954
|
-
describe "##{property}?" do
|
|
1955
|
-
|
|
1956
|
-
context "when a #{property} property exists" do
|
|
1957
|
-
|
|
1958
|
-
let(:metadata) do
|
|
1959
|
-
described_class.new(
|
|
1960
|
-
property.to_sym => :value,
|
|
1961
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
1962
|
-
)
|
|
1963
|
-
end
|
|
1964
|
-
|
|
1965
|
-
it "returns true" do
|
|
1966
|
-
expect(metadata.send("#{property}?")).to be true
|
|
1967
|
-
end
|
|
1968
|
-
end
|
|
1969
|
-
|
|
1970
|
-
context "when the #{property} property is nil" do
|
|
1971
|
-
|
|
1972
|
-
let(:metadata) do
|
|
1973
|
-
described_class.new(
|
|
1974
|
-
relation: Mongoid::Relations::Embedded::Many
|
|
1975
|
-
)
|
|
1976
|
-
end
|
|
1977
|
-
|
|
1978
|
-
it "returns false" do
|
|
1979
|
-
expect(metadata.send("#{property}?")).to be false
|
|
1980
|
-
end
|
|
1981
|
-
end
|
|
1982
|
-
end
|
|
1983
|
-
end
|
|
1984
|
-
end
|
|
1985
|
-
end
|