mongoid 1.2.8 → 1.2.9

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 (44) hide show
  1. data/Rakefile +1 -1
  2. data/VERSION +1 -1
  3. data/lib/mongoid.rb +2 -3
  4. data/lib/mongoid/associations.rb +5 -3
  5. data/lib/mongoid/associations/belongs_to_related.rb +6 -9
  6. data/lib/mongoid/associations/has_many.rb +2 -0
  7. data/lib/mongoid/associations/has_many_related.rb +10 -0
  8. data/lib/mongoid/attributes.rb +6 -1
  9. data/lib/mongoid/commands.rb +2 -10
  10. data/lib/mongoid/components.rb +11 -12
  11. data/lib/mongoid/contexts/enumerable.rb +21 -11
  12. data/lib/mongoid/contexts/mongo.rb +40 -1
  13. data/lib/mongoid/criteria.rb +3 -29
  14. data/lib/mongoid/criterion/inclusion.rb +2 -1
  15. data/lib/mongoid/document.rb +5 -6
  16. data/lib/mongoid/extensions.rb +10 -0
  17. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  18. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  19. data/lib/mongoid/{caching.rb → extras.rb} +26 -3
  20. data/lib/mongoid/field.rb +20 -7
  21. data/lib/mongoid/finders.rb +10 -80
  22. data/mongoid.gemspec +15 -13
  23. data/spec/integration/mongoid/document_spec.rb +1 -1
  24. data/spec/models/person.rb +1 -0
  25. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +4 -0
  26. data/spec/unit/mongoid/associations/has_many_related_spec.rb +42 -13
  27. data/spec/unit/mongoid/associations/has_many_spec.rb +12 -7
  28. data/spec/unit/mongoid/associations_spec.rb +16 -0
  29. data/spec/unit/mongoid/attributes_spec.rb +23 -0
  30. data/spec/unit/mongoid/commands/destroy_spec.rb +3 -0
  31. data/spec/unit/mongoid/commands_spec.rb +4 -11
  32. data/spec/unit/mongoid/contexts/enumerable_spec.rb +16 -0
  33. data/spec/unit/mongoid/contexts/mongo_spec.rb +96 -0
  34. data/spec/unit/mongoid/criteria_spec.rb +11 -4
  35. data/spec/unit/mongoid/document_spec.rb +11 -0
  36. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +22 -0
  37. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
  38. data/spec/unit/mongoid/{enslavement_spec.rb → extras_spec.rb} +55 -2
  39. data/spec/unit/mongoid/field_spec.rb +62 -0
  40. data/spec/unit/mongoid/finders_spec.rb +36 -0
  41. metadata +69 -37
  42. data/HISTORY +0 -342
  43. data/lib/mongoid/enslavement.rb +0 -38
  44. data/spec/unit/mongoid/caching_spec.rb +0 -63
@@ -129,14 +129,16 @@ describe Mongoid::Associations::HasMany do
129
129
  @document,
130
130
  Mongoid::Associations::Options.new(:name => :addresses)
131
131
  )
132
- @address = Address.new(:street => "Yet Another")
132
+ @address = mock(:parentize => true, :write_attributes => true)
133
+ Address.expects(:instantiate).returns(@address)
134
+ @address.expects(:run_callbacks).with(:before_create)
135
+ @address.expects(:run_callbacks).with(:after_create)
133
136
  end
134
137
 
135
138
  it "builds and saves a new object" do
136
- Mongoid::Commands::Save.expects(:execute).returns(true)
139
+ @address.expects(:save).returns(true)
137
140
  address = @association.create({ :street => "Yet Another" })
138
- address.should be_a_kind_of(Address)
139
- address.street.should == "Yet Another"
141
+ address.should == @address
140
142
  end
141
143
 
142
144
  end
@@ -148,13 +150,16 @@ describe Mongoid::Associations::HasMany do
148
150
  @document,
149
151
  Mongoid::Associations::Options.new(:name => :shapes)
150
152
  )
