mongoid 7.2.0.rc1 → 7.3.0

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.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +45 -10
  5. data/lib/config/locales/en.yml +2 -2
  6. data/lib/mongoid/association/accessors.rb +1 -1
  7. data/lib/mongoid/association/constrainable.rb +1 -1
  8. data/lib/mongoid/association/depending.rb +4 -4
  9. data/lib/mongoid/association/embedded/batchable.rb +1 -1
  10. data/lib/mongoid/association/embedded/embedded_in.rb +1 -1
  11. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +10 -3
  12. data/lib/mongoid/association/nested/many.rb +1 -1
  13. data/lib/mongoid/association/nested/one.rb +4 -2
  14. data/lib/mongoid/association/proxy.rb +6 -1
  15. data/lib/mongoid/association/referenced/auto_save.rb +2 -2
  16. data/lib/mongoid/association/referenced/has_many/enumerable.rb +493 -495
  17. data/lib/mongoid/association/referenced/has_many/proxy.rb +2 -2
  18. data/lib/mongoid/association/referenced/has_one/buildable.rb +8 -0
  19. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_one/proxy.rb +6 -1
  21. data/lib/mongoid/attributes.rb +32 -14
  22. data/lib/mongoid/attributes/projector.rb +120 -0
  23. data/lib/mongoid/cacheable.rb +2 -2
  24. data/lib/mongoid/clients.rb +1 -1
  25. data/lib/mongoid/clients/factory.rb +22 -8
  26. data/lib/mongoid/config.rb +19 -2
  27. data/lib/mongoid/contextual/aggregable/mongo.rb +10 -8
  28. data/lib/mongoid/copyable.rb +6 -2
  29. data/lib/mongoid/criteria.rb +4 -5
  30. data/lib/mongoid/criteria/findable.rb +1 -1
  31. data/lib/mongoid/criteria/queryable/expandable.rb +0 -24
  32. data/lib/mongoid/criteria/queryable/extensions.rb +0 -4
  33. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
  34. data/lib/mongoid/criteria/queryable/mergeable.rb +46 -20
  35. data/lib/mongoid/criteria/queryable/selectable.rb +8 -8
  36. data/lib/mongoid/criteria/queryable/selector.rb +0 -4
  37. data/lib/mongoid/document.rb +4 -17
  38. data/lib/mongoid/errors/delete_restriction.rb +8 -9
  39. data/lib/mongoid/evolvable.rb +1 -1
  40. data/lib/mongoid/extensions.rb +1 -0
  41. data/lib/mongoid/extensions/boolean.rb +1 -2
  42. data/lib/mongoid/extensions/false_class.rb +1 -1
  43. data/lib/mongoid/extensions/hash.rb +2 -2
  44. data/lib/mongoid/extensions/true_class.rb +1 -1
  45. data/lib/mongoid/fields.rb +46 -5
  46. data/lib/mongoid/inspectable.rb +1 -1
  47. data/lib/mongoid/interceptable.rb +3 -1
  48. data/lib/mongoid/matcher.rb +26 -43
  49. data/lib/mongoid/matcher/bits.rb +41 -0
  50. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  51. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  52. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  53. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  54. data/lib/mongoid/matcher/elem_match.rb +2 -1
  55. data/lib/mongoid/matcher/expression.rb +9 -14
  56. data/lib/mongoid/matcher/field_expression.rb +4 -5
  57. data/lib/mongoid/matcher/field_operator.rb +13 -11
  58. data/lib/mongoid/matcher/mod.rb +17 -0
  59. data/lib/mongoid/matcher/type.rb +99 -0
  60. data/lib/mongoid/persistable/deletable.rb +1 -2
  61. data/lib/mongoid/persistable/destroyable.rb +8 -2
  62. data/lib/mongoid/persistable/updatable.rb +27 -2
  63. data/lib/mongoid/query_cache.rb +35 -29
  64. data/lib/mongoid/reloadable.rb +5 -0
  65. data/lib/mongoid/selectable.rb +5 -7
  66. data/lib/mongoid/shardable.rb +21 -5
  67. data/lib/mongoid/stringified_symbol.rb +53 -0
  68. data/lib/mongoid/touchable.rb +23 -4
  69. data/lib/mongoid/version.rb +1 -1
  70. data/lib/rails/generators/mongoid/config/config_generator.rb +8 -1
  71. data/spec/README.md +19 -4
  72. data/spec/integration/app_spec.rb +175 -88
  73. data/spec/integration/associations/embeds_many_spec.rb +68 -0
  74. data/spec/integration/associations/embeds_one_spec.rb +24 -0
  75. data/spec/integration/associations/has_many_spec.rb +60 -0
  76. data/spec/integration/associations/has_one_spec.rb +108 -0
  77. data/spec/integration/callbacks_models.rb +49 -0
  78. data/spec/integration/callbacks_spec.rb +216 -0
  79. data/spec/integration/criteria/date_field_spec.rb +1 -1
  80. data/spec/integration/document_spec.rb +30 -0
  81. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  82. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  83. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  84. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  85. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  86. data/spec/integration/matcher_operator_data/elem_match.yml +46 -0
  87. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  88. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  89. data/spec/integration/matcher_operator_data/implicit_traversal.yml +96 -0
  90. data/spec/integration/matcher_operator_data/in.yml +16 -0
  91. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  92. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  93. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  94. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  95. data/spec/integration/matcher_operator_data/type.yml +70 -0
  96. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  97. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  98. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  99. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  100. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  101. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  102. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  103. data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
  104. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  105. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  106. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  107. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  108. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  109. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  110. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  111. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  112. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  113. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  114. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  115. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  116. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  117. data/spec/integration/stringified_symbol_field_spec.rb +190 -0
  118. data/spec/lite_spec_helper.rb +9 -7
  119. data/spec/mongoid/association/depending_spec.rb +391 -352
  120. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +50 -0
  121. data/spec/mongoid/association/nested/one_spec.rb +18 -14
  122. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +25 -8
  123. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +1 -1
  124. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +1 -1
  125. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1 -1
  126. data/spec/mongoid/association/referenced/has_many_models.rb +12 -0
  127. data/spec/mongoid/association/referenced/has_one_models.rb +20 -0
  128. data/spec/mongoid/association/referenced/has_one_spec.rb +1 -1
  129. data/spec/mongoid/atomic/paths_spec.rb +105 -12
  130. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  131. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  132. data/spec/mongoid/attributes/projector_spec.rb +41 -0
  133. data/spec/mongoid/attributes_spec.rb +333 -0
  134. data/spec/mongoid/clients/factory_spec.rb +48 -0
  135. data/spec/mongoid/config_spec.rb +32 -0
  136. data/spec/mongoid/contextual/atomic_spec.rb +17 -4
  137. data/spec/mongoid/contextual/mongo_spec.rb +2 -2
  138. data/spec/mongoid/copyable_spec.rb +44 -17
  139. data/spec/mongoid/copyable_spec_models.rb +14 -0
  140. data/spec/mongoid/criteria/modifiable_spec.rb +1 -1
  141. data/spec/mongoid/criteria/queryable/expandable_spec.rb +0 -73
  142. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +1 -1
  143. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +105 -7
  144. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +265 -24
  145. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +39 -0
  146. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -565
  147. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +590 -0
  148. data/spec/mongoid/criteria_projection_spec.rb +411 -0
  149. data/spec/mongoid/criteria_spec.rb +0 -275
  150. data/spec/mongoid/document_fields_spec.rb +26 -0
  151. data/spec/mongoid/document_spec.rb +13 -13
  152. data/spec/mongoid/equality_spec.rb +0 -1
  153. data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
  154. data/spec/mongoid/extensions/false_class_spec.rb +1 -1
  155. data/spec/mongoid/extensions/string_spec.rb +5 -5
  156. data/spec/mongoid/extensions/stringified_symbol_spec.rb +85 -0
  157. data/spec/mongoid/extensions/true_class_spec.rb +1 -1
  158. data/spec/mongoid/fields/localized_spec.rb +4 -4
  159. data/spec/mongoid/fields_spec.rb +4 -4
  160. data/spec/mongoid/inspectable_spec.rb +12 -4
  161. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  162. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +68 -88
  163. data/spec/mongoid/matcher/extract_attribute_spec.rb +3 -13
  164. data/spec/mongoid/persistable/deletable_spec.rb +175 -1
  165. data/spec/mongoid/persistable/destroyable_spec.rb +191 -3
  166. data/spec/mongoid/persistable/savable_spec.rb +3 -5
  167. data/spec/mongoid/persistable/settable_spec.rb +30 -0
  168. data/spec/mongoid/persistable/upsertable_spec.rb +1 -1
  169. data/spec/mongoid/query_cache_middleware_spec.rb +8 -0
  170. data/spec/mongoid/reloadable_spec.rb +18 -1
  171. data/spec/mongoid/shardable_spec.rb +44 -0
  172. data/spec/mongoid/touchable_spec.rb +104 -16
  173. data/spec/mongoid/touchable_spec_models.rb +52 -0
  174. data/spec/mongoid/validatable_spec.rb +1 -1
  175. data/spec/shared/LICENSE +20 -0
  176. data/spec/shared/bin/get-mongodb-download-url +17 -0
  177. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  178. data/spec/shared/lib/mrss/cluster_config.rb +221 -0
  179. data/spec/shared/lib/mrss/constraints.rb +354 -0
  180. data/spec/shared/lib/mrss/docker_runner.rb +265 -0
  181. data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
  182. data/spec/shared/lib/mrss/server_version_registry.rb +115 -0
  183. data/spec/shared/lib/mrss/spec_organizer.rb +162 -0
  184. data/spec/shared/lib/mrss/utils.rb +15 -0
  185. data/spec/shared/share/Dockerfile.erb +231 -0
  186. data/spec/shared/shlib/distro.sh +73 -0
  187. data/spec/shared/shlib/server.sh +290 -0
  188. data/spec/shared/shlib/set_env.sh +128 -0
  189. data/spec/spec_helper.rb +7 -1
  190. data/spec/support/client_registry.rb +9 -0
  191. data/spec/support/constraints.rb +0 -226
  192. data/spec/support/models/bolt.rb +8 -0
  193. data/spec/support/models/customer.rb +11 -0
  194. data/spec/support/models/customer_address.rb +12 -0
  195. data/spec/support/models/dictionary.rb +6 -0
  196. data/spec/support/models/hole.rb +13 -0
  197. data/spec/support/models/mop.rb +9 -0
  198. data/spec/support/models/nut.rb +8 -0
  199. data/spec/support/models/order.rb +11 -0
  200. data/spec/support/models/person.rb +8 -0
  201. data/spec/support/models/sealer.rb +8 -0
  202. data/spec/support/models/series.rb +1 -0
  203. data/spec/support/models/shirt.rb +12 -0
  204. data/spec/support/models/spacer.rb +8 -0
  205. data/spec/support/models/threadlocker.rb +8 -0
  206. data/spec/support/models/washer.rb +8 -0
  207. data/spec/support/models/wiki_page.rb +1 -0
  208. data/spec/support/spec_config.rb +8 -0
  209. metadata +655 -507
  210. metadata.gz.sig +0 -0
  211. data/spec/support/child_process_helper.rb +0 -79
  212. data/spec/support/cluster_config.rb +0 -158
  213. data/spec/support/lite_constraints.rb +0 -22
  214. data/spec/support/spec_organizer.rb +0 -130
