mongoid 0.6.6 → 0.6.7
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 +0 -4
- data/lib/mongoid/associations.rb +6 -8
- data/lib/mongoid/associations/accessor.rb +2 -17
- data/lib/mongoid/associations/belongs_to.rb +5 -7
- data/lib/mongoid/document.rb +19 -2
- data/mongoid.gemspec +2 -2
- data/spec/integration/mongoid/document_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/mongoid/associations/accessor_spec.rb +8 -25
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +36 -36
- data/spec/unit/mongoid/associations_spec.rb +3 -3
- data/spec/unit/mongoid/document_spec.rb +89 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.7
|
data/lib/mongoid.rb
CHANGED
@@ -52,10 +52,6 @@ module Mongoid
|
|
52
52
|
# checking equality on objects of different types.
|
53
53
|
class TypeMismatchError < RuntimeError; end
|
54
54
|
|
55
|
-
# Raised when an association is defined that is not valid. Must
|
56
|
-
# be belongs_to, has_many, has_one
|
57
|
-
class InvalidAssociationError < RuntimeError; end
|
58
|
-
|
59
55
|
# Raised when a persisence method ending in ! fails validation.
|
60
56
|
class ValidationsError < RuntimeError; end
|
61
57
|
|
data/lib/mongoid/associations.rb
CHANGED
@@ -41,9 +41,9 @@ module Mongoid # :nodoc:
|
|
41
41
|
# class Address < Mongoid::Document
|
42
42
|
# belongs_to :person
|
43
43
|
# end
|
44
|
-
def belongs_to(association_name)
|
44
|
+
def belongs_to(association_name, options = {})
|
45
45
|
@embedded = true
|
46
|
-
add_association(
|
46
|
+
add_association(Associations::BelongsTo, association_name, options)
|
47
47
|
end
|
48
48
|
|
49
49
|
# Adds the association from a parent document to its children. The name
|
@@ -64,7 +64,7 @@ module Mongoid # :nodoc:
|
|
64
64
|
# belongs_to :person
|
65
65
|
# end
|
66
66
|
def has_many(association_name, options = {})
|
67
|
-
add_association(
|
67
|
+
add_association(Associations::HasMany, association_name, options)
|
68
68
|
end
|
69
69
|
|
70
70
|
# Adds the association from a parent document to its child. The name
|
@@ -85,18 +85,16 @@ module Mongoid # :nodoc:
|
|
85
85
|
# belongs_to :person
|
86
86
|
# end
|
87
87
|
def has_one(association_name, options = {})
|
88
|
-
add_association(
|
88
|
+
add_association(Associations::HasOne, association_name, options)
|
89
89
|
end
|
90
90
|
|
91
91
|
private
|
92
92
|
# Adds the association to the associations hash with the type as the key,
|
93
93
|
# then adds the accessors for the association.
|
94
|
-
def add_association(type,
|
94
|
+
def add_association(type, name, options = {})
|
95
95
|
associations[name] = type
|
96
96
|
define_method(name) do
|
97
|
-
if instance_variable_defined?("@#{name}")
|
98
|
-
return instance_variable_get("@#{name}")
|
99
|
-
end
|
97
|
+
return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")
|
100
98
|
proxy = Associations::Accessor.get(type, name, self, options)
|
101
99
|
instance_variable_set("@#{name}", proxy)
|
102
100
|
end
|
@@ -5,18 +5,8 @@ module Mongoid #:nodoc:
|
|
5
5
|
# Gets an association, based on the type provided and
|
6
6
|
# passes the name and document into the newly instantiated
|
7
7
|
# association.
|
8
|
-
#
|
9
|
-
# If the type is invalid a InvalidAssociationError will be thrown.
|
10
8
|
def get(type, name, document, options = {})
|
11
|
-
|
12
|
-
when :belongs_to
|
13
|
-
BelongsTo.new(document)
|
14
|
-
when :has_many
|
15
|
-
HasMany.new(name, document, options)
|
16
|
-
when :has_one
|
17
|
-
document ? HasOne.new(name, document, options) : nil
|
18
|
-
else raise InvalidAssociationError
|
19
|
-
end
|
9
|
+
document ? type.new(name, document, options) : nil
|
20
10
|
end
|
21
11
|
|
22
12
|
# Set an object association. This is used to set the parent reference
|
@@ -31,12 +21,7 @@ module Mongoid #:nodoc:
|
|
31
21
|
# object: The object that was passed in to the setter method.
|
32
22
|
# options: optional options.
|
33
23
|
def set(type, name, document, object, options ={})
|
34
|
-
|
35
|
-
when :belongs_to then BelongsTo.update(document, object)
|
36
|
-
when :has_many then HasMany.update(object, document, name)
|
37
|
-
when :has_one then HasOne.update(object, document, name)
|
38
|
-
else raise InvalidAssociationError
|
39
|
-
end
|
24
|
+
type.update(object, document, name)
|
40
25
|
end
|
41
26
|
end
|
42
27
|
end
|
@@ -9,7 +9,7 @@ module Mongoid #:nodoc:
|
|
9
9
|
#
|
10
10
|
# All method calls on this object will then be delegated
|
11
11
|
# to the internal document itself.
|
12
|
-
def initialize(document)
|
12
|
+
def initialize(name, document, options = {})
|
13
13
|
@document = document.parent
|
14
14
|
decorate!
|
15
15
|
end
|
@@ -25,13 +25,11 @@ module Mongoid #:nodoc:
|
|
25
25
|
# Perform an update of the relationship of the parent and child. This
|
26
26
|
# is initialized by setting a parent object as the association on the
|
27
27
|
# +Document+. Will properly set a has_one or a has_many.
|
28
|
-
def update(child,
|
28
|
+
def update(parent, child, name)
|
29
29
|
name = child.class.name.demodulize.downcase
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
child.parentize(parent, name.tableize)
|
34
|
-
end
|
30
|
+
has_one = parent.associations[name]
|
31
|
+
child.parentize(parent, name) if has_one
|
32
|
+
child.parentize(parent, name.tableize) unless has_one
|
35
33
|
child.notify
|
36
34
|
end
|
37
35
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -37,7 +37,7 @@ module Mongoid #:nodoc:
|
|
37
37
|
#
|
38
38
|
# Returns: <tt>Mongo::Collection</tt>
|
39
39
|
def collection
|
40
|
-
return nil if
|
40
|
+
return nil if embedded?
|
41
41
|
@collection_name = self.to_s.demodulize.tableize
|
42
42
|
@collection ||= Mongoid.database.collection(@collection_name)
|
43
43
|
end
|
@@ -50,6 +50,11 @@ module Mongoid #:nodoc:
|
|
50
50
|
Criteria.translate(*args).count(self)
|
51
51
|
end
|
52
52
|
|
53
|
+
# return true if the +Document+ is embedded in another +Documnet+.
|
54
|
+
def embedded?
|
55
|
+
@embedded == true
|
56
|
+
end
|
57
|
+
|
53
58
|
# Defines all the fields that are accessable on the Document
|
54
59
|
# For each field that is defined, a getter and setter will be
|
55
60
|
# added as an instance method to the Document.
|
@@ -186,6 +191,11 @@ module Mongoid #:nodoc:
|
|
186
191
|
self.class.collection
|
187
192
|
end
|
188
193
|
|
194
|
+
# Return true if the +Document+ is embedded in another +Document+.
|
195
|
+
def embedded?
|
196
|
+
self.class.embedded?
|
197
|
+
end
|
198
|
+
|
189
199
|
# Get the fields for the Document class.
|
190
200
|
def fields
|
191
201
|
self.class.fields
|
@@ -206,7 +216,7 @@ module Mongoid #:nodoc:
|
|
206
216
|
end
|
207
217
|
|
208
218
|
def inspect
|
209
|
-
"
|
219
|
+
"#{self.class.name} : #{@attributes.inspect}"
|
210
220
|
end
|
211
221
|
|
212
222
|
# Return the +Document+ primary key.
|
@@ -235,6 +245,13 @@ module Mongoid #:nodoc:
|
|
235
245
|
@attributes = HashWithIndifferentAccess.new(collection.find_one(:_id => id))
|
236
246
|
end
|
237
247
|
|
248
|
+
# Return the root +Document+ in the object graph.
|
249
|
+
def root
|
250
|
+
object = self
|
251
|
+
while (object.parent) do object = object.parent; end
|
252
|
+
object || self
|
253
|
+
end
|
254
|
+
|
238
255
|
# Returns the id of the Document
|
239
256
|
def to_param
|
240
257
|
id.to_s
|
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.6.
|
8
|
+
s.version = "0.6.7"
|
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-11-
|
12
|
+
s.date = %q{2009-11-10}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
data/spec/spec_helper.rb
CHANGED
@@ -12,7 +12,7 @@ describe Mongoid::Associations::Accessor do
|
|
12
12
|
context "when type is has_many" do
|
13
13
|
|
14
14
|
it "returns a HasMany" do
|
15
|
-
association = Mongoid::Associations::Accessor.get(
|
15
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::HasMany, :addresses, @document)
|
16
16
|
association.should be_a_kind_of(Mongoid::Associations::HasMany)
|
17
17
|
end
|
18
18
|
|
@@ -23,7 +23,7 @@ describe Mongoid::Associations::Accessor do
|
|
23
23
|
context "when document is not nil" do
|
24
24
|
|
25
25
|
it "returns a HasOne" do
|
26
|
-
association = Mongoid::Associations::Accessor.get(
|
26
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::HasOne, :name, @document)
|
27
27
|
association.should be_a_kind_of(Mongoid::Associations::HasOne)
|
28
28
|
end
|
29
29
|
|
@@ -32,7 +32,7 @@ describe Mongoid::Associations::Accessor do
|
|
32
32
|
context "when document is nil" do
|
33
33
|
|
34
34
|
it "returns nil" do
|
35
|
-
association = Mongoid::Associations::Accessor.get(
|
35
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::HasOne, :name, nil)
|
36
36
|
association.should be_nil
|
37
37
|
end
|
38
38
|
|
@@ -43,20 +43,12 @@ describe Mongoid::Associations::Accessor do
|
|
43
43
|
context "when type is belongs_to" do
|
44
44
|
|
45
45
|
it "returns a BelongsTo" do
|
46
|
-
association = Mongoid::Associations::Accessor.get(
|
46
|
+
association = Mongoid::Associations::Accessor.get(Mongoid::Associations::BelongsTo, :person, @document)
|
47
47
|
association.should be_a_kind_of(Mongoid::Associations::BelongsTo)
|
48
48
|
end
|
49
49
|
|
50
50
|
end
|
51
51
|
|
52
|
-
context "when type is invalid" do
|
53
|
-
|
54
|
-
it "raises an InvalidAssociationError" do
|
55
|
-
lambda { Mongoid::Associations::Accessor.get(:something, :person, @document) }.should raise_error
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
52
|
end
|
61
53
|
|
62
54
|
describe "#set" do
|
@@ -65,7 +57,7 @@ describe Mongoid::Associations::Accessor do
|
|
65
57
|
|
66
58
|
it "returns a HasMany" do
|
67
59
|
Mongoid::Associations::HasMany.expects(:update).with(@document, @object, :addresses)
|
68
|
-
Mongoid::Associations::Accessor.set(
|
60
|
+
Mongoid::Associations::Accessor.set(Mongoid::Associations::HasMany, :addresses, @document, @object)
|
69
61
|
end
|
70
62
|
|
71
63
|
end
|
@@ -74,7 +66,7 @@ describe Mongoid::Associations::Accessor do
|
|
74
66
|
|
75
67
|
it "returns a HasOne" do
|
76
68
|
Mongoid::Associations::HasOne.expects(:update).with(@document, @object, :name)
|
77
|
-
Mongoid::Associations::Accessor.set(
|
69
|
+
Mongoid::Associations::Accessor.set(Mongoid::Associations::HasOne, :name, @document, @object)
|
78
70
|
end
|
79
71
|
|
80
72
|
end
|
@@ -82,21 +74,12 @@ describe Mongoid::Associations::Accessor do
|
|
82
74
|
context "when type is belongs_to" do
|
83
75
|
|
84
76
|
it "returns a BelongsTo" do
|
85
|
-
Mongoid::Associations::BelongsTo.expects(:update).with(@document,
|
86
|
-
Mongoid::Associations::Accessor.set(
|
77
|
+
Mongoid::Associations::BelongsTo.expects(:update).with(@object, @document, :person)
|
78
|
+
Mongoid::Associations::Accessor.set(Mongoid::Associations::BelongsTo, :person, @document, @object)
|
87
79
|
end
|
88
80
|
|
89
81
|
end
|
90
82
|
|
91
|
-
context "when type is invalid" do
|
92
|
-
|
93
|
-
it "raises an InvalidAssociationError" do
|
94
|
-
lambda {
|
95
|
-
Mongoid::Associations::Accessor.set(:something, :person, @document, @object)
|
96
|
-
}.should raise_error
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
83
|
end
|
101
84
|
|
102
85
|
end
|
@@ -2,46 +2,12 @@ require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helpe
|
|
2
2
|
|
3
3
|
describe Mongoid::Associations::BelongsTo do
|
4
4
|
|
5
|
-
describe "#update" do
|
6
|
-
|
7
|
-
context "when child is a has one" do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@name = Name.new(:first_name => "Test", :last_name => "User")
|
11
|
-
@person = Person.new(:title => "Mrs")
|
12
|
-
Mongoid::Associations::BelongsTo.update(@name, @person)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "updates the parent document" do
|
16
|
-
@person.name.should == @name
|
17
|
-
@person.attributes[:name].except(:_id).should ==
|
18
|
-
{ "first_name" => "Test", "last_name" => "User" }
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
context "when child is a has many" do
|
24
|
-
|
25
|
-
before do
|
26
|
-
@address = Address.new(:street => "Broadway")
|
27
|
-
@person = Person.new(:title => "Mrs")
|
28
|
-
Mongoid::Associations::BelongsTo.update(@address, @person)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "updates the parent document" do
|
32
|
-
@person.addresses.first.should == @address
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
5
|
describe "#find" do
|
40
6
|
|
41
7
|
before do
|
42
8
|
@parent = Name.new(:first_name => "Drexel")
|
43
9
|
@document = stub(:parent => @parent)
|
44
|
-
@association = Mongoid::Associations::BelongsTo.new(@document)
|
10
|
+
@association = Mongoid::Associations::BelongsTo.new(:person, @document)
|
45
11
|
end
|
46
12
|
|
47
13
|
context "when finding by id" do
|
@@ -60,7 +26,7 @@ describe Mongoid::Associations::BelongsTo do
|
|
60
26
|
before do
|
61
27
|
@parent = Name.new(:first_name => "Drexel")
|
62
28
|
@document = stub(:parent => @parent)
|
63
|
-
@association = Mongoid::Associations::BelongsTo.new(@document)
|
29
|
+
@association = Mongoid::Associations::BelongsTo.new(:person, @document)
|
64
30
|
end
|
65
31
|
|
66
32
|
context "when getting values" do
|
@@ -82,4 +48,38 @@ describe Mongoid::Associations::BelongsTo do
|
|
82
48
|
|
83
49
|
end
|
84
50
|
|
51
|
+
describe "#update" do
|
52
|
+
|
53
|
+
context "when child is a has one" do
|
54
|
+
|
55
|
+
before do
|
56
|
+
@name = Name.new(:first_name => "Test", :last_name => "User")
|
57
|
+
@person = Person.new(:title => "Mrs")
|
58
|
+
Mongoid::Associations::BelongsTo.update(@person, @name, :person)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "updates the parent document" do
|
62
|
+
@person.name.should == @name
|
63
|
+
@person.attributes[:name].except(:_id).should ==
|
64
|
+
{ "first_name" => "Test", "last_name" => "User" }
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when child is a has many" do
|
70
|
+
|
71
|
+
before do
|
72
|
+
@address = Address.new(:street => "Broadway")
|
73
|
+
@person = Person.new(:title => "Mrs")
|
74
|
+
Mongoid::Associations::BelongsTo.update(@person, @address, :person)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "updates the parent document" do
|
78
|
+
@person.addresses.first.should == @address
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
85
|
end
|
@@ -40,7 +40,7 @@ describe Mongoid::Associations do
|
|
40
40
|
before do
|
41
41
|
@person = Person.new(:title => "Mr")
|
42
42
|
@address = Address.new(:street => "Picadilly Circus")
|
43
|
-
@address.
|
43
|
+
@address.addressable = @person
|
44
44
|
end
|
45
45
|
|
46
46
|
it "re-parentizes the association" do
|
@@ -60,12 +60,12 @@ describe Mongoid::Associations do
|
|
60
60
|
|
61
61
|
it "creates a reader for the association" do
|
62
62
|
address = Address.new
|
63
|
-
address.should respond_to(:
|
63
|
+
address.should respond_to(:addressable)
|
64
64
|
end
|
65
65
|
|
66
66
|
it "creates a writer for the association" do
|
67
67
|
address = Address.new
|
68
|
-
address.should respond_to(:
|
68
|
+
address.should respond_to(:addressable=)
|
69
69
|
end
|
70
70
|
|
71
71
|
it "allows the parent to be any type of class" do
|
@@ -134,6 +134,48 @@ describe Mongoid::Document do
|
|
134
134
|
|
135
135
|
end
|
136
136
|
|
137
|
+
describe ".embedded?" do
|
138
|
+
|
139
|
+
context "when the document is embedded" do
|
140
|
+
|
141
|
+
it "returns true" do
|
142
|
+
Address.embedded?.should be_true
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
context "when the document is not embedded" do
|
148
|
+
|
149
|
+
it "returns false" do
|
150
|
+
Person.embedded?.should be_false
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "#embedded?" do
|
158
|
+
|
159
|
+
context "when the document is embedded" do
|
160
|
+
|
161
|
+
it "returns true" do
|
162
|
+
address = Address.new
|
163
|
+
address.embedded?.should be_true
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
context "when the document is not embedded" do
|
169
|
+
|
170
|
+
it "returns false" do
|
171
|
+
person = Person.new
|
172
|
+
person.embedded?.should be_false
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
137
179
|
describe "#field" do
|
138
180
|
|
139
181
|
context "with no options" do
|
@@ -524,6 +566,42 @@ describe Mongoid::Document do
|
|
524
566
|
|
525
567
|
end
|
526
568
|
|
569
|
+
describe "#root" do
|
570
|
+
|
571
|
+
before do
|
572
|
+
@person = Person.new(:title => "Mr")
|
573
|
+
@phone_number = Phone.new(:number => "415-555-1212")
|
574
|
+
@country_code = CountryCode.new(:code => 1)
|
575
|
+
@phone_number.country_code = @country_code
|
576
|
+
@person.phone_numbers << @phone_number
|
577
|
+
end
|
578
|
+
|
579
|
+
context "when document is the root" do
|
580
|
+
|
581
|
+
it "returns self" do
|
582
|
+
@person.root.should == @person
|
583
|
+
end
|
584
|
+
|
585
|
+
end
|
586
|
+
|
587
|
+
context "when document is embedded one level" do
|
588
|
+
|
589
|
+
it "returns the parent" do
|
590
|
+
@phone_number.root.should == @person
|
591
|
+
end
|
592
|
+
|
593
|
+
end
|
594
|
+
|
595
|
+
context "when document is embedded multiple levels" do
|
596
|
+
|
597
|
+
it "returns the top level parent" do
|
598
|
+
@country_code.root.should == @person
|
599
|
+
end
|
600
|
+
|
601
|
+
end
|
602
|
+
|
603
|
+
end
|
604
|
+
|
527
605
|
describe "#select" do
|
528
606
|
|
529
607
|
it "returns a new criteria with select conditions added" do
|
@@ -773,7 +851,17 @@ describe Mongoid::Document do
|
|
773
851
|
|
774
852
|
context "when association is a has_many" do
|
775
853
|
|
776
|
-
it "fails when any association fails validation"
|
854
|
+
it "fails when any association fails validation" do
|
855
|
+
Person.class_eval do
|
856
|
+
validates_associated :addresses
|
857
|
+
end
|
858
|
+
Address.class_eval do
|
859
|
+
validates_presence_of :street
|
860
|
+
end
|
861
|
+
@person.addresses << Address.new
|
862
|
+
@person.valid?.should be_false
|
863
|
+
@person.errors.on(:addresses).should_not be_nil
|
864
|
+
end
|
777
865
|
|
778
866
|
end
|
779
867
|
|
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.6.
|
4
|
+
version: 0.6.7
|
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-11-
|
12
|
+
date: 2009-11-10 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|