153
+ @circle = mock(:parentize => true, :write_attributes => true)
154
+ Circle.expects(:instantiate).returns(@circle)
155
+ @circle.expects(:run_callbacks).with(:before_create)
156
+ @circle.expects(:run_callbacks).with(:after_create)
151
157
  end
152
158
 
153
159
  it "instantiates a class of that type" do
154
- Mongoid::Commands::Save.expects(:execute).returns(true)
160
+ @circle.expects(:save).returns(true)
155
161
  circle = @association.create({ :radius => 100 }, Circle)
156
- circle.should be_a_kind_of(Circle)
157
- circle.radius.should == 100
162
+ circle.should == @circle
158
163
  end
159
164
 
160
165
  end
@@ -453,6 +453,22 @@ describe Mongoid::Associations do
453
453
 
454
454
  end
455
455
 
456
+ context "when using object ids" do
457
+
458
+ before do
459
+ Mongoid.use_object_ids = true
460
+ end
461
+
462
+ after do
463
+ Mongoid.use_object_ids = false
464
+ end
465
+
466
+ it "sets the foreign key as an object id" do
467
+ Game.expects(:field).with("person_id", :type => Mongo::ObjectID)
468
+ Game.belongs_to_related :person
469
+ end
470
+ end
471
+
456
472
  end
457
473
 
458
474
  describe ".has_one_related" do
@@ -142,6 +142,17 @@ describe Mongoid::Attributes do
142
142
 
143
143
  describe "#process" do
144
144
 
145
+ context "when passing non accessible fields" do
146
+
147
+ before do
148
+ @person = Person.new(:owner_id => 6)
149
+ end
150
+
151
+ it "does not set the value" do
152
+ @person.owner_id.should be_nil
153
+ end
154
+ end
155
+
145
156
  context "when attributes dont have fields defined" do
146
157
 
147
158
  before do
@@ -336,6 +347,18 @@ describe Mongoid::Attributes do
336
347
 
337
348
  end
338
349
 
350
+ context "when attribute is not accessible" do
351
+
352
+ before do
353
+ @person = Person.new
354
+ @person.owner_id = 5
355
+ end
356
+
357
+ it "returns the value" do
358
+ @person.read_attribute(:owner_id).should == 5
359
+ end
360
+ end
361
+
339
362
  end
340
363
 
341
364
  describe "#remove_attribute" do
@@ -29,10 +29,13 @@ describe Mongoid::Commands::Destroy do
29
29
  @parent = Person.new
30
30
  @address = Address.new(:street => "Genoa Pl")
31
31
  @parent.addresses << @address
32
+ @collection = mock
33
+ Person.expects(:collection).returns(@collection)
32
34
  end
33
35
 
34
36
  it "removes the document from the parent attributes" do
35
37
  @parent.addresses.should == [@address]
38
+ @collection.expects(:save).returns(true)
36
39
  Mongoid::Commands::Destroy.execute(@address)
37
40
  @parent.addresses.should be_empty
38
41
  end
@@ -86,10 +86,9 @@ describe Mongoid::Commands do
86
86
  @person = Person.new
87
87
  end
88
88
 
89
- it "returns false" do
89
+ it "lets the error bubble up" do
90
90
  Mongoid::Commands::Save.expects(:execute).raises(Mongo::OperationFailure.new("Operation Failed"))
91
- @person.save
92
- @person.errors.on(:mongoid).should == "Operation Failed"
91
+ lambda { @person.save }.should raise_error
93
92
  end
94
93
 
95
94
  end
@@ -204,14 +203,8 @@ describe Mongoid::Commands do
204
203
  Mongoid::Commands::Save.expects(:execute).raises(Mongo::OperationFailure.new("Operation Failed"))
205
204
  end
206
205
 
207
- it "returns the document with errors" do
208
- person = Person.create
209
- person.errors.on(:mongoid).should == "Operation Failed"
210
- end
211
-
212
- it "keeps the document's new record flag" do
213
- person = Person.create
214
- person.should be_a_new_record
206
+ it "lets the error bubble up" do
207
+ lambda { Person.create }.should raise_error
215
208
  end
