mongoid 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/commands.rb +6 -2
- data/mongoid.gemspec +1 -1
- data/spec/unit/mongoid/commands_spec.rb +37 -2
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.2
|
data/lib/mongoid/commands.rb
CHANGED
@@ -47,13 +47,17 @@ module Mongoid #:nodoc:
|
|
47
47
|
|
48
48
|
# Save the +Document+. Delegates to the Save command.
|
49
49
|
def save
|
50
|
-
Save.execute(self)
|
50
|
+
new_record? ? Create.execute(self) : Save.execute(self)
|
51
51
|
end
|
52
52
|
|
53
53
|
# Save the +Document+. Delegates to the Save command. If the command
|
54
54
|
# returns false then a +ValidationError+ will be raised.
|
55
55
|
def save!
|
56
|
-
|
56
|
+
if new_record?
|
57
|
+
return Create.execute(self) || (raise ValidationsError.new(self.errors.full_messages))
|
58
|
+
else
|
59
|
+
return Save.execute(self) || (raise ValidationsError.new(self.errors.full_messages))
|
60
|
+
end
|
57
61
|
end
|
58
62
|
|
59
63
|
# Update the attributes of the +Document+. Will call save after the
|
data/mongoid.gemspec
CHANGED
@@ -5,7 +5,7 @@ describe Mongoid::Commands do
|
|
5
5
|
context "instance methods" do
|
6
6
|
|
7
7
|
before do
|
8
|
-
@person = Person.new(:_id => Mongo::ObjectID.new)
|
8
|
+
@person = Person.new(:_id => Mongo::ObjectID.new.to_s)
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "#delete" do
|
@@ -33,6 +33,19 @@ describe Mongoid::Commands do
|
|
33
33
|
@person.save
|
34
34
|
end
|
35
35
|
|
36
|
+
context "when document is new" do
|
37
|
+
|
38
|
+
before do
|
39
|
+
@person = Person.new
|
40
|
+
end
|
41
|
+
|
42
|
+
it "delegates to the Create command" do
|
43
|
+
Mongoid::Commands::Create.expects(:execute).with(@person).returns(@person)
|
44
|
+
@person.save
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
36
49
|
end
|
37
50
|
|
38
51
|
describe "#save!" do
|
@@ -55,6 +68,28 @@ describe Mongoid::Commands do
|
|
55
68
|
|
56
69
|
end
|
57
70
|
|
71
|
+
context "when document is new" do
|
72
|
+
|
73
|
+
before do
|
74
|
+
@person = Person.new
|
75
|
+
end
|
76
|
+
|
77
|
+
it "delegates to the Create command" do
|
78
|
+
Mongoid::Commands::Create.expects(:execute).with(@person).returns(@person)
|
79
|
+
@person.save!
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when validation fails" do
|
83
|
+
|
84
|
+
it "it raises a ValidationsError" do
|
85
|
+
Mongoid::Commands::Create.expects(:execute).with(@person).returns(false)
|
86
|
+
lambda { @person.save! }.should raise_error
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
58
93
|
end
|
59
94
|
|
60
95
|
describe "#update_attributes" do
|
@@ -123,7 +158,7 @@ describe Mongoid::Commands do
|
|
123
158
|
it "raises an exception" do
|
124
159
|
person = stub(:errors => stub(:empty? => false))
|
125
160
|
Mongoid::Commands::Create.expects(:execute).returns(person)
|
126
|
-
lambda { Person.create! }.should raise_error
|
161
|
+
lambda { Person.create! }.should raise_error
|
127
162
|
end
|
128
163
|
|
129
164
|
end
|