@@ -0,0 +1,93 @@
1
+ - name: simple inclusion - integer
2
+ projection:
3
+ foo: 1
4
+
5
+ queries: &field-inclusion-expectations
6
+ - query: foo
7
+ allowed: true
8
+ - query: bar
9
+ allowed: false
10
+
11
+ - name: simple inclusion - boolean
12
+ projection:
13
+ foo: true
14
+
15
+ queries: &field-inclusion-expectations
16
+ - query: foo
17
+ allowed: true
18
+ - query: bar
19
+ allowed: false
20
+
21
+ - name: positive integer - inclusion
22
+ projection:
23
+ foo: 2
24
+
25
+ queries: *field-inclusion-expectations
26
+
27
+ - name: simple exclusion - integer
28
+ projection:
29
+ foo: 0
30
+
31
+ queries: &field-exclusion-expectations
32
+ - query: foo
33
+ allowed: false
34
+ - query: bar
35
+ allowed: true
36
+
37
+ - name: simple exclusion - boolean
38
+ projection:
39
+ foo: false
40
+
41
+ queries: &field-exclusion-expectations
42
+ - query: foo
43
+ allowed: false
44
+ - query: bar
45
+ allowed: true
46
+
47
+ - name: negative integer - exclusion
48
+ projection:
49
+ foo: -2
50
+
51
+ queries: *field-exclusion-expectations
52
+
53
+ - name: field inclusion with _id exclusion
54
+ projection:
55
+ foo: 1
56
+ _id: 0
57
+
58
+ queries: &field-inclusion-with-id-exclusion-expectations
59
+ - query: foo
60
+ allowed: true
61
+
62
+ - query: bar
63
+ allowed: false
64
+
65
+ - query: _id
66
+ allowed: false
67
+
68
+ - name: _id exclusion with field inclusion
69
+ projection:
70
+ _id: 0
71
+ foo: 1
72
+
73
+ queries: *field-inclusion-with-id-exclusion-expectations
74
+
75
+ - name: id inclusion
76
+ projection:
77
+ _id: 1
78
+
79
+ queries:
80
+ - query: _id
81
+ allowed: true
82
+ - query: foo
83
+ allowed: false
84
+
85
+ - name: id exclusion
86
+ projection:
87
+ _id: false
88
+
89
+ queries:
90
+ - query: _id
91
+ allowed: false
92
+ - query: foo
93
+ allowed: true
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require "spec_helper"
5
+
6
+ describe Mongoid::Attributes::Projector do
7
+ Dir[File.join(File.dirname(__FILE__), 'projector_data', '*.yml')].sort.each do |path|
8
+ context File.basename(path) do
9
+ specs = YAML.load(File.read(path))
10
+
11
+ specs.each do |spec|
12
+ context spec['name'] do
13
+
14
+ if spec['pending']
15
+ pending spec['pending'].to_s
16
+ end
17
+
18
+ let(:projection) do
19
+ spec['projection']
20
+ end
21
+
22
+ let(:projector) do
23
+ Mongoid::Attributes::Projector.new(projection)
24
+ end
25
+
26
+ spec.fetch('queries').each do |query_spec|
27
+ context query_spec.fetch('query').inspect do
28
+ let(:query) { query_spec['query'] }
29
+
30
+ context 'attribute_or_path_allowed?' do
31
+ it "is #{query_spec.fetch('allowed')}" do
32
+ projector.attribute_or_path_allowed?(query).should be query_spec['allowed']
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -96,6 +96,49 @@ describe Mongoid::Attributes do
96
96
  it "does not raise an error" do
