mongoid 4.0.0.alpha1 → 4.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +1 -1
  4. data/lib/config/locales/en.yml +4 -6
  5. data/lib/mongoid/attributes/processing.rb +1 -1
  6. data/lib/mongoid/contextual.rb +2 -0
  7. data/lib/mongoid/contextual/map_reduce.rb +3 -1
  8. data/lib/mongoid/contextual/none.rb +90 -0
  9. data/lib/mongoid/criteria.rb +25 -0
  10. data/lib/mongoid/extensions/array.rb +1 -1
  11. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  12. data/lib/mongoid/extensions/date.rb +1 -1
  13. data/lib/mongoid/extensions/date_time.rb +3 -3
  14. data/lib/mongoid/extensions/float.rb +1 -1
  15. data/lib/mongoid/extensions/hash.rb +1 -1
  16. data/lib/mongoid/extensions/integer.rb +1 -1
  17. data/lib/mongoid/extensions/object.rb +3 -3
  18. data/lib/mongoid/extensions/object_id.rb +1 -1
  19. data/lib/mongoid/extensions/range.rb +1 -1
  20. data/lib/mongoid/extensions/regexp.rb +1 -1
  21. data/lib/mongoid/extensions/set.rb +1 -1
  22. data/lib/mongoid/extensions/string.rb +1 -1
  23. data/lib/mongoid/extensions/symbol.rb +1 -1
  24. data/lib/mongoid/extensions/time.rb +1 -1
  25. data/lib/mongoid/extensions/time_with_zone.rb +1 -1
  26. data/lib/mongoid/findable.rb +1 -0
  27. data/lib/mongoid/interceptable.rb +4 -2
  28. data/lib/mongoid/persistable.rb +4 -3
  29. data/lib/mongoid/relations/embedded/many.rb +1 -1
  30. data/lib/mongoid/relations/metadata.rb +8 -4
  31. data/lib/mongoid/relations/referenced/many.rb +1 -1
  32. data/lib/mongoid/relations/touchable.rb +1 -1
  33. data/lib/mongoid/scopable.rb +2 -26
  34. data/lib/mongoid/validatable/uniqueness.rb +13 -0
  35. data/lib/mongoid/version.rb +1 -1
  36. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +0 -4
  37. data/spec/app/models/acolyte.rb +1 -1
  38. data/spec/app/models/address.rb +2 -2
  39. data/spec/app/models/appointment.rb +1 -1
  40. data/spec/app/models/audio.rb +1 -1
  41. data/spec/app/models/dog.rb +1 -1
  42. data/spec/app/models/event.rb +1 -1
  43. data/spec/app/models/house.rb +1 -1
  44. data/spec/app/models/message.rb +8 -0
  45. data/spec/app/models/person.rb +3 -2
  46. data/spec/app/models/player.rb +2 -2
  47. data/spec/app/models/post.rb +3 -2
  48. data/spec/app/models/preference.rb +1 -1
  49. data/spec/app/models/sound.rb +1 -1
  50. data/spec/app/models/symptom.rb +1 -1
  51. data/spec/app/models/tree.rb +2 -2
  52. data/spec/app/models/video.rb +1 -1
  53. data/spec/mongoid/attributes_spec.rb +3 -3
  54. data/spec/mongoid/changeable_spec.rb +3 -3
  55. data/spec/mongoid/contextual/mongo_spec.rb +14 -14
  56. data/spec/mongoid/contextual/none_spec.rb +127 -0
  57. data/spec/mongoid/criteria/scopable_spec.rb +6 -6
  58. data/spec/mongoid/criteria_spec.rb +52 -96
  59. data/spec/mongoid/errors/invalid_scope_spec.rb +2 -2
  60. data/spec/mongoid/errors/mongoid_error_spec.rb +1 -1
  61. data/spec/mongoid/extensions/date_time_spec.rb +1 -1
  62. data/spec/mongoid/fields_spec.rb +6 -7
  63. data/spec/mongoid/findable_spec.rb +65 -0
  64. data/spec/mongoid/interceptable_spec.rb +11 -11
  65. data/spec/mongoid/persistable_spec.rb +4 -4
  66. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  67. data/spec/mongoid/relations/bindings/embedded/in_spec.rb +5 -5
  68. data/spec/mongoid/relations/bindings/embedded/many_spec.rb +1 -1
  69. data/spec/mongoid/relations/bindings/embedded/one_spec.rb +2 -2
  70. data/spec/mongoid/relations/bindings/referenced/in_spec.rb +14 -14
  71. data/spec/mongoid/relations/bindings/referenced/many_spec.rb +4 -4
  72. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +10 -10
  73. data/spec/mongoid/relations/bindings/referenced/one_spec.rb +6 -6
  74. data/spec/mongoid/relations/builders/referenced/in_spec.rb +2 -2
  75. data/spec/mongoid/relations/builders/referenced/one_spec.rb +1 -1
  76. data/spec/mongoid/relations/cascading/delete_spec.rb +2 -2
  77. data/spec/mongoid/relations/cascading/destroy_spec.rb +4 -4
  78. data/spec/mongoid/relations/cascading/nullify_spec.rb +2 -2
  79. data/spec/mongoid/relations/cascading/restrict_spec.rb +4 -4
  80. data/spec/mongoid/relations/eager_spec.rb +6 -6
  81. data/spec/mongoid/relations/embedded/many_spec.rb +36 -5
  82. data/spec/mongoid/relations/metadata_spec.rb +34 -15
  83. data/spec/mongoid/relations/referenced/many_spec.rb +19 -4
  84. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +7 -7
  85. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  86. data/spec/mongoid/relations/targets/enumerable_spec.rb +2 -2
  87. data/spec/mongoid/relations/touchable_spec.rb +17 -0
  88. data/spec/mongoid/scopable_spec.rb +17 -40
  89. data/spec/mongoid/sessions/options_spec.rb +1 -1
  90. data/spec/mongoid/timestamps/timeless_spec.rb +6 -6
  91. data/spec/mongoid/timestamps/updated/short_spec.rb +1 -1
  92. data/spec/mongoid/timestamps/updated_spec.rb +1 -1
  93. data/spec/mongoid/timestamps_spec.rb +2 -2
  94. data/spec/mongoid/validatable/associated_spec.rb +4 -4
  95. data/spec/mongoid/validatable/length_spec.rb +7 -7
  96. data/spec/mongoid/validatable/presence_spec.rb +6 -6
  97. data/spec/mongoid/validatable/uniqueness_spec.rb +44 -3
  98. data/spec/rails/mongoid_spec.rb +20 -20
  99. data/spec/spec_helper.rb +1 -0
  100. metadata +55 -50
