mongoid 1.2.6 → 1.2.7

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 (52) hide show
  1. data/VERSION +1 -1
  2. data/lib/mongoid.rb +1 -0
  3. data/lib/mongoid/associations.rb +1 -1
  4. data/lib/mongoid/attributes.rb +1 -0
  5. data/lib/mongoid/collection.rb +1 -1
  6. data/lib/mongoid/commands.rb +1 -1
  7. data/lib/mongoid/commands/delete_all.rb +2 -1
  8. data/lib/mongoid/commands/destroy_all.rb +1 -1
  9. data/lib/mongoid/components.rb +1 -0
  10. data/lib/mongoid/config.rb +3 -1
  11. data/lib/mongoid/contexts.rb +21 -0
  12. data/lib/mongoid/contexts/enumerable.rb +15 -12
  13. data/lib/mongoid/contexts/ids.rb +25 -0
  14. data/lib/mongoid/contexts/mongo.rb +25 -23
  15. data/lib/mongoid/contexts/paging.rb +2 -2
  16. data/lib/mongoid/criteria.rb +5 -43
  17. data/lib/mongoid/document.rb +1 -0
  18. data/lib/mongoid/enslavement.rb +38 -0
  19. data/lib/mongoid/fields.rb +5 -2
  20. data/lib/mongoid/identity.rb +7 -1
  21. data/lib/mongoid/named_scope.rb +2 -0
  22. data/mongoid.gemspec +8 -2
  23. data/spec/integration/mongoid/commands_spec.rb +2 -2
  24. data/spec/integration/mongoid/contexts/enumerable_spec.rb +13 -0
  25. data/spec/integration/mongoid/criteria_spec.rb +2 -2
  26. data/spec/integration/mongoid/document_spec.rb +5 -1
  27. data/spec/integration/mongoid/finders_spec.rb +85 -28
  28. data/spec/models/person.rb +1 -0
  29. data/spec/unit/mongoid/associations_spec.rb +12 -0
  30. data/spec/unit/mongoid/attributes_spec.rb +60 -51
  31. data/spec/unit/mongoid/collection_spec.rb +30 -0
  32. data/spec/unit/mongoid/commands/delete_all_spec.rb +3 -3
  33. data/spec/unit/mongoid/commands_spec.rb +16 -0
  34. data/spec/unit/mongoid/config_spec.rb +7 -0
  35. data/spec/unit/mongoid/contexts/enumerable_spec.rb +151 -11
  36. data/spec/unit/mongoid/contexts/mongo_spec.rb +168 -42
  37. data/spec/unit/mongoid/contexts_spec.rb +25 -0
  38. data/spec/unit/mongoid/criteria_spec.rb +49 -75
  39. data/spec/unit/mongoid/criterion/exclusion_spec.rb +3 -13
  40. data/spec/unit/mongoid/criterion/inclusion_spec.rb +17 -19
  41. data/spec/unit/mongoid/criterion/optional_spec.rb +25 -8
  42. data/spec/unit/mongoid/document_spec.rb +4 -0
  43. data/spec/unit/mongoid/enslavement_spec.rb +63 -0
  44. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
  45. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -2
  46. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +1 -1
  47. data/spec/unit/mongoid/fields_spec.rb +10 -0
  48. data/spec/unit/mongoid/finders_spec.rb +1 -1
  49. data/spec/unit/mongoid/identity_spec.rb +23 -3
  50. data/spec/unit/mongoid/named_scope_spec.rb +15 -2
  51. data/spec/unit/mongoid/scope_spec.rb +1 -1
  52. metadata +8 -2
@@ -229,6 +229,7 @@ module Mongoid #:nodoc:
229
229
  # Reloads the +Document+ attributes from the database.
230
230
  def reload
231
231
  @attributes = collection.find_one(:_id => id)
232
+ self
232
233
  end
233
234
 
234
235
  # Remove a child document from this parent +Document+. Will reset the
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Enslavement #:nodoc:
4
+ def self.included(base)
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ class_inheritable_accessor :enslaved
8
+ self.enslaved = false
9
+
10
+ delegate :enslaved?, :to => "self.class"
11
+ end
12
+ end
13
+
14
+ module ClassMethods #:nodoc
15
+ # Set whether or not this documents read operations should delegate to
16
+ # the slave database by default.
17
+ #
18
+ # Example:
19
+ #
20
+ # class Person
21
+ # include Mongoid::Document
22
+ # enslave
23
+ # end
24
+ def enslave
25
+ self.enslaved = true
26
+ end
27
+
28
+ # Determines if the class is enslaved or not.
29
+ #
30
+ # Returns:
31
+ #
32
+ # True if enslaved, false if not.
33
+ def enslaved?
34
+ self.enslaved == true
35
+ end
36
+ end
37
+ end
38
+ end
@@ -46,13 +46,16 @@ module Mongoid #:nodoc
46
46
  def create_accessors(name, meth, options = {})