97
97
  expect(from_db.map).to eq(map)
98
98
  end
99
+
100
+ context 'when only one of the hash fields is projected' do
101
+
102
+ let(:map) do
103
+ { 'dates' => { 'y' => { '2016' => 'Berlin', '2017' => 'Munich' } } }
104
+ end
105
+
106
+ let(:expected) do
107
+ { 'dates' => { 'y' => {
108
+ '2016' => 'Berlin',
109
+ } } }
110
+ end
111
+
112
+ it 'retrieves only the projected fields' do
113
+ expect(from_db.map).to eq(expected)
114
+ end
115
+ end
116
+
117
+ context 'when several of the hash fields is projected' do
118
+
119
+ let(:map) do
120
+ { 'dates' => { 'y' => {
121
+ '2016' => 'Berlin',
122
+ '2017' => 'Munich',
123
+ '2018' => 'Krakow',
124
+ } } }
125
+ end
126
+
127
+ let(:expected) do
128
+ { 'dates' => { 'y' => {
129
+ '2016' => 'Berlin',
130
+ '2018' => 'Krakow',
131
+ } } }
132
+ end
133
+
134
+ let(:from_db) do
135
+ Person.only('map.dates.y.2016', 'map.dates.y.2018').first
136
+ end
137
+
138
+ it 'retrieves only the projected fields' do
139
+ expect(from_db.map).to eq(expected)
140
+ end
141
+ end
99
142
  end