@@ -45,7 +45,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
45
45
  end
46
46
 
47
47
  it "does not persist the child document" do
48
- preference.should_receive(:save).never
48
+ expect(preference).to receive(:save).never
49
49
  article.preferences.send(method, preference)
50
50
  end
51
51
  end
@@ -515,7 +515,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
515
515
  context "when errors are raised" do
516
516
 
517
517
  before do
518
- post.should_receive(:before_add_tag).and_raise
518
+ expect(post).to receive(:before_add_tag).and_raise
519
519
  end
520
520
 
521
521
  it "does not add the document to the relation" do
@@ -537,7 +537,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
537
537
  context "when errors are raised" do
538
538
 
539
539
  before do
540
- post.should_receive(:after_add_tag).and_raise
540
+ expect(post).to receive(:after_add_tag).and_raise
541
541
  end
542
542
 
543
543
  it "adds the document to the relation" do
@@ -1185,7 +1185,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
1185
1185
  context "when errors are raised" do
1186
1186
 
1187
1187
  before do
1188
- post.should_receive(:before_remove_tag).and_raise
1188
+ expect(post).to receive(:before_remove_tag).and_raise
1189
1189
  end
1190
1190
 
1191
1191
  it "does not remove the document from the relation" do
@@ -1217,7 +1217,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
1217
1217
  context "when errors are raised" do
1218
1218
 
1219
1219
  before do
