mongoid 0.9.8 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/README.textile +5 -4
  2. data/VERSION +1 -1
  3. data/lib/mongoid.rb +3 -28
  4. data/lib/mongoid/associations.rb +63 -44
  5. data/lib/mongoid/associations/{relates_to_one.rb → belongs_to_related.rb} +4 -4
  6. data/lib/mongoid/associations/has_many.rb +26 -12
  7. data/lib/mongoid/associations/has_many_related.rb +100 -0
  8. data/lib/mongoid/associations/has_one.rb +13 -2
  9. data/lib/mongoid/associations/{relates_to_many.rb → has_one_related.rb} +34 -9
  10. data/lib/mongoid/attributes.rb +108 -8
  11. data/lib/mongoid/commands.rb +3 -11
  12. data/lib/mongoid/commands/save.rb +2 -6
  13. data/lib/mongoid/document.rb +6 -70
  14. data/lib/mongoid/errors.rb +34 -0
  15. data/mongoid.gemspec +14 -11
  16. data/spec/integration/mongoid/associations_spec.rb +19 -14
  17. data/spec/spec_helper.rb +6 -3
  18. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +112 -0
  19. data/spec/unit/mongoid/associations/has_many_related_spec.rb +292 -0
  20. data/spec/unit/mongoid/associations/has_many_spec.rb +17 -0
  21. data/spec/unit/mongoid/associations/has_one_related_spec.rb +130 -0
  22. data/spec/unit/mongoid/associations/has_one_spec.rb +53 -0
  23. data/spec/unit/mongoid/associations_spec.rb +36 -8
  24. data/spec/unit/mongoid/attributes_spec.rb +218 -0
  25. data/spec/unit/mongoid/commands/save_spec.rb +4 -2
  26. data/spec/unit/mongoid/commands_spec.rb +4 -17
  27. data/spec/unit/mongoid/criteria_spec.rb +0 -5
  28. data/spec/unit/mongoid/document_spec.rb +26 -156
  29. data/spec/unit/mongoid/errors_spec.rb +87 -0
  30. metadata +14 -11
  31. data/lib/mongoid/commands/quick_save.rb +0 -19
  32. data/spec/unit/mongoid/associations/relates_to_many_spec.rb +0 -76
  33. data/spec/unit/mongoid/associations/relates_to_one_spec.rb +0 -112
  34. data/spec/unit/mongoid/commands/quick_save_spec.rb +0 -24
@@ -11,11 +11,13 @@ describe Mongoid::Commands::Save do
11
11
  :valid? => true,
12
12
  :run_callbacks => true,
13
13
  :parent => nil,
14
- :attributes => {})
14
+ :attributes => {},
15
+ :new_record= => false)
15
16
  @document = stub(:collection => @doc_collection,
16
17
  :run_callbacks => true,
17
18
  :parent => @parent,
18
- :attributes => {})
19
+ :attributes => {},
20
+ :new_record= => false)
19
21
  end
20
22
 
21
23
  context "when document is valid" do
@@ -59,7 +59,7 @@ describe Mongoid::Commands do
59
59
 
60
60
  context "when validation fails" do
61
61
 
62
- it "it raises a ValidationsError" do
62
+ it "it raises an error" do
63
63
  Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
64
64
  lambda { @person.save! }.should raise_error
65
65
  end
@@ -79,7 +79,7 @@ describe Mongoid::Commands do
79
79
 
80
80
  context "when validation fails" do
81
81
 
82
- it "it raises a ValidationsError" do
82
+ it "it raises an error " do
83
83
  Mongoid::Commands::Create.expects(:execute).with(@person).returns(false)
84
84
  lambda { @person.save! }.should raise_error
85
85
  end
@@ -90,19 +90,6 @@ describe Mongoid::Commands do
90
90
 
91
91
  end
92
92
 
93
- describe "#quick_save" do
94
-
95
- before do
96
- @person = Person.new
97
- end
98
-
99
- it "delegates to the QuickSave command" do
100
- Mongoid::Commands::QuickSave.expects(:execute).with(@person).returns(true)
101
- @person.quick_save
102
- end
103
-
104
- end
105
-
106
93
  describe "#update_attributes" do
107
94
 
108
95
  it "delegates to the Save command" do
@@ -125,7 +112,7 @@ describe Mongoid::Commands do
125
112
 
126
113
  context "when validation fails" do
127
114
 
128
- it "it raises a ValidationsError" do
115
+ it "it raises an error" do
129
116
  Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
130
117
  lambda { @person.update_attributes!({}) }.should raise_error
131
118
  end
