mongoid 7.1.0.rc0 → 7.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +6 -6
  5. data/LICENSE +1 -1
  6. data/README.md +5 -5
  7. data/Rakefile +14 -0
  8. data/lib/config/locales/en.yml +5 -5
  9. data/lib/mongoid.rb +3 -2
  10. data/lib/mongoid/association/accessors.rb +37 -2
  11. data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
  13. data/lib/mongoid/association/many.rb +3 -2
  14. data/lib/mongoid/association/proxy.rb +6 -4
  15. data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
  16. data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
  17. data/lib/mongoid/association/referenced/eager.rb +29 -9
  18. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -22
  19. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
  20. data/lib/mongoid/atomic.rb +13 -3
  21. data/lib/mongoid/attributes.rb +28 -20
  22. data/lib/mongoid/clients/factory.rb +2 -2
  23. data/lib/mongoid/clients/options.rb +8 -8
  24. data/lib/mongoid/clients/sessions.rb +20 -4
  25. data/lib/mongoid/clients/storage_options.rb +5 -5
  26. data/lib/mongoid/config.rb +42 -12
  27. data/lib/mongoid/config/options.rb +5 -2
  28. data/lib/mongoid/contextual.rb +5 -4
  29. data/lib/mongoid/contextual/geo_near.rb +3 -2
  30. data/lib/mongoid/contextual/map_reduce.rb +3 -2
  31. data/lib/mongoid/contextual/mongo.rb +2 -1
  32. data/lib/mongoid/criteria.rb +23 -4
  33. data/lib/mongoid/criteria/modifiable.rb +2 -1
  34. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
  35. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
  36. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
  37. data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
  38. data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
  39. data/lib/mongoid/criteria/queryable/selectable.rb +120 -13
  40. data/lib/mongoid/criteria/queryable/storable.rb +104 -99
  41. data/lib/mongoid/errors/eager_load.rb +2 -0
  42. data/lib/mongoid/errors/no_client_config.rb +2 -2
  43. data/lib/mongoid/errors/no_default_client.rb +1 -1
  44. data/lib/mongoid/extensions/hash.rb +4 -2
  45. data/lib/mongoid/extensions/regexp.rb +1 -1
  46. data/lib/mongoid/fields.rb +2 -1
  47. data/lib/mongoid/fields/standard.rb +2 -1
  48. data/lib/mongoid/fields/validators/macro.rb +4 -1
  49. data/lib/mongoid/findable.rb +5 -4
  50. data/lib/mongoid/interceptable.rb +5 -1
  51. data/lib/mongoid/matchable/regexp.rb +2 -2
  52. data/lib/mongoid/persistable/pushable.rb +11 -2
  53. data/lib/mongoid/persistence_context.rb +6 -6
  54. data/lib/mongoid/query_cache.rb +61 -18
  55. data/lib/mongoid/railties/database.rake +7 -0
  56. data/lib/mongoid/serializable.rb +10 -2
  57. data/lib/mongoid/shardable.rb +56 -4
  58. data/lib/mongoid/tasks/database.rake +10 -5
  59. data/lib/mongoid/tasks/database.rb +83 -0
  60. data/lib/mongoid/timestamps/timeless.rb +3 -1
  61. data/lib/mongoid/validatable/uniqueness.rb +1 -1
  62. data/lib/mongoid/version.rb +1 -1
  63. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
  64. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  65. data/spec/app/models/coding.rb +4 -0
  66. data/spec/app/models/coding/pull_request.rb +12 -0
  67. data/spec/app/models/delegating_patient.rb +16 -0
  68. data/spec/app/models/passport.rb +1 -0
  69. data/spec/app/models/phone.rb +1 -0
  70. data/spec/app/models/publication.rb +5 -0
  71. data/spec/app/models/publication/encyclopedia.rb +12 -0
  72. data/spec/app/models/publication/review.rb +14 -0
  73. data/spec/integration/app_spec.rb +254 -0
  74. data/spec/integration/associations/embedded_spec.rb +54 -0
  75. data/spec/integration/associations/has_many_spec.rb +34 -0
  76. data/spec/integration/associations/has_one_spec.rb +34 -0
  77. data/spec/integration/bson_regexp_raw_spec.rb +20 -0
  78. data/spec/integration/criteria/date_field_spec.rb +41 -0
  79. data/spec/integration/criteria/logical_spec.rb +13 -0
  80. data/spec/integration/document_spec.rb +22 -0
  81. data/spec/integration/shardable_spec.rb +149 -0
  82. data/spec/lite_spec_helper.rb +15 -4
  83. data/spec/mongoid/association/accessors_spec.rb +238 -63
  84. data/spec/mongoid/association/embedded/embeds_many_models.rb +19 -0
  85. data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
  86. data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
  87. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
  88. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
  89. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
  90. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
  91. data/spec/mongoid/attributes_spec.rb +19 -7
  92. data/spec/mongoid/changeable_spec.rb +23 -0
  93. data/spec/mongoid/clients/factory_spec.rb +8 -8
  94. data/spec/mongoid/clients/options_spec.rb +11 -11
  95. data/spec/mongoid/clients/sessions_spec.rb +8 -4
  96. data/spec/mongoid/clients/transactions_spec.rb +20 -8
  97. data/spec/mongoid/clients_spec.rb +2 -2
  98. data/spec/mongoid/contextual/atomic_spec.rb +22 -11
  99. data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
  100. data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
  101. data/spec/mongoid/contextual/mongo_spec.rb +76 -53
  102. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
  103. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
  104. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
  105. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
  106. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
  107. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
  108. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
  109. data/spec/mongoid/criteria/queryable/selectable_spec.rb +52 -0
  110. data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
  111. data/spec/mongoid/criteria_spec.rb +36 -2
  112. data/spec/mongoid/document_fields_spec.rb +29 -0
  113. data/spec/mongoid/document_persistence_context_spec.rb +33 -0
  114. data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
  115. data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
  116. data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
  117. data/spec/mongoid/fields_spec.rb +24 -1
  118. data/spec/mongoid/indexable_spec.rb +6 -4
  119. data/spec/mongoid/interceptable_spec.rb +62 -0
  120. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  121. data/spec/mongoid/matchable/default_spec.rb +1 -1
  122. data/spec/mongoid/matchable/regexp_spec.rb +2 -2
  123. data/spec/mongoid/matchable_spec.rb +2 -2
  124. data/spec/mongoid/persistable/pushable_spec.rb +55 -1
  125. data/spec/mongoid/query_cache_spec.rb +77 -9
  126. data/spec/mongoid/relations/proxy_spec.rb +1 -1
  127. data/spec/mongoid/scopable_spec.rb +2 -1
  128. data/spec/mongoid/serializable_spec.rb +129 -18
  129. data/spec/mongoid/shardable_models.rb +61 -0
  130. data/spec/mongoid/shardable_spec.rb +69 -16
  131. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  132. data/spec/mongoid/tasks/database_spec.rb +1 -1
  133. data/spec/spec_helper.rb +2 -31
  134. data/spec/support/child_process_helper.rb +76 -0
  135. data/spec/support/cluster_config.rb +3 -3
  136. data/spec/support/constraints.rb +26 -10
  137. data/spec/support/expectations.rb +3 -1
  138. data/spec/support/helpers.rb +11 -0
  139. data/spec/support/lite_constraints.rb +22 -0
  140. data/spec/support/session_registry.rb +50 -0
  141. data/spec/support/spec_config.rb +12 -4
  142. metadata +518 -480
  143. metadata.gz.sig +0 -0
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'spec_helper'
5
5
  require_relative '../../mongoid/association/embedded/embeds_many_models'