1220
- post.should_receive(:after_remove_tag).and_raise
1220
+ expect(post).to receive(:after_remove_tag).and_raise
1221
1221
  end
1222
1222
 
1223
1223
  it "removes the document from the relation" do
@@ -2119,7 +2119,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
2119
2119
  context "when errors are raised" do
2120
2120
 
2121
2121
  before do
2122
- post.should_receive(:before_remove_tag).and_raise
2122
+ expect(post).to receive(:before_remove_tag).and_raise
2123
2123
  end
2124
2124
 
2125
2125
  it "does not remove the document from the relation" do
@@ -2151,7 +2151,7 @@ describe Mongoid::Relations::Referenced::ManyToMany do
2151
2151
  context "when errors are raised" do
2152
2152
 
2153
2153
  before do
2154
- post.should_receive(:after_remove_tag).and_raise
2154
+ expect(post).to receive(:after_remove_tag).and_raise
2155
2155
  end
2156
2156
 
2157
2157
  it "removes the document from the relation" do
@@ -57,7 +57,7 @@ describe Mongoid::Relations::Referenced::One do
57
57
  end
58
58
 
59
59
  before do
60
- metadata.should_receive(:criteria).never
60
+ expect(metadata).to receive(:criteria).never
61
61
  person.game = game
62
62
  end
63
63
 
@@ -787,7 +787,7 @@ describe Mongoid::Relations::Targets::Enumerable do
787
787
  end
788
788
 
789
789
  it "receives query only once" do
790
- criteria.should_receive(:first).once
790
+ expect(criteria).to receive(:first).once
791
791
  first
792
792
  end
793
793
  end
@@ -1212,7 +1212,7 @@ describe Mongoid::Relations::Targets::Enumerable do
1212
1212
  end
1213
1213
 
1214
1214
  it "receives query only once" do
1215
- criteria.should_receive(:last).once
1215
+ expect(criteria).to receive(:last).once
1216
1216
  last
1217
1217
  end
1218
1218
  end
@@ -269,6 +269,23 @@ describe Mongoid::Relations::Touchable do
269
269
  end
270
270
  end
271
271
 
272
+ context "when modifying the child" do
273
+
274
+ let!(:agency) do
275
+ Agency.create
276
+ end
277
+
278
+ let!(:agent) do
279
+ agency.agents.create(number: '1')
280
+ end
281
+
282
+ it "updates the parent's updated at" do
283
+ expect {
284
+ agent.update_attributes(number: '2')
285
+ }.to change { agency.updated_at }
286
+ end
287
+ end
288
+
272
289
  context "when destroying the child" do
273
290
 
274
291
  let!(:agency) do
@@ -4,29 +4,6 @@ describe Mongoid::Scopable do
4
4
 
5
5
  describe ".default_scope" do
6
6
 
7
- context "when provided a criteria" do
8
-
9
- let(:criteria) do
10
- Band.where(name: "Depeche Mode")
11
- end
12
-
13
- before do
14
- Band.default_scope criteria
15
- end
16
-
17
- after do
18
- Band.default_scoping = nil
19
- end
20
-
21
- it "adds the default scope to the class" do
22
- expect(Band.default_scoping.call).to eq(criteria)
23
- end
24
-
25
- it "flags as being default scoped" do
26
- expect(Band).to be_default_scoping
27
- end
28
- end
29
-
30
7
  context "when provided a proc" do
31
8
 
32
9
  let(:criteria) do
@@ -50,7 +27,7 @@ describe Mongoid::Scopable do
50
27
  end
51
28
  end
52
29
 
53
- context "when provided a non proc or criteria" do
30
+ context "when provided a non proc" do
54
31
 
55
32
  it "raises an error" do
