mongoid 0.9.6 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/VERSION +1 -1
  2. data/lib/mongoid.rb +2 -2
  3. data/lib/mongoid/associations.rb +16 -9
  4. data/lib/mongoid/associations/belongs_to.rb +29 -5
  5. data/lib/mongoid/associations/has_many.rb +12 -0
  6. data/lib/mongoid/associations/has_one.rb +29 -9
  7. data/lib/mongoid/associations/options.rb +5 -0
  8. data/lib/mongoid/associations/relates_to_many.rb +10 -0
  9. data/lib/mongoid/associations/relates_to_one.rb +24 -5
  10. data/lib/mongoid/commands.rb +8 -0
  11. data/lib/mongoid/commands/quick_save.rb +19 -0
  12. data/lib/mongoid/commands/save.rb +1 -1
  13. data/lib/mongoid/criteria.rb +8 -18
  14. data/lib/mongoid/document.rb +27 -59
  15. data/mongoid.gemspec +8 -9
  16. data/spec/integration/mongoid/associations_spec.rb +41 -0
  17. data/spec/integration/mongoid/document_spec.rb +0 -13
  18. data/spec/spec_helper.rb +1 -8
  19. data/spec/unit/mongoid/associations/belongs_to_spec.rb +34 -4
  20. data/spec/unit/mongoid/associations/has_many_spec.rb +9 -0
  21. data/spec/unit/mongoid/associations/has_one_spec.rb +23 -2
  22. data/spec/unit/mongoid/associations/options_spec.rb +12 -1
  23. data/spec/unit/mongoid/associations/relates_to_many_spec.rb +22 -0
  24. data/spec/unit/mongoid/associations/relates_to_one_spec.rb +65 -1
  25. data/spec/unit/mongoid/associations_spec.rb +53 -1
  26. data/spec/unit/mongoid/commands/quick_save_spec.rb +24 -0
  27. data/spec/unit/mongoid/commands/save_spec.rb +2 -2
  28. data/spec/unit/mongoid/commands_spec.rb +107 -102
  29. data/spec/unit/mongoid/criteria_spec.rb +33 -1
  30. metadata +7 -8
  31. data/lib/mongoid/associations/accessor.rb +0 -30
  32. data/lib/mongoid/associations/decorator.rb +0 -27
  33. data/spec/unit/mongoid/associations/accessor_spec.rb +0 -123
  34. data/spec/unit/mongoid/associations/decorator_spec.rb +0 -36
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.9.6"
8
+ s.version = "0.9.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{2009-12-09}
12
+ s.date = %q{2009-12-12}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -23,9 +23,7 @@ Gem::Specification.new do |s|
23
23
  "VERSION",
24
24
  "lib/mongoid.rb",
25
25
  "lib/mongoid/associations.rb",
26
- "lib/mongoid/associations/accessor.rb",
27
26
  "lib/mongoid/associations/belongs_to.rb",
28
- "lib/mongoid/associations/decorator.rb",
29
27
  "lib/mongoid/associations/has_many.rb",
30
28
  "lib/mongoid/associations/has_one.rb",
31
29
  "lib/mongoid/associations/options.rb",
@@ -38,6 +36,7 @@ Gem::Specification.new do |s|
38
36
  "lib/mongoid/commands/delete_all.rb",
39
37
  "lib/mongoid/commands/destroy.rb",
40
38
  "lib/mongoid/commands/destroy_all.rb",
39
+ "lib/mongoid/commands/quick_save.rb",
41
40
  "lib/mongoid/commands/save.rb",
42
41
  "lib/mongoid/commands/validate.rb",
43
42
  "lib/mongoid/criteria.rb",
@@ -68,12 +67,11 @@ Gem::Specification.new do |s|
68
67
  "lib/mongoid/versioning.rb",
69
68
  "mongoid.gemspec",
70
69
  "perf/benchmark.rb",
70
+ "spec/integration/mongoid/associations_spec.rb",
71
71
  "spec/integration/mongoid/document_spec.rb",
72
72
  "spec/spec.opts",