@@ -162,7 +149,7 @@ describe Mongoid::Commands do
162
149
 
163
150
  context "when validation fails" do
164
151
 
165
- it "raises an exception" do
152
+ it "raises an error" do
166
153
  person = stub(:errors => stub(:empty? => false))
167
154
  Mongoid::Commands::Create.expects(:execute).returns(person)
168
155
  lambda { Person.create! }.should raise_error
@@ -138,11 +138,6 @@ describe Mongoid::Criteria do
138
138
  @criteria.count.should == 44
139
139
  end
140
140
 
141
- end
142
-
143
- context "when type is :first" do
144
-
145
-
146
141
  end
147
142
 
148
143
  context "when type is not :first" do
@@ -101,8 +101,8 @@ describe Mongoid::Document do
101
101
  @address = Address.new
102
102
  end
103
103
 
104
- it "returns nil" do
105
- Address.collection.should be_nil
104
+ it "raises an error" do
105
+ lambda { Address.collection }.should raise_error
106
106
  end
107
107
 
108
108
  end
@@ -439,46 +439,6 @@ describe Mongoid::Document do
439
439
 
440
440
  end
441
441
 
442
- describe "#read_attribute" do
443
-
444
- context "when attribute does not exist" do
445
-
446
- before do
447
- @person = Person.new
448
- end
449
-
450
- it "returns the default value" do
451
- @person.age.should == 100
452
- end
453
-
454
- end
455
-
456
- end
457
-
458
- describe "#remove_attribute" do
459
-
460
- context "when the attribute exists" do
461
-
462
- it "removes the attribute" do
463
- person = Person.new(:title => "Sir")
464
- person.remove_attribute(:title)
465
- person.title.should be_nil
466
- end
467
-
468
- end
469
-
470
- context "when the attribute does not exist" do
471
-
472
- it "does nothing" do
473
- person = Person.new
474
- person.remove_attribute(:title)
475
- person.title.should be_nil
476
- end
477
-
478
- end
479
-
480
- end
481
-
482
442
  describe "#reload" do
483
443
 
484
444
  before do
@@ -539,112 +499,6 @@ describe Mongoid::Document do
539
499
 
540
500
  end
541
501
 
542
- describe "#write_attribute" do
543
-
544
- context "when attribute does not exist" do
545
-
546
- before do
547
- @person = Person.new
548
- end
549
-
550
- it "returns the default value" do
551
- @person.age = nil
552
- @person.age.should == 100
553
- end
554
-
555
- end
556
-
557
- context "when field has a default value" do
558
-
559
- before do
560
- @person = Person.new
561
- end
562
-
563
- it "should allow overwriting of the default value" do
564
- @person.terms = true
565
- @person.terms.should be_true
566
- end
567
-
568
- end
569
-
570
- end
571
-
572
- describe "#write_attributes" do
573
-
574
- context "typecasting" do
575
-
576
- before do
577
- @person = Person.new
578
- @attributes = { :age => "50" }
579
- end
580
-
581
- it "properly casts values" do
582
- @person.write_attributes(@attributes)
583
- @person.age.should == 50
584
- end
585
-
586
- end
587
-
588
- context "on a parent document" do
589
-
590
- context "when the parent has a has many through a has one" do
591
-
592
- before do
593
- @owner = PetOwner.new(:title => "Mr")
594
- @pet = Pet.new(:name => "Fido")
595
- @owner.pet = @pet
596
- @vet_visit = VetVisit.new(:date => Date.today)
597
- @pet.vet_visits = [@vet_visit]
598
- end
599
-
600
- it "does not overwrite child attributes if not in the hash" do
601
- @owner.write_attributes({ :pet => { :name => "Bingo" } })
602
- @owner.pet.name.should == "Bingo"
603
- @owner.pet.vet_visits.size.should == 1
604
- end
605
-
606
- end
607
-
608
- end
609
-
610
- context "on a child document" do
611
-
612
- context "when child is part of a has one" do
613
-
614
- before do
615
- @person = Person.new(:title => "Sir", :age => 30)
616
- @name = Name.new(:first_name => "Test", :last_name => "User")
617
- @person.name = @name
618
- end
619
-
620
- it "sets the child attributes on the parent" do
621
- @name.write_attributes(:first_name => "Test2", :last_name => "User2")
622
- @person.attributes[:name].should ==
623
- { "_id" => "test-user", "first_name" => "Test2", "last_name" => "User2" }
624
- end
625
-
626
- end
627
-
628
- context "when child is part of a has many" do
629
-
630
- before do
631
- @person = Person.new(:title => "Sir")
632
- @address = Address.new(:street => "Test")
633
- @person.addresses << @address
634
- end
635
-
636
- it "updates the child attributes on the parent" do
637
- @address.write_attributes("street" => "Test2")
638
- @person.attributes[:addresses].should ==
639
- [ { "_id" => "test", "street" => "Test2" } ]
640
- end
641
-
642
- end
643
-
644
- end
645
-
646
- end
647
-
648
502
  context "validations" do