216
209
 
217
210
  end
@@ -145,6 +145,22 @@ describe Mongoid::Contexts::Enumerable do
145
145
 
146
146
  end
147
147
 
148
+ describe "#iterate" do
149
+ before do
150
+ @criteria.where(:street => "Bourke Street")
151
+ @criteria.documents = @docs
152
+ @context = Mongoid::Contexts::Enumerable.new(@criteria)
153
+ end
154
+
155
+ it "executes the criteria" do
156
+ acc = []
157
+ @context.iterate do |doc|
158
+ acc << doc
159
+ end
160
+ acc.should == [@melbourne]
161
+ end
162
+ end
163
+
148
164
  describe "#last" do
149
165
 
150
166
  it "returns the last matching in the enumerable" do
@@ -27,6 +27,43 @@ describe Mongoid::Contexts::Mongo do
27
27
 
28
28
  end
29
29
 
30
+ describe "blank?" do
31
+
32
+ before do
33
+ @criteria = Mongoid::Criteria.new(Game)
34
+ @criteria.where(:test => 'Testing')
35
+ @context = Mongoid::Contexts::Mongo.new(@criteria)
36
+ end
37
+
38
+ context "when a document exists" do
39
+
40
+ before do
41
+ @doc = mock
42
+ @collection = mock
43
+ Game.expects(:collection).returns(@collection)
44
+ @collection.expects(:find_one).with({ :test => "Testing" }, { :fields => [ :_id ] }).returns(@doc)
45
+ end
46
+
47
+ it "returns false" do
48
+ @context.blank?.should be_false
49
+ end
50
+ end
51
+
52
+ context "when a document does not exist" do
53
+
54
+ before do
55
+ @doc = mock
56
+ @collection = mock
57
+ Game.expects(:collection).returns(@collection)
58
+ @collection.expects(:find_one).with({ :test => "Testing" }, { :fields => [ :_id ] }).returns(nil)
59
+ end
60
+
61
+ it "returns true" do
62
+ @context.blank?.should be_true
63
+ end
64
+ end
65
+ end
66
+
30
67
  describe "#count" do
31
68
 
32
69
  before do
@@ -214,6 +251,65 @@ describe Mongoid::Contexts::Mongo do
214
251
  end
215
252
  end
216
253
 
254
+ describe "#iterate" do
255
+ before do
256
+ @criteria = Mongoid::Criteria.new(Person)
257
+ @context = Mongoid::Contexts::Mongo.new(@criteria)
258
+ @person = Person.new(:title => "Sir")
259
+ @cursor = stub('cursor')
260
+ @cursor.stubs(:each).yields(@person)
261
+ end
262
+
263
+ context "when not caching" do
264
+
265
+ it "executes the criteria" do
266
+ @context.expects(:execute).returns(@cursor)
267
+ @context.iterate do |person|
268
+ person.should == @person
269
+ end
270
+
271
+ end
272
+
273
+ end
274
+
275
+ context "when caching" do
276
+ before do
277
+ @criteria.cache
278
+ end
279
+
280
+ it "executes the criteria" do
281
+ @context.expects(:execute).returns(@cursor)
282
+ @context.iterate do |person|
283
+ person.should == @person
284
+ end
285
+ end
286
+
287
+ it "executes only once and it caches the result" do
288
+ @context.expects(:execute).once.returns(@cursor)
289
+ @context.iterate do |person|
290
+ person.should == @person
291
+ end
292
+ @context.iterate do |person|
293
+ person.should == @person
294
+ end
295
+ end
296
+
297
+ it "executes even if there is no block" do
298
+ @context.expects(:execute).once.returns(@cursor)
299
+ @context.iterate
300
+ end
301
+
302
+ it "caches even if there is no block" do
303
+ @context.expects(:execute).once.returns(@cursor)
304
+ @context.iterate
305
+ @context.iterate do |person|
306
+ person.should == @person
307
+ end
308
+ end
309
+ end
310
+
311
+ end
312
+
217
313
  describe "#last" do
218
314
 
219
315
  before do
