mongoid 0.9.9 → 0.9.10
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +7 -7
- data/VERSION +1 -1
- data/lib/mongoid/associations.rb +1 -0
- data/lib/mongoid/associations/belongs_to.rb +3 -3
- data/lib/mongoid/associations/has_many.rb +8 -3
- data/lib/mongoid/associations/has_one.rb +6 -5
- data/lib/mongoid/associations/proxy.rb +13 -0
- data/lib/mongoid/criteria.rb +14 -11
- data/lib/mongoid/document.rb +6 -4
- data/lib/mongoid/errors.rb +11 -0
- data/mongoid.gemspec +26 -23
- data/perf/benchmark.rb +9 -5
- data/spec/integration/mongoid/associations_spec.rb +41 -2
- data/spec/integration/mongoid/document_spec.rb +0 -36
- data/spec/integration/mongoid/finders_spec.rb +37 -0
- data/spec/spec_helper.rb +3 -3
- data/spec/unit/mongoid/associations/has_one_spec.rb +28 -0
- data/spec/unit/mongoid/associations_spec.rb +2 -3
- data/spec/unit/mongoid/criteria_spec.rb +10 -1
- data/spec/unit/mongoid/document_spec.rb +38 -2
- data/spec/unit/mongoid/errors_spec.rb +20 -0
- data/spec/unit/mongoid/finders_spec.rb +10 -0
- metadata +12 -9
data/Rakefile
CHANGED
@@ -12,14 +12,14 @@ begin
|
|
12
12
|
gem.homepage = "http://github.com/durran/mongoid"
|
13
13
|
gem.authors = ["Durran Jordan"]
|
14
14
|
|
15
|
-
gem.add_dependency("durran-validatable", "1.8.3")
|
16
|
-
gem.add_dependency("leshill-will_paginate", "2.3.11")
|
17
|
-
gem.add_dependency("activesupport", "2.3.4")
|
18
|
-
gem.add_dependency("mongo", "0.18.1")
|
19
|
-
gem.add_dependency("mongo_ext", "0.18.1")
|
15
|
+
gem.add_dependency("durran-validatable", ">= 1.8.3")
|
16
|
+
gem.add_dependency("leshill-will_paginate", ">= 2.3.11")
|
17
|
+
gem.add_dependency("activesupport", ">= 2.3.4")
|
18
|
+
gem.add_dependency("mongo", ">= 0.18.1")
|
19
|
+
gem.add_dependency("mongo_ext", ">= 0.18.1")
|
20
20
|
|
21
|
-
gem.add_development_dependency("rspec", "1.2.9")
|
22
|
-
gem.add_development_dependency("mocha", "0.9.8")
|
21
|
+
gem.add_development_dependency("rspec", ">= 1.2.9")
|
22
|
+
gem.add_development_dependency("mocha", ">= 0.9.8")
|
23
23
|
end
|
24
24
|
Jeweler::GemcutterTasks.new
|
25
25
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.10
|
data/lib/mongoid/associations.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Associations #:nodoc:
|
4
4
|
class BelongsTo #:nodoc:
|
5
|
+
include Proxy
|
5
6
|
|
6
|
-
delegate :==, :to => :document
|
7
7
|
attr_reader :document, :options
|
8
8
|
|
9
9
|
# Creates the new association by setting the internal
|
@@ -29,8 +29,8 @@ module Mongoid #:nodoc:
|
|
29
29
|
end
|
30
30
|
|
31
31
|
# Delegate all missing methods over to the parent +Document+.
|
32
|
-
def method_missing(name, *args)
|
33
|
-
@document.send(name, *args)
|
32
|
+
def method_missing(name, *args, &block)
|
33
|
+
@document.send(name, *args, &block)
|
34
34
|
end
|
35
35
|
|
36
36
|
class << self
|
@@ -1,7 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Associations #:nodoc:
|
4
|
-
class HasMany
|
4
|
+
class HasMany
|
5
|
+
include Proxy
|
5
6
|
|
6
7
|
attr_accessor :association_name, :klass
|
7
8
|
|
@@ -20,7 +21,7 @@ module Mongoid #:nodoc:
|
|
20
21
|
object = @documents.first
|
21
22
|
object.changed(true)
|
22
23
|
object.notify_observers(object, true)
|
23
|
-
|
24
|
+
@documents.clear
|
24
25
|
end
|
25
26
|
|
26
27
|
# Appends the object to the +Array+, setting its parent in
|
@@ -75,7 +76,11 @@ module Mongoid #:nodoc:
|
|
75
76
|
child.parentize(@parent, @association_name)
|
76
77
|
child
|
77
78
|
end : []
|
78
|
-
|
79
|
+
end
|
80
|
+
|
81
|
+
# Delegate all missing methods over to the documents array.
|
82
|
+
def method_missing(name, *args, &block)
|
83
|
+
@documents.send(name, *args, &block)
|
79
84
|
end
|
80
85
|
|
81
86
|
# Used for setting associations via a nested attributes setter from the
|
@@ -2,8 +2,8 @@
|
|
2
2
|
module Mongoid #:nodoc:
|
3
3
|
module Associations #:nodoc:
|
4
4
|
class HasOne #:nodoc:
|
5
|
+
include Proxy
|
5
6
|
|
6
|
-
delegate :==, :to => :document
|
7
7
|
attr_reader :association_name, :document, :parent, :options
|
8
8
|
|
9
9
|
# Build a new object for the association.
|
@@ -19,8 +19,8 @@ module Mongoid #:nodoc:
|
|
19
19
|
self
|
20
20
|
end
|
21
21
|
|
22
|
-
# Creates the new association by finding the attributes in
|
23
|
-
# the parent document with its name, and instantiating a
|
22
|
+
# Creates the new association by finding the attributes in
|
23
|
+
# the parent document with its name, and instantiating a
|
24
24
|
# new document for it.
|
25
25
|
#
|
26
26
|
# All method calls on this object will then be delegated
|
@@ -39,8 +39,9 @@ module Mongoid #:nodoc:
|
|
39
39
|
end
|
40
40
|
|
41
41
|
# Delegate all missing methods over to the +Document+.
|
42
|
-
def method_missing(name, *args)
|
43
|
-
@document.
|
42
|
+
def method_missing(name, *args, &block)
|
43
|
+
@document = {}.assimilate(@parent, @options) if @document.nil?
|
44
|
+
@document.send(name, *args, &block)
|
44
45
|
end
|
45
46
|
|
46
47
|
# Used for setting the association via a nested attributes setter on the
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Mongoid #:nodoc
|
2
|
+
module Associations #:nodoc
|
3
|
+
module Proxy #:nodoc
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
instance_methods.each do |method|
|
7
|
+
undef_method(method) unless method =~ /(^__|^nil\?$|^send$|^object_id$)/
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -151,16 +151,6 @@ module Mongoid #:nodoc:
|
|
151
151
|
@options = extras; filter_options; self
|
152
152
|
end
|
153
153
|
|
154
|
-
# Return the first result for the +Criteria+.
|
155
|
-
#
|
156
|
-
# Example:
|
157
|
-
#
|
158
|
-
# <tt>Criteria.select(:name).where(:name = "Chrissy").one</tt>
|
159
|
-
def one
|
160
|
-
attributes = @klass.collection.find_one(@selector, @options.dup)
|
161
|
-
attributes ? @klass.instantiate(attributes) : nil
|
162
|
-
end
|
163
|
-
|
164
154
|
GROUP_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
|
165
155
|
# Groups the criteria. This will take the internally built selector and options
|
166
156
|
# and pass them on to the Ruby driver's +group()+ method on the collection. The
|
@@ -341,6 +331,16 @@ module Mongoid #:nodoc:
|
|
341
331
|
@options[:skip]
|
342
332
|
end
|
343
333
|
|
334
|
+
# Return the first result for the +Criteria+.
|
335
|
+
#
|
336
|
+
# Example:
|
337
|
+
#
|
338
|
+
# <tt>Criteria.select(:name).where(:name = "Chrissy").one</tt>
|
339
|
+
def one
|
340
|
+
attributes = @klass.collection.find_one(@selector, @options.dup)
|
341
|
+
attributes ? @klass.instantiate(attributes) : nil
|
342
|
+
end
|
343
|
+
|
344
344
|
# Adds a criterion to the +Criteria+ that specifies the sort order of
|
345
345
|
# the returned documents in the database. Similar to a SQL "ORDER BY".
|
346
346
|
#
|
@@ -438,7 +438,10 @@ module Mongoid #:nodoc:
|
|
438
438
|
def self.translate(*args)
|
439
439
|
klass = args[0]
|
440
440
|
params = args[1] || {}
|
441
|
-
|
441
|
+
if params.is_a?(String)
|
442
|
+
document = new(klass).id(params).one
|
443
|
+
return document ? document : (raise Errors::DocumentNotFound.new(klass, params))
|
444
|
+
end
|
442
445
|
return new(klass).where(params.delete(:conditions) || {}).extras(params)
|
443
446
|
end
|
444
447
|
|
data/lib/mongoid/document.rb
CHANGED
@@ -100,11 +100,12 @@ module Mongoid #:nodoc:
|
|
100
100
|
|
101
101
|
# Define a field attribute for the +Document+.
|
102
102
|
def define(name, options = {})
|
103
|
+
meth = options.delete(:as) || name
|
103
104
|
@fields ||= {}.with_indifferent_access
|
104
105
|
@fields[name] = Field.new(name.to_s, options)
|
105
|
-
define_method(
|
106
|
-
define_method("#{
|
107
|
-
define_method("#{
|
106
|
+
define_method(meth) { read_attribute(name) }
|
107
|
+
define_method("#{meth}=") { |value| write_attribute(name, value) }
|
108
|
+
define_method("#{meth}?") { read_attribute(name) == true } if options[:type] == Boolean
|
108
109
|
end
|
109
110
|
|
110
111
|
# Set up a default value for a field.
|
@@ -180,7 +181,8 @@ module Mongoid #:nodoc:
|
|
180
181
|
@attributes = {}.with_indifferent_access
|
181
182
|
process(defaults.merge(attrs))
|
182
183
|
@new_record = true if id.nil?
|
183
|
-
|
184
|
+
document = yield self if block_given?
|
185
|
+
generate_key; document
|
184
186
|
end
|
185
187
|
|
186
188
|
# Returns the class name plus its attributes.
|
data/lib/mongoid/errors.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
module Mongoid #:nodoc
|
2
2
|
module Errors #:nodoc
|
3
3
|
|
4
|
+
# Raised when querying the database for a document by a specific id which
|
5
|
+
# does not exist.
|
6
|
+
class DocumentNotFound < RuntimeError
|
7
|
+
def initialize(klass, identifier)
|
8
|
+
@klass, @identifier = klass, identifier
|
9
|
+
end
|
10
|
+
def message
|
11
|
+
"Document not found for class #{@klass} and id #{@identifier}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
4
15
|
# Raised when invalid options are passed into a constructor.
|
5
16
|
class InvalidOptions < RuntimeError; end
|
6
17
|
|
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 = "0.9.
|
8
|
+
s.version = "0.9.10"
|
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-
|
12
|
+
s.date = %q{2009-12-19}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/mongoid/associations/has_one.rb",
|
31
31
|
"lib/mongoid/associations/has_one_related.rb",
|
32
32
|
"lib/mongoid/associations/options.rb",
|
33
|
+
"lib/mongoid/associations/proxy.rb",
|
33
34
|
"lib/mongoid/attributes.rb",
|
34
35
|
"lib/mongoid/commands.rb",
|
35
36
|
"lib/mongoid/commands/create.rb",
|
@@ -70,6 +71,7 @@ Gem::Specification.new do |s|
|
|
70
71
|
"perf/benchmark.rb",
|
71
72
|
"spec/integration/mongoid/associations_spec.rb",
|
72
73
|
"spec/integration/mongoid/document_spec.rb",
|
74
|
+
"spec/integration/mongoid/finders_spec.rb",
|
73
75
|
"spec/spec.opts",
|
74
76
|
"spec/spec_helper.rb",
|
75
77
|
"spec/unit/mongoid/associations/belongs_to_related_spec.rb",
|
@@ -124,6 +126,7 @@ Gem::Specification.new do |s|
|
|
124
126
|
s.test_files = [
|
125
127
|
"spec/integration/mongoid/associations_spec.rb",
|
126
128
|
"spec/integration/mongoid/document_spec.rb",
|
129
|
+
"spec/integration/mongoid/finders_spec.rb",
|
127
130
|
"spec/spec_helper.rb",
|
128
131
|
"spec/unit/mongoid/associations/belongs_to_related_spec.rb",
|
129
132
|
"spec/unit/mongoid/associations/belongs_to_spec.rb",
|
@@ -175,30 +178,30 @@ Gem::Specification.new do |s|
|
|
175
178
|
s.specification_version = 3
|
176
179
|
|
177
180
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
178
|
-
s.add_runtime_dependency(%q<durran-validatable>, ["
|
179
|
-
s.add_runtime_dependency(%q<leshill-will_paginate>, ["
|
180
|
-
s.add_runtime_dependency(%q<activesupport>, ["
|
181
|
-
s.add_runtime_dependency(%q<mongo>, ["
|
182
|
-
s.add_runtime_dependency(%q<mongo_ext>, ["
|
183
|
-
s.add_development_dependency(%q<rspec>, ["
|
184
|
-
s.add_development_dependency(%q<mocha>, ["
|
181
|
+
s.add_runtime_dependency(%q<durran-validatable>, [">= 1.8.3"])
|
182
|
+
s.add_runtime_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
|
183
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
|
184
|
+
s.add_runtime_dependency(%q<mongo>, [">= 0.18.1"])
|
185
|
+
s.add_runtime_dependency(%q<mongo_ext>, [">= 0.18.1"])
|
186
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
187
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
|
185
188
|
else
|
186
|
-
s.add_dependency(%q<durran-validatable>, ["
|
187
|
-
s.add_dependency(%q<leshill-will_paginate>, ["
|
188
|
-
s.add_dependency(%q<activesupport>, ["
|
189
|
-
s.add_dependency(%q<mongo>, ["
|
190
|
-
s.add_dependency(%q<mongo_ext>, ["
|
191
|
-
s.add_dependency(%q<rspec>, ["
|
192
|
-
s.add_dependency(%q<mocha>, ["
|
189
|
+
s.add_dependency(%q<durran-validatable>, [">= 1.8.3"])
|
190
|
+
s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
|
191
|
+
s.add_dependency(%q<activesupport>, [">= 2.3.4"])
|
192
|
+
s.add_dependency(%q<mongo>, [">= 0.18.1"])
|
193
|
+
s.add_dependency(%q<mongo_ext>, [">= 0.18.1"])
|
194
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
195
|
+
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
193
196
|
end
|
194
197
|
else
|
195
|
-
s.add_dependency(%q<durran-validatable>, ["
|
196
|
-
s.add_dependency(%q<leshill-will_paginate>, ["
|
197
|
-
s.add_dependency(%q<activesupport>, ["
|
198
|
-
s.add_dependency(%q<mongo>, ["
|
199
|
-
s.add_dependency(%q<mongo_ext>, ["
|
200
|
-
s.add_dependency(%q<rspec>, ["
|
201
|
-
s.add_dependency(%q<mocha>, ["
|
198
|
+
s.add_dependency(%q<durran-validatable>, [">= 1.8.3"])
|
199
|
+
s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
|
200
|
+
s.add_dependency(%q<activesupport>, [">= 2.3.4"])
|
201
|
+
s.add_dependency(%q<mongo>, [">= 0.18.1"])
|
202
|
+
s.add_dependency(%q<mongo_ext>, [">= 0.18.1"])
|
203
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
204
|
+
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
202
205
|
end
|
203
206
|
end
|
204
207
|
|
data/perf/benchmark.rb
CHANGED
@@ -16,7 +16,7 @@ class Name < Mongoid::Document
|
|
16
16
|
field :given
|
17
17
|
field :family
|
18
18
|
field :middle
|
19
|
-
belongs_to :person
|
19
|
+
belongs_to :person, :inverse_of => :name
|
20
20
|
end
|
21
21
|
|
22
22
|
class Address < Mongoid::Document
|
@@ -26,7 +26,7 @@ class Address < Mongoid::Document
|
|
26
26
|
field :state
|
27
27
|
field :post_code
|
28
28
|
field :type
|
29
|
-
belongs_to :person
|
29
|
+
belongs_to :person, :inverse_of => :addresses
|
30
30
|
end
|
31
31
|
|
32
32
|
class Phone < Mongoid::Document
|
@@ -34,15 +34,18 @@ class Phone < Mongoid::Document
|
|
34
34
|
field :country_code, :type => Integer
|
35
35
|
field :number
|
36
36
|
field :type
|
37
|
-
belongs_to :person
|
37
|
+
belongs_to :person, :inverse_of => :phones
|
38
38
|
end
|
39
39
|
|
40
|
-
|
40
|
+
connection = Mongo::Connection.new
|
41
|
+
Mongoid.database = connection.db("mongoid_perf_test")
|
41
42
|
|
42
43
|
Mongoid.database.collection("people").drop
|
43
44
|
|
44
45
|
RubyProf.start
|
45
46
|
|
47
|
+
puts "Starting benchmark..."
|
48
|
+
|
46
49
|
1000.times do |n|
|
47
50
|
person = Person.new(:birth_date => Date.new(1970, 1, 1))
|
48
51
|
name = Name.new(:given => "James", :family => "Kirk", :middle => "Tiberius")
|
@@ -58,9 +61,10 @@ RubyProf.start
|
|
58
61
|
person.addresses << address
|
59
62
|
person.phones << phone
|
60
63
|
person.save
|
61
|
-
puts "Saved person #{n}"
|
62
64
|
end
|
63
65
|
|
64
66
|
result = RubyProf.stop
|
65
67
|
printer = RubyProf::FlatPrinter.new(result)
|
66
68
|
printer.print(STDOUT, 0)
|
69
|
+
|
70
|
+
Mongoid.database.collection("people").drop
|
@@ -3,7 +3,9 @@ require "spec_helper"
|
|
3
3
|
describe Mongoid::Associations do
|
4
4
|
|
5
5
|
before do
|
6
|
-
|
6
|
+
Mongoid.database.collection(:people).drop
|
7
|
+
Mongoid.database.collection(:games).drop
|
8
|
+
Mongoid.database.collection(:posts).drop
|
7
9
|
end
|
8
10
|
|
9
11
|
context "one-to-one relational associations" do
|
@@ -27,7 +29,7 @@ describe Mongoid::Associations do
|
|
27
29
|
|
28
30
|
end
|
29
31
|
|
30
|
-
context "one-
|
32
|
+
context "one-to-many relational associations" do
|
31
33
|
|
32
34
|
before do
|
33
35
|
@person = Person.new(:title => "Sir")
|
@@ -43,4 +45,41 @@ describe Mongoid::Associations do
|
|
43
45
|
|
44
46
|
end
|
45
47
|
|
48
|
+
context "nested embedded associations" do
|
49
|
+
|
50
|
+
before do
|
51
|
+
@person = Person.create(:title => "Mr")
|
52
|
+
end
|
53
|
+
|
54
|
+
context "one level nested" do
|
55
|
+
|
56
|
+
before do
|
57
|
+
@address = @person.addresses.create(:street => "Oxford St")
|
58
|
+
@name = @person.name.create(:first_name => "Gordon")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "persists all the associations properly" do
|
62
|
+
@name.last_name = "Brown"
|
63
|
+
@person.name.last_name.should == "Brown"
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
context "multiple levels nested" do
|
69
|
+
|
70
|
+
before do
|
71
|
+
@person.phone_numbers.create(:number => "4155551212")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "persists all the associations properly" do
|
75
|
+
from_db = Person.find(@person.id)
|
76
|
+
phone = from_db.phone_numbers.first
|
77
|
+
phone.country_code.create(:code => 1)
|
78
|
+
from_db.phone_numbers.first.country_code.code.should == 1
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
46
85
|
end
|
@@ -246,42 +246,6 @@ describe Mongoid::Document do
|
|
246
246
|
|
247
247
|
end
|
248
248
|
|
249
|
-
context "associating relational data" do
|
250
|
-
|
251
|
-
context "when relational data is a single object" do
|
252
|
-
|
253
|
-
before do
|
254
|
-
@game = Game.create(:score => 100)
|
255
|
-
@person = Person.new(:title => "Sir")
|
256
|
-
@person.game = @game
|
257
|
-
@person.save
|
258
|
-
end
|
259
|
-
|
260
|
-
it "properly associates the object" do
|
261
|
-
person = Person.find(@person.id)
|
262
|
-
person.game.should == @game
|
263
|
-
end
|
264
|
-
|
265
|
-
end
|
266
|
-
|
267
|
-
context "when relational data is many objects" do
|
268
|
-
|
269
|
-
before do
|
270
|
-
@post = Post.new(:title => "New Post")
|
271
|
-
@person = Person.new(:title => "Sir")
|
272
|
-
@person.posts = [@post]
|
273
|
-
@person.save
|
274
|
-
end
|
275
|
-
|
276
|
-
it "properly associates the objects" do
|
277
|
-
person = Person.find(@person.id)
|
278
|
-
person.posts.should == [@post]
|
279
|
-
end
|
280
|
-
|
281
|
-
end
|
282
|
-
|
283
|
-
end
|
284
|
-
|
285
249
|
context "the lot" do
|
286
250
|
|
287
251
|
before do
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Finders do
|
4
|
+
|
5
|
+
describe "#find" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@document = Person.create(:title => "Mrs.")
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
Person.delete_all
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with an id as an argument" do
|
16
|
+
|
17
|
+
context "when the document is found" do
|
18
|
+
|
19
|
+
it "returns the document" do
|
20
|
+
Person.find(@document.id).should == @document
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when the document is not found" do
|
26
|
+
|
27
|
+
it "raises an error" do
|
28
|
+
lambda { Person.find("5") }.should raise_error
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
3
|
require 'rubygems'
|
4
4
|
|
5
|
-
gem "mocha", "0.9.8"
|
5
|
+
gem "mocha", ">= 0.9.8"
|
6
6
|
|
7
7
|
require "mocha"
|
8
8
|
require "mongoid"
|
@@ -48,8 +48,8 @@ class Person < Mongoid::Document
|
|
48
48
|
has_many_related :posts
|
49
49
|
|
50
50
|
def update_addresses
|
51
|
-
addresses.
|
52
|
-
address.street = "Updated
|
51
|
+
addresses.each do |address|
|
52
|
+
address.street = "Updated Address"
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -78,6 +78,34 @@ describe Mongoid::Associations::HasOne do
|
|
78
78
|
|
79
79
|
end
|
80
80
|
|
81
|
+
context "when the document is nil" do
|
82
|
+
|
83
|
+
before do
|
84
|
+
@association = Mongoid::Associations::HasOne.new(
|
85
|
+
@document,
|
86
|
+
{},
|
87
|
+
Mongoid::Associations::Options.new(:name => :mixed_drink)
|
88
|
+
)
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#new_record?" do
|
92
|
+
|
93
|
+
it "returns true" do
|
94
|
+
@association.should be_new_record
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
context "attribute getters" do
|
100
|
+
|
101
|
+
it "returns nil" do
|
102
|
+
@association.name.should be_nil
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
81
109
|
end
|
82
110
|
|
83
111
|
describe "#nested_build" do
|
@@ -183,7 +183,7 @@ describe Mongoid::Associations do
|
|
183
183
|
end
|
184
184
|
|
185
185
|
it "retains its references to the original objects" do
|
186
|
-
@address.street.should == "Updated
|
186
|
+
@address.street.should == "Updated Address"
|
187
187
|
end
|
188
188
|
|
189
189
|
end
|
@@ -194,8 +194,7 @@ describe Mongoid::Associations do
|
|
194
194
|
|
195
195
|
it "adds a new Association to the document" do
|
196
196
|
person = Person.new
|
197
|
-
person.name.
|
198
|
-
person.attributes[:name].should be_nil
|
197
|
+
person.name.first_name.should be_nil
|
199
198
|
end
|
200
199
|
|
201
200
|
it "creates a reader for the association" do
|
@@ -797,13 +797,22 @@ describe Mongoid::Criteria do
|
|
797
797
|
@criteria = mock
|
798
798
|
Mongoid::Criteria.expects(:new).returns(@criteria)
|
799
799
|
@criteria.expects(:id).with(@id).returns(@criteria)
|
800
|
-
@criteria.expects(:one).returns(@document)
|
801
800
|
end
|
802
801
|
|
803
802
|
it "creates a criteria for a string" do
|
803
|
+
@criteria.expects(:one).returns(@document)
|
804
804
|
Mongoid::Criteria.translate(Person, @id)
|
805
805
|
end
|
806
806
|
|
807
|
+
context "when the document is not found" do
|
808
|
+
|
809
|
+
it "raises an error" do
|
810
|
+
@criteria.expects(:one).returns(nil)
|
811
|
+
lambda { Mongoid::Criteria.translate(Person, @id) }.should raise_error
|
812
|
+
end
|
813
|
+
|
814
|
+
end
|
815
|
+
|
807
816
|
end
|
808
817
|
|
809
818
|
context "multiple arguments" do
|
@@ -219,6 +219,30 @@ describe Mongoid::Document do
|
|
219
219
|
|
220
220
|
end
|
221
221
|
|
222
|
+
context "when as is specified" do
|
223
|
+
|
224
|
+
before do
|
225
|
+
Person.field :aliased, :as => :alias, :type => Boolean
|
226
|
+
@person = Person.new(:alias => true)
|
227
|
+
end
|
228
|
+
|
229
|
+
it "uses the alias to write the attribute" do
|
230
|
+
@person.expects(:write_attribute).with(:aliased, true)
|
231
|
+
@person.alias = true
|
232
|
+
end
|
233
|
+
|
234
|
+
it "uses the alias to read the attribute" do
|
235
|
+
@person.expects(:read_attribute).with(:aliased)
|
236
|
+
@person.alias
|
237
|
+
end
|
238
|
+
|
239
|
+
it "uses the alias for the query method" do
|
240
|
+
@person.expects(:read_attribute).with(:aliased)
|
241
|
+
@person.alias?
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
|
222
246
|
end
|
223
247
|
|
224
248
|
describe ".human_name" do
|
@@ -314,6 +338,19 @@ describe Mongoid::Document do
|
|
314
338
|
|
315
339
|
describe "#new" do
|
316
340
|
|
341
|
+
context "when passed a block" do
|
342
|
+
|
343
|
+
it "yields self to the block" do
|
344
|
+
person = Person.new do |p|
|
345
|
+
p.title = "Sir"
|
346
|
+
p.age = 60
|
347
|
+
end
|
348
|
+
person.title.should == "Sir"
|
349
|
+
person.age.should == 60
|
350
|
+
end
|
351
|
+
|
352
|
+
end
|
353
|
+
|
317
354
|
context "with no attributes" do
|
318
355
|
|
319
356
|
it "sets default attributes" do
|
@@ -679,12 +716,11 @@ describe Mongoid::Document do
|
|
679
716
|
|
680
717
|
context "when the associated is nil" do
|
681
718
|
|
682
|
-
it "
|
719
|
+
it "returns false" do
|
683
720
|
Person.class_eval do
|
684
721
|
validates_associated :name
|
685
722
|
end
|
686
723
|
@person.valid?.should be_false
|
687
|
-
@person.errors.on(:name).should_not be_nil
|
688
724
|
end
|
689
725
|
|
690
726
|
end
|
@@ -2,6 +2,26 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Mongoid::Errors do
|
4
4
|
|
5
|
+
describe Mongoid::Errors::DocumentNotFound do
|
6
|
+
|
7
|
+
describe "#message" do
|
8
|
+
|
9
|
+
context "default" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
@error = Mongoid::Errors::DocumentNotFound.new(Person, "3")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "contains document not found" do
|
16
|
+
@error.message.should include("Document not found")
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
5
25
|
describe Mongoid::Errors::InvalidOptions do
|
6
26
|
|
7
27
|
describe "#message" do
|
@@ -83,6 +83,16 @@ describe Mongoid::Finders do
|
|
83
83
|
Person.find(@id.to_s)
|
84
84
|
end
|
85
85
|
|
86
|
+
context "when no document is found" do
|
87
|
+
|
88
|
+
it "raises an error" do
|
89
|
+
@error = Mongoid::Errors::DocumentNotFound.new(Person, @id.to_s)
|
90
|
+
Mongoid::Criteria.expects(:translate).with(Person, @id.to_s).raises(@error)
|
91
|
+
lambda { Person.find(@id.to_s) }.should raise_error
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
86
96
|
end
|
87
97
|
|
88
98
|
context "when finding first" do
|
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.
|
4
|
+
version: 0.9.10
|
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
|
+
date: 2009-12-19 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
version_requirement:
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
|
-
- - "
|
21
|
+
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 1.8.3
|
24
24
|
version:
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
version_requirement:
|
29
29
|
version_requirements: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 2.3.11
|
34
34
|
version:
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
version_requirement:
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
|
-
- - "
|
41
|
+
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: 2.3.4
|
44
44
|
version:
|
@@ -48,7 +48,7 @@ dependencies:
|
|
48
48
|
version_requirement:
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: 0.18.1
|
54
54
|
version:
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
version_requirement:
|
59
59
|
version_requirements: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
|
-
- - "
|
61
|
+
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: 0.18.1
|
64
64
|
version:
|
@@ -68,7 +68,7 @@ dependencies:
|
|
68
68
|
version_requirement:
|
69
69
|
version_requirements: !ruby/object:Gem::Requirement
|
70
70
|
requirements:
|
71
|
-
- - "
|
71
|
+
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: 1.2.9
|
74
74
|
version:
|
@@ -78,7 +78,7 @@ dependencies:
|
|
78
78
|
version_requirement:
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
|
-
- - "
|
81
|
+
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: 0.9.8
|
84
84
|
version:
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- lib/mongoid/associations/has_one.rb
|
107
107
|
- lib/mongoid/associations/has_one_related.rb
|
108
108
|
- lib/mongoid/associations/options.rb
|
109
|
+
- lib/mongoid/associations/proxy.rb
|
109
110
|
- lib/mongoid/attributes.rb
|
110
111
|
- lib/mongoid/commands.rb
|
111
112
|
- lib/mongoid/commands/create.rb
|
@@ -146,6 +147,7 @@ files:
|
|
146
147
|
- perf/benchmark.rb
|
147
148
|
- spec/integration/mongoid/associations_spec.rb
|
148
149
|
- spec/integration/mongoid/document_spec.rb
|
150
|
+
- spec/integration/mongoid/finders_spec.rb
|
149
151
|
- spec/spec.opts
|
150
152
|
- spec/spec_helper.rb
|
151
153
|
- spec/unit/mongoid/associations/belongs_to_related_spec.rb
|
@@ -222,6 +224,7 @@ summary: ODM framework for MongoDB
|
|
222
224
|
test_files:
|
223
225
|
- spec/integration/mongoid/associations_spec.rb
|
224
226
|
- spec/integration/mongoid/document_spec.rb
|
227
|
+
- spec/integration/mongoid/finders_spec.rb
|
225
228
|
- spec/spec_helper.rb
|
226
229
|
- spec/unit/mongoid/associations/belongs_to_related_spec.rb
|
227
230
|
- spec/unit/mongoid/associations/belongs_to_spec.rb
|