47
47
  define_method(meth) { read_attribute(name) }
48
48
  define_method("#{meth}=") { |value| write_attribute(name, value) }
49
- define_method("#{meth}?") { read_attribute(name) == true } if options[:type] == Boolean
49
+ define_method("#{meth}?") do
50
+ attr = read_attribute(name)
51
+ (options[:type] == Boolean) ? attr == true : attr.present?
52
+ end
50
53
  end
51
54
 
52
55
  # Set up a default value for a field.
53
56
  def set_default(name, options = {})
54
57
  value = options[:default]
55
- defaults[name] = value if value
58
+ defaults[name] = value unless value.nil?
56
59
  end
57
60
  end
58
61
  end
@@ -13,10 +13,16 @@ module Mongoid #:nodoc:
13
13
  end
14
14
 
15
15
  protected
16
+ # Return the proper id for the document.
17
+ def generate_id
18
+ id = Mongo::ObjectID.new
19
+ Mongoid.use_object_ids ? id : id.to_s
20
+ end
21
+
16
22
  # Set the id for the document.
17
23
  def identify(doc)
18
24
  doc.id = compose(doc).join(" ").identify if doc.primary_key
19
- doc.id = Mongo::ObjectID.new.to_s unless doc.id
25
+ doc.id = generate_id unless doc.id
20
26
  end
21
27
 
22
28
  # Set the _type field on the document.
@@ -28,6 +28,8 @@ module Mongoid #:nodoc:
28
28
  EOT
29
29
  end
30
30
 
31
+ alias :scope :named_scope
32
+
31
33
  # Return the scopes or default to an empty +Hash+.
32
34
  def scopes
33
35
  read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.2.6"
8
+ s.version = "1.2.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2010-02-10}
12
+ s.date = %q{2010-02-18}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
53
53
  "lib/mongoid/config.rb",
54
54
  "lib/mongoid/contexts.rb",
55
55
  "lib/mongoid/contexts/enumerable.rb",
56
+ "lib/mongoid/contexts/ids.rb",
56
57
  "lib/mongoid/contexts/mongo.rb",
57
58
  "lib/mongoid/contexts/paging.rb",
58
59
  "lib/mongoid/criteria.rb",
@@ -62,6 +63,7 @@ Gem::Specification.new do |s|
62
63
  "lib/mongoid/criterion/optional.rb",
63
64
  "lib/mongoid/cursor.rb",
64
65
  "lib/mongoid/document.rb",
66
+ "lib/mongoid/enslavement.rb",
65
67
  "lib/mongoid/errors.rb",
66
68
  "lib/mongoid/extensions.rb",
67
69
  "lib/mongoid/extensions/array/accessors.rb",
@@ -167,6 +169,7 @@ Gem::Specification.new do |s|
167
169
  "spec/unit/mongoid/config_spec.rb",
168
170
  "spec/unit/mongoid/contexts/enumerable_spec.rb",
169
171
  "spec/unit/mongoid/contexts/mongo_spec.rb",
172
+ "spec/unit/mongoid/contexts_spec.rb",
170
173
  "spec/unit/mongoid/criteria_spec.rb",
171
174
  "spec/unit/mongoid/criterion/complex_spec.rb",
172
175
  "spec/unit/mongoid/criterion/exclusion_spec.rb",
@@ -174,6 +177,7 @@ Gem::Specification.new do |s|
174
177
  "spec/unit/mongoid/criterion/optional_spec.rb",
175
178
  "spec/unit/mongoid/cursor_spec.rb",
176
179
  "spec/unit/mongoid/document_spec.rb",
180
+ "spec/unit/mongoid/enslavement_spec.rb",
177
181
  "spec/unit/mongoid/errors_spec.rb",
178
182
  "spec/unit/mongoid/extensions/array/accessors_spec.rb",
179
183
  "spec/unit/mongoid/extensions/array/assimilation_spec.rb",
@@ -282,6 +286,7 @@ Gem::Specification.new do |s|
282
286
  "spec/unit/mongoid/config_spec.rb",
283
287
  "spec/unit/mongoid/contexts/enumerable_spec.rb",