6
+ require_relative '../../mongoid/association/embedded/embeds_one_models'
6
7
 
7
8
  describe 'embedded associations' do
8
9
  context 'without default order' do
@@ -118,4 +119,57 @@ describe 'embedded associations' do
118
119
  it_behaves_like 'an embedded association'
119
120
  end
120
121
  end
122
+
123
+ describe 'parent association' do
124
+ let(:parent) do
125
+ parent_cls.new
126
+ end
127
+
128
+ context 'embeds_one' do
129
+
130
+ shared_examples 'is set' do
131
+ it 'is set' do
132
+ parent.child = child_cls.new
133
+ parent.child.parent.should == parent
134
+ end
135
+ end
136
+
137
+ context 'class_name set without leading ::' do
138
+ let(:parent_cls) { EomParent }
139
+ let(:child_cls) { EomChild }
140
+
141
+ it_behaves_like 'is set'
142
+ end
143
+
144
+ context 'class_name set with leading ::' do
145
+ let(:parent_cls) { EomCcParent }
146
+ let(:child_cls) { EomCcChild }
147
+
148
+ it_behaves_like 'is set'
149
+ end
150
+ end
151
+
152
+ context 'embeds_many' do
153
+
154
+ let(:child) { parent.legislators.new }
155
+
156
+ shared_examples 'is set' do
157
+ it 'is set' do
158
+ child.congress.should == parent
159
+ end
160
+ end
161
+
162
+ context 'class_name set without leading ::' do
163
+ let(:parent_cls) { EmmCongress }
164
+
165
+ it_behaves_like 'is set'
166
+ end
167
+
168
+ context 'class_name set with leading ::' do
169
+ let(:parent_cls) { EmmCcCongress }
170
+
171
+ it_behaves_like 'is set'
172
+ end
173
+ end
174
+ end
121
175
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ describe 'has_many associations' do
7
+ context 'destroying parent in transaction with dependent child' do
8
+ require_transaction_support
9
+
10
+ let(:artist) { Artist.create! }
11
+ let(:album) { Album.create!(artist: artist) }
12
+
13
+ before do
14
+ Artist.delete_all
15
+ Album.delete_all
16
+
17
+ album
18
+ end
19
+
20
+ it 'works' do
21
+ Artist.count.should == 1
22
+ Album.count.should == 1
23
+
24
+ artist.with_session do |session|
25
+ session.with_transaction do
26
+ artist.destroy
27
+ end
28
+ end
29
+
30
+ Artist.count.should == 0
31
+ Album.count.should == 0
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ describe 'has_one associations' do
7
+ context 'destroying parent in transaction with dependent child' do
8
+ require_transaction_support
9
+
10
+ let(:person) { Person.create! }
11
+ let(:game) { Game.create!(person: person) }
12
+
13
+ before do
14
+ Person.delete_all
15
+ Game.delete_all
16
+
17
+ game
18
+ end
19
+
20
+ it 'works' do
21
+ Person.count.should == 1
22
+ Game.count.should == 1
23
+
24
+ Person.with_session do |session|
25
+ session.with_transaction do
26
+ person.destroy
27
+ end
28
+ end
29
+
30
+ Person.count.should == 0
31
+ Game.count.should == 0
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ describe BSON::Regexp::Raw do
7
+ context 'fully qualified name' do
8
+ it 'can be created' do
9
+ regexp = BSON::Regexp::Raw.new('foo')
10
+ regexp.pattern.should == 'foo'
11
+ end
12
+ end
13
+
14
+ context 'via ::Regexp' do
15
+ it 'can be created' do
16
+ regexp = Regexp::Raw.new('foo')
17
+ regexp.pattern.should == 'foo'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ describe 'Queries on Date fields' do
7
+ let(:query) do
8
+ Band.where(founded: arg)
9
+ end
10
+
11
+ let(:selector) { query.selector }
12
+
13
+ shared_examples 'converts to beginning of day in UTC' do
14
+ it 'converts to beginning of day in UTC' do
15
+ selector['founded'].should == arg.dup.beginning_of_day.utc.beginning_of_day
16
+ end
17
+ end
18
+
19
+ context 'using Time' do
20
+ let(:arg) do
21
+ Time.now.freeze
22
+ end
23
+
24
+ it_behaves_like 'converts to beginning of day in UTC'
25
+ end
26
+
27
+ context 'using TimeWithZone' do
28
+ let(:time_zone_name) { 'Pacific Time (US & Canada)' }
29
+ let(:arg) { Time.now.in_time_zone(time_zone_name).freeze }
30
+
31
+ it_behaves_like 'converts to beginning of day in UTC'
32
+ end
33
+
34
+ context 'using DateTime' do
35
+ let(:arg) do
36
+ DateTime.now.freeze
37
+ end
38
+
39
+ it_behaves_like 'converts to beginning of day in UTC'
40
+ end
41
+ end
@@ -47,6 +47,19 @@ describe 'Criteria logical operations' do
47
47
  bands = Band.where(name: /jecti/).or(Band.where(name: /ush/))
