mongoid 1.1.0 → 1.1.1
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.
- data/VERSION +1 -1
- data/lib/mongoid.rb +14 -3
- data/lib/mongoid/commands.rb +17 -7
- data/lib/mongoid/commands/create.rb +2 -2
- data/lib/mongoid/commands/deletion.rb +6 -1
- data/lib/mongoid/commands/save.rb +4 -4
- data/lib/mongoid/config.rb +7 -2
- data/lib/mongoid/document.rb +14 -1
- data/mongoid.gemspec +2 -2
- data/spec/integration/mongoid/commands_spec.rb +90 -16
- data/spec/integration/mongoid/document_spec.rb +1 -1
- data/spec/integration/mongoid/extensions_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/mongoid/associations/has_many_spec.rb +8 -1
- data/spec/unit/mongoid/commands/create_spec.rb +3 -3
- data/spec/unit/mongoid/commands/save_spec.rb +4 -4
- data/spec/unit/mongoid/commands_spec.rb +46 -13
- data/spec/unit/mongoid/config_spec.rb +28 -0
- data/spec/unit/mongoid/document_spec.rb +1 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -2
- data/spec/unit/mongoid/scope_spec.rb +4 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.1
|
data/lib/mongoid.rb
CHANGED
@@ -65,9 +65,20 @@ require "mongoid/document"
|
|
65
65
|
module Mongoid #:nodoc
|
66
66
|
|
67
67
|
class << self
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
|
69
|
+
delegate \
|
70
|
+
:allow_dynamic_fields,
|
71
|
+
:allow_dynamic_fields=,
|
72
|
+
:database,
|
73
|
+
:database=,
|
74
|
+
:persist_in_safe_mode,
|
75
|
+
:persist_in_safe_mode=,
|
76
|
+
:raise_not_found_error,
|
77
|
+
:raise_not_found_error=, :to => :config
|
78
|
+
|
79
|
+
protected
|
80
|
+
def config
|
81
|
+
Config.instance
|
71
82
|
end
|
72
83
|
end
|
73
84
|
|
data/lib/mongoid/commands.rb
CHANGED
@@ -59,10 +59,14 @@ module Mongoid #:nodoc:
|
|
59
59
|
# <tt>document.save(false) # save without validations</tt>
|
60
60
|
#
|
61
61
|
# Returns: true if validation passes, false if not.
|
62
|
-
def save(validate = true
|
62
|
+
def save(validate = true)
|
63
63
|
new = new_record?
|
64
64
|
run_callbacks(:before_create) if new
|
65
|
-
|
65
|
+
begin
|
66
|
+
saved = Save.execute(self, validate)
|
67
|
+
rescue Mongo::OperationFailure => e
|
68
|
+
errors.add(:mongoid, e.message)
|
69
|
+
end
|
66
70
|
run_callbacks(:after_create) if new
|
67
71
|
saved
|
68
72
|
end
|
@@ -76,7 +80,7 @@ module Mongoid #:nodoc:
|
|
76
80
|
#
|
77
81
|
# Returns: true if validation passes
|
78
82
|
def save!
|
79
|
-
return save(true
|
83
|
+
return save(true) || (raise Errors::Validations.new(self.errors))
|
80
84
|
end
|
81
85
|
|
82
86
|
# Update the document attributes and persist the document to the
|
@@ -119,7 +123,13 @@ module Mongoid #:nodoc:
|
|
119
123
|
#
|
120
124
|
# Returns: the +Document+.
|
121
125
|
def create(attributes = {})
|
122
|
-
|
126
|
+
document = new(attributes)
|
127
|
+
begin
|
128
|
+
Create.execute(document)
|
129
|
+
rescue Mongo::OperationFailure => e
|
130
|
+
document.errors.add(:mongoid, e.message)
|
131
|
+
end
|
132
|
+
document
|
123
133
|
end
|
124
134
|
|
125
135
|
# Create a new +Document+. This will instantiate a new document and save
|
@@ -132,9 +142,9 @@ module Mongoid #:nodoc:
|
|
132
142
|
#
|
133
143
|
# Returns: the +Document+.
|
134
144
|
def create!(attributes = {})
|
135
|
-
document = Create.execute(new(attributes), true
|
136
|
-
raise Errors::Validations.new(
|
137
|
-
|
145
|
+
document = Create.execute(new(attributes), true)
|
146
|
+
raise Errors::Validations.new(document.errors) unless document.errors.empty?
|
147
|
+
document
|
138
148
|
end
|
139
149
|
|
140
150
|
# Delete all documents given the supplied conditions. If no conditions
|
@@ -10,9 +10,9 @@ module Mongoid #:nodoc:
|
|
10
10
|
# doc: A new +Document+ that is going to be persisted.
|
11
11
|
#
|
12
12
|
# Returns: +Document+.
|
13
|
-
def self.execute(doc, validate = true
|
13
|
+
def self.execute(doc, validate = true)
|
14
14
|
doc.run_callbacks :before_create
|
15
|
-
Save.execute(doc, validate
|
15
|
+
Save.execute(doc, validate)
|
16
16
|
doc.run_callbacks :after_create
|
17
17
|
return doc
|
18
18
|
end
|
@@ -6,7 +6,12 @@ module Mongoid #:nodoc
|
|
6
6
|
# otherwise delete it from it's collection.
|
7
7
|
def delete(doc)
|
8
8
|
parent = doc._parent
|
9
|
-
|
9
|
+
if parent
|
10
|
+
parent.remove(doc)
|
11
|
+
parent.save
|
12
|
+
else
|
13
|
+
doc.collection.remove(:_id => doc.id)
|
14
|
+
end
|
10
15
|
end
|
11
16
|
end
|
12
17
|
end
|
@@ -9,13 +9,13 @@ module Mongoid #:nodoc:
|
|
9
9
|
#
|
10
10
|
# doc: A +Document+ that is going to be persisted.
|
11
11
|
#
|
12
|
-
# Returns: +
|
13
|
-
def self.execute(doc, validate = true
|
12
|
+
# Returns: +true+ if validation passes, +false+ if not.
|
13
|
+
def self.execute(doc, validate = true)
|
14
14
|
return false if validate && !doc.valid?
|
15
15
|
doc.run_callbacks :before_save
|
16
16
|
parent = doc._parent
|
17
|
-
doc.
|
18
|
-
|
17
|
+
if parent ? Save.execute(parent, validate) : doc.collection.save(doc.raw_attributes, :safe => Mongoid.persist_in_safe_mode)
|
18
|
+
doc.new_record = false
|
19
19
|
doc.run_callbacks :after_save
|
20
20
|
return true
|
21
21
|
else
|
data/lib/mongoid/config.rb
CHANGED
@@ -3,11 +3,16 @@ module Mongoid #:nodoc
|
|
3
3
|
class Config #:nodoc
|
4
4
|
include Singleton
|
5
5
|
|
6
|
-
attr_accessor
|
6
|
+
attr_accessor \
|
7
|
+
:allow_dynamic_fields,
|
8
|
+
:persist_in_safe_mode,
|
9
|
+
:raise_not_found_error
|
7
10
|
|
11
|
+
# Defaults the configuration options to true.
|
8
12
|
def initialize
|
9
|
-
@raise_not_found_error = true
|
10
13
|
@allow_dynamic_fields = true
|
14
|
+
@persist_in_safe_mode = true
|
15
|
+
@raise_not_found_error = true
|
11
16
|
end
|
12
17
|
|
13
18
|
# Sets the Mongo::DB to be used.
|
data/lib/mongoid/document.rb
CHANGED
@@ -163,12 +163,20 @@ module Mongoid #:nodoc:
|
|
163
163
|
def initialize(attrs = {})
|
164
164
|
@attributes = {}
|
165
165
|
process(attrs)
|
166
|
-
@attributes =
|
166
|
+
@attributes = attributes_with_defaults(@attributes)
|
167
167
|
@new_record = true if id.nil?
|
168
168
|
document = yield self if block_given?
|
169
169
|
identify
|
170
170
|
end
|
171
171
|
|
172
|
+
# apply default values to attributes - calling procs as required
|
173
|
+
def attributes_with_defaults(attributes = {})
|
174
|
+
default_values = defaults.merge(attributes)
|
175
|
+
default_values.each do |key, val|
|
176
|
+
default_values[key] = val.call if val.respond_to?(:call)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
172
180
|
# Returns the class name plus its attributes.
|
173
181
|
def inspect
|
174
182
|
attrs = fields.map { |name, field| "#{name}: #{@attributes[name].inspect}" } * ", "
|
@@ -215,6 +223,11 @@ module Mongoid #:nodoc:
|
|
215
223
|
add_observer(object)
|
216
224
|
end
|
217
225
|
|
226
|
+
# Return the attributes hash.
|
227
|
+
def raw_attributes
|
228
|
+
@attributes
|
229
|
+
end
|
230
|
+
|
218
231
|
# Reloads the +Document+ attributes from the database.
|
219
232
|
def reload
|
220
233
|
@attributes = collection.find_one(:_id => id)
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "1.1.
|
8
|
+
s.version = "1.1.1"
|
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-01-
|
12
|
+
s.date = %q{2010-01-22}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.rdoc"
|
@@ -43,34 +43,108 @@ describe Mongoid::Commands do
|
|
43
43
|
|
44
44
|
describe "#delete" do
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
context "deleting a root document" do
|
47
|
+
|
48
|
+
before do
|
49
|
+
@person.save
|
50
|
+
end
|
51
|
+
|
52
|
+
it "deletes the document" do
|
53
|
+
@person.delete
|
54
|
+
lambda { Person.find(@person.id) }.should raise_error
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns true" do
|
58
|
+
@person.delete.should be_true
|
59
|
+
end
|
49
60
|
|
50
|
-
it "deletes the document" do
|
51
|
-
@person.delete
|
52
|
-
lambda { Person.find(@person.id) }.should raise_error
|
53
61
|
end
|
54
62
|
|
55
|
-
|
56
|
-
|
63
|
+
context "deleting an embedded document" do
|
64
|
+
|
65
|
+
before do
|
66
|
+
@address = Address.new(:street => "Bond Street")
|
67
|
+
@person.addresses << @address
|
68
|
+
end
|
69
|
+
|
70
|
+
context "when the document is not yet saved" do
|
71
|
+
|
72
|
+
it "removes the document from the parent" do
|
73
|
+
@address.delete
|
74
|
+
@person.addresses.should be_empty
|
75
|
+
@person.attributes[:addresses].should be_empty
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
context "when the document has been saved" do
|
81
|
+
|
82
|
+
before do
|
83
|
+
@address.save
|
84
|
+
end
|
85
|
+
|
86
|
+
it "removes the object from the parent and database" do
|
87
|
+
@address.delete
|
88
|
+
from_db = Person.find(@person.id)
|
89
|
+
from_db.addresses.should be_empty
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
57
94
|
end
|
58
95
|
|
59
96
|
end
|
60
97
|
|
61
98
|
describe "#destroy" do
|
62
99
|
|
63
|
-
|
64
|
-
|
65
|
-
|
100
|
+
context "destroying a root document" do
|
101
|
+
|
102
|
+
before do
|
103
|
+
@person.save
|
104
|
+
end
|
105
|
+
|
106
|
+
it "destroys the document" do
|
107
|
+
@person.destroy
|
108
|
+
lambda { Person.find(@person.id) }.should raise_error
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns true" do
|
112
|
+
@person.destroy.should be_true
|
113
|
+
end
|
66
114
|
|
67
|
-
it "deletes the document" do
|
68
|
-
@person.destroy
|
69
|
-
lambda { Person.find(@person.id) }.should raise_error
|
70
115
|
end
|
71
116
|
|
72
|
-
|
73
|
-
|
117
|
+
context "deleting an embedded document" do
|
118
|
+
|
119
|
+
before do
|
120
|
+
@address = Address.new(:street => "Bond Street")
|
121
|
+
@person.addresses << @address
|
122
|
+
end
|
123
|
+
|
124
|
+
context "when the document is not yet saved" do
|
125
|
+
|
126
|
+
it "removes the document from the parent" do
|
127
|
+
@address.destroy
|
128
|
+
@person.addresses.should be_empty
|
129
|
+
@person.attributes[:addresses].should be_empty
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
context "when the document has been saved" do
|
135
|
+
|
136
|
+
before do
|
137
|
+
@address.save
|
138
|
+
end
|
139
|
+
|
140
|
+
it "removes the object from the parent and database" do
|
141
|
+
@address.destroy
|
142
|
+
from_db = Person.find(@person.id)
|
143
|
+
from_db.addresses.should be_empty
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
74
148
|
end
|
75
149
|
|
76
150
|
end
|
@@ -154,7 +154,7 @@ describe Mongoid::Document do
|
|
154
154
|
context "when the document is not found" do
|
155
155
|
|
156
156
|
it "creates a new document" do
|
157
|
-
person = Person.find_or_create_by(:title => "Senorita")
|
157
|
+
person = Person.find_or_create_by(:title => "Senorita", :ssn => "1234567")
|
158
158
|
person.title.should == "Senorita"
|
159
159
|
person.should_not be_a_new_record
|
160
160
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -37,7 +37,7 @@ class Person
|
|
37
37
|
field :aliases, :type => Array
|
38
38
|
field :map, :type => Hash
|
39
39
|
field :score, :type => Integer
|
40
|
-
field :blood_alcohol_content, :type => Float
|
40
|
+
field :blood_alcohol_content, :type => Float, :default => lambda{ 0.0 }
|
41
41
|
field :ssn
|
42
42
|
|
43
43
|
index :age
|
@@ -353,8 +353,15 @@ describe Mongoid::Associations::HasMany do
|
|
353
353
|
|
354
354
|
context "when no class method exists" do
|
355
355
|
|
356
|
-
|
356
|
+
before do
|
357
|
+
@association = Mongoid::Associations::HasMany.new(
|
358
|
+
@document,
|
359
|
+
Mongoid::Associations::Options.new(:name => :addresses)
|
360
|
+
)
|
361
|
+
end
|
357
362
|
|
363
|
+
it "delegates to the array" do
|
364
|
+
@association.entries.size.should == 2
|
358
365
|
end
|
359
366
|
|
360
367
|
end
|
@@ -9,19 +9,19 @@ describe Mongoid::Commands::Create do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "executes a save command" do
|
12
|
-
Mongoid::Commands::Save.expects(:execute).with(@document, true
|
12
|
+
Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
|
13
13
|
Mongoid::Commands::Create.execute(@document)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "runs the before and after create callbacks" do
|
17
17
|
@document.expects(:run_callbacks).with(:before_create)
|
18
|
-
Mongoid::Commands::Save.expects(:execute).with(@document, true
|
18
|
+
Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
|
19
19
|
@document.expects(:run_callbacks).with(:after_create)
|
20
20
|
Mongoid::Commands::Create.execute(@document)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "returns the document" do
|
24
|
-
Mongoid::Commands::Save.expects(:execute).with(@document, true
|
24
|
+
Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
|
25
25
|
Mongoid::Commands::Create.execute(@document).should == @document
|
26
26
|
end
|
27
27
|
|
@@ -11,12 +11,12 @@ describe Mongoid::Commands::Save do
|
|
11
11
|
:valid? => true,
|
12
12
|
:run_callbacks => true,
|
13
13
|
:_parent => nil,
|
14
|
-
:
|
14
|
+
:raw_attributes => {},
|
15
15
|
:new_record= => false)
|
16
16
|
@document = stub(:collection => @doc_collection,
|
17
17
|
:run_callbacks => true,
|
18
18
|
:_parent => @parent,
|
19
|
-
:
|
19
|
+
:raw_attributes => {},
|
20
20
|
:new_record= => false)
|
21
21
|
end
|
22
22
|
|
@@ -39,7 +39,7 @@ describe Mongoid::Commands::Save do
|
|
39
39
|
context "when the document has a parent" do
|
40
40
|
|
41
41
|
it "executes a save on the parent" do
|
42
|
-
@parent_collection.expects(:save).with(@parent.
|
42
|
+
@parent_collection.expects(:save).with(@parent.raw_attributes, :safe => true)
|
43
43
|
Mongoid::Commands::Save.execute(@document)
|
44
44
|
end
|
45
45
|
|
@@ -52,7 +52,7 @@ describe Mongoid::Commands::Save do
|
|
52
52
|
end
|
53
53
|
|
54
54
|
it "calls save on the document collection" do
|
55
|
-
@doc_collection.expects(:save).with(@document.
|
55
|
+
@doc_collection.expects(:save).with(@document.raw_attributes, :safe => true)
|
56
56
|
Mongoid::Commands::Save.execute(@document)
|
57
57
|
end
|
58
58
|
|
@@ -27,7 +27,7 @@ describe Mongoid::Commands do
|
|
27
27
|
describe "#save" do
|
28
28
|
|
29
29
|
it "delegates to the Save command" do
|
30
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
30
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
31
31
|
@person.save
|
32
32
|
end
|
33
33
|
|
@@ -38,13 +38,13 @@ describe Mongoid::Commands do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "delegates to the save command" do
|
41
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
41
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
42
42
|
@person.save
|
43
43
|
end
|
44
44
|
|
45
45
|
it "runs the before and after create callbacks" do
|
46
46
|
@person.expects(:run_callbacks).with(:before_create)
|
47
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
47
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
48
48
|
@person.expects(:run_callbacks).with(:after_create)
|
49
49
|
@person.save
|
50
50
|
end
|
@@ -58,12 +58,26 @@ describe Mongoid::Commands do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
it "passes the validate param to the command" do
|
61
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, false
|
61
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, false).returns(true)
|
62
62
|
@person.save(false)
|
63
63
|
end
|
64
64
|
|
65
65
|
end
|
66
66
|
|
67
|
+
context "when the database raises an error" do
|
68
|
+
|
69
|
+
before do
|
70
|
+
@person = Person.new
|
71
|
+
end
|
72
|
+
|
73
|
+
it "returns false" do
|
74
|
+
Mongoid::Commands::Save.expects(:execute).raises(Mongo::OperationFailure.new("Operation Failed"))
|
75
|
+
@person.save
|
76
|
+
@person.errors.on(:mongoid).should == "Operation Failed"
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
67
81
|
end
|
68
82
|
|
69
83
|
describe "#save!" do
|
@@ -71,7 +85,7 @@ describe Mongoid::Commands do
|
|
71
85
|
context "when validation passes" do
|
72
86
|
|
73
87
|
it "it returns the person" do
|
74
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
88
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
75
89
|
@person.save!
|
76
90
|
end
|
77
91
|
|
@@ -80,7 +94,7 @@ describe Mongoid::Commands do
|
|
80
94
|
context "when validation fails" do
|
81
95
|
|
82
96
|
it "it raises an error" do
|
83
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
97
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
|
84
98
|
lambda { @person.save! }.should raise_error
|
85
99
|
end
|
86
100
|
|
@@ -93,14 +107,14 @@ describe Mongoid::Commands do
|
|
93
107
|
end
|
94
108
|
|
95
109
|
it "delegates to the save command" do
|
96
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
110
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
97
111
|
@person.save!
|
98
112
|
end
|
99
113
|
|
100
114
|
context "when validation fails" do
|
101
115
|
|
102
116
|
it "it raises an error " do
|
103
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
117
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
|
104
118
|
lambda { @person.save! }.should raise_error
|
105
119
|
end
|
106
120
|
|
@@ -108,7 +122,7 @@ describe Mongoid::Commands do
|
|
108
122
|
|
109
123
|
it "runs the before and after create callbacks" do
|
110
124
|
@person.expects(:run_callbacks).with(:before_create)
|
111
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
125
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
112
126
|
@person.expects(:run_callbacks).with(:after_create)
|
113
127
|
@person.save!
|
114
128
|
end
|
@@ -120,7 +134,7 @@ describe Mongoid::Commands do
|
|
120
134
|
describe "#update_attributes" do
|
121
135
|
|
122
136
|
it "delegates to the Save command" do
|
123
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
137
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
124
138
|
@person.update_attributes({})
|
125
139
|
end
|
126
140
|
|
@@ -131,7 +145,7 @@ describe Mongoid::Commands do
|
|
131
145
|
context "when validation passes" do
|
132
146
|
|
133
147
|
it "it returns the person" do
|
134
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
148
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
|
135
149
|
@person.update_attributes({}).should be_true
|
136
150
|
end
|
137
151
|
|
@@ -140,7 +154,7 @@ describe Mongoid::Commands do
|
|
140
154
|
context "when validation fails" do
|
141
155
|
|
142
156
|
it "it raises an error" do
|
143
|
-
Mongoid::Commands::Save.expects(:execute).with(@person, true
|
157
|
+
Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
|
144
158
|
lambda { @person.update_attributes!({}) }.should raise_error
|
145
159
|
end
|
146
160
|
|
@@ -160,6 +174,25 @@ describe Mongoid::Commands do
|
|
160
174
|
Person.create.should_not be_nil
|
161
175
|
end
|
162
176
|
|
177
|
+
context "when the database raises an error" do
|
178
|
+
|
179
|
+
before do
|
180
|
+
@person = Person.new
|
181
|
+
Mongoid::Commands::Save.expects(:execute).raises(Mongo::OperationFailure.new("Operation Failed"))
|
182
|
+
end
|
183
|
+
|
184
|
+
it "returns the document with errors" do
|
185
|
+
person = Person.create
|
186
|
+
person.errors.on(:mongoid).should == "Operation Failed"
|
187
|
+
end
|
188
|
+
|
189
|
+
it "keeps the document's new record flag" do
|
190
|
+
person = Person.create
|
191
|
+
person.should be_a_new_record
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
163
196
|
end
|
164
197
|
|
165
198
|
describe ".create!" do
|
@@ -179,7 +212,7 @@ describe Mongoid::Commands do
|
|
179
212
|
it "raises an error" do
|
180
213
|
person = stub(:errors => stub(:empty? => false))
|
181
214
|
Mongoid::Commands::Create.expects(:execute).returns(person)
|
182
|
-
lambda { Person.create! }.should raise_error
|
215
|
+
lambda { Person.create! }.should raise_error(Mongoid::Errors::Validations)
|
183
216
|
end
|
184
217
|
|
185
218
|
end
|
@@ -20,6 +20,34 @@ describe Mongoid::Config do
|
|
20
20
|
|
21
21
|
end
|
22
22
|
|
23
|
+
describe "#persist_in_safe_mode=" do
|
24
|
+
|
25
|
+
context "when setting to true" do
|
26
|
+
|
27
|
+
before do
|
28
|
+
config.persist_in_safe_mode = true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "sets the value" do
|
32
|
+
config.persist_in_safe_mode.should == true
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when setting to false" do
|
38
|
+
|
39
|
+
before do
|
40
|
+
config.persist_in_safe_mode = false
|
41
|
+
end
|
42
|
+
|
43
|
+
it "sets the value" do
|
44
|
+
config.persist_in_safe_mode.should == false
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
23
51
|
describe "#raise_not_found_error=" do
|
24
52
|
|
25
53
|
context "when setting to true" do
|
@@ -10,8 +10,8 @@ describe Mongoid::Extensions::Array::Conversions do
|
|
10
10
|
Person.new(:_id => 2, :title => "Madam")
|
11
11
|
]
|
12
12
|
array.mongoidize.should ==
|
13
|
-
[ HashWithIndifferentAccess.new({ :_id => 1, :title => "Sir", :age => 100, :_type => "Person" }),
|
14
|
-
HashWithIndifferentAccess.new({ :_id => 2, :title => "Madam", :age => 100, :_type => "Person" }) ]
|
13
|
+
[ HashWithIndifferentAccess.new({ :_id => 1, :title => "Sir", :age => 100, :_type => "Person", "blood_alcohol_content" => 0.0 }),
|
14
|
+
HashWithIndifferentAccess.new({ :_id => 2, :title => "Madam", :age => 100, :_type => "Person", "blood_alcohol_content" => 0.0 }) ]
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
@@ -6,7 +6,7 @@ describe Mongoid::Extensions::Object::Conversions do
|
|
6
6
|
|
7
7
|
it "returns its attributes" do
|
8
8
|
Person.new(:_id => 1, :title => "Sir").mongoidize.should ==
|
9
|
-
{ "_id" => 1, "title" => "Sir", "age" => 100, "_type" => "Person" }
|
9
|
+
{ "_id" => 1, "title" => "Sir", "age" => 100, "_type" => "Person", "blood_alcohol_content" => 0.0 }
|
10
10
|
end
|
11
11
|
|
12
12
|
end
|
@@ -28,7 +28,7 @@ describe Mongoid::Extensions::Object::Conversions do
|
|
28
28
|
context "when object has attributes" do
|
29
29
|
|
30
30
|
before do
|
31
|
-
@attributes = { "_id" => "test", "title" => "Sir", "age" => 100, "_type" => "Person" }
|
31
|
+
@attributes = { "_id" => "test", "title" => "Sir", "age" => 100, "_type" => "Person", "blood_alcohol_content" => 0.0 }
|
32
32
|
@person = Person.new(@attributes)
|
33
33
|
end
|
34
34
|
|
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: 1.1.
|
4
|
+
version: 1.1.1
|
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: 2010-01-
|
12
|
+
date: 2010-01-22 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|