284
288
  "spec/unit/mongoid/contexts/mongo_spec.rb",
289
+ "spec/unit/mongoid/contexts_spec.rb",
285
290
  "spec/unit/mongoid/criteria_spec.rb",
286
291
  "spec/unit/mongoid/criterion/complex_spec.rb",
287
292
  "spec/unit/mongoid/criterion/exclusion_spec.rb",
@@ -289,6 +294,7 @@ Gem::Specification.new do |s|
289
294
  "spec/unit/mongoid/criterion/optional_spec.rb",
290
295
  "spec/unit/mongoid/cursor_spec.rb",
291
296
  "spec/unit/mongoid/document_spec.rb",
297
+ "spec/unit/mongoid/enslavement_spec.rb",
292
298
  "spec/unit/mongoid/errors_spec.rb",
293
299
  "spec/unit/mongoid/extensions/array/accessors_spec.rb",
294
300
  "spec/unit/mongoid/extensions/array/assimilation_spec.rb",
@@ -200,7 +200,7 @@ describe Mongoid::Commands do
200
200
  describe ".delete_all" do
201
201
 
202
202
  it "returns true" do
203
- Person.delete_all.should be_true
203
+ Person.delete_all.should == true
204
204
  end
205
205
 
206
206
  end
@@ -208,7 +208,7 @@ describe Mongoid::Commands do
208
208
  describe ".destroy_all" do
209
209
 
210
210
  it "returns true" do
211
- Person.destroy_all.should be_true
211
+ Person.destroy_all.should == true
212
212
  end
213
213
 
214
214
  end
@@ -17,4 +17,17 @@ describe Mongoid::Contexts::Enumerable do
17
17
  addresses.size.should == 5
18
18
  end
19
19
  end
20
+
21
+ describe "limit and skip" do
22
+
23
+ it "limits" do
24
+ @person.addresses.criteria.limit(5).size.should == 5
25
+ end
26
+
27
+ it "skips" do
28
+ @person.addresses.criteria.skip(5).limit(10).
29
+ map(&:number).should == [5, 6, 7, 8, 9]
30
+ end
31
+
32
+ end
20
33
  end
@@ -215,9 +215,9 @@ describe Mongoid::Criteria do
215
215
 
216
216
  it "iterates over the cursor only once" do
217
217
  criteria = Person.where(:title => "Sir").cache
218
- criteria.collect.size.should == 10
218
+ criteria.collect.to_a.size.should == 10
219
219
  # Do it again!
220
- criteria.collect.size.should == 10
220
+ criteria.collect.to_a.size.should == 10
221
221
  end
222
222
  end
223
223
 
@@ -285,7 +285,7 @@ describe Mongoid::Document do
285
285
  end
286
286
 
287
287
  it "returns a pretty string of class name and attributes" do
288
- attrs = Person.fields.map { |name, field| "#{name}: #{@person.attributes[name] || 'nil'}" } * ", "
288
+ attrs = Person.fields.map { |name, field| "#{name}: #{@person.attributes[name].nil? ? 'nil' : @person.attributes[name]}" } * ", "
289
289
  @person.inspect.should == "#<Person _id: #{@person.id}, #{attrs}>"
290
290
  end
291
291
 
@@ -325,6 +325,10 @@ describe Mongoid::Document do
325
325
  @person.age.should == 35
326
326
  end
327
327
 
328
+ it "reload should return self" do
329
+ @person.reload.should == @from_db
330
+ end
331
+
328
332
  end
329
333
 
330
334
  describe "#save" do
@@ -4,59 +4,116 @@ describe Mongoid::Finders do
4
4
 
5
5
  describe "#find" do
6
6
 
7
- before do
8
- @documents = []
9
- @document = Person.create(:title => "Mrs.", :ssn => "another")
10
- 3.times do |n|
11
- @documents << Person.create(:title => "Mr.", :ssn => "#{n}22")
7
+ context "using string ids" do
8
+
9
+ before do
10
+ @documents = []
11
+ @document = Person.create(:title => "Mrs.", :ssn => "another")
12
+ 3.times do |n|
13
+ @documents << Person.create(:title => "Mr.", :ssn => "#{n}22")
14
+ end
12
15
  end
13
- end
14
16
 
15
- after do
16
- Person.delete_all
17
- end
17
+ after do
18
+ Person.delete_all
19
+ end
18
20
 
19
- context "with an id as an argument" do
21
+ context "with an id as an argument" do
20
22
 