48
48
  expect(bands.order_by(name: 1).to_a).to eq([ap, im])
49
49
  end
50
+
51
+ context 'when using a symbol operator' do
52
+ context 'when field has a serializer' do
53
+ let!(:doc) { Dokument.create! }
54
+
55
+ it 'works' do
56
+ scope = Dokument.or(:created_at.lte => DateTime.now).sort(id: 1)
57
+ # input was converted from DateTime to Time
58
+ scope.criteria.selector['$or'].first['created_at']['$lte'].should be_a(Time)
59
+ scope.to_a.should == [doc]
60
+ end
61
+ end
62
+ end
50
63
  end
51
64
 
52
65
  describe 'not' do
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ describe Mongoid::Document do
7
+ context 'when including class uses delegate' do
8
+ let(:patient) do
9
+ DelegatingPatient.new(
10
+ email: Email.new(address: 'test@example.com'),
11
+ )
12
+ end
13
+
14
+ it 'works for instance level delegation' do
15
+ patient.address.should == 'test@example.com'
16
+ end
17
+
18
+ it 'works for class level delegation' do
19
+ DelegatingPatient.default_client.should be Mongoid.default_client
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+ require_relative '../mongoid/shardable_models'
6
+
7
+ describe 'Sharding helpers' do
8
+ require_topology :sharded
9
+
10
+ describe 'shard_collection rake task' do
11
+ let(:shard_collections) do
12
+ Mongoid::Tasks::Database.shard_collections([model_cls])
13
+ end
14
+
15
+ let(:create_indexes) do
16
+ Mongoid::Tasks::Database.create_indexes([model_cls])
17
+ end
18
+
19
+ shared_examples_for 'shards collection' do
20
+ it 'returns the model class' do
21
+ shard_collections.should == [model_cls]
22
+ end
23
+
24
+ it 'shards collection' do
25
+ shard_collections
26
+
27
+ stats = model_cls.collection.database.command(collStats: model_cls.collection.name).first
28
+ stats[:sharded].should be true
29
+ end
30
+ end
31
+
32
+ context 'simple model' do
33
+ let(:model_cls) { SmMovie }
34
+
35
+ before do
36
+ SmMovie.collection.drop
37
+ end
38
+
39
+ it_behaves_like 'shards collection'
40
+
41
+ it 'makes inserts work' do
42
+ shard_collections
43
+ create_indexes
44
+
45
+ SmMovie.create!(year: 2020)
46
+ end
47
+ end
48
+
49
+ context 'when database does not exist' do
50
+ let(:model_cls) { SmMovie }
51
+
52
+ before do
53
+ SmMovie.collection.database.drop
54
+ end
55
+
56
+ it_behaves_like 'shards collection'
57
+ end
58
+
59
+ context 'when database exists but collection does not exist' do
60
+ let(:model_cls) { SmMovie }
61
+
62
+ before do
63
+ SmMovie.collection.database.drop
64
+ SmMovie.collection.create
65
+ SmMovie.collection.drop
66
+ end
67
+
68
+ it_behaves_like 'shards collection'
69
+ end
70
+
71
+ context 'when collection is already sharded' do
72
+ let(:model_cls) { SmMovie }
73
+
74
+ before do
75
+ Mongoid::Tasks::Database.shard_collections([model_cls])
76
+ end
77
+
78
+ it_behaves_like 'shards collection'
79
+ end
80
+
81
+ context 'when hashed shard key is given' do
82
+ let(:model_cls) { SmAssistant }
83
+
84
+ before do
85
+ SmAssistant.collection.drop
86
+ end
87
+
88
+ it_behaves_like 'shards collection'
89
+
90
+ it 'makes inserts work' do
91
+ shard_collections
92
+ create_indexes
93
+
94
+ SmAssistant.create!(gender: 'm')
95
+ end
96
+ end
97
+
98
+ context 'when shard configuration is invalid' do
99
+ let(:model_cls) { SmActor }
100
+
101
+ it 'returns empty array' do
102
+ shard_collections.should == []
103
+ end
104
+
105
+ it 'does not shards collection' do
106
+ shard_collections
107
+
108
+ stats = model_cls.collection.database.command(collStats: model_cls.collection.name).first
109
+ stats[:sharded].should be false
110
+ end
111
+
112
+ end
113
+
114
+ context 'when models have no sharded configuration' do
115
+ let(:model_cls) { SmNotSharded }
116
+
117
+ before do
118
+ model_cls.shard_config.should be nil
119
+ end
120
+
121
+ it 'returns empty array' do
122
+ shard_collections.should == []
123
+ end
124
+
125
+ context 'pre-4.2' do
126
+ max_server_version '4.0'
127
+
128
+ it 'does not shards collection' do
129
+ shard_collections
130
+
131
+ lambda do
132
+ model_cls.collection.database.command(collStats: model_cls.collection.name).first
133
+ end.should raise_error(Mongo::Error::OperationFailure, /Collection.*not found/)
134
+ end
135
+ end
136
+
137
+ context '4.2+' do
138
+ min_server_version '4.2'
139
+
140
+ it 'does not shards collection' do
141
+ shard_collections
142
+
143
+ stats = model_cls.collection.database.command(collStats: model_cls.collection.name).first
144
+ stats[:sharded].should be false
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -15,6 +15,8 @@ require "mongo"
15
15
  require 'pp'