56
33
  expect {
@@ -62,11 +39,11 @@ describe Mongoid::Scopable do
62
39
  context "when there is more then one default_scope" do
63
40
 
64
41
  let(:criteria) do
65
- Band.where(name: "Depeche Mode")
42
+ ->{ Band.where(name: "Depeche Mode") }
66
43
  end
67
44
 
68
45
  let(:additional_criteria) do
69
- Band.where(origin: "England")
46
+ ->{ Band.where(origin: "England") }
70
47
  end
71
48
 
72
49
  let(:proc_criteria) do
@@ -187,7 +164,7 @@ describe Mongoid::Scopable do
187
164
  context "when a block is provided" do
188
165
 
189
166
  before do
190
- Band.scope(:active, Band.where(active: true)) do
167
+ Band.scope(:active, ->{ Band.where(active: true) }) do
191
168
  def add_origin
192
169
  tap { |c| c.selector[:origin] = "Deutschland" }
193
170
  end
@@ -215,7 +192,7 @@ describe Mongoid::Scopable do
215
192
  before do
216
193
  Record.scope(
217
194
  :tool,
218
- Record.where(:name.in => [ "undertow", "aenima", "lateralus" ])
195
+ ->{ Record.where(:name.in => [ "undertow", "aenima", "lateralus" ]) }
219
196
  )
220
197
  end
221
198
 
@@ -249,7 +226,7 @@ describe Mongoid::Scopable do
249
226
  context "when no block is provided" do
250
227
 
251
228
  before do
252
- Band.scope(:active, Band.where(active: true).skip(10))
229
+ Band.scope(:active, ->{ Band.where(active: true).skip(10) })
253
230
  end
254
231
 
255
232
  after do
@@ -287,7 +264,7 @@ describe Mongoid::Scopable do
287
264
  context "when chained to another scope" do
288
265
 
289
266
  before do
290
- Band.scope(:english, Band.where(origin: "England"))
267
+ Band.scope(:english, ->{ Band.where(origin: "England") })
291
268
  end
292
269
 
293
270
  after do
@@ -369,8 +346,8 @@ describe Mongoid::Scopable do
369
346
 
370
347
  it "raises an exception" do
371
348
  expect {
372
- Band.scope(:active, Band.where(active: true))
373
- Band.scope(:active, Band.where(active: true))
349
+ Band.scope(:active, ->{ Band.where(active: true) })
350
+ Band.scope(:active, ->{ Band.where(active: true) })
374
351
  }.to raise_error(Mongoid::Errors::ScopeOverwrite)
375
352
  end
376
353
  end
@@ -386,8 +363,8 @@ describe Mongoid::Scopable do
386
363
  end
387
364
 
388
365
  it "raises no exception" do
389
- Band.scope(:active, Band.where(active: true))
390
- Band.scope(:active, Band.where(active: true))
366
+ Band.scope(:active, ->{ Band.where(active: true) })
367
+ Band.scope(:active, ->{ Band.where(active: true) })
391
368
  end
392
369
  end
393
370
  end
@@ -586,7 +563,7 @@ describe Mongoid::Scopable do
586
563
  end
587
564
  end
588
565
 
589
- context "when chaining a non proc with a proc" do
566
+ context "when chaining a proc with a proc" do
590
567
 
591
568
  context "when both scopes are or queries" do
592
569
 
@@ -595,7 +572,7 @@ describe Mongoid::Scopable do
595
572
  end
596
573
 
597
574
  before do
598
- Band.scope(:xxx, Band.any_of({ :aaa.gt => 0 }, { :bbb.gt => 0 }))
575
+ Band.scope(:xxx, ->{ Band.any_of({ :aaa.gt => 0 }, { :bbb.gt => 0 }) })
599
576
  Band.scope(:yyy, ->{ Band.any_of({ :ccc => nil }, { :ccc.gt => time }) })
600
577
  end
601
578
 
@@ -745,7 +722,7 @@ describe Mongoid::Scopable do
745
722
  end
746
723
 
747
724
  before do
748
- Band.default_scope criteria
725
+ Band.default_scope ->{ criteria }
749
726
  end
750
727
 
751
728
  after do
@@ -780,7 +757,7 @@ describe Mongoid::Scopable do
780
757
  end
781
758
 
782
759
  before do
783
- Band.default_scope criteria
760
+ Band.default_scope ->{ criteria }
784
761
  end
785
762
 
786
763
  after do
@@ -832,7 +809,7 @@ describe Mongoid::Scopable do
832
809
  context "when a named scope is called in the block" do
833
810
 
834
811
  before do
835
- Band.scope(:skipped, Band.skip(10))
812
+ Band.scope(:skipped, ->{ Band.skip(10) })
836
813
  end
837
814
 
838
815
  after do
@@ -858,7 +835,7 @@ describe Mongoid::Scopable do
858
835
  end
859
836
 
860
837
  before do
861
- Band.default_scope criteria
838
+ Band.default_scope ->{ criteria }
862
839
  end
863
840
 
864
841
  after do
@@ -50,7 +50,7 @@ describe Mongoid::Sessions::Options do
50
50
  end
51
51
 
52
52
  it "passes down the options to collection" do
53
- Moped::Session.any_instance.should_receive(:with).with(options).and_return({})
53
+ expect_any_instance_of(Moped::Session).to receive(:with).with(options).and_return({})
54
54
  instance.collection
55
55
  end
56
56
  end
@@ -73,11 +73,11 @@ describe Mongoid::Timestamps::Timeless do
73
73
  end
74
74
 
75
75
  it "does not set the created timestamp" do
76
- document.created_at.should be_nil
76
+ expect(document.created_at).to be_nil
77
77
  end
78
78
 
79
79
  it "does not set the updated timestamp" do
80
- document.updated_at.should be_nil
80
+ expect(document.updated_at).to be_nil
81
81
  end
82
82
 
83
83
  it "clears out the timeless option after save" do
@@ -91,7 +91,7 @@ describe Mongoid::Timestamps::Timeless do
91
91
  end
92
92
 
93
93
  it "sets the updated timestamp" do
94
- document.updated_at.should_not be_nil
94
+ expect(document.updated_at).to_not be_nil
95
95
  end
96
96
  end
97
97
  end
@@ -103,11 +103,11 @@ describe Mongoid::Timestamps::Timeless do
103
103
  end
104
104
 
105
105
  it "does not set the created timestamp" do
106
- document.created_at.should be_nil
106
+ expect(document.created_at).to be_nil
107
107
  end
108
108
 
109
109
  it "does not set the updated timestamp" do
110
- document.updated_at.should be_nil
110
+ expect(document.updated_at).to be_nil
111
111
  end
112
112
 
113
113
  it "clears out the timeless option after save" do
@@ -121,7 +121,7 @@ describe Mongoid::Timestamps::Timeless do
121
121
  end
122
122
 
123
123
  it "sets the updated timestamp" do
124
- document.updated_at.should_not be_nil
124
+ expect(document.updated_at).to_not be_nil
125
125
  end
126
126
  end
127
127
  end
@@ -71,7 +71,7 @@ describe Mongoid::Timestamps::Updated::Short do
71
71
  end
72
72
 
73
73
  it "does not run the update callbacks" do
74
- agent.should_receive(:updated_at=).never
74
+ expect(agent).to receive(:updated_at=).never
75
75
  agent.save
76
76
  end
77
77
  end
@@ -63,7 +63,7 @@ describe Mongoid::Timestamps::Updated do
63
63
  end
64
64
 
65
65
  it "does not run the update callbacks" do
66
- agent.should_receive(:updated_at=).never
66
+ expect(agent).to receive(:updated_at=).never
67
67
  agent.save
68
68
  end
69
69
  end
@@ -49,7 +49,7 @@ describe Mongoid::Timestamps do
49
49
  end
50
50
 
51
51
  it "does not run the update callbacks" do
52
- document.should_receive(:updated_at=).never
52
+ expect(document).to receive(:updated_at=).never
53
53
  document.save
54
54
  end
55
55
  end
@@ -66,7 +66,7 @@ describe Mongoid::Timestamps do
66
66
  end
67
67
 
68
68
  it "does not set updated at" do
69
- document.should_receive(:updated_at=).never
69
+ expect(document).to receive(:updated_at=).never
70
70
  document.save
71
71
  end
72
72
  end
@@ -89,7 +89,7 @@ describe Mongoid::Validatable::AssociatedValidator do
89
89
  end
90
90
 
91
91
  before do
92
- associated.should_receive(:validated?).and_return(false)
92
+ expect(associated).to receive(:validated?).and_return(false)
93
93
  validator.validate_each(person, :name, associated)
94
94
  end
95
95
 
@@ -105,7 +105,7 @@ describe Mongoid::Validatable::AssociatedValidator do
105
105
  end
106
106
 
107
107
  before do
108
- associated.should_receive(:validated?).and_return(false)
108
+ expect(associated).to receive(:validated?).and_return(false)
109
109
  validator.validate_each(person, :name, associated)
110
110
  end
111
111
 
@@ -139,7 +139,7 @@ describe Mongoid::Validatable::AssociatedValidator do
139
139
  end
140
140
 
141
141
  before do
142
- associated.should_receive(:validated?).and_return(false)
142
+ expect(associated).to receive(:validated?).and_return(false)
143
143
  validator.validate_each(person, :addresses, [ associated ])
144
144
  end
145
145
 
@@ -155,7 +155,7 @@ describe Mongoid::Validatable::AssociatedValidator do
155
155
  end
156
156
 
157
157
  before do
158
- associated.should_receive(:validated?).and_return(false)
158
+ expect(associated).to receive(:validated?).and_return(false)
159
159
  validator.validate_each(person, :addresses, [ associated ])
160
160
  end
161
161
 
@@ -101,7 +101,7 @@ describe Mongoid::Validatable::LengthValidator do
101
101
  end
102
102
 
103
103
  it "adds no errors" do
104
- product.errors[:sku].should be_empty
104
+ expect(product.errors[:sku]).to be_empty
105
105
  end
106
106
  end
107
107
 
@@ -112,7 +112,7 @@ describe Mongoid::Validatable::LengthValidator do
112
112
  end
113
113
 
114
114
  it "adds errors" do
115
- product.errors[:sku].should eq(["is too long (maximum is 5 characters)"])
115
+ expect(product.errors[:sku]).to eq(["is too long (maximum is 5 characters)"])
116
116
  end
117
117
  end
118
118
  end
@@ -130,7 +130,7 @@ describe Mongoid::Validatable::LengthValidator do
130
130
  end
131
131
 
132
132
  it "adds no errors" do
133
- product.errors[:stock_keeping_unit].should be_empty
133
+ expect(product.errors[:stock_keeping_unit]).to be_empty
134
134
  end
135
135
  end
136
136
 
@@ -141,7 +141,7 @@ describe Mongoid::Validatable::LengthValidator do
141
141
  end
142
142
 
143
143
  it "adds errors" do
144
- product.errors[:stock_keeping_unit].should eq(["is too long (maximum is 5 characters)"])
144
+ expect(product.errors[:stock_keeping_unit]).to eq(["is too long (maximum is 5 characters)"])
145
145
  end
146
146
  end
147
147
  end
@@ -159,7 +159,7 @@ describe Mongoid::Validatable::LengthValidator do
159
159
  end
160
160
 
161
161
  it "adds no errors" do
162
- product.errors[:tagline].should be_empty
162
+ expect(product.errors[:tagline]).to be_empty
163
163
  end
164
164
  end
165
165
 
@@ -173,7 +173,7 @@ describe Mongoid::Validatable::LengthValidator do
173
173
  end
174
174
 
175
175
  it "adds errors" do
176
- product.errors[:tagline].should eq(["is too long (maximum is 5 characters)"])
176
+ expect(product.errors[:tagline]).to eq(["is too long (maximum is 5 characters)"])
177
177
  end
178
178
  end
179
179
 
@@ -184,7 +184,7 @@ describe Mongoid::Validatable::LengthValidator do
184
184
  end
185
185
 
186
186
  it "adds errors" do
187
- product.errors[:tagline].should eq(["is too long (maximum is 5 characters)"])
187
+ expect(product.errors[:tagline]).to eq(["is too long (maximum is 5 characters)"])
188
188
  end
189
189
  end
190
190
  end