21
- context "when the document is found" do
23
+ context "when the document is found" do
24
+
25
+ it "returns the document" do
26
+ Person.find(@document.id).should == @document
27
+ end
22
28
 
23
- it "returns the document" do
24
- Person.find(@document.id).should == @document
25
29
  end
26
30
 
27
- end
31
+ context "when the document is not found" do
28
32
 
29
- context "when the document is not found" do
33
+ it "raises an error" do
34
+ lambda { Person.find("5") }.should raise_error
35
+ end
30
36
 
31
- it "raises an error" do
32
- lambda { Person.find("5") }.should raise_error
33
37
  end
34
38
 
35
39
  end
36
40
 
37
- end
41
+ context "with an array of ids as args" do
38
42
 
39
- context "with an array of ids as args" do
43
+ context "when the documents are found" do
40
44
 
41
- context "when the documents are found" do
45
+ it "returns an array of the documents" do
46
+ @people = Person.find(@documents.map(&:id))
47
+ @people.should == @documents
48
+ end
42
49
 
43
- it "returns an array of the documents" do
44
- @people = Person.find(@documents.map(&:id))
45
- @people.should == @documents
46
50
  end
47
51
 
52
+ context "when no documents found" do
53
+
54
+ it "raises an error" do
55
+ lambda { Person.find(["11", "21", "31"]) }.should raise_error
56
+ end
57
+
58
+ end
48
59
  end
60
+ end
49
61
 
50
- context "when no documents found" do
62
+ context "using object ids" do
51
63
 
52
- it "raises an error" do
53
- lambda { Person.find(["11", "21", "31"]) }.should raise_error
64
+ before do
65
+ Mongoid.use_object_ids = true
66
+ @documents = []
67
+ @document = Person.create(:title => "Mrs.", :ssn => "another")
68
+ 3.times do |n|
69
+ @documents << Person.create(:title => "Mr.", :ssn => "#{n}22")
54
70
  end
71
+ end
55
72
 
73
+ after do
74
+ Mongoid.use_object_ids = false
75
+ Person.delete_all
56
76
  end
57
77
 
58
- end
78
+ context "with an id as an argument" do
59
79
 
60
- end
80
+ context "when the document is found" do
81
+
82
+ it "returns the document" do
83
+ Person.find(@document.id).should == @document
84
+ end
85
+
86
+ end
61
87
 
88
+ context "when the document is not found" do
89
+
90
+ it "raises an error" do
91
+ lambda { Person.find("5") }.should raise_error
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ context "with an array of ids as args" do
99
+
100
+ context "when the documents are found" do
101
+
102
+ it "returns an array of the documents" do
103
+ @people = Person.find(@documents.map(&:id))
104
+ @people.should == @documents
105
+ end
106
+
107
+ end
108
+
109
+ context "when no documents found" do
110
+
111
+ it "raises an error" do
112
+ lambda { Person.find(["11", "21", "31"]) }.should raise_error
113
+ end
114
+
115
+ end
116
+ end
117
+ end
118
+ end
62
119
  end
@@ -4,6 +4,7 @@ class Person
4
4
 
5
5
  field :title
6
6
  field :terms, :type => Boolean
7
+ field :pets, :type => Boolean, :default => false
7
8
  field :age, :type => Integer, :default => 100
8
9
  field :dob, :type => Date
9
10
  field :mixed_drink, :type => MixedDrink
@@ -199,6 +199,18 @@ describe Mongoid::Associations do
199
199
 
200
200
  end
201
201
 
202
+ context "when attributes are nil" do
203
+
204
+ before do
205
+ @writer = @canvas.build_writer(nil)
206
+ end
207
+
208
+ it "defaults them to empty" do
209
+ @writer.should be_a_kind_of(Writer)
210
+ end
211
+
212
+ end
213
+
202
214
  end
203
215
 
204
216
  describe "#create_*" do
@@ -331,6 +331,7 @@ describe Mongoid::Attributes do
331
331
 
332
332
  it "returns the default value" do
333
333
  @person.age.should == 100
334
+ @person.pets.should == false
334
335
  end
335
336
 
336
337
  end
@@ -391,83 +392,91 @@ describe Mongoid::Attributes do
391
392
 
392
393
  end
393
394
 
394
- describe "#write_attributes" do
395
-
396
- context "typecasting" do
397
-
398
- before do
399
- @person = Person.new
400
- @attributes = { :age => "50" }
401
- end
402
-
403
- it "properly casts values" do
404
- @person.write_attributes(@attributes)
405
- @person.age.should == 50
406
- end
407
-
408
- it "allows passing of nil" do
409
- @person.write_attributes(nil)
410
- @person.age.should == 100
411
- end
395
+ describe "#attributes=" do
396
+ it 'should be a alias of write_attributes' do
412
397
 
