mongoid 0.6.4 → 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/associations.rb +7 -2
- data/lib/mongoid/associations/accessor.rb +6 -3
- data/lib/mongoid/associations/has_one.rb +1 -0
- data/mongoid.gemspec +1 -1
- data/spec/integration/mongoid/document_spec.rb +1 -49
- data/spec/spec_helper.rb +92 -0
- data/spec/unit/mongoid/associations/accessor_spec.rb +17 -23
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +1 -19
- data/spec/unit/mongoid/associations/decorator_spec.rb +1 -30
- data/spec/unit/mongoid/associations/has_many_spec.rb +1 -12
- data/spec/unit/mongoid/associations/has_one_spec.rb +1 -16
- data/spec/unit/mongoid/associations_spec.rb +16 -45
- data/spec/unit/mongoid/attributes_spec.rb +1 -6
- data/spec/unit/mongoid/commands/create_spec.rb +1 -1
- data/spec/unit/mongoid/commands/delete_all_spec.rb +1 -1
- data/spec/unit/mongoid/commands/delete_spec.rb +1 -1
- data/spec/unit/mongoid/commands/destroy_all_spec.rb +1 -1
- data/spec/unit/mongoid/commands/destroy_spec.rb +1 -1
- data/spec/unit/mongoid/commands/save_spec.rb +1 -1
- data/spec/unit/mongoid/commands_spec.rb +1 -5
- data/spec/unit/mongoid/criteria_spec.rb +1 -5
- data/spec/unit/mongoid/document_spec.rb +1 -27
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +1 -6
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +1 -6
- data/spec/unit/mongoid/extensions/object/parentization_spec.rb +1 -13
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/time/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/field_spec.rb +1 -1
- data/spec/unit/mongoid/timestamps_spec.rb +1 -5
- data/spec/unit/mongoid/versioning_spec.rb +1 -6
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.5
|
data/lib/mongoid/associations.rb
CHANGED
@@ -94,10 +94,15 @@ module Mongoid # :nodoc:
|
|
94
94
|
def add_association(type, class_name, name, options = {})
|
95
95
|
associations[name] = type
|
96
96
|
define_method(name) do
|
97
|
-
|
97
|
+
if instance_variable_defined?("@#{name}")
|
98
|
+
return instance_variable_get("@#{name}")
|
99
|
+
end
|
100
|
+
proxy = Associations::Accessor.get(type, name, self, options)
|
101
|
+
instance_variable_set("@#{name}", proxy)
|
98
102
|
end
|
99
103
|
define_method("#{name}=") do |object|
|
100
|
-
Associations::Accessor.set(type, name, self, object, options)
|
104
|
+
proxy = Associations::Accessor.set(type, name, self, object, options)
|
105
|
+
instance_variable_set("@#{name}", proxy)
|
101
106
|
end
|
102
107
|
end
|
103
108
|
end
|
@@ -9,9 +9,12 @@ module Mongoid #:nodoc:
|
|
9
9
|
# If the type is invalid a InvalidAssociationError will be thrown.
|
10
10
|
def get(type, name, document, options = {})
|
11
11
|
case type
|
12
|
-
when :belongs_to
|
13
|
-
|
14
|
-
when :
|
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
|
15
18
|
else raise InvalidAssociationError
|
16
19
|
end
|
17
20
|
end
|
data/mongoid.gemspec
CHANGED
@@ -1,52 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
include Mongoid::Timestamps
|
5
|
-
field :title
|
6
|
-
field :terms, :type => Boolean
|
7
|
-
field :age, :type => Integer, :default => 100
|
8
|
-
field :dob, :type => Date
|
9
|
-
has_many :addresses
|
10
|
-
has_one :name
|
11
|
-
end
|
12
|
-
|
13
|
-
class PetOwner < Mongoid::Document
|
14
|
-
field :title
|
15
|
-
has_one :pet
|
16
|
-
end
|
17
|
-
|
18
|
-
class Pet < Mongoid::Document
|
19
|
-
field :name
|
20
|
-
field :weight, :type => Float, :default => 0.0
|
21
|
-
has_many :vet_visits
|
22
|
-
belongs_to :pet_owner
|
23
|
-
end
|
24
|
-
|
25
|
-
class VetVisit < Mongoid::Document
|
26
|
-
field :date, :type => Date
|
27
|
-
belongs_to :pet
|
28
|
-
end
|
29
|
-
|
30
|
-
class Address < Mongoid::Document
|
31
|
-
field :street
|
32
|
-
field :city
|
33
|
-
field :state
|
34
|
-
field :comment_code
|
35
|
-
key :street
|
36
|
-
belongs_to :person
|
37
|
-
end
|
38
|
-
|
39
|
-
class Name < Mongoid::Document
|
40
|
-
field :first_name
|
41
|
-
field :last_name
|
42
|
-
key :first_name, :last_name
|
43
|
-
belongs_to :person
|
44
|
-
end
|
45
|
-
|
46
|
-
class Comment < Mongoid::Document
|
47
|
-
include Mongoid::Versioning
|
48
|
-
field :text
|
49
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
50
2
|
|
51
3
|
describe Mongoid::Document do
|
52
4
|
|
data/spec/spec_helper.rb
CHANGED
@@ -14,3 +14,95 @@ Spec::Runner.configure do |config|
|
|
14
14
|
config.mock_with :mocha
|
15
15
|
Mocha::Configuration.prevent(:stubbing_non_existent_method)
|
16
16
|
end
|
17
|
+
|
18
|
+
class MixedDrink < Mongoid::Document
|
19
|
+
field :name
|
20
|
+
end
|
21
|
+
|
22
|
+
class Person < Mongoid::Document
|
23
|
+
include Mongoid::Timestamps
|
24
|
+
field :title
|
25
|
+
field :terms, :type => Boolean
|
26
|
+
field :age, :type => Integer, :default => 100
|
27
|
+
field :dob, :type => Date
|
28
|
+
field :mixed_drink, :type => MixedDrink
|
29
|
+
has_many :addresses
|
30
|
+
has_many :phone_numbers, :class_name => "Phone"
|
31
|
+
has_one :name
|
32
|
+
has_one :pet, :class_name => "Animal"
|
33
|
+
|
34
|
+
def update_addresses
|
35
|
+
addresses.each_with_index do |address, i|
|
36
|
+
address.street = "Updated #{i}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class CountryCode < Mongoid::Document
|
42
|
+
field :code, :type => Integer
|
43
|
+
key :code
|
44
|
+
belongs_to :phone_number
|
45
|
+
end
|
46
|
+
|
47
|
+
class Phone < Mongoid::Document
|
48
|
+
field :number
|
49
|
+
key :number
|
50
|
+
belongs_to :person
|
51
|
+
has_one :country_code
|
52
|
+
end
|
53
|
+
|
54
|
+
class Animal < Mongoid::Document
|
55
|
+
field :name
|
56
|
+
key :name
|
57
|
+
belongs_to :person
|
58
|
+
end
|
59
|
+
|
60
|
+
class PetOwner < Mongoid::Document
|
61
|
+
field :title
|
62
|
+
has_one :pet
|
63
|
+
end
|
64
|
+
|
65
|
+
class Pet < Mongoid::Document
|
66
|
+
field :name
|
67
|
+
field :weight, :type => Float, :default => 0.0
|
68
|
+
has_many :vet_visits
|
69
|
+
belongs_to :pet_owner
|
70
|
+
end
|
71
|
+
|
72
|
+
class VetVisit < Mongoid::Document
|
73
|
+
field :date, :type => Date
|
74
|
+
belongs_to :pet
|
75
|
+
end
|
76
|
+
|
77
|
+
class Address < Mongoid::Document
|
78
|
+
field :street
|
79
|
+
field :city
|
80
|
+
field :state
|
81
|
+
field :comment_code
|
82
|
+
key :street
|
83
|
+
belongs_to :person
|
84
|
+
end
|
85
|
+
|
86
|
+
class Name < Mongoid::Document
|
87
|
+
field :first_name
|
88
|
+
field :last_name
|
89
|
+
key :first_name, :last_name
|
90
|
+
belongs_to :person
|
91
|
+
end
|
92
|
+
|
93
|
+
class Comment < Mongoid::Document
|
94
|
+
include Mongoid::Versioning
|
95
|
+
field :text
|
96
|
+
end
|
97
|
+
|
98
|
+
class Decorated
|
99
|
+
include Mongoid::Associations::Decorator
|
100
|
+
def initialize(doc)
|
101
|
+
@document = doc
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class Post < Mongoid::Document
|
106
|
+
include Mongoid::Versioning
|
107
|
+
field :title
|
108
|
+
end
|
@@ -1,23 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
has_many :addresses
|
6
|
-
has_one :name
|
7
|
-
end
|
8
|
-
|
9
|
-
class Address < Mongoid::Document
|
10
|
-
field :street
|
11
|
-
key :street
|
12
|
-
belongs_to :person
|
13
|
-
end
|
14
|
-
|
15
|
-
class Name < Mongoid::Document
|
16
|
-
field :first_name
|
17
|
-
field :last_name
|
18
|
-
key :first_name, :last_name
|
19
|
-
belongs_to :person
|
20
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))
|
21
2
|
|
22
3
|
describe Mongoid::Associations::Accessor do
|
23
4
|
|
@@ -39,9 +20,22 @@ describe Mongoid::Associations::Accessor do
|
|
39
20
|
|
40
21
|
context "when type is has_one" do
|
41
22
|
|
42
|
-
|
43
|
-
|
44
|
-
|
23
|
+
context "when document is not nil" do
|
24
|
+
|
25
|
+
it "returns a HasOne" do
|
26
|
+
association = Mongoid::Associations::Accessor.get(:has_one, :name, @document)
|
27
|
+
association.should be_a_kind_of(Mongoid::Associations::HasOne)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when document is nil" do
|
33
|
+
|
34
|
+
it "returns nil" do
|
35
|
+
association = Mongoid::Associations::Accessor.get(:has_one, :name, nil)
|
36
|
+
association.should be_nil
|
37
|
+
end
|
38
|
+
|
45
39
|
end
|
46
40
|
|
47
41
|
end
|
@@ -1,22 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
has_one :name
|
6
|
-
has_many :addresses
|
7
|
-
end
|
8
|
-
|
9
|
-
class Name < Mongoid::Document
|
10
|
-
field :first_name
|
11
|
-
field :last_name
|
12
|
-
key :first_name, :last_name
|
13
|
-
belongs_to :person
|
14
|
-
end
|
15
|
-
|
16
|
-
class Address < Mongoid::Document
|
17
|
-
field :street
|
18
|
-
belongs_to :person
|
19
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))
|
20
2
|
|
21
3
|
describe Mongoid::Associations::BelongsTo do
|
22
4
|
|
@@ -1,33 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
field :terms, :type => Boolean
|
6
|
-
field :age, :type => Integer, :default => 100
|
7
|
-
field :dob, :type => Date
|
8
|
-
has_many :addresses
|
9
|
-
has_one :name
|
10
|
-
end
|
11
|
-
|
12
|
-
class Address < Mongoid::Document
|
13
|
-
field :street
|
14
|
-
key :street
|
15
|
-
belongs_to :person
|
16
|
-
end
|
17
|
-
|
18
|
-
class Name < Mongoid::Document
|
19
|
-
field :first_name
|
20
|
-
field :last_name
|
21
|
-
key :first_name, :last_name
|
22
|
-
belongs_to :person
|
23
|
-
end
|
24
|
-
|
25
|
-
class Decorated
|
26
|
-
include Mongoid::Associations::Decorator
|
27
|
-
def initialize(doc)
|
28
|
-
@document = doc
|
29
|
-
end
|
30
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))
|
31
2
|
|
32
3
|
describe Mongoid::Associations::Decorator do
|
33
4
|
|
@@ -1,15 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
has_many :addresses
|
6
|
-
end
|
7
|
-
|
8
|
-
class Address < Mongoid::Document
|
9
|
-
field :street
|
10
|
-
key :street
|
11
|
-
belongs_to :person
|
12
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))
|
13
2
|
|
14
3
|
describe Mongoid::Associations::HasMany do
|
15
4
|
|
@@ -1,19 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class MixedDrink < Mongoid::Document
|
4
|
-
field :name
|
5
|
-
end
|
6
|
-
|
7
|
-
class Person < Mongoid::Document
|
8
|
-
field :title
|
9
|
-
has_one :name
|
10
|
-
end
|
11
|
-
|
12
|
-
class Name < Mongoid::Document
|
13
|
-
field :first_name
|
14
|
-
key :first_name
|
15
|
-
belongs_to :person
|
16
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))
|
17
2
|
|
18
3
|
describe Mongoid::Associations::HasOne do
|
19
4
|
|
@@ -1,48 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
field :terms, :type => Boolean, :default => false
|
6
|
-
has_many :addresses
|
7
|
-
has_many :phone_numbers, :class_name => "Phone"
|
8
|
-
has_one :name
|
9
|
-
has_one :pet, :class_name => "Animal"
|
10
|
-
end
|
11
|
-
|
12
|
-
class Animal < Mongoid::Document
|
13
|
-
field :name
|
14
|
-
key :name
|
15
|
-
belongs_to :person
|
16
|
-
end
|
17
|
-
|
18
|
-
class CountryCode < Mongoid::Document
|
19
|
-
field :code, :type => Integer
|
20
|
-
key :code
|
21
|
-
belongs_to :phone_number
|
22
|
-
end
|
23
|
-
|
24
|
-
class Address < Mongoid::Document
|
25
|
-
field :street
|
26
|
-
field :city
|
27
|
-
field :state
|
28
|
-
field :post_code
|
29
|
-
key :street
|
30
|
-
belongs_to :person
|
31
|
-
end
|
32
|
-
|
33
|
-
class Name < Mongoid::Document
|
34
|
-
field :first_name
|
35
|
-
field :last_name
|
36
|
-
key :first_name, :last_name
|
37
|
-
belongs_to :person
|
38
|
-
end
|
39
|
-
|
40
|
-
class Phone < Mongoid::Document
|
41
|
-
field :number
|
42
|
-
key :number
|
43
|
-
belongs_to :person
|
44
|
-
has_one :country_code
|
45
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
46
2
|
|
47
3
|
describe Mongoid::Associations do
|
48
4
|
|
@@ -171,6 +127,21 @@ describe Mongoid::Associations do
|
|
171
127
|
|
172
128
|
end
|
173
129
|
|
130
|
+
context "when updating objects internally" do
|
131
|
+
|
132
|
+
before do
|
133
|
+
@address = Address.new(:street => "Bourke Street")
|
134
|
+
@person = Person.new(:title => "Sir")
|
135
|
+
@person.addresses << @address
|
136
|
+
@person.update_addresses
|
137
|
+
end
|
138
|
+
|
139
|
+
it "retains its references to the original objects" do
|
140
|
+
@address.street.should == "Updated 0"
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
174
145
|
end
|
175
146
|
|
176
147
|
describe "#has_one" do
|
@@ -1,9 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :terms, :type => Boolean
|
5
|
-
field :age, :type => Integer, :default => 100
|
6
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
7
2
|
|
8
3
|
describe Mongoid::Attributes do
|
9
4
|
|
@@ -1,30 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
class MixedDrink < Mongoid::Document
|
4
|
-
field :name
|
5
|
-
end
|
6
|
-
|
7
|
-
class Person < Mongoid::Document
|
8
|
-
field :title
|
9
|
-
field :terms, :type => Boolean
|
10
|
-
field :age, :type => Integer, :default => 100
|
11
|
-
field :mixed_drink, :type => MixedDrink
|
12
|
-
has_many :addresses
|
13
|
-
has_one :name
|
14
|
-
end
|
15
|
-
|
16
|
-
class Name < Mongoid::Document
|
17
|
-
field :first_name
|
18
|
-
field :last_name
|
19
|
-
key :first_name, :last_name
|
20
|
-
belongs_to :person
|
21
|
-
end
|
22
|
-
|
23
|
-
class Address < Mongoid::Document
|
24
|
-
field :street
|
25
|
-
key :street
|
26
|
-
belongs_to :person
|
27
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
28
2
|
|
29
3
|
describe Mongoid::Document do
|
30
4
|
|
@@ -1,9 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
field :age, :type => Integer, :default => 100
|
6
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
|
7
2
|
|
8
3
|
describe Mongoid::Extensions::Array::Conversions do
|
9
4
|
|
@@ -1,9 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
field :age, :type => Integer, :default => 100
|
6
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
|
7
2
|
|
8
3
|
describe Mongoid::Extensions::Object::Conversions do
|
9
4
|
|
@@ -1,16 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
has_one :name
|
6
|
-
end
|
7
|
-
|
8
|
-
class Name < Mongoid::Document
|
9
|
-
field :first_name
|
10
|
-
field :last_name
|
11
|
-
key :first_name, :last_name
|
12
|
-
belongs_to :person
|
13
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
|
14
2
|
|
15
3
|
describe Mongoid::Extensions::Object::Parentization do
|
16
4
|
|
@@ -1,8 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
include Mongoid::Timestamps
|
5
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
6
2
|
|
7
3
|
describe Mongoid::Timestamps do
|
8
4
|
|
@@ -1,9 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Post < Mongoid::Document
|
4
|
-
include Mongoid::Versioning
|
5
|
-
field :title
|
6
|
-
end
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
|
7
2
|
|
8
3
|
describe Mongoid::Versioning do
|
9
4
|
|