16
16
 
17
17
  require 'support/spec_config'
18
+ require 'support/lite_constraints'
19
+ require "support/session_registry"
18
20
 
19
21
  unless SpecConfig.instance.ci?
20
22
  begin
@@ -49,15 +51,24 @@ RSpec.configure do |config|
49
51
  end
50
52
 
51
53
  if SpecConfig.instance.ci?
52
- # Allow a max of 30 seconds per test.
53
- # Tests should take under 10 seconds ideally but it seems
54
- # we have some that run for more than 10 seconds in CI.
54
+ timeout = if SpecConfig.instance.app_tests?
55
+ # Allow 5 minutes per test for the app tests, since they install
56
+ # gems for Rails applications which can take a long time.
57
+ 300
58
+ else
59
+ # Allow a max of 30 seconds per test.
60
+ # Tests should take under 10 seconds ideally but it seems
61
+ # we have some that run for more than 10 seconds in CI.
62
+ 30
63
+ end
55
64
  config.around(:each) do |example|
56
- TimeoutInterrupt.timeout(30) do
65
+ TimeoutInterrupt.timeout(timeout) do
57
66
  example.run
58
67
  end
59
68
  end
60
69
  end
70
+
71
+ config.extend(LiteConstraints)
61
72
  end
62
73
 
63
74
  # require all shared examples
@@ -11,9 +11,9 @@ describe Mongoid::Association::Accessors do
11
11
  Person.create
12
12
  end
13
13
 
14
- context "when the relation is a has one" do
14
+ context "when the association is a has one" do
15
15
 
16
- context "when the relation exists" do
16
+ context "when the association exists" do
17
17
 
18
18
  let!(:game) do
19
19
  person.build_game
@@ -24,7 +24,7 @@ describe Mongoid::Association::Accessors do
24
24
  end
25
25
  end
26
26
 
27
- context "when the relation does not exist" do
27
+ context "when the association does not exist" do
28
28
 
29
29
  context "when not autobuilding" do
30
30
 
@@ -42,9 +42,9 @@ describe Mongoid::Association::Accessors do
42
42
  end
43
43
  end
44
44
 
45
- context "when the relation is a has many" do
45
+ context "when the association is a has many" do
46
46
 
47
- context "when the relation has documents" do
47
+ context "when the association has documents" do
48
48
 
49
49
  let!(:post) do
50
50
  person.posts.build
@@ -55,7 +55,7 @@ describe Mongoid::Association::Accessors do
55
55
  end