413
398
  end
399
+ end
414
400
 
415
- context "on a parent document" do
401
+ [:attributes=, :write_attributes].each do |method|
402
+ describe "##{method}" do
416
403
 
417
- context "when the parent has a has many through a has one" do
404
+ context "typecasting" do
418
405
 
419
406
  before do
420
- @owner = PetOwner.new(:title => "Mr")
421
- @pet = Pet.new(:name => "Fido")
422
- @owner.pet = @pet
423
- @vet_visit = VetVisit.new(:date => Date.today)
424
- @pet.vet_visits = [@vet_visit]
407
+ @person = Person.new
408
+ @attributes = { :age => "50" }
409
+ end
410
+
411
+ it "properly casts values" do
412
+ @person.send(method, @attributes)
413
+ @person.age.should == 50
425
414
  end
426
415
 
427
- it "does not overwrite child attributes if not in the hash" do
428
- @owner.write_attributes({ :pet => { :name => "Bingo" } })
429
- @owner.pet.name.should == "Bingo"
430
- @owner.pet.vet_visits.size.should == 1
416
+ it "allows passing of nil" do
417
+ @person.send(method, nil)
418
+ @person.age.should == 100
431
419
  end
432
420
 
433
421
  end
434
422
 
435
- end
423
+ context "on a parent document" do
436
424
 
437
- context "on a child document" do
425
+ context "when the parent has a has many through a has one" do
438
426
 
439
- context "when child is part of a has one" do
427
+ before do
428
+ @owner = PetOwner.new(:title => "Mr")
429
+ @pet = Pet.new(:name => "Fido")
430
+ @owner.pet = @pet
431
+ @vet_visit = VetVisit.new(:date => Date.today)
432
+ @pet.vet_visits = [@vet_visit]
433
+ end
440
434
 
441
- before do
442
- @person = Person.new(:title => "Sir", :age => 30)
443
- @name = Name.new(:first_name => "Test", :last_name => "User")
444
- @person.name = @name
445
- end
435
+ it "does not overwrite child attributes if not in the hash" do
436
+ @owner.send(method, { :pet => { :name => "Bingo" } })
437
+ @owner.pet.name.should == "Bingo"
438
+ @owner.pet.vet_visits.size.should == 1
439
+ end
446
440
 
447
- it "sets the child attributes on the parent" do
448
- @name.write_attributes(:first_name => "Test2", :last_name => "User2")
449
- @person.attributes[:name].should ==
450
- { "_id" => "test-user", "first_name" => "Test2", "last_name" => "User2", "_type" => "Name" }
451
441
  end
452
442
 
453
443
  end
454
444
 
455
- context "when child is part of a has many" do
445
+ context "on a child document" do
446
+
447
+ context "when child is part of a has one" do
448
+
449
+ before do
450
+ @person = Person.new(:title => "Sir", :age => 30)
451
+ @name = Name.new(:first_name => "Test", :last_name => "User")
452
+ @person.name = @name
453
+ end
454
+
455
+ it "sets the child attributes on the parent" do
456
+ @name.send(method, :first_name => "Test2", :last_name => "User2")
457
+ @person.attributes[:name].should ==
458
+ { "_id" => "test-user", "first_name" => "Test2", "last_name" => "User2", "_type" => "Name" }
459
+ end
456
460
 
457
- before do
458
- @person = Person.new(:title => "Sir")
459
- @address = Address.new(:street => "Test")
460
- @person.addresses << @address
461
461
  end
462
462
 
463
- it "updates the child attributes on the parent" do
464
- @address.write_attributes("street" => "Test2")
465
- @person.attributes[:addresses].should ==
466
- [ { "_id" => "test", "street" => "Test2", "_type" => "Address" } ]
463
+ context "when child is part of a has many" do
464
+
465
+ before do
466
+ @person = Person.new(:title => "Sir")
467
+ @address = Address.new(:street => "Test")
468
+ @person.addresses << @address
469
+ end
470
+
471
+ it "updates the child attributes on the parent" do
472
+ @address.send(method, "street" => "Test2")
473
+ @person.attributes[:addresses].should ==
474
+ [ { "_id" => "test", "street" => "Test2", "_type" => "Address" } ]
475
+ end
476
+
467
477
  end
468
478
 
469
479
  end
470
-
471
480
  end
472
481
 
473
482
  end