100
143
  end
101
144
 
@@ -245,6 +288,91 @@ describe Mongoid::Attributes do
245
288
  end
246
289
  end
247
290
 
291
+ context "when the field was not explicitly defined" do
292
+
293
+ context "when excluding with only and the field was not excluded" do
294
+
295
+ let(:from_db) do
296
+ Person.only(:_id).first
297
+ end
298
+
299
+ it "raises an error" do
300
+ expect {
301
+ from_db[:undefined_field]
302
+ }.to raise_error(ActiveModel::MissingAttributeError)
303
+ end
304
+ end
305
+
306
+ context "when excluding with without and the field was excluded" do
307
+
308
+ let(:from_db) do
309
+ Person.without(:title).first
310
+ end
311
+
312
+ it "raises an error" do
313
+ expect {
314
+ from_db[:title]
315
+ }.to raise_error(ActiveModel::MissingAttributeError)
316
+ end
317
+ end
318
+
319
+ context "when excluding with without and the field was not excluded" do
320
+
321
+ let(:from_db) do
322
+ Person.without(:title).first
323
+ end
324
+
325
+ it "returns nil" do
326
+ from_db[:undefined_field].should be nil
327
+ end
328
+ end
329
+ end
330
+
331
+ context 'when projecting with #only' do
332
+ let!(:person) do
333
+ Person.create(title: 'sir', name: { first_name: 'Jose', language: { name: 'es' } })
334
+ end
335
+
336
+ context 'when projecting an embedded association' do
337
+ let(:from_db) do
338
+ Person.only(:name).first
339
+ end
340
+
341
+ context 'when retrieving a field of the association using the dot notation' do
342
+
343
+ it 'retrieves the field' do
344
+ expect(from_db['name.first_name']).to eq 'Jose'
345
+ end
346
+ end
347
+
348
+ context 'when retrieving a field of a nested association using the dot notation' do
349
+ it 'retrieves the field' do
350
+ expect(from_db['name.language.name']).to eq 'es'
351
+ end
352
+ end
353
+ end
354
+
355
+ context 'when projecting a sub-association of an embedded association' do
356
+ let(:from_db) do
357
+ Person.only('name.language').first
358
+ end
359
+
360
+ context 'when retrieving a field under the projected sub-association' do
361
+ it 'retrieves the field' do
362
+ expect(from_db['name.language.name']).to eq 'es'
363
+ end
364
+ end
365
+
366
+ context 'when retrieving a non-projected field' do
367
+ it 'raises MissingAttributeError' do
368
+ expect do
369
+ from_db['name.first_name']
370
+ end.to raise_error(ActiveModel::MissingAttributeError)
371
+ end
372
+ end
373
+ end
374
+ end
375
+
248
376
  context "when the attribute does not exist" do