56
56
  end
57
57
 
58
- context "when the relation does not have documents" do
58
+ context "when the association does not have documents" do
59
59
 
60
60
  it "returns false" do
61
61
  expect(person).to_not have_posts
@@ -63,9 +63,9 @@ describe Mongoid::Association::Accessors do
63
63
  end
64
64
  end
65
65
 
66
- context "when the relation is a has and belongs to many" do
66
+ context "when the association is a has and belongs to many" do
67
67
 
68
- context "when the relation has documents" do
68
+ context "when the association has documents" do
69
69
 
70
70
  let!(:preference) do
71
71
  person.preferences.build
@@ -76,7 +76,7 @@ describe Mongoid::Association::Accessors do
76
76
  end
77
77
  end
78
78
 
79
- context "when the relation does not have documents" do
79
+ context "when the association does not have documents" do
80
80
 
81
81
  it "returns false" do
82
82
  expect(person).to_not have_preferences
@@ -84,9 +84,9 @@ describe Mongoid::Association::Accessors do
84
84
  end
85
85
  end
86
86
 
87
- context "when the relation is a belongs to" do
87
+ context "when the association is a belongs to" do
88
88
 
89
- context "when the relation is named next" do
89
+ context "when the association is named next" do
90
90
 
91
91
  let(:user) do
92
92
  User.create
@@ -97,7 +97,7 @@ describe Mongoid::Association::Accessors do
97
97
  end
98
98
  end
99
99
 
100
- context "when the relation exists" do
100
+ context "when the association exists" do
101
101
 
102
102
  let!(:game) do
103
103
  person.build_game
@@ -108,9 +108,9 @@ describe Mongoid::Association::Accessors do
108
108
  end
109
109
  end
110
110
 
111
- context "when the relation does not exist" do
111
+ context "when the association does not exist" do
112
112
 
113
- context "when the relation does not autobuild" do
113
+ context "when the association does not autobuild" do
114
114
 
115
115
  let(:game) do
116
116
  Game.new
@@ -121,7 +121,7 @@ describe Mongoid::Association::Accessors do
121
121
  end
122
122
  end
123
123
 
124
- context "when the relation autobuilds" do
124
+ context "when the association autobuilds" do
125
125
 
126
126
  let(:book) do
127
127
  Book.new
@@ -134,9 +134,9 @@ describe Mongoid::Association::Accessors do
134
134
  end
135
135
  end
136
136
 
137
- context "when the relation is an embeds one" do
137
+ context "when the association is an embeds one" do
138
138
 
139
- context "when the relation exists" do
139
+ context "when the association exists" do
140
140
 
141
141
  let!(:name) do
142
142
  person.build_name
@@ -147,16 +147,16 @@ describe Mongoid::Association::Accessors do
147
147
  end
148
148
  end
149
149
 
150
- context "when the relation does not exist" do
150
+ context "when the association does not exist" do
151
151
 
152
- context "when the relation does not autobuild" do
152
+ context "when the association does not autobuild" do
153
153
 
154
154
  it "returns false" do
155
155
  expect(person).to_not have_name
156
156
  end
157
157
  end
158
158
 
159
- context "when the relation autobuilds" do
159
+ context "when the association autobuilds" do
160
160
 
161
161
  let(:person) do
162
162
  Person.new
@@ -169,9 +169,9 @@ describe Mongoid::Association::Accessors do
169
169
  end
170
170
  end
171
171
 
172
- context "when the relation is an embeds many" do
172
+ context "when the association is an embeds many" do
173
173
 
174
- context "when the relation has documents" do
174
+ context "when the association has documents" do
175
175
 
176
176
  let!(:address) do
177
177
  person.addresses.build
@@ -182,7 +182,7 @@ describe Mongoid::Association::Accessors do
182
182
  end
183
183
  end
184
184
 
185
- context "when the relation does not have documents" do
185
+ context "when the association does not have documents" do
186
186
 
187
187
  it "returns false" do
188
188
  expect(person).to_not have_addresses
@@ -190,9 +190,9 @@ describe Mongoid::Association::Accessors do
190
190
  end
191
191
  end
192
192
 
193
- context "when the relation is an embedded in" do
193
+ context "when the association is an embedded in" do
194
194
 
195
- context "when the relation exists" do
195
+ context "when the association exists" do
196
196
 
197
197
  let!(:name) do
198
198
  person.build_name
@@ -203,9 +203,9 @@ describe Mongoid::Association::Accessors do
203
203
  end
204
204
  end
205
205
 
206
- context "when the relation does not exist" do
206
+ context "when the association does not exist" do
207
207
 
208
- context "when the relation does not autobuild" do
208
+ context "when the association does not autobuild" do
209
209
 
210
210
  let(:name) do
211
211
  Name.new
@@ -216,7 +216,7 @@ describe Mongoid::Association::Accessors do
216
216
  end
217
217
  end
218
218
 
219
- context "when the relation autobuilds" do
219
+ context "when the association autobuilds" do
220
220
 
221
221
  let(:passport) do
222
222
  Passport.new
@@ -236,11 +236,11 @@ describe Mongoid::Association::Accessors do
236
236
  Person.new