649
503
 
650
504
  context "when defining using macros" do
@@ -807,16 +661,32 @@ describe Mongoid::Document do
807
661
 
808
662
  context "when association is a has_one" do
809
663
 
810
- it "fails when the association fails validation" do
811
- Person.class_eval do
812
- validates_associated :name
664
+ context "when the associated is not nil" do
665
+
666
+ it "fails when the association fails validation" do
667
+ Person.class_eval do
668
+ validates_associated :name
669
+ end
670
+ Name.class_eval do
671
+ validates_presence_of :first_name
672
+ end
673
+ @person.name = Name.new
674
+ @person.valid?.should be_false
675
+ @person.errors.on(:name).should_not be_nil
813
676
  end
814
- Name.class_eval do
815
- validates_presence_of :first_name
677
+
678
+ end
679
+
680
+ context "when the associated is nil" do
681
+
682
+ it "fails when the association fails validation" do
683
+ Person.class_eval do
684
+ validates_associated :name
685
+ end
686
+ @person.valid?.should be_false
687
+ @person.errors.on(:name).should_not be_nil
816
688
  end
817
- @person.name = Name.new
818
- @person.valid?.should be_false
819
- @person.errors.on(:name).should_not be_nil
689
+
820
690
  end
821
691
 
822
692
  end
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Errors do
4
+
5
+ describe Mongoid::Errors::InvalidOptions do
6
+
7
+ describe "#message" do
8
+
9
+ context "default" do
10
+
11
+ before do
12
+ @error = Mongoid::Errors::InvalidOptions.new
13
+ end
14
+
15
+ it "returns the class name" do
16
+ @error.message.should == @error.class.name
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ describe Mongoid::Errors::InvalidDatabase do
26
+
27
+ describe "#message" do
28
+
29
+ context "default" do
30
+
31
+ before do
32
+ @error = Mongoid::Errors::InvalidDatabase.new
33
+ end
34
+
35
+ it "returns the class name" do
36
+ @error.message.should == @error.class.name
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ describe Mongoid::Errors::Validations do
46
+
47
+ describe "#message" do
48
+
49
+ context "default" do
50
+
51
+ before do
52
+ @errors = stub(:full_messages => "Testing")
53
+ @error = Mongoid::Errors::Validations.new(@errors)
54
+ end
55
+
56
+ it "contains the errors' full messages" do
57
+ @error.message.should include("Testing")
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ describe Mongoid::Errors::InvalidCollection do
67
+
68
+ describe "#message" do
69
+
70
+ context "default" do
71
+
72
+ before do
73
+ @klass = Address
74
+ @error = Mongoid::Errors::InvalidCollection.new(@klass)
75
+ end
76
+
77
+ it "contains class is not allowed" do
78
+ @error.message.should include("Address is not allowed")
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+
87
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-12 00:00:00 -05:00
12
+ date: 2009-12-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -100,11 +100,12 @@ files:
100
100
  - lib/mongoid.rb
101
101
  - lib/mongoid/associations.rb
102
102
  - lib/mongoid/associations/belongs_to.rb
103
+ - lib/mongoid/associations/belongs_to_related.rb
103
104
  - lib/mongoid/associations/has_many.rb
105
+ - lib/mongoid/associations/has_many_related.rb
104
106
  - lib/mongoid/associations/has_one.rb
107
+ - lib/mongoid/associations/has_one_related.rb
105
108
  - lib/mongoid/associations/options.rb
106
- - lib/mongoid/associations/relates_to_many.rb
107
- - lib/mongoid/associations/relates_to_one.rb
108
109
  - lib/mongoid/attributes.rb
109
110
  - lib/mongoid/commands.rb
110
111
  - lib/mongoid/commands/create.rb
@@ -112,12 +113,12 @@ files:
112
113
  - lib/mongoid/commands/delete_all.rb
113
114
  - lib/mongoid/commands/destroy.rb
114
115
  - lib/mongoid/commands/destroy_all.rb
115
- - lib/mongoid/commands/quick_save.rb
116
116
  - lib/mongoid/commands/save.rb
117
117
  - lib/mongoid/commands/validate.rb
118
118
  - lib/mongoid/criteria.rb
119
119
  - lib/mongoid/document.rb