73
73
  "spec/spec_helper.rb",
74
- "spec/unit/mongoid/associations/accessor_spec.rb",
75
74
  "spec/unit/mongoid/associations/belongs_to_spec.rb",
76
- "spec/unit/mongoid/associations/decorator_spec.rb",
77
75
  "spec/unit/mongoid/associations/has_many_spec.rb",
78
76
  "spec/unit/mongoid/associations/has_one_spec.rb",
79
77
  "spec/unit/mongoid/associations/options_spec.rb",
@@ -86,6 +84,7 @@ Gem::Specification.new do |s|
86
84
  "spec/unit/mongoid/commands/delete_spec.rb",
87
85
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
88
86
  "spec/unit/mongoid/commands/destroy_spec.rb",
87
+ "spec/unit/mongoid/commands/quick_save_spec.rb",
89
88
  "spec/unit/mongoid/commands/save_spec.rb",
90
89
  "spec/unit/mongoid/commands/validate_spec.rb",
91
90
  "spec/unit/mongoid/commands_spec.rb",
@@ -121,11 +120,10 @@ Gem::Specification.new do |s|
121
120
  s.rubygems_version = %q{1.3.5}
122
121
  s.summary = %q{ODM framework for MongoDB}
123
122
  s.test_files = [
124
- "spec/integration/mongoid/document_spec.rb",
123
+ "spec/integration/mongoid/associations_spec.rb",
124
+ "spec/integration/mongoid/document_spec.rb",
125
125
  "spec/spec_helper.rb",
126
- "spec/unit/mongoid/associations/accessor_spec.rb",
127
126
  "spec/unit/mongoid/associations/belongs_to_spec.rb",
128
- "spec/unit/mongoid/associations/decorator_spec.rb",
129
127
  "spec/unit/mongoid/associations/has_many_spec.rb",
130
128
  "spec/unit/mongoid/associations/has_one_spec.rb",
131
129
  "spec/unit/mongoid/associations/options_spec.rb",
@@ -138,6 +136,7 @@ Gem::Specification.new do |s|
138
136
  "spec/unit/mongoid/commands/delete_spec.rb",
139
137
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
140
138
  "spec/unit/mongoid/commands/destroy_spec.rb",
139
+ "spec/unit/mongoid/commands/quick_save_spec.rb",
141
140
  "spec/unit/mongoid/commands/save_spec.rb",
142
141
  "spec/unit/mongoid/commands/validate_spec.rb",
143
142
  "spec/unit/mongoid/commands_spec.rb",
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Associations do
4
+
5
+ before do
6
+ Person.delete_all; Game.delete_all; Post.delete_all
7
+ end
8
+
9
+ # context "one-to-one relational associations" do
10
+
11
+ # before do
12
+ # @person = Person.new(:title => "Sir")
13
+ # @game = Game.new(:score => 1)
14
+ # @person.game = @game
15
+ # @person.save
16
+ # end
17
+
18
+ # it "sets the association on save" do
19
+ # @from_db = Person.find(@person.id)
20
+ # @from_db.game.should == @game
21
+ # end
22
+
23
+ # end
24
+
25
+ context "one-to_many relational associations" do
26
+
27
+ before do
28
+ @person = Person.new(:title => "Sir")
29
+ @post = Post.new(:title => "Testing")
30
+ @person.posts = [@post]
31
+ @person.save
32
+ end
33
+
34
+ it "sets the association on save" do
35
+ # @from_db = Person.find(@person.id)
36
+ # @from_db.posts.should == [@post]
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -49,19 +49,6 @@ describe Mongoid::Document do
49
49
 
50
50
  end
51
51
 
52
- context "when creating a has one" do
53
-
54
- before do
55
- @person = Person.new(:title => "Esquire")
56
- @person.name.create(:first_name => "Jorge")
57
- end
58
-
59
- it "should create and save the entire graph" do
60
- person = Person.find(@person.id)
61
- person.name.first_name.should == "Jorge"
62
- end
63
-
64
- end
65
52
  end
66
53
 
67
54
  context "chaining criteria scopes" do
@@ -133,13 +133,6 @@ class Comment < Mongoid::Document
133
133
  key :text
134
134
  end
135
135
 
136
- class Decorated
137
- include Mongoid::Associations::Decorator
138
- def initialize(doc)
139
- @document = doc
140
- end
141
- end
142
-
143
136
  class Post < Mongoid::Document
144
137
  include Mongoid::Versioning
145
138
  field :title
@@ -148,7 +141,7 @@ end
148
141
 
149
142
  class Game < Mongoid::Document
150
143
  field :high_score, :default => 500
151
- field :score, :default => 0
144
+ field :score, :type => Integer, :default => 0
152
145
  end
153
146
 
154
147
  if RUBY_VERSION == '1.8.6'
@@ -6,9 +6,8 @@ describe Mongoid::Associations::BelongsTo do
6
6
 
7
7
  before do
8
8
  @parent = Name.new(:first_name => "Drexel")
9
- @document = stub(:parent => @parent)
10
9
  @options = Mongoid::Associations::Options.new(:name => :person)
11
- @association = Mongoid::Associations::BelongsTo.new(@document, @options)
10
+ @association = Mongoid::Associations::BelongsTo.new(@parent, @options)
12
11
  end
13
12
 
14
13
  context "when finding by id" do
@@ -26,9 +25,8 @@ describe Mongoid::Associations::BelongsTo do
26
25
 
27
26
  before do
28
27
  @parent = Name.new(:first_name => "Drexel")
29
- @document = stub(:parent => @parent)
30
28
  @options = Mongoid::Associations::Options.new(:name => :person)
31
- @association = Mongoid::Associations::BelongsTo.new(@document, @options)
29
+ @association = Mongoid::Associations::BelongsTo.new(@parent, @options)
32
30
  end
33
31
 
34
32
  context "when getting values" do
@@ -50,6 +48,38 @@ describe Mongoid::Associations::BelongsTo do
50
48
 
51
49
  end
52
50
 
51
+ describe ".instantiate" do
52
+
53
+ context "when parent exists" do
54
+
55
+ before do
56
+ @parent = Name.new(:first_name => "Drexel")
57
+ @document = stub(:parent => @parent)
58
+ @options = Mongoid::Associations::Options.new(:name => :person)
59
+ end
60
+
61
+ it "delegates to new" do
62
+ Mongoid::Associations::BelongsTo.expects(:new).with(@parent, @options)
63
+ Mongoid::Associations::BelongsTo.instantiate(@document, @options)
64
+ end
65
+
66
+ end
67
+
68
+ context "when parent is nil" do
69
+
70
+ before do
71
+ @document = stub(:parent => nil)
72
+ @options = Mongoid::Associations::Options.new(:name => :person)
73
+ end
74
+
75
+ it "returns nil" do
76
+ Mongoid::Associations::BelongsTo.instantiate(@document, @options).should be_nil
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
53
83
  describe ".macro" do
54
84
 
55
85
  it "returns :belongs_to" do
@@ -176,6 +176,15 @@ describe Mongoid::Associations::HasMany do
176
176
 
177
177
  end
178
178
 
179
+ describe ".instantiate" do
180
+
181
+ it "delegates to new" do
182
+ Mongoid::Associations::HasMany.expects(:new).with(@document, @options)
183
+ Mongoid::Associations::HasMany.instantiate(@document, @options)
184
+ end
185
+
186
+ end
187
+
179
188
  describe "#length" do
180
189
 
181
190
  context "#length" do
@@ -14,6 +14,7 @@ describe Mongoid::Associations::HasOne do
14
14
  before do
15
15
  @association = Mongoid::Associations::HasOne.new(
16
16
  @document,
17
+ @attributes[:mixed_drink],
17
18
  Mongoid::Associations::Options.new(:name => :mixed_drink)
18
19
  )
19
20
  end
@@ -34,6 +35,7 @@ describe Mongoid::Associations::HasOne do
34
35
  before do
35
36
  @association = Mongoid::Associations::HasOne.new(
36
37
  @document,
38
+ @attributes[:mixed_drink],
37
39
  Mongoid::Associations::Options.new(:name => :mixed_drink)
38
40
  )
39
41
  @drink = MixedDrink.new(:name => "Sapphire and Tonic")
@@ -49,11 +51,12 @@ describe Mongoid::Associations::HasOne do
49
51
 
50
52
  end
51
53
 
52
- describe "#decorate!" do
54
+ describe "#method_missing" do
53
55
 
54
56
  before do
55
57
  @association = Mongoid::Associations::HasOne.new(
56
58
  @document,
59
+ @attributes[:mixed_drink],
57
60
  Mongoid::Associations::Options.new(:name => :mixed_drink)
58
61
  )
59
62
  end
@@ -77,9 +80,27 @@ describe Mongoid::Associations::HasOne do
77
80
 
78
81
  end
79
82
 
83
+ describe ".instantiate" do
84
+
85
+ context "when attributes exist" do
86
+
87
+ before do
88
+ @document = stub(:attributes => { :name => { :first_name => "Test" } })
89
+ @options = Mongoid::Associations::Options.new(:name => :name)
90
+ end
91
+
92
+ it "delegates to new" do
93
+ Mongoid::Associations::HasOne.expects(:new).with(@document, { :first_name => "Test" }, @options)
94
+ Mongoid::Associations::HasOne.instantiate(@document, @options)
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
80
101
  describe ".macro" do
81
102
 
82
- it "returns :has_many" do
103
+ it "returns :has_one" do
83
104
  Mongoid::Associations::HasOne.macro.should == :has_one
84
105
  end
85
106
 
@@ -84,7 +84,19 @@ describe Mongoid::Associations::Options do
84
84
 
85
85
  end
86
86
 
87
+ end
88
+
89
+ end
87
90
 
91
+ describe "#parent_key" do
92
+
93
+ before do
94
+ @attributes = { :parent_key => :person_id }
95
+ @options = Mongoid::Associations::Options.new(@attributes)
96
+ end
97
+
98
+ it "returns the parent's foreign key value" do
99
+ @options.parent_key.should == :person_id
88
100
  end
89
101
 
90
102
  end
@@ -96,7 +108,6 @@ describe Mongoid::Associations::Options do
96
108
  before do
97
109
  @attributes = { :polymorphic => true }
98
110
  @options = Mongoid::Associations::Options.new(@attributes)
99
-
100
111
  end
101
112
 
102
113
  it "returns the attribute" do
@@ -25,6 +25,24 @@ describe Mongoid::Associations::RelatesToMany do
25
25
 
26
26
  end
27
27
 
28
+ describe ".instantiate" do
29
+
30
+ context "when related id has been set" do
31
+
32
+ before do
33
+ @document = Person.new
34
+ @options = Mongoid::Associations::Options.new(:name => :posts)
35
+ end
36
+
37
+ it "delegates to new" do
38
+ Mongoid::Associations::RelatesToMany.expects(:new).with(@document, @options)
39
+ association = Mongoid::Associations::RelatesToMany.instantiate(@document, @options)
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
28
46
  describe ".macro" do
29
47
 
30
48
  it "returns :relates_to_many" do
@@ -49,6 +67,10 @@ describe Mongoid::Associations::RelatesToMany do
49
67
  @second.person_id.should == @parent.id
50
68
  end
51
69
 
70
+ it "returns the related objects" do
71
+ Mongoid::Associations::RelatesToMany.update(@related, @parent, @options).should == @related
72
+ end
73
+
52
74
  end
53
75
 
54
76
  end
@@ -14,7 +14,7 @@ describe Mongoid::Associations::RelatesToOne do
14
14
 
15
15
  it "finds the object by id" do
16
16
  Game.expects(:find).with(@document.game_id).returns(@related)
17
- association = Mongoid::Associations::RelatesToOne.new(@document, @options)
17
+ association = Mongoid::Associations::RelatesToOne.new(@document, "5", @options)
18
18
  association.should == @related
19
19
  end
20
20
 
@@ -22,6 +22,66 @@ describe Mongoid::Associations::RelatesToOne do
22
22
 
23
23
  end
24
24
 
25
+ describe ".instantiate" do
26
+
27
+ context "when foreign key is not nil" do
28
+
29
+ before do
30
+ @document = stub(:game_id => "5")
31
+ @options = Mongoid::Associations::Options.new(:name => :game)
32
+ end
33
+
34
+ it "delegates to new" do
35
+ Mongoid::Associations::RelatesToOne.expects(:new).with(@document, "5", @options)
36
+ Mongoid::Associations::RelatesToOne.instantiate(@document, @options)
37
+ end
38
+
39
+ end
40
+
41
+ context "when foreign key is nil" do
42
+
43
+ before do
44
+ @document = stub(:game_id => nil)
45
+ @options = Mongoid::Associations::Options.new(:name => :game)
46
+ end
47
+
48
+ it "returns nil" do
49
+ Mongoid::Associations::RelatesToOne.instantiate(@document, @options).should be_nil
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ describe "#method_missing" do
57
+
58
+ before do
59
+ @game = Game.new(:score => 5000)
60
+ @document = stub(:game_id => "5")
61
+ @options = Mongoid::Associations::Options.new(:name => :game)
62
+ Game.expects(:find).with(@document.game_id).returns(@game)
63
+ @association = Mongoid::Associations::RelatesToOne.new(@document, "5", @options)
64
+ end
65
+
66
+ context "when getting values" do
67
+
68
+ it "delegates to the document" do
69
+ @association.score.should == 5000
70
+ end
71
+
72
+ end
73
+
74
+ context "when setting values" do
75
+
76
+ it "delegates to the document" do
77
+ @association.score = 400
78
+ @association.score.should == 400
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
25
85
  describe ".macro" do
26
86
 
27
87
  it "returns :relates_to_one" do
@@ -43,6 +103,10 @@ describe Mongoid::Associations::RelatesToOne do
43
103
  @parent.game_id.should == "5"
44
104
  end
45
105
 
106
+ it "returns the related object" do
107
+ Mongoid::Associations::RelatesToOne.update(@related, @parent, @options).should == @related
108
+ end
109
+
46
110
  end
47
111
 
48
112
  end
@@ -184,9 +184,10 @@ describe Mongoid::Associations do
184
184
 
185
185
  describe ".has_one" do
186
186
 
187
- it "adds a new Association to the collection" do
187
+ it "adds a new Association to the document" do
188
188
  person = Person.new
189
189
  person.name.should_not be_nil
190
+ person.attributes[:name].should be_nil
190
191
  end
191
192
 
192
193
  it "creates a reader for the association" do
@@ -262,4 +263,55 @@ describe Mongoid::Associations do
262
263
 
263
264
  end
264
265
 
266
+ describe "#update_associations" do
267
+
268
+ context "when associations exist" do
269
+
270
+ before do
271
+ @related = stub(:id => "100", :person= => true)
272
+ @person = Person.new
273
+ @person.posts = [@related]
274
+ end
275
+
276
+ it "saves each association" do
277
+ @related.expects(:quick_save).returns(@related)
278
+ @person.update_associations(:posts)
279
+ end
280
+
281
+ end
282
+
283
+ context "when no associations exist" do
284
+
285
+ before do
286
+ @person = Person.new
287
+ end
288
+
289
+ it "does nothing" do
290
+ @person.update_associations(:posts)
291
+ @person.posts.first.should be_nil
292
+ end
293
+
294
+ end
295
+
296
+ end
297
+
298
+ describe "#update_association" do
299
+
300
+ context "when the association exists" do
301
+
302
+ before do
303
+ @related = stub(:id => "100", :person= => true)
304
+ @person = Person.new
305
+ @person.game = @related
306
+ end
307
+
308
+ it "saves each association" do
309
+ @related.expects(:quick_save).returns(@related)
310
+ @person.update_association(:game)
311
+ end
312
+
313
+ end
314
+
315
+ end
316
+
265
317
  end