237
237
  end
238
238
 
239
- context "when autobuilding the relation" do
239
+ context "when autobuilding the association" do
240
240
 
241
- context "when the relation is an embeds one" do
241
+ context "when the association is an embeds one" do
242
242
 
243
- context "when the relation does not exist" do
243
+ context "when the association does not exist" do
244
244
 
245
245
  let!(:passport) do
246
246
  person.passport
@@ -255,7 +255,7 @@ describe Mongoid::Association::Accessors do
255
255
  end
256
256
  end
257
257
 
258
- context "when the relation exists" do
258
+ context "when the association exists" do
259
259
 
260
260
  let!(:passport) do
261
261
  person.build_passport(number: "123123321")
@@ -264,16 +264,17 @@ describe Mongoid::Association::Accessors do
264
264
  it "does not build a new document" do
265
265
  expect(person.passport).to eq(passport)
266
266
  end
267
+
267
268
  end
268
269
  end
269
270
 
270
- context "when the relation is an embedded in" do
271
+ context "when the association is an embedded in" do
271
272
 
272
273
  let(:passport) do
273
274
  Passport.new
274
275
  end
275
276
 
276
- context "when the relation does not exist" do
277
+ context "when the association does not exist" do
277
278
 
278
279
  let(:person) do
279
280
  passport.person
@@ -284,7 +285,7 @@ describe Mongoid::Association::Accessors do
284
285
  end
285
286
  end
286
287
 
287
- context "when the relation exists" do
288
+ context "when the association exists" do
288
289
 
289
290
  let!(:person) do
290
291
  passport.build_person(title: "sir")
@@ -296,9 +297,9 @@ describe Mongoid::Association::Accessors do
296
297
  end
297
298
  end
298
299
 
299
- context "when the relation is a has one" do
300
+ context "when the association is a has one" do
300
301
 
301
- context "when the relation does not exist" do
302
+ context "when the association does not exist" do
302
303
 
303
304
  let(:book) do
304
305
  person.book
@@ -309,7 +310,7 @@ describe Mongoid::Association::Accessors do
309
310
  end
310
311
  end
311
312
 
312
- context "when the relation exists" do
313
+ context "when the association exists" do
313
314
 
314
315
  let!(:book) do
315
316
  person.build_book(title: "art of war")
@@ -321,13 +322,13 @@ describe Mongoid::Association::Accessors do
321
322
  end
322
323
  end
323
324
 
324
- context "when the relation is a belongs to" do
325
+ context "when the association is a belongs to" do
325
326
 
326
327
  let(:book) do
327
328
  Book.new
328
329
  end
329
330
 
330
- context "when the relation does not exist" do
331
+ context "when the association does not exist" do
331
332
 
332
333
  let(:person) do
333
334
  book.person
@@ -338,7 +339,7 @@ describe Mongoid::Association::Accessors do
338
339
  end
339
340
  end
340
341
 
341
- context "when the relation exists" do
342
+ context "when the association exists" do
342
343
 
343
344
  let!(:person) do
344
345
  book.build_person(title: "sir")
@@ -351,13 +352,13 @@ describe Mongoid::Association::Accessors do
351
352
  end
352
353
  end
353
354
 
354
- context "when the relation is not polymorphic" do
355
+ context "when the association is not polymorphic" do
355
356
 
356
357
  let(:person) do
357
358
  Person.create
358
359
  end
359
360
 
360
- context "when the relation is a many to many" do
361
+ context "when the association is a many to many" do
361
362
 
362
363
  let!(:preference) do
363
364
  Preference.create(name: "Setting")
@@ -367,7 +368,7 @@ describe Mongoid::Association::Accessors do
367
368
  person.preferences << Preference.last
368
369
  end
369
370
 
370
- context "when reloading the relation directly" do
371
+ context "when reloading the association directly" do
371
372
 
372
373
  let(:preferences) do
373
374
  person.preferences(true)
@@ -409,7 +410,7 @@ describe Mongoid::Association::Accessors do
409
410
  end
410
411
  end
411
412
 
412
- context "when the relation is a many to one" do
413
+ context "when the association is a many to one" do
413
414
 
414
415
  let!(:post) do
415
416
  Post.create(title: "First!")
@@ -419,7 +420,7 @@ describe Mongoid::Association::Accessors do
419
420
  person.posts << Post.last
420
421
  end
421
422
 
422
- context "when reloading the relation directly" do
423
+ context "when reloading the association directly" do
423
424
 
424
425
  let(:posts) do
425
426
  person.posts(true)
@@ -461,7 +462,7 @@ describe Mongoid::Association::Accessors do
461
462
  end
462
463
  end
463
464
 
464
- context "when the relation is a references one" do
465
+ context "when the association is a references one" do
465
466
 
466
467
  let!(:game) do
467
468
  Game.create(name: "Centipeded")
@@ -471,7 +472,7 @@ describe Mongoid::Association::Accessors do
471
472
  person.game = Game.last
472
473
  end
473
474
 
474
- context "when reloading the relation directly" do
475
+ context "when reloading the association directly" do
475
476
 
476
477
  let(:reloaded_game) do
477
478
  person.game(true)