249
377
 
250
378
  before do
@@ -329,6 +457,67 @@ describe Mongoid::Attributes do
329
457
  expect(terms).to eq(true)
330
458
  end
331
459
  end
460
+
461
+ context 'when the field is not explicitly defined' do
462
+ let(:bar) { Bar.new }
463
+
464
+ before do
465
+ bar['missing_field'] = 42
466
+ end
467
+
468
+ it 'writes the value into attributes' do
469
+ bar.attributes.should == {'_id' => bar.id, 'missing_field' => 42}
470
+ end
471
+
472
+ it 'makes the attribute accessible via []' do
473
+ bar['missing_field'].should == 42
474
+ end
475
+
476
+ context 'when writing fields on a document with projection' do
477
+
478
+ let!(:person) do
479
+ Person.create(title: "sir")
480
+ end
481
+
482
+ context "when excluding with only and the field was not excluded" do
483
+
484
+ let(:from_db) do
485
+ Person.only(:_id).first
486
+ end
487
+
488
+ it "raises an error" do
489
+ expect {
490
+ from_db[:undefined_field] = 'x'
491
+ }.to raise_error(ActiveModel::MissingAttributeError)
492
+ end
493
+ end
494
+
495
+ context "when excluding with without and the field was excluded" do
496
+
497
+ let(:from_db) do
498
+ Person.without(:title).first
499
+ end
500
+
501
+ it "raises an error" do
502
+ expect {
503
+ from_db[:title] = 'x'
504
+ }.to raise_error(ActiveModel::MissingAttributeError)
505
+ end
506
+ end
507
+
508
+ context "when excluding with without and the field was not excluded" do
509
+
510
+ let(:from_db) do
511
+ Person.without(:title).first
512
+ end
513
+
514
+ it "writes the value" do
515
+ from_db[:undefined_field] = 'x'
516
+ from_db[:undefined_field].should == 'x'
517
+ end
518
+ end
519
+ end
520
+ end
332
521
  end
333
522
 
334
523
  describe "#_id" do
@@ -893,6 +1082,50 @@ describe Mongoid::Attributes do
893
1082
  expect(person.age_before_type_cast).to eq("old")
894
1083
  end
895
1084
  end
