mongoid 1.1.1 → 1.1.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.rb +2 -2
- data/lib/mongoid/associations/has_many.rb +2 -2
- data/lib/mongoid/associations/has_one.rb +4 -3
- data/lib/mongoid/associations/has_one_related.rb +1 -0
- data/lib/mongoid/associations/options.rb +1 -1
- data/lib/mongoid/attributes.rb +2 -2
- data/lib/mongoid/criteria.rb +16 -8
- data/lib/mongoid/document.rb +7 -6
- data/lib/mongoid/errors.rb +3 -3
- data/lib/mongoid/extensions/hash/assimilation.rb +1 -1
- data/mongoid.gemspec +2 -2
- data/spec/integration/mongoid/associations_spec.rb +67 -4
- data/spec/integration/mongoid/commands_spec.rb +2 -2
- data/spec/integration/mongoid/finders_spec.rb +25 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/unit/mongoid/associations/has_many_spec.rb +9 -6
- data/spec/unit/mongoid/associations/has_one_related_spec.rb +15 -0
- data/spec/unit/mongoid/associations/has_one_spec.rb +12 -12
- data/spec/unit/mongoid/associations/options_spec.rb +1 -1
- data/spec/unit/mongoid/criteria_spec.rb +120 -23
- data/spec/unit/mongoid/document_spec.rb +92 -62
- data/spec/unit/mongoid/finders_spec.rb +13 -0
- data/spec/unit/mongoid_spec.rb +25 -7
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.2
|
data/lib/mongoid.rb
CHANGED
@@ -74,9 +74,9 @@ module Mongoid #:nodoc:
|
|
74
74
|
# essentially a proxy to an array itself.
|
75
75
|
def initialize(document, options)
|
76
76
|
@parent, @association_name, @klass, @options = document, options.name, options.klass, options
|
77
|
-
attributes = document.
|
77
|
+
attributes = document.raw_attributes[@association_name]
|
78
78
|
@documents = attributes ? attributes.collect do |attrs|
|
79
|
-
type = attrs[
|
79
|
+
type = attrs["_type"]
|
80
80
|
child = type ? type.constantize.instantiate(attrs) : @klass.instantiate(attrs)
|
81
81
|
child.parentize(@parent, @association_name)
|
82
82
|
child
|
@@ -20,8 +20,9 @@ module Mongoid #:nodoc:
|
|
20
20
|
# options: The association options.
|
21
21
|
def initialize(document, attributes, options)
|
22
22
|
@parent, @options, @association_name = document, options, options.name
|
23
|
-
|
24
|
-
|
23
|
+
attrs = attributes.stringify_keys
|
24
|
+
klass = attrs["_type"] ? attrs["_type"].constantize : nil
|
25
|
+
@document = attrs.assimilate(@parent, @options, klass)
|
25
26
|
end
|
26
27
|
|
27
28
|
# Delegate all missing methods over to the +Document+.
|
@@ -61,7 +62,7 @@ module Mongoid #:nodoc:
|
|
61
62
|
# document: The parent +Document+
|
62
63
|
# options: The association options.
|
63
64
|
def instantiate(document, options)
|
64
|
-
attributes = document.
|
65
|
+
attributes = document.raw_attributes[options.name]
|
65
66
|
return nil if attributes.blank?
|
66
67
|
new(document, attributes, options)
|
67
68
|
end
|
data/lib/mongoid/attributes.rb
CHANGED
@@ -38,8 +38,8 @@ module Mongoid #:nodoc:
|
|
38
38
|
# field exists for them on the +Document+. This will be limited to only the
|
39
39
|
# attributes provided in the suppied +Hash+ so that no extra nil values get
|
40
40
|
# put into the document's attributes.
|
41
|
-
def process(attrs =
|
42
|
-
attrs.each_pair do |key, value|
|
41
|
+
def process(attrs = nil)
|
42
|
+
(attrs || {}).each_pair do |key, value|
|
43
43
|
if Mongoid.allow_dynamic_fields && !respond_to?("#{key}=")
|
44
44
|
@attributes[key.to_s] = value
|
45
45
|
else
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -210,8 +210,9 @@ module Mongoid #:nodoc:
|
|
210
210
|
# <tt>criteria.id("4ab2bc4b8ad548971900005c")</tt>
|
211
211
|
#
|
212
212
|
# Returns: <tt>self</tt>
|
213
|
-
def id(
|
214
|
-
@selector[:_id] =
|
213
|
+
def id(*args)
|
214
|
+
(args.flatten.size > 1) ? self.in(:_id => args.flatten) : (@selector[:_id] = *args)
|
215
|
+
self
|
215
216
|
end
|
216
217
|
|
217
218
|
# Create the new +Criteria+ object. This will initialize the selector
|
@@ -410,12 +411,8 @@ module Mongoid #:nodoc:
|
|
410
411
|
def self.translate(*args)
|
411
412
|
klass = args[0]
|
412
413
|
params = args[1] || {}
|
413
|
-
|
414
|
-
|
415
|
-
if Mongoid.raise_not_found_error
|
416
|
-
raise Errors::DocumentNotFound.new(klass, params) unless document
|
417
|
-
end
|
418
|
-
return document
|
414
|
+
unless params.is_a?(Hash)
|
415
|
+
return id_criteria(klass, params)
|
419
416
|
end
|
420
417
|
return new(klass).where(params.delete(:conditions) || {}).extras(params)
|
421
418
|
end
|
@@ -472,5 +469,16 @@ module Mongoid #:nodoc:
|
|
472
469
|
attributes.each { |key, value| @selector[key] = { operator => value } }; self
|
473
470
|
end
|
474
471
|
|
472
|
+
class << self
|
473
|
+
# Return a criteria or single document based on an id search.
|
474
|
+
def id_criteria(klass, params)
|
475
|
+
criteria = new(klass).id(params)
|
476
|
+
result = params.is_a?(String) ? criteria.one : criteria.entries
|
477
|
+
if Mongoid.raise_not_found_error
|
478
|
+
raise Errors::DocumentNotFound.new(klass, params) if result.blank?
|
479
|
+
end
|
480
|
+
return result
|
481
|
+
end
|
482
|
+
end
|
475
483
|
end
|
476
484
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -58,10 +58,11 @@ module Mongoid #:nodoc:
|
|
58
58
|
# Example:
|
59
59
|
#
|
60
60
|
# <tt>Person.instantiate(:title => "Sir", :age => 30)</tt>
|
61
|
-
def instantiate(attrs =
|
62
|
-
|
61
|
+
def instantiate(attrs = nil, allocating = false)
|
62
|
+
attributes = attrs || {}
|
63
|
+
if attributes["_id"] || allocating
|
63
64
|
document = allocate
|
64
|
-
document.instance_variable_set(:@attributes,
|
65
|
+
document.instance_variable_set(:@attributes, attributes)
|
65
66
|
return document
|
66
67
|
else
|
67
68
|
return new(attrs)
|
@@ -160,7 +161,7 @@ module Mongoid #:nodoc:
|
|
160
161
|
# Example:
|
161
162
|
#
|
162
163
|
# <tt>Person.new(:title => "Mr", :age => 30)</tt>
|
163
|
-
def initialize(attrs =
|
164
|
+
def initialize(attrs = nil)
|
164
165
|
@attributes = {}
|
165
166
|
process(attrs)
|
166
167
|
@attributes = attributes_with_defaults(@attributes)
|
@@ -172,7 +173,7 @@ module Mongoid #:nodoc:
|
|
172
173
|
# apply default values to attributes - calling procs as required
|
173
174
|
def attributes_with_defaults(attributes = {})
|
174
175
|
default_values = defaults.merge(attributes)
|
175
|
-
default_values.
|
176
|
+
default_values.each_pair do |key, val|
|
176
177
|
default_values[key] = val.call if val.respond_to?(:call)
|
177
178
|
end
|
178
179
|
end
|
@@ -237,7 +238,7 @@ module Mongoid #:nodoc:
|
|
237
238
|
# memoized association and notify the parent of the change.
|
238
239
|
def remove(child)
|
239
240
|
name = child.association_name
|
240
|
-
reset(name) { @attributes.remove(name, child.
|
241
|
+
reset(name) { @attributes.remove(name, child.raw_attributes) }
|
241
242
|
notify
|
242
243
|
end
|
243
244
|
|
data/lib/mongoid/errors.rb
CHANGED
@@ -5,11 +5,11 @@ module Mongoid #:nodoc
|
|
5
5
|
# Raised when querying the database for a document by a specific id which
|
6
6
|
# does not exist.
|
7
7
|
class DocumentNotFound < RuntimeError
|
8
|
-
def initialize(klass,
|
9
|
-
@klass, @identifier = klass,
|
8
|
+
def initialize(klass, ids)
|
9
|
+
@klass, @identifier = klass, ids.is_a?(Array) ? ids.join(", ") : ids
|
10
10
|
end
|
11
11
|
def message
|
12
|
-
"Document not found for class #{@klass} and id #{@identifier}"
|
12
|
+
"Document not found for class #{@klass} and id(s) #{@identifier}"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -20,7 +20,7 @@ module Mongoid #:nodoc:
|
|
20
20
|
def assimilate(parent, options, type = nil)
|
21
21
|
klass = type ? type : options.klass
|
22
22
|
child = klass.instantiate(:_parent => parent)
|
23
|
-
child.write_attributes(self.merge(
|
23
|
+
child.write_attributes(self.merge("_type" => klass.name))
|
24
24
|
child.identify
|
25
25
|
child.assimilate(parent, options)
|
26
26
|
end
|
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.2"
|
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-23}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.rdoc"
|
@@ -94,12 +94,75 @@ describe Mongoid::Associations do
|
|
94
94
|
|
95
95
|
before do
|
96
96
|
@extra_post = Post.create(:title => "Orphan")
|
97
|
+
@from_db = Person.find(@person.id)
|
97
98
|
end
|
98
99
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
100
|
+
context "finding all" do
|
101
|
+
|
102
|
+
it "returns only those objects scoped to the parent" do
|
103
|
+
Post.all.size.should == 2
|
104
|
+
@from_db.posts.all.size.should == 1
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
context "finding with conditions" do
|
110
|
+
|
111
|
+
context "finding all" do
|
112
|
+
|
113
|
+
it "returns only those objects scoped to the parent" do
|
114
|
+
posts = @from_db.posts.find(:all, :conditions => { :title => "Testing" })
|
115
|
+
posts.size.should == 1
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
context "finding first" do
|
121
|
+
|
122
|
+
it "returns only those objects scoped to the parent" do
|
123
|
+
post = @from_db.posts.find(:first, :conditions => { :title => "Testing" })
|
124
|
+
post.should == @post
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
context "finding last" do
|
130
|
+
|
131
|
+
it "returns only those objects scoped to the parent" do
|
132
|
+
post = @from_db.posts.find(:last, :conditions => { :title => "Testing" })
|
133
|
+
post.should == @post
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
context "using a named scope" do
|
139
|
+
|
140
|
+
before do
|
141
|
+
@post.created_at = 15.days.ago
|
142
|
+
@post.save
|
143
|
+
end
|
144
|
+
|
145
|
+
it "returns only those scoped to the parent plus the named scope" do
|
146
|
+
posts = @from_db.posts.recent
|
147
|
+
posts.size.should == 1
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
context "using a criteria class method" do
|
153
|
+
|
154
|
+
before do
|
155
|
+
@post.created_at = 45.days.ago
|
156
|
+
@post.save
|
157
|
+
end
|
158
|
+
|
159
|
+
it "returns only those scoped to the parent plus the named scope" do
|
160
|
+
posts = @from_db.posts.old
|
161
|
+
posts.size.should == 1
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
103
166
|
end
|
104
167
|
|
105
168
|
end
|
@@ -3,7 +3,7 @@ require "spec_helper"
|
|
3
3
|
describe Mongoid::Commands do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@person = Person.new(:title => "Sir")
|
6
|
+
@person = Person.new(:title => "Sir", :ssn => "6969696")
|
7
7
|
end
|
8
8
|
|
9
9
|
after do
|
@@ -13,7 +13,7 @@ describe Mongoid::Commands do
|
|
13
13
|
describe ".create" do
|
14
14
|
|
15
15
|
it "saves and returns the document" do
|
16
|
-
person = Person.create(:title => "Sensei")
|
16
|
+
person = Person.create(:title => "Sensei", :ssn => "666-66-6666")
|
17
17
|
person.should be_a_kind_of(Person)
|
18
18
|
person.should_not be_a_new_record
|
19
19
|
end
|
@@ -5,7 +5,11 @@ describe Mongoid::Finders do
|
|
5
5
|
describe "#find" do
|
6
6
|
|
7
7
|
before do
|
8
|
+
@documents = []
|
8
9
|
@document = Person.create(:title => "Mrs.", :ssn => "another")
|
10
|
+
3.times do |n|
|
11
|
+
@documents << Person.create(:title => "Mr.", :ssn => "#{n}22")
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
after do
|
@@ -32,6 +36,27 @@ describe Mongoid::Finders do
|
|
32
36
|
|
33
37
|
end
|
34
38
|
|
39
|
+
context "with an array of ids as args" do
|
40
|
+
|
41
|
+
context "when the documents are found" do
|
42
|
+
|
43
|
+
it "returns an array of the documents" do
|
44
|
+
@people = Person.find(@documents.map(&:id))
|
45
|
+
@people.should == @documents
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when no documents found" do
|
51
|
+
|
52
|
+
it "raises an error" do
|
53
|
+
lambda { Person.find(["11", "21", "31"]) }.should raise_error
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
35
60
|
end
|
36
61
|
|
37
62
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -218,6 +218,14 @@ class Post
|
|
218
218
|
include Mongoid::Timestamps
|
219
219
|
field :title
|
220
220
|
belongs_to_related :person
|
221
|
+
|
222
|
+
named_scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
|
223
|
+
|
224
|
+
class << self
|
225
|
+
def old
|
226
|
+
where(:created_at => { "$lt" => 30.days.ago })
|
227
|
+
end
|
228
|
+
end
|
221
229
|
end
|
222
230
|
|
223
231
|
class Game
|
@@ -3,16 +3,19 @@ require "spec_helper"
|
|
3
3
|
describe Mongoid::Associations::HasMany do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@attributes = {
|
7
|
-
{
|
8
|
-
{
|
9
|
-
@document = stub(:
|
6
|
+
@attributes = { "addresses" => [
|
7
|
+
{ "_id" => "street-1", "street" => "Street 1", "state" => "CA" },
|
8
|
+
{ "_id" => "street-2", "street" => "Street 2" } ] }
|
9
|
+
@document = stub(:raw_attributes => @attributes, :add_observer => true, :update => true)
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "#[]" do
|
13
13
|
|
14
14
|
before do
|
15
|
-
@association = Mongoid::Associations::HasMany.new(
|
15
|
+
@association = Mongoid::Associations::HasMany.new(
|
16
|
+
@document,
|
17
|
+
Mongoid::Associations::Options.new(:name => :addresses)
|
18
|
+
)
|
16
19
|
end
|
17
20
|
|
18
21
|
context "when the index is present in the association" do
|
@@ -257,7 +260,7 @@ describe Mongoid::Associations::HasMany do
|
|
257
260
|
describe "#initialize" do
|
258
261
|
|
259
262
|
before do
|
260
|
-
@canvas = stub(:
|
263
|
+
@canvas = stub(:raw_attributes => { "shapes" => [{ "_type" => "Circle", "radius" => 5 }] }, :update => true)
|
261
264
|
@association = Mongoid::Associations::HasMany.new(
|
262
265
|
@canvas,
|
263
266
|
Mongoid::Associations::Options.new(:name => :shapes)
|
@@ -55,6 +55,21 @@ describe Mongoid::Associations::HasOneRelated do
|
|
55
55
|
|
56
56
|
end
|
57
57
|
|
58
|
+
describe "#id" do
|
59
|
+
|
60
|
+
before do
|
61
|
+
@parent = stub(:id => "5", :class => Person)
|
62
|
+
@game = Game.new
|
63
|
+
Game.expects(:first).returns(@game)
|
64
|
+
@association = Mongoid::Associations::HasOneRelated.new(@parent, options)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "delegates to the proxied document" do
|
68
|
+
@association.id.should == @game.id
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
58
73
|
describe ".initialize" do
|
59
74
|
|
60
75
|
before do
|
@@ -3,11 +3,11 @@ require "spec_helper"
|
|
3
3
|
describe Mongoid::Associations::HasOne do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@attributes = {
|
7
|
-
|
8
|
-
|
6
|
+
@attributes = { "mixed_drink" => {
|
7
|
+
"name" => "Jack and Coke", "_type" => "MixedDrink" },
|
8
|
+
"writer" => { "speed" => 50, "_type" => "HtmlWriter" }
|
9
9
|
}
|
10
|
-
@document = stub(:
|
10
|
+
@document = stub(:raw_attributes => @attributes, :update => true)
|
11
11
|
end
|
12
12
|
|
13
13
|
describe "#build_*" do
|
@@ -17,7 +17,7 @@ describe Mongoid::Associations::HasOne do
|
|
17
17
|
before do
|
18
18
|
@association = Mongoid::Associations::HasOne.new(
|
19
19
|
@document,
|
20
|
-
@attributes[
|
20
|
+
@attributes["mixed_drink"],
|
21
21
|
Mongoid::Associations::Options.new(:name => :mixed_drink)
|
22
22
|
)
|
23
23
|
end
|
@@ -34,7 +34,7 @@ describe Mongoid::Associations::HasOne do
|
|
34
34
|
before do
|
35
35
|
@association = Mongoid::Associations::HasOne.new(
|
36
36
|
@document,
|
37
|
-
@attributes[
|
37
|
+
@attributes["writer"],
|
38
38
|
Mongoid::Associations::Options.new(:name => :writer)
|
39
39
|
)
|
40
40
|
end
|
@@ -84,7 +84,7 @@ describe Mongoid::Associations::HasOne do
|
|
84
84
|
context "when attributes are empty" do
|
85
85
|
|
86
86
|
before do
|
87
|
-
@document = stub(:
|
87
|
+
@document = stub(:raw_attributes => { "name" => {} })
|
88
88
|
@association = Mongoid::Associations::HasOne.instantiate(
|
89
89
|
@document,
|
90
90
|
Mongoid::Associations::Options.new(:name => :name)
|
@@ -100,12 +100,12 @@ describe Mongoid::Associations::HasOne do
|
|
100
100
|
context "when attributes exist" do
|
101
101
|
|
102
102
|
before do
|
103
|
-
@document = stub(:
|
103
|
+
@document = stub(:raw_attributes => { "name" => { "first_name" => "Test" } })
|
104
104
|
@options = Mongoid::Associations::Options.new(:name => :name)
|
105
105
|
end
|
106
106
|
|
107
107
|
it "delegates to new" do
|
108
|
-
Mongoid::Associations::HasOne.expects(:new).with(@document, {
|
108
|
+
Mongoid::Associations::HasOne.expects(:new).with(@document, { "first_name" => "Test" }, @options)
|
109
109
|
Mongoid::Associations::HasOne.instantiate(@document, @options)
|
110
110
|
end
|
111
111
|
|
@@ -118,7 +118,7 @@ describe Mongoid::Associations::HasOne do
|
|
118
118
|
before do
|
119
119
|
@association = Mongoid::Associations::HasOne.new(
|
120
120
|
@document,
|
121
|
-
@attributes[
|
121
|
+
@attributes["mixed_drink"],
|
122
122
|
Mongoid::Associations::Options.new(:name => :mixed_drink)
|
123
123
|
)
|
124
124
|
end
|
@@ -149,7 +149,7 @@ describe Mongoid::Associations::HasOne do
|
|
149
149
|
before do
|
150
150
|
@association = Mongoid::Associations::HasOne.new(
|
151
151
|
@document,
|
152
|
-
@attributes[
|
152
|
+
@attributes["mixed_drink"],
|
153
153
|
Mongoid::Associations::Options.new(:name => :mixed_drink)
|
154
154
|
)
|
155
155
|
end
|
@@ -221,7 +221,7 @@ describe Mongoid::Associations::HasOne do
|
|
221
221
|
context "when the document is not nil" do
|
222
222
|
|
223
223
|
before do
|
224
|
-
@document = stub(:
|
224
|
+
@document = stub(:raw_attributes => { "name" => { "first_name" => "Test" } }, :update => true)
|
225
225
|
@options = Mongoid::Associations::Options.new(:name => :name)
|
226
226
|
@association = Mongoid::Associations::HasOne.instantiate(@document, @options)
|
227
227
|
end
|
@@ -497,7 +497,12 @@ describe Mongoid::Criteria do
|
|
497
497
|
end
|
498
498
|
|
499
499
|
it "calls group on the collection with the aggregate js" do
|
500
|
-
@collection.expects(:group).with(
|
500
|
+
@collection.expects(:group).with(
|
501
|
+
[ :field1 ],
|
502
|
+
{ :_type =>
|
503
|
+
{ "$in" => ["Doctor", "Person"] }},
|
504
|
+
{:group => []}, @reduce, true
|
505
|
+
).returns(@grouping)
|
501
506
|
@criteria.only(:field1).group
|
502
507
|
end
|
503
508
|
|
@@ -512,7 +517,11 @@ describe Mongoid::Criteria do
|
|
512
517
|
end
|
513
518
|
|
514
519
|
it "calls group on the collection with the aggregate js" do
|
515
|
-
@collection.expects(:group).with(
|
520
|
+
@collection.expects(:group).with(
|
521
|
+
[ :field1 ],
|
522
|
+
{ :_type =>
|
523
|
+
{ "$in" => ["Doctor", "Person"] }
|
524
|
+
}, { :group => []}, @reduce, true).returns(@grouping)
|
516
525
|
@criteria.only(:field1).group
|
517
526
|
end
|
518
527
|
|
@@ -522,15 +531,34 @@ describe Mongoid::Criteria do
|
|
522
531
|
|
523
532
|
describe "#id" do
|
524
533
|
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
534
|
+
context "when passing a single id" do
|
535
|
+
|
536
|
+
it "adds the _id query to the selector" do
|
537
|
+
id = Mongo::ObjectID.new.to_s
|
538
|
+
@criteria.id(id)
|
539
|
+
@criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :_id => id }
|
540
|
+
end
|
541
|
+
|
542
|
+
it "returns self" do
|
543
|
+
id = Mongo::ObjectID.new.to_s
|
544
|
+
@criteria.id(id.to_s).should == @criteria
|
545
|
+
end
|
546
|
+
|
529
547
|
end
|
530
548
|
|
531
|
-
|
532
|
-
|
533
|
-
|
549
|
+
context "when passing in an array of ids" do
|
550
|
+
|
551
|
+
before do
|
552
|
+
@ids = []
|
553
|
+
3.times { @ids << Mongo::ObjectID.new.to_s }
|
554
|
+
end
|
555
|
+
|
556
|
+
it "adds the _id query to the selector" do
|
557
|
+
@criteria.id(@ids)
|
558
|
+
@criteria.selector.should ==
|
559
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :_id => { "$in" => @ids } }
|
560
|
+
end
|
561
|
+
|
534
562
|
end
|
535
563
|
|
536
564
|
end
|
@@ -539,7 +567,12 @@ describe Mongoid::Criteria do
|
|
539
567
|
|
540
568
|
it "adds the $in clause to the selector" do
|
541
569
|
@criteria.in(:title => ["title1", "title2"], :text => ["test"])
|
542
|
-
@criteria.selector.should ==
|
570
|
+
@criteria.selector.should ==
|
571
|
+
{ :_type =>
|
572
|
+
{ "$in" =>
|
573
|
+
["Doctor", "Person"]
|
574
|
+
}, :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
|
575
|
+
}
|
543
576
|
end
|
544
577
|
|
545
578
|
it "returns self" do
|
@@ -969,7 +1002,11 @@ describe Mongoid::Criteria do
|
|
969
1002
|
@collection = mock
|
970
1003
|
Person.expects(:collection).returns(@collection)
|
971
1004
|
@criteria = Person.where(:_id => "1").skip(60).limit(20)
|
972
|
-
@collection.expects(:find).with(
|
1005
|
+
@collection.expects(:find).with(
|
1006
|
+
{ :_type =>
|
1007
|
+
{ "$in" => ["Doctor", "Person"] },
|
1008
|
+
:_id => "1"
|
1009
|
+
}, :skip => 60, :limit => 20).returns([])
|
973
1010
|
@results = @criteria.paginate
|
974
1011
|
end
|
975
1012
|
|
@@ -1112,6 +1149,7 @@ describe Mongoid::Criteria do
|
|
1112
1149
|
|
1113
1150
|
it "creates a criteria for a string" do
|
1114
1151
|
@criteria.expects(:one).returns(@document)
|
1152
|
+
@document.expects(:blank? => false)
|
1115
1153
|
Mongoid::Criteria.translate(Person, @id)
|
1116
1154
|
end
|
1117
1155
|
|
@@ -1128,6 +1166,53 @@ describe Mongoid::Criteria do
|
|
1128
1166
|
|
1129
1167
|
context "multiple arguments" do
|
1130
1168
|
|
1169
|
+
context "when an array of ids" do
|
1170
|
+
|
1171
|
+
before do
|
1172
|
+
@ids = []
|
1173
|
+
@documents = []
|
1174
|
+
3.times do
|
1175
|
+
@ids << Mongo::ObjectID.new.to_s
|
1176
|
+
@documents << stub
|
1177
|
+
end
|
1178
|
+
@collection = stub
|
1179
|
+
Person.expects(:collection).returns(@collection)
|
1180
|
+
end
|
1181
|
+
|
1182
|
+
context "when documents are found" do
|
1183
|
+
|
1184
|
+
it "returns an ids criteria" do
|
1185
|
+
@collection.expects(:find).with(
|
1186
|
+
{ :_type =>
|
1187
|
+
{ "$in" =>
|
1188
|
+
["Doctor", "Person"]
|
1189
|
+
},
|
1190
|
+
:_id =>
|
1191
|
+
{ "$in" => @ids }
|
1192
|
+
}, {}).returns([{ "_id" => "4", "title" => "Sir", "_type" => "Person" }])
|
1193
|
+
@criteria = Mongoid::Criteria.translate(Person, @ids)
|
1194
|
+
end
|
1195
|
+
|
1196
|
+
end
|
1197
|
+
|
1198
|
+
context "when documents are not found" do
|
1199
|
+
|
1200
|
+
it "returns an ids criteria" do
|
1201
|
+
@collection.expects(:find).with(
|
1202
|
+
{ :_type =>
|
1203
|
+
{ "$in" =>
|
1204
|
+
["Doctor", "Person"]
|
1205
|
+
},
|
1206
|
+
:_id =>
|
1207
|
+
{ "$in" => @ids }
|
1208
|
+
}, {}).returns([])
|
1209
|
+
lambda { Mongoid::Criteria.translate(Person, @ids) }.should raise_error
|
1210
|
+
end
|
1211
|
+
|
1212
|
+
end
|
1213
|
+
|
1214
|
+
end
|
1215
|
+
|
1131
1216
|
context "when Person, :conditions => {}" do
|
1132
1217
|
|
1133
1218
|
before do
|
@@ -1200,7 +1285,8 @@ describe Mongoid::Criteria do
|
|
1200
1285
|
|
1201
1286
|
it "adds the clause to the selector" do
|
1202
1287
|
@criteria.where(:title => "Title", :text => "Text")
|
1203
|
-
@criteria.selector.should ==
|
1288
|
+
@criteria.selector.should ==
|
1289
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Title", :text => "Text" }
|
1204
1290
|
end
|
1205
1291
|
|
1206
1292
|
end
|
@@ -1211,7 +1297,8 @@ describe Mongoid::Criteria do
|
|
1211
1297
|
|
1212
1298
|
it "returns those matching an all clause" do
|
1213
1299
|
@criteria.where(:title.all => ["Sir"])
|
1214
|
-
@criteria.selector.should ==
|
1300
|
+
@criteria.selector.should ==
|
1301
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$all" => ["Sir"] } }
|
1215
1302
|
end
|
1216
1303
|
|
1217
1304
|
end
|
@@ -1220,7 +1307,8 @@ describe Mongoid::Criteria do
|
|
1220
1307
|
|
1221
1308
|
it "returns those matching an exists clause" do
|
1222
1309
|
@criteria.where(:title.exists => true)
|
1223
|
-
@criteria.selector.should ==
|
1310
|
+
@criteria.selector.should ==
|
1311
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$exists" => true } }
|
1224
1312
|
end
|
1225
1313
|
|
1226
1314
|
end
|
@@ -1229,7 +1317,8 @@ describe Mongoid::Criteria do
|
|
1229
1317
|
|
1230
1318
|
it "returns those matching a gt clause" do
|
1231
1319
|
@criteria.where(:age.gt => 30)
|
1232
|
-
@criteria.selector.should ==
|
1320
|
+
@criteria.selector.should ==
|
1321
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$gt" => 30 } }
|
1233
1322
|
end
|
1234
1323
|
|
1235
1324
|
end
|
@@ -1238,7 +1327,8 @@ describe Mongoid::Criteria do
|
|
1238
1327
|
|
1239
1328
|
it "returns those matching a gte clause" do
|
1240
1329
|
@criteria.where(:age.gte => 33)
|
1241
|
-
@criteria.selector.should ==
|
1330
|
+
@criteria.selector.should ==
|
1331
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$gte" => 33 } }
|
1242
1332
|
end
|
1243
1333
|
|
1244
1334
|
end
|
@@ -1247,7 +1337,8 @@ describe Mongoid::Criteria do
|
|
1247
1337
|
|
1248
1338
|
it "returns those matching an in clause" do
|
1249
1339
|
@criteria.where(:title.in => ["Sir", "Madam"])
|
1250
|
-
@criteria.selector.should ==
|
1340
|
+
@criteria.selector.should ==
|
1341
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$in" => ["Sir", "Madam"] } }
|
1251
1342
|
end
|
1252
1343
|
|
1253
1344
|
end
|
@@ -1256,7 +1347,8 @@ describe Mongoid::Criteria do
|
|
1256
1347
|
|
1257
1348
|
it "returns those matching a lt clause" do
|
1258
1349
|
@criteria.where(:age.lt => 34)
|
1259
|
-
@criteria.selector.should ==
|
1350
|
+
@criteria.selector.should ==
|
1351
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$lt" => 34 } }
|
1260
1352
|
end
|
1261
1353
|
|
1262
1354
|
end
|
@@ -1265,7 +1357,8 @@ describe Mongoid::Criteria do
|
|
1265
1357
|
|
1266
1358
|
it "returns those matching a lte clause" do
|
1267
1359
|
@criteria.where(:age.lte => 33)
|
1268
|
-
@criteria.selector.should ==
|
1360
|
+
@criteria.selector.should ==
|
1361
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$lte" => 33 } }
|
1269
1362
|
end
|
1270
1363
|
|
1271
1364
|
end
|
@@ -1274,7 +1367,8 @@ describe Mongoid::Criteria do
|
|
1274
1367
|
|
1275
1368
|
it "returns those matching a ne clause" do
|
1276
1369
|
@criteria.where(:age.ne => 50)
|
1277
|
-
@criteria.selector.should ==
|
1370
|
+
@criteria.selector.should ==
|
1371
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$ne" => 50 } }
|
1278
1372
|
end
|
1279
1373
|
|
1280
1374
|
end
|
@@ -1283,7 +1377,8 @@ describe Mongoid::Criteria do
|
|
1283
1377
|
|
1284
1378
|
it "returns those matching a nin clause" do
|
1285
1379
|
@criteria.where(:title.nin => ["Esquire", "Congressman"])
|
1286
|
-
@criteria.selector.should ==
|
1380
|
+
@criteria.selector.should ==
|
1381
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$nin" => ["Esquire", "Congressman"] } }
|
1287
1382
|
end
|
1288
1383
|
|
1289
1384
|
end
|
@@ -1292,7 +1387,8 @@ describe Mongoid::Criteria do
|
|
1292
1387
|
|
1293
1388
|
it "returns those matching a size clause" do
|
1294
1389
|
@criteria.where(:aliases.size => 2)
|
1295
|
-
@criteria.selector.should ==
|
1390
|
+
@criteria.selector.should ==
|
1391
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, :aliases => { "$size" => 2 } }
|
1296
1392
|
end
|
1297
1393
|
|
1298
1394
|
end
|
@@ -1305,7 +1401,8 @@ describe Mongoid::Criteria do
|
|
1305
1401
|
|
1306
1402
|
it "adds the $where clause to the selector" do
|
1307
1403
|
@criteria.where("this.date < new Date()")
|
1308
|
-
@criteria.selector.should ==
|
1404
|
+
@criteria.selector.should ==
|
1405
|
+
{ :_type => { "$in" => ["Doctor", "Person"] }, "$where" => "this.date < new Date()" }
|
1309
1406
|
end
|
1310
1407
|
|
1311
1408
|
end
|
@@ -223,68 +223,7 @@ describe Mongoid::Document do
|
|
223
223
|
|
224
224
|
end
|
225
225
|
|
226
|
-
describe ".
|
227
|
-
|
228
|
-
before do
|
229
|
-
@attributes = { :_id => "1", :_type => "Person", :title => "Sir", :age => 30 }
|
230
|
-
@person = Person.new(@attributes)
|
231
|
-
end
|
232
|
-
|
233
|
-
it "sets the attributes directly" do
|
234
|
-
Person.instantiate(@attributes).should == @person
|
235
|
-
end
|
236
|
-
|
237
|
-
end
|
238
|
-
|
239
|
-
describe ".key" do
|
240
|
-
|
241
|
-
context "when key is single field" do
|
242
|
-
|
243
|
-
before do
|
244
|
-
Address.key :street
|
245
|
-
@address = Address.new(:street => "Testing Street Name")
|
246
|
-
@address.expects(:collection).returns(@collection)
|
247
|
-
@collection.expects(:save)
|
248
|
-
end
|
249
|
-
|
250
|
-
it "adds the callback for primary key generation" do
|
251
|
-
@address.save
|
252
|
-
@address.id.should == "testing-street-name"
|
253
|
-
end
|
254
|
-
|
255
|
-
end
|
256
|
-
|
257
|
-
context "when key is composite" do
|
258
|
-
|
259
|
-
before do
|
260
|
-
Address.key :street, :post_code
|
261
|
-
@address = Address.new(:street => "Testing Street Name", :post_code => "94123")
|
262
|
-
@address.expects(:collection).returns(@collection)
|
263
|
-
@collection.expects(:save)
|
264
|
-
end
|
265
|
-
|
266
|
-
it "combines all fields" do
|
267
|
-
@address.save
|
268
|
-
@address.id.should == "testing-street-name-94123"
|
269
|
-
end
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
context "when key is on a subclass" do
|
274
|
-
|
275
|
-
before do
|
276
|
-
Firefox.key :name
|
277
|
-
end
|
278
|
-
|
279
|
-
it "sets the key for the entire hierarchy" do
|
280
|
-
Canvas.primary_key.should == [:name]
|
281
|
-
end
|
282
|
-
|
283
|
-
end
|
284
|
-
|
285
|
-
end
|
286
|
-
|
287
|
-
describe "#new" do
|
226
|
+
describe ".initialize" do
|
288
227
|
|
289
228
|
context "when passed a block" do
|
290
229
|
|
@@ -310,6 +249,20 @@ describe Mongoid::Document do
|
|
310
249
|
|
311
250
|
end
|
312
251
|
|
252
|
+
context "with nil attributes" do
|
253
|
+
|
254
|
+
before do
|
255
|
+
@person = Person.new(nil)
|
256
|
+
end
|
257
|
+
|
258
|
+
it "sets default attributes" do
|
259
|
+
@person.attributes.empty?.should be_false
|
260
|
+
@person.age.should == 100
|
261
|
+
@person.blood_alcohol_content.should == 0.0
|
262
|
+
end
|
263
|
+
|
264
|
+
end
|
265
|
+
|
313
266
|
context "with attributes" do
|
314
267
|
|
315
268
|
before do
|
@@ -363,6 +316,83 @@ describe Mongoid::Document do
|
|
363
316
|
|
364
317
|
end
|
365
318
|
|
319
|
+
describe ".instantiate" do
|
320
|
+
|
321
|
+
context "when attributes have an id" do
|
322
|
+
|
323
|
+
before do
|
324
|
+
@attributes = { "_id" => "1", "_type" => "Person", "title" => "Sir", "age" => 30 }
|
325
|
+
end
|
326
|
+
|
327
|
+
it "sets the attributes directly" do
|
328
|
+
person = Person.instantiate(@attributes)
|
329
|
+
person._id.should == "1"
|
330
|
+
person._type.should == "Person"
|
331
|
+
person.title.should == "Sir"
|
332
|
+
person.age.should == 30
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
|
337
|
+
context "with nil attributes" do
|
338
|
+
|
339
|
+
it "sets the attributes directly" do
|
340
|
+
person = Person.instantiate(nil)
|
341
|
+
person.id.should_not be_nil
|
342
|
+
end
|
343
|
+
|
344
|
+
end
|
345
|
+
|
346
|
+
end
|
347
|
+
|
348
|
+
describe ".key" do
|
349
|
+
|
350
|
+
context "when key is single field" do
|
351
|
+
|
352
|
+
before do
|
353
|
+
Address.key :street
|
354
|
+
@address = Address.new(:street => "Testing Street Name")
|
355
|
+
@address.expects(:collection).returns(@collection)
|
356
|
+
@collection.expects(:save)
|
357
|
+
end
|
358
|
+
|
359
|
+
it "adds the callback for primary key generation" do
|
360
|
+
@address.save
|
361
|
+
@address.id.should == "testing-street-name"
|
362
|
+
end
|
363
|
+
|
364
|
+
end
|
365
|
+
|
366
|
+
context "when key is composite" do
|
367
|
+
|
368
|
+
before do
|
369
|
+
Address.key :street, :post_code
|
370
|
+
@address = Address.new(:street => "Testing Street Name", :post_code => "94123")
|
371
|
+
@address.expects(:collection).returns(@collection)
|
372
|
+
@collection.expects(:save)
|
373
|
+
end
|
374
|
+
|
375
|
+
it "combines all fields" do
|
376
|
+
@address.save
|
377
|
+
@address.id.should == "testing-street-name-94123"
|
378
|
+
end
|
379
|
+
|
380
|
+
end
|
381
|
+
|
382
|
+
context "when key is on a subclass" do
|
383
|
+
|
384
|
+
before do
|
385
|
+
Firefox.key :name
|
386
|
+
end
|
387
|
+
|
388
|
+
it "sets the key for the entire hierarchy" do
|
389
|
+
Canvas.primary_key.should == [:name]
|
390
|
+
end
|
391
|
+
|
392
|
+
end
|
393
|
+
|
394
|
+
end
|
395
|
+
|
366
396
|
describe "#new_record?" do
|
367
397
|
|
368
398
|
context "when the object has been saved" do
|
@@ -95,6 +95,19 @@ describe Mongoid::Finders do
|
|
95
95
|
|
96
96
|
end
|
97
97
|
|
98
|
+
context "when an array of ids is passed in" do
|
99
|
+
|
100
|
+
before do
|
101
|
+
@ids = []
|
102
|
+
3.times { @ids << Mongo::ObjectID.new.to_s }
|
103
|
+
end
|
104
|
+
|
105
|
+
it "delegates to the criteria" do
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
98
111
|
context "when nil passed in" do
|
99
112
|
|
100
113
|
it "raises an error" do
|
data/spec/unit/mongoid_spec.rb
CHANGED
@@ -2,16 +2,34 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Mongoid do
|
4
4
|
|
5
|
-
describe ".
|
5
|
+
describe ".config" do
|
6
|
+
|
7
|
+
context "when no block supplied" do
|
8
|
+
|
9
|
+
it "returns the config singleton" do
|
10
|
+
Mongoid.config.should == Mongoid::Config.instance
|
11
|
+
end
|
6
12
|
|
7
|
-
before do
|
8
|
-
@config = mock
|
9
|
-
Mongoid::Config.expects(:instance).returns(@config)
|
10
13
|
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
context "when a block is supplied" do
|
16
|
+
|
17
|
+
before do
|
18
|
+
Mongoid.config do |config|
|
19
|
+
config.allow_dynamic_fields = false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
Mongoid.config do |config|
|
25
|
+
config.allow_dynamic_fields = true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "sets the values on the config instance" do
|
30
|
+
Mongoid.allow_dynamic_fields.should be_false
|
31
|
+
end
|
32
|
+
|
15
33
|
end
|
16
34
|
|
17
35
|
end
|
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.2
|
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-23 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|