@@ -127,10 +127,10 @@ describe Mongoid::Criteria do
127
127
  @criteria.instance_variable_set(:@context, @context)
128
128
  end
129
129
 
130
- context "when the count is 0" do
130
+ context "when the context is blank" do
131
131
 
132
132
  before do
133
- @context.expects(:count).returns(0)
133
+ @context.expects(:blank?).returns(true)
134
134
  end
135
135
 
136
136
  it "returns true" do
@@ -138,10 +138,10 @@ describe Mongoid::Criteria do
138
138
  end
139
139
  end
140
140
 
141
- context "when the count is greater than 0" do
141
+ context "when the context is not blank" do
142
142
 
143
143
  before do
144
- @context.expects(:count).returns(10)
144
+ @context.expects(:blank?).returns(false)
145
145
  end
146
146
 
147
147
  it "returns false" do
@@ -274,6 +274,13 @@ describe Mongoid::Criteria do
274
274
  @cursor = stub(:count => 10)
275
275
  end
276
276
 
277
+ it "delegates to the context#iterate" do
278
+ @context = stub('context')
279
+ @criteria.stubs(:context).returns(@context)
280
+ @context.expects(:iterate)
281
+ @criteria.each
282
+ end
283
+
277
284
  context "when the criteria has not been executed" do
278
285
 
279
286
  before do
@@ -499,6 +499,17 @@ describe Mongoid::Document do
499
499
 
500
500
  end
501
501
 
502
+ describe "#persisted?" do
503
+
504
+ before do
505
+ @person = Person.new
506
+ end
507
+
508
+ it "delegates to new_record?" do
509
+ @person.persisted?.should be_false
510
+ end
511
+ end
512
+
502
513
  describe "#_parent" do
503
514
 
504
515
  before do
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Extensions::BigDecimal::Conversions do
4
+
5
+ let(:number) do
6
+ BigDecimal.new("123456.789")
7
+ end
8
+
9
+ describe "#get" do
10
+
11
+ it "converts the string to a big decimal" do
12
+ BigDecimal.get("123456.789").should == number
13
+ end
14
+ end
15
+
16
+ describe "#set" do
17
+
18
+ it "converts the big decimal to a string" do
19
+ BigDecimal.set(number).should == "123456.789"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Extensions::Binary::Conversions do
4
+
5
+ let(:bin) do
6
+ Binary.new
7
+ end
8
+
9
+ describe "#get" do
10
+
11
+ it "returns self" do
12
+ Binary.get(bin).should == bin
13
+ end
14
+ end
15
+
16
+ describe "#set" do
17
+
18
+ it "returns self" do
19
+ Binary.set(bin).should == bin
20
+ end
21
+ end
22
+ end
@@ -1,10 +1,63 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Enslavement do
3
+ describe Mongoid::Extras do
4
4
 
5
5
  before do
6
6
  @klass = Class.new do
7
- include Mongoid::Enslavement
7
+ include Mongoid::Extras
8
+ end
9
+ end
10
+
11
+ describe ".cache" do
12
+
13
+ before do
14
+ @klass.cache
15
+ end
16
+
17
+ it "sets the cached boolean on the class" do
18
+ @klass.cached.should be_true
19
+ end
20
+
21
+ end
22
+
23
+ describe ".cached" do
24
+
25
+ it "defaults to false" do
26
+ @klass.cached.should be_false
27
+ end
28
+ end
29
+
30
+ describe ".cached?" do
31
+
32
+ context "when the class is cached" do
33
+
34
+ before do
35
+ @klass.cache
36
+ end
37
+
38
+ it "returns true" do
39
+ @klass.should be_cached
40
+ end
41
+ end
42
+
43
+ context "when the class is not cached" do
44
+
45
+ it "returns false" do
46
+ @klass.should_not be_cached
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ describe "#cached?" do
53
+
54
+ before do
55
+ @klass.cache
56
+ @doc = @klass.new
57
+ end
58
+
59
+ it "returns the class cached? value" do
60
+ @doc.should be_cached
8
61
  end
9
62
  end
10
63