1085
+
1086
+ context 'when reading fields on a document with projection' do
1087
+
1088
+ let!(:person) do
1089
+ Person.create(title: "sir")
1090
+ end
1091
+
1092
+ context "when excluding with only and the field was not excluded" do
1093
+
1094
+ let(:from_db) do
1095
+ Person.only(:_id).first
1096
+ end
1097
+
1098
+ it "raises an error" do
1099
+ expect {
1100
+ from_db.read_attribute(:undefined_field)
1101
+ }.to raise_error(ActiveModel::MissingAttributeError)
1102
+ end
1103
+ end
1104
+
1105
+ context "when excluding with without and the field was excluded" do
1106
+
1107
+ let(:from_db) do
1108
+ Person.without(:title).first
1109
+ end
1110
+
1111
+ it "raises an error" do
1112
+ expect {
1113
+ from_db.read_attribute(:title)
1114
+ }.to raise_error(ActiveModel::MissingAttributeError)
1115
+ end
1116
+ end
1117
+
1118
+ context "when excluding with without and the field was not excluded" do
1119
+
1120
+ let(:from_db) do
1121
+ Person.without(:title).first
1122
+ end
1123
+
1124
+ it "returns nil" do
1125
+ from_db.read_attribute(:undefined_field).should be nil
1126
+ end
1127
+ end
1128
+ end
896
1129
  end
897
1130
 
898
1131
  describe "#attribute_present?" do
@@ -1398,6 +1631,51 @@ describe Mongoid::Attributes do
1398
1631
  expect(dictionary.description).to eq('foo')
1399
1632
  end
1400
1633
  end
1634
+
1635
+ context 'when writing fields on a document with projection' do
1636
+
1637
+ let!(:person) do
1638
+ Person.create(title: "sir")
1639
+ end
1640
+
1641
+ context "when excluding with only and the field was not excluded" do
1642
+
1643
+ let(:from_db) do
1644
+ Person.only(:_id).first
1645
+ end
1646
+
1647
+ it "raises an error" do
1648
+ expect {
1649
+ from_db.write_attribute(:undefined_field, 'x')
1650
+ }.to raise_error(ActiveModel::MissingAttributeError)
1651
+ end
1652
+ end
1653
+
1654
+ context "when excluding with without and the field was excluded" do
1655
+
1656
+ let(:from_db) do
1657
+ Person.without(:title).first
1658
+ end
1659
+
1660
+ it "raises an error" do
1661
+ expect {
1662
+ from_db.write_attribute(:title, 'x')
1663
+ }.to raise_error(ActiveModel::MissingAttributeError)
1664
+ end
1665
+ end
1666
+
1667
+ context "when excluding with without and the field was not excluded" do
1668
+
1669
+ let(:from_db) do
1670
+ Person.without(:title).first
1671
+ end
1672
+
1673
+ it "writes the value" do
1674
+ from_db.write_attribute(:undefined_field, 'x')
1675
+ from_db.read_attribute(:undefined_field).should == 'x'
1676
+ end
1677
+ end
1678
+ end
1401
1679
  end
1402
1680
 
1403
1681
  describe "#typed_value_for" do
@@ -1651,6 +1929,61 @@ describe Mongoid::Attributes do
1651
1929
  end
1652
1930
  end
1653
1931
 
1932
+ describe '#unalias_attribute' do
1933
+ let(:shirt) { Shirt.new }
1934
+ let(:id) { '1234' }
1935
+
1936
+ context 'when creating object' do
1937
+ let(:shirt) { Shirt.new(id: id) }
1938
+
1939
+ it 'id and _id are not the same' do
1940
+ expect(shirt.id).to eq(id)
1941
+ expect(shirt._id).not_to eq(id)
1942
+ end
1943
+ end
1944
+
1945
+ context 'update' do
1946
+ before do
1947
+ shirt.update(id: id)
1948
+ end
1949
+
1950
+ it 'updates id but not_id' do
1951
+ expect(shirt.id).to eq(id)
1952
+ expect(shirt._id).not_to eq(id)
1953
+ end
1954
+ end
1955
+
1956
+ context 'id=' do
1957
+ before do
1958
+ shirt.id = id
1959
+ end
1960
+
1961
+ it 'sets id and not _id' do
1962
+ expect(shirt.id).to eq(id)
1963
+ expect(shirt._id).not_to eq(id)
1964
+ end
1965
+ end
1966
+
1967
+ context 'id?' do
1968
+ context 'with null id' do
1969
+ it 'returns false' do
1970
+ expect(shirt.id?).to be false
1971
+ expect(shirt._id?).to be true
1972
+ end
1973
+ end
1974
+
1975
+ context 'with id' do
1976
+ before do
1977
+ shirt.id = id
1978
+ end
1979
+
1980
+ it 'returns true' do
1981
+ expect(shirt.id?).to be true
1982
+ end
1983
+ end
1984
+ end
1985
+ end
1986
+
1654
1987
  describe "#alias_attribute" do
1655
1988
 
1656
1989
  let(:product) do