@@ -514,7 +515,7 @@ describe Mongoid::Association::Accessors do
514
515
  end
515
516
  end
516
517
 
517
- context "when the relation is polymorphic" do
518
+ context "when the association is polymorphic" do
518
519
 
519
520
  context "when there's a single references many/one" do
520
521
 
@@ -568,9 +569,183 @@ describe Mongoid::Association::Accessors do
568
569
  end
569
570
  end
570
571
  end
572
+
573
+ context 'when projecting' do
574
+ context 'embeds_one' do
575
+
576
+ let!(:person) do
577
+ Person.create!(passport: Passport.new(number: "123123321", country: "USA"))
578
+ end
579
+
580
+ context "when the record is queried with the embedded association projected" do
581
+ let(:persisted_person) { Person.only(:passport).first }
582
+
583
+ it 'creates an accessor for the projected embedded document' do
584
+ expect(persisted_person.passport.number).to eq("123123321")
585
+ expect(persisted_person.passport.country).to eq("USA")
586
+ end
587
+ end
588
+
589
+ context 'when the record is queried with a field on the embedded association projected' do
590
+ let(:persisted_person) { Person.only("pass.number").first }
591
+
592
+ it 'creates an accessor for the projected field on the embedded document' do
593
+ expect(persisted_person.passport.number).to eq("123123321")
594
+ end
595
+
596
+ it 'does not create an accessor for another field on the embedded document' do
597
+ expect do
598
+ persisted_person.passport.country
599
+ end.to raise_error(ActiveModel::MissingAttributeError)
600
+ end
601
+ end
602
+
603
+ context 'when projecting association and a field in association' do
604
+ shared_examples 'is prohibited on 4.4+ server' do
605
+ context '4.4 server and higher' do
606
+ min_server_version '4.4'
607
+
608
+ it 'is not allowed by server' do
609
+ lambda do
610
+ persisted_person
611
+ end.should raise_error(Mongo::Error::OperationFailure, /Path collision at pass/)
612
+ end
613
+ end
614
+ end
615
+
616
+ context 'association then field' do
617
+ let(:persisted_person) { Person.only(:pass, "pass.number").first }
618
+
619
+ context '4.2 server and lower' do
620
+ max_server_version '4.2'
621
+
622
+ it 'creates an accessor for the projected field on the embedded document' do
623
+ expect(persisted_person.passport.number).to eq("123123321")
624
+ end
625
+
626
+ it 'does not create an accessor for another field on the embedded document' do
627
+ expect do
628
+ persisted_person.passport.country
629
+ end.to raise_error(ActiveModel::MissingAttributeError)
630
+ end
631
+ end
632
+
633
+ include_examples 'is prohibited on 4.4+ server'
634
+ end
635
+
636
+ context 'field then association' do
637
+ let(:persisted_person) { Person.only('pass.number', :pass).first }
638
+
639
+ context '4.2 server and lower' do
640
+ max_server_version '4.2'
641
+
642
+ it 'creates an accessor for the projected field on the embedded document' do
643
+ expect(persisted_person.passport.number).to eq("123123321")
644
+ end
645
+
646
+ it 'retrieves other fields' do
647
+ persisted_person.passport.country.should == 'USA'
648
+ end
649
+ end
650
+
651
+ include_examples 'is prohibited on 4.4+ server'
652
+ end
653
+ end
654
+ end
655
+
656
+ context 'embeds_many' do
657
+
658
+ let!(:person) do
659
+ Person.create!(phone_numbers: [
660
+ Phone.new(number: '111-111-1111', landline: true),
661
+ ])
662
+ end
663
+
664
+ context 'when the record is queried with the embedded association projected' do
665
+ let(:persisted_person) { Person.only(:phone_numbers).first }
666
+
667
+ it 'creates an accessor for the embedded document' do
668
+ expect(persisted_person.phone_numbers.first.number).to eq('111-111-1111')
669
+ expect(persisted_person.phone_numbers.first.landline).to be true
670
+ end
671
+ end
672
+
673
+ shared_examples 'allows access to field of projected association' do
674
+ it 'creates an accessor for the embedded document' do
675
+ expect(persisted_person.phone_numbers.first).to be_a_kind_of(Phone)
676
+ end
677
+
678
+ it 'creates an accessor for the projected field on the embedded document' do
679
+ expect(persisted_person.phone_numbers.first.number).to eq('111-111-1111')
680
+ end
681
+ end
682
+
683
+ context 'when the record is queried with a field on the embedded association projected' do
684
+ let(:persisted_person) { Person.only("phone_numbers.number").first }
685
+
686
+ include_examples 'allows access to field of projected association'
687
+
688
+ it 'does not create an accessor for another field on the embedded document' do
689
+ expect do
690
+ persisted_person.phone_numbers.first.landline
691
+ end.to raise_error(ActiveModel::MissingAttributeError)
692
+ end
693
+ end
694
+
695
+ context 'when projecting association and a field in association' do
696
+
697
+ shared_examples 'is prohibited on 4.4+ server' do
698
+ context '4.4 server and higher' do
699
+ min_server_version '4.4'
700
+
701
+ it 'is not allowed by server' do
702
+ lambda do
703
+ persisted_person
704
+ end.should raise_error(Mongo::Error::OperationFailure, /Path collision at phone_numbers/)
705
+ end
706
+ end
707
+ end
708
+
709
+ context 'association then field' do
710
+ let(:persisted_person) { Person.only(:phone_numbers, 'phone_numbers.number').first }
711
+
712
+ context '4.2 server and lower' do
713
+ max_server_version '4.2'
714
+
715
+ include_examples 'allows access to field of projected association'
716
+
717
+ it 'does not create an accessor for another field on the embedded document' do
718
+ expect do
719
+ persisted_person.phone_numbers.first.landline
720
+ end.to raise_error(ActiveModel::MissingAttributeError)
721
+ end
722
+
723
+ end
724
+
725
+ include_examples 'is prohibited on 4.4+ server'
726
+ end
727
+
728
+ context 'field then association' do
729
+ let(:persisted_person) { Person.only('phone_numbers.number', :phone_numbers).first }
730
+
731
+ context '4.2 server and lower' do
732
+ max_server_version '4.2'
733
+
734
+ include_examples 'allows access to field of projected association'
735
+
736
+ it 'retrieves all fields of association' do
737
+ persisted_person.phone_numbers.first.landline.should be true
738
+ end
739
+ end
740
+
741
+ include_examples 'is prohibited on 4.4+ server'
742
+ end
743
+ end
744
+ end
745
+ end
571
746
  end
