mongoid 0.9.8 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +5 -4
- data/VERSION +1 -1
- data/lib/mongoid.rb +3 -28
- data/lib/mongoid/associations.rb +63 -44
- data/lib/mongoid/associations/{relates_to_one.rb → belongs_to_related.rb} +4 -4
- data/lib/mongoid/associations/has_many.rb +26 -12
- data/lib/mongoid/associations/has_many_related.rb +100 -0
- data/lib/mongoid/associations/has_one.rb +13 -2
- data/lib/mongoid/associations/{relates_to_many.rb → has_one_related.rb} +34 -9
- data/lib/mongoid/attributes.rb +108 -8
- data/lib/mongoid/commands.rb +3 -11
- data/lib/mongoid/commands/save.rb +2 -6
- data/lib/mongoid/document.rb +6 -70
- data/lib/mongoid/errors.rb +34 -0
- data/mongoid.gemspec +14 -11
- data/spec/integration/mongoid/associations_spec.rb +19 -14
- data/spec/spec_helper.rb +6 -3
- data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +112 -0
- data/spec/unit/mongoid/associations/has_many_related_spec.rb +292 -0
- data/spec/unit/mongoid/associations/has_many_spec.rb +17 -0
- data/spec/unit/mongoid/associations/has_one_related_spec.rb +130 -0
- data/spec/unit/mongoid/associations/has_one_spec.rb +53 -0
- data/spec/unit/mongoid/associations_spec.rb +36 -8
- data/spec/unit/mongoid/attributes_spec.rb +218 -0
- data/spec/unit/mongoid/commands/save_spec.rb +4 -2
- data/spec/unit/mongoid/commands_spec.rb +4 -17
- data/spec/unit/mongoid/criteria_spec.rb +0 -5
- data/spec/unit/mongoid/document_spec.rb +26 -156
- data/spec/unit/mongoid/errors_spec.rb +87 -0
- metadata +14 -11
- data/lib/mongoid/commands/quick_save.rb +0 -19
- data/spec/unit/mongoid/associations/relates_to_many_spec.rb +0 -76
- data/spec/unit/mongoid/associations/relates_to_one_spec.rb +0 -112
- data/spec/unit/mongoid/commands/quick_save_spec.rb +0 -24
@@ -1,19 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid #:nodoc:
|
3
|
-
module Commands
|
4
|
-
class QuickSave
|
5
|
-
# Performs a save of the supplied +Document+ without any validations or
|
6
|
-
# callbacks. This is a dangerous command only intended for internal use
|
7
|
-
# with saving relational associations.
|
8
|
-
#
|
9
|
-
# Options:
|
10
|
-
#
|
11
|
-
# doc: A +Document+ that is going to be persisted.
|
12
|
-
#
|
13
|
-
# Returns: true
|
14
|
-
def self.execute(doc)
|
15
|
-
doc.collection.save(doc.attributes)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Associations::RelatesToMany do
|
4
|
-
|
5
|
-
describe ".initialize" do
|
6
|
-
|
7
|
-
context "when related id has been set" do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@document = Person.new
|
11
|
-
@options = Mongoid::Associations::Options.new(:name => :posts)
|
12
|
-
@criteria = stub
|
13
|
-
@first = stub(:person_id => @document.id)
|
14
|
-
@second = stub(:person_id => @document.id)
|
15
|
-
@related = [@first, @second]
|
16
|
-
end
|
17
|
-
|
18
|
-
it "finds the object by id" do
|
19
|
-
Post.expects(:all).with(:conditions => { "person_id" => @document.id }).returns(@related)
|
20
|
-
association = Mongoid::Associations::RelatesToMany.new(@document, @options)
|
21
|
-
association.should == @related
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
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
|
-
|
46
|
-
describe ".macro" do
|
47
|
-
|
48
|
-
it "returns :relates_to_many" do
|
49
|
-
Mongoid::Associations::RelatesToMany.macro.should == :relates_to_many
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
describe ".update" do
|
55
|
-
|
56
|
-
before do
|
57
|
-
@first = Post.new
|
58
|
-
@second = Post.new
|
59
|
-
@related = [@first, @second]
|
60
|
-
@parent = Person.new
|
61
|
-
@options = Mongoid::Associations::Options.new(:name => :posts)
|
62
|
-
end
|
63
|
-
|
64
|
-
it "sets the related object id on the parent" do
|
65
|
-
Mongoid::Associations::RelatesToMany.update(@related, @parent, @options)
|
66
|
-
@first.person_id.should == @parent.id
|
67
|
-
@second.person_id.should == @parent.id
|
68
|
-
end
|
69
|
-
|
70
|
-
it "returns the related objects" do
|
71
|
-
Mongoid::Associations::RelatesToMany.update(@related, @parent, @options).should == @related
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
@@ -1,112 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Associations::RelatesToOne do
|
4
|
-
|
5
|
-
describe ".initialize" do
|
6
|
-
|
7
|
-
context "when related id has been set" do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@document = stub(:game_id => "5")
|
11
|
-
@options = Mongoid::Associations::Options.new(:name => :game)
|
12
|
-
@related = stub
|
13
|
-
end
|
14
|
-
|
15
|
-
it "finds the object by id" do
|
16
|
-
Game.expects(:find).with(@document.game_id).returns(@related)
|
17
|
-
association = Mongoid::Associations::RelatesToOne.new(@document, "5", @options)
|
18
|
-
association.should == @related
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
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
|
-
|
85
|
-
describe ".macro" do
|
86
|
-
|
87
|
-
it "returns :relates_to_one" do
|
88
|
-
Mongoid::Associations::RelatesToOne.macro.should == :relates_to_one
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
describe ".update" do
|
94
|
-
|
95
|
-
before do
|
96
|
-
@related = stub(:id => "5")
|
97
|
-
@parent = Person.new
|
98
|
-
@options = Mongoid::Associations::Options.new(:name => :game)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "sets the related object id on the parent" do
|
102
|
-
Mongoid::Associations::RelatesToOne.update(@related, @parent, @options)
|
103
|
-
@parent.game_id.should == "5"
|
104
|
-
end
|
105
|
-
|
106
|
-
it "returns the related object" do
|
107
|
-
Mongoid::Associations::RelatesToOne.update(@related, @parent, @options).should == @related
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Commands::QuickSave do
|
4
|
-
|
5
|
-
describe "#execute" do
|
6
|
-
|
7
|
-
before do
|
8
|
-
@collection = mock
|
9
|
-
@document = stub(:collection => @collection, :attributes => {})
|
10
|
-
end
|
11
|
-
|
12
|
-
it "saves the document" do
|
13
|
-
@collection.expects(:save).with(@document.attributes)
|
14
|
-
Mongoid::Commands::QuickSave.execute(@document)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "returns true" do
|
18
|
-
@collection.expects(:save).with(@document.attributes)
|
19
|
-
Mongoid::Commands::QuickSave.execute(@document)
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|