120
120
  - lib/mongoid/dynamic_finder.rb
121
+ - lib/mongoid/errors.rb
121
122
  - lib/mongoid/extensions.rb
122
123
  - lib/mongoid/extensions/array/accessors.rb
123
124
  - lib/mongoid/extensions/array/assimilation.rb
@@ -147,12 +148,13 @@ files:
147
148
  - spec/integration/mongoid/document_spec.rb
148
149
  - spec/spec.opts
149
150
  - spec/spec_helper.rb
151
+ - spec/unit/mongoid/associations/belongs_to_related_spec.rb
150
152
  - spec/unit/mongoid/associations/belongs_to_spec.rb
153
+ - spec/unit/mongoid/associations/has_many_related_spec.rb
151
154
  - spec/unit/mongoid/associations/has_many_spec.rb
155
+ - spec/unit/mongoid/associations/has_one_related_spec.rb
152
156
  - spec/unit/mongoid/associations/has_one_spec.rb
153
157
  - spec/unit/mongoid/associations/options_spec.rb
154
- - spec/unit/mongoid/associations/relates_to_many_spec.rb
155
- - spec/unit/mongoid/associations/relates_to_one_spec.rb
156
158
  - spec/unit/mongoid/associations_spec.rb
157
159
  - spec/unit/mongoid/attributes_spec.rb
158
160
  - spec/unit/mongoid/commands/create_spec.rb
@@ -160,13 +162,13 @@ files:
160
162
  - spec/unit/mongoid/commands/delete_spec.rb
161
163
  - spec/unit/mongoid/commands/destroy_all_spec.rb
162
164
  - spec/unit/mongoid/commands/destroy_spec.rb
163
- - spec/unit/mongoid/commands/quick_save_spec.rb
164
165
  - spec/unit/mongoid/commands/save_spec.rb
165
166
  - spec/unit/mongoid/commands/validate_spec.rb
166
167
  - spec/unit/mongoid/commands_spec.rb
167
168
  - spec/unit/mongoid/criteria_spec.rb
168
169
  - spec/unit/mongoid/document_spec.rb
169
170
  - spec/unit/mongoid/dynamic_finder_spec.rb
171
+ - spec/unit/mongoid/errors_spec.rb
170
172
  - spec/unit/mongoid/extensions/array/accessors_spec.rb
171
173
  - spec/unit/mongoid/extensions/array/assimilation_spec.rb
172
174
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
@@ -221,12 +223,13 @@ test_files:
221
223
  - spec/integration/mongoid/associations_spec.rb
222
224
  - spec/integration/mongoid/document_spec.rb
223
225
  - spec/spec_helper.rb
226
+ - spec/unit/mongoid/associations/belongs_to_related_spec.rb
224
227
  - spec/unit/mongoid/associations/belongs_to_spec.rb
228
+ - spec/unit/mongoid/associations/has_many_related_spec.rb
225
229
  - spec/unit/mongoid/associations/has_many_spec.rb
230
+ - spec/unit/mongoid/associations/has_one_related_spec.rb
226
231
  - spec/unit/mongoid/associations/has_one_spec.rb
227
232
  - spec/unit/mongoid/associations/options_spec.rb
228
- - spec/unit/mongoid/associations/relates_to_many_spec.rb
229
- - spec/unit/mongoid/associations/relates_to_one_spec.rb
230
233
  - spec/unit/mongoid/associations_spec.rb
231
234
  - spec/unit/mongoid/attributes_spec.rb
232
235
  - spec/unit/mongoid/commands/create_spec.rb
@@ -234,13 +237,13 @@ test_files:
234
237
  - spec/unit/mongoid/commands/delete_spec.rb
235
238
  - spec/unit/mongoid/commands/destroy_all_spec.rb
236
239
  - spec/unit/mongoid/commands/destroy_spec.rb
237
- - spec/unit/mongoid/commands/quick_save_spec.rb
238
240
  - spec/unit/mongoid/commands/save_spec.rb
239
241
  - spec/unit/mongoid/commands/validate_spec.rb
240
242
  - spec/unit/mongoid/commands_spec.rb
241
243
  - spec/unit/mongoid/criteria_spec.rb
242
244
  - spec/unit/mongoid/document_spec.rb
243
245
  - spec/unit/mongoid/dynamic_finder_spec.rb
246
+ - spec/unit/mongoid/errors_spec.rb
244
247
  - spec/unit/mongoid/extensions/array/accessors_spec.rb
245
248
  - spec/unit/mongoid/extensions/array/assimilation_spec.rb
246
249
  - spec/unit/mongoid/extensions/array/conversions_spec.rb