572
747
 
573
- context "when setting relations to empty values" do
748
+ context "when setting associations to empty values" do
574
749
 
575
750
  context "when the document is a referenced in" do
576
751
 
@@ -578,7 +753,7 @@ describe Mongoid::Association::Accessors do
578
753
  Post.new
579
754
  end
580
755
 
581
- context "when setting the relation directly" do
756
+ context "when setting the association directly" do
582
757
 
583
758
  before do
584
759
  post.person = ""
@@ -607,7 +782,7 @@ describe Mongoid::Association::Accessors do
607
782
  Person.new
608
783
  end
609
784
 
610
- context "when setting the relation directly" do
785
+ context "when setting the association directly" do
611
786
 
612
787
  before do
613
788
  person.game = ""
@@ -743,7 +918,7 @@ describe Mongoid::Association::Accessors do
743
918
 
744
919
  before do
745
920
  game.person_id = ""
746
- game.save
921
+ game.save!
747
922
  end
748
923
 
749
924
  it "sets the foreign key to empty" do
@@ -755,7 +930,7 @@ describe Mongoid::Association::Accessors do
755
930
 
756
931
  before do
757
932
  game.person_id = person.id.to_s
758
- game.save
933
+ game.save!
759
934
  end
760
935
 
761
936
  it "sets the foreign key as ObjectID" do
@@ -767,7 +942,7 @@ describe Mongoid::Association::Accessors do
767
942
 
768
943
  before do
769
944
  game.person_id = person.id
770
- game.save
945
+ game.save!
771
946
  end
772
947
 
773
948
  it "keeps the foreign key as ObjectID" do
@@ -775,30 +950,30 @@ describe Mongoid::Association::Accessors do
775
950
  end
776
951
  end
777
952
 
778
- context "when setting ids multiple times on the relation itself" do
953
+ context "when setting ids multiple times on the association itself" do
779
954
 
780
955
  before do
781
956
  game.person = person.id
782
957
  game.person = person.id
783
958
  end
784
959
 
785
- it "sets the relation foreign key" do
960
+ it "sets the association foreign key" do
786
961
  expect(game.person_id).to eq(person.id)
787
962
  end
788
963
 
789
- it "sets the appropriate relation" do
964
+ it "sets the appropriate association" do
790
965
  expect(game.person).to eq(person)
791
966
  end
792
967
  end
793
968
  end
794
969
 
795
- context 'when setting the relation more than once' do
970
+ context 'when setting the association more than once' do
796
971
 
797
972
  let(:person) do
798
973
  Person.create
799
974
  end
800
975
 
801
- context 'when the relation is a references one' do
976
+ context 'when the association is a references one' do
802
977
 
803
978
  let(:game) do
804
979
  Game.create
@@ -814,7 +989,7 @@ describe Mongoid::Association::Accessors do
814
989
  end
815
990
  end
816
991
 
817
- context 'when the relation is a references many' do
992
+ context 'when the association is a references many' do
818
993
 
819
994
  let!(:preference) do
820
995
  Preference.create(name: "Setting")
@@ -829,7 +1004,7 @@ describe Mongoid::Association::Accessors do
829
1004
  end
830
1005
  end
831
1006
 
832
- context 'when the relation is an embeds one' do
1007
+ context 'when the association is an embeds one' do
833
1008
 
834
1009
  let!(:name) do
835
1010
  Name.new
@@ -845,7 +1020,7 @@ describe Mongoid::Association::Accessors do
845
1020
  end
846
1021
  end
847
1022
 
848
- context 'when the relation is an embeds many' do
1023
+ context 'when the association is an embeds many' do
849
1024
 
850
1025
  let!(:address) do
851
1026
  Address.new