mongo_mapper-unstable 2010.3.8 → 2010.06.23
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -8
- data/bin/mmconsole +1 -1
- data/examples/keys.rb +37 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +11 -329
- data/lib/mongo_mapper/embedded_document.rb +9 -38
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
- data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
- data/lib/mongo_mapper/plugins/associations.rb +14 -22
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
- data/lib/mongo_mapper/plugins/clone.rb +10 -4
- data/lib/mongo_mapper/plugins/descendants.rb +3 -2
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +4 -10
- data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/keys.rb +85 -110
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
- data/lib/mongo_mapper/plugins/pagination.rb +5 -15
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +9 -1
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/rails.rb +6 -1
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +9 -5
- data/lib/mongo_mapper/plugins.rb +1 -20
- data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
- data/lib/mongo_mapper/version.rb +4 -0
- data/lib/mongo_mapper.rb +71 -128
- data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
- data/test/functional/associations/test_in_array_proxy.rb +7 -9
- data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
- data/test/functional/associations/test_many_documents_proxy.rb +186 -64
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
- data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
- data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +70 -49
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +11 -11
- data/test/functional/test_binary.rb +5 -5
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +104 -34
- data/test/functional/test_dirty.rb +16 -16
- data/test/functional/test_document.rb +12 -924
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +88 -8
- data/test/functional/test_identity_map.rb +41 -43
- data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
- data/test/functional/test_logger.rb +1 -1
- data/test/functional/test_modifiers.rb +275 -181
- data/test/functional/test_pagination.rb +13 -15
- data/test/functional/test_protected.rb +25 -11
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +11 -11
- data/test/functional/test_timestamps.rb +0 -2
- data/test/functional/test_userstamps.rb +0 -1
- data/test/functional/test_validations.rb +44 -31
- data/test/models.rb +18 -17
- data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
- data/test/test_helper.rb +59 -16
- data/test/unit/associations/test_base.rb +47 -42
- data/test/unit/associations/test_proxy.rb +15 -15
- data/test/unit/serializers/test_json_serializer.rb +29 -29
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +3 -3
- data/test/unit/test_document.rb +49 -67
- data/test/unit/test_dynamic_finder.rb +53 -51
- data/test/unit/test_embedded_document.rb +19 -38
- data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +29 -147
- data/test/unit/test_mongo_mapper.rb +3 -48
- data/test/unit/test_pagination.rb +1 -150
- data/test/unit/test_rails.rb +77 -19
- data/test/unit/test_rails_compatibility.rb +12 -12
- data/test/unit/test_serialization.rb +5 -5
- data/test/unit/test_time_zones.rb +9 -9
- data/test/unit/test_validations.rb +46 -46
- metadata +157 -155
- data/.gitignore +0 -10
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
- data/lib/mongo_mapper/query.rb +0 -130
- data/lib/mongo_mapper/support.rb +0 -215
- data/mongo_mapper.gemspec +0 -196
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/support/custom_matchers.rb +0 -55
- data/test/support/timing.rb +0 -16
- data/test/unit/test_query.rb +0 -340
@@ -11,7 +11,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
11
11
|
key :title, String
|
12
12
|
end
|
13
13
|
@post_class.many :comments, :class => @comment_class
|
14
|
-
|
14
|
+
|
15
15
|
@pet_class = EDoc do
|
16
16
|
key :name, String
|
17
17
|
end
|
@@ -21,17 +21,17 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
21
21
|
end
|
22
22
|
@person_class.key :child, @person_class
|
23
23
|
@person_class.many :pets, :class => @pet_class
|
24
|
-
|
24
|
+
|
25
25
|
@owner_class = Doc do
|
26
26
|
key :name, String
|
27
27
|
end
|
28
28
|
@owner_class.many :pets, :class => @pet_class
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
should "default reader to empty array" do
|
32
32
|
@post_class.new.comments.should == []
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
should "allow adding to association like it was an array" do
|
36
36
|
post = @post_class.new
|
37
37
|
post.comments << @comment_class.new
|
@@ -54,33 +54,33 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
54
54
|
post.comments[1].should == bill
|
55
55
|
post.comments[1].new?.should == false
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
should "allow embedding arbitrarily deep" do
|
59
59
|
@klass = Doc()
|
60
60
|
@klass.key :person, @person_class
|
61
|
-
|
61
|
+
|
62
62
|
meg = @person_class.new(:name => 'Meg')
|
63
63
|
meg.child = @person_class.new(:name => 'Steve')
|
64
64
|
meg.child.child = @person_class.new(:name => 'Linda')
|
65
|
-
|
65
|
+
|
66
66
|
doc = @klass.new(:person => meg)
|
67
67
|
doc.save
|
68
68
|
doc.reload
|
69
|
-
|
69
|
+
|
70
70
|
doc.person.name.should == 'Meg'
|
71
71
|
doc.person.child.name.should == 'Steve'
|
72
72
|
doc.person.child.child.name.should == 'Linda'
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
should "allow assignment of many embedded documents using a hash" do
|
76
|
-
person_attributes = {
|
77
|
-
'name' => 'Mr. Pet Lover',
|
76
|
+
person_attributes = {
|
77
|
+
'name' => 'Mr. Pet Lover',
|
78
78
|
'pets' => [
|
79
79
|
{'name' => 'Jimmy', 'species' => 'Cocker Spainel'},
|
80
|
-
{'name' => 'Sasha', 'species' => 'Siberian Husky'},
|
81
|
-
]
|
80
|
+
{'name' => 'Sasha', 'species' => 'Siberian Husky'},
|
81
|
+
]
|
82
82
|
}
|
83
|
-
|
83
|
+
|
84
84
|
owner = @owner_class.new(person_attributes)
|
85
85
|
owner.name.should == 'Mr. Pet Lover'
|
86
86
|
owner.pets[0].name.should == 'Jimmy'
|
@@ -109,12 +109,12 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
109
109
|
@pet_class.new(:name => 'Sparky', :species => 'Dog'),
|
110
110
|
@pet_class.new(:name => 'Koda', :species => 'Dog')
|
111
111
|
])
|
112
|
-
|
112
|
+
|
113
113
|
doc = @klass.new
|
114
114
|
doc.people << meg
|
115
115
|
doc.save
|
116
116
|
doc.reload
|
117
|
-
|
117
|
+
|
118
118
|
doc.people.first.name.should == 'Meg'
|
119
119
|
doc.people.first.pets.should_not == []
|
120
120
|
doc.people.first.pets.first.name.should == 'Sparky'
|
@@ -127,7 +127,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
127
127
|
doc = @klass.new
|
128
128
|
meg = @person_class.new(:name => 'Meg')
|
129
129
|
pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
|
130
|
-
|
130
|
+
|
131
131
|
doc.people << meg
|
132
132
|
meg.pets << pet
|
133
133
|
|
@@ -138,7 +138,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
138
138
|
doc = @klass.new
|
139
139
|
meg = @person_class.new(:name => 'Meg')
|
140
140
|
pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
|
141
|
-
|
141
|
+
|
142
142
|
doc.people << meg
|
143
143
|
meg.pets << pet
|
144
144
|
|
@@ -161,7 +161,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
161
161
|
doc = @klass.new
|
162
162
|
meg = @person_class.new(:name => 'Meg')
|
163
163
|
pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
|
164
|
-
|
164
|
+
|
165
165
|
doc.people << meg
|
166
166
|
meg.pets << pet
|
167
167
|
doc.save
|
@@ -175,7 +175,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
175
175
|
doc = @klass.new
|
176
176
|
meg = @person_class.new(:name => 'Meg')
|
177
177
|
pet = @pet_class.new(:name => 'Sparky', :species => 'Dog')
|
178
|
-
|
178
|
+
|
179
179
|
doc.people << meg
|
180
180
|
meg.pets << pet
|
181
181
|
doc.save
|
@@ -184,20 +184,20 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
184
184
|
doc.people.first.pets.first.person.should == doc.people.first
|
185
185
|
end
|
186
186
|
end
|
187
|
-
|
187
|
+
|
188
188
|
should "allow finding by id" do
|
189
189
|
sparky = @pet_class.new(:name => 'Sparky', :species => 'Dog')
|
190
190
|
meg = @owner_class.create(:name => 'Meg', :pets => [sparky])
|
191
|
-
|
191
|
+
|
192
192
|
meg.pets.find(sparky._id).should == sparky # oid
|
193
193
|
meg.pets.find(sparky.id.to_s).should == sparky # string
|
194
194
|
end
|
195
|
-
|
195
|
+
|
196
196
|
context "count" do
|
197
197
|
should "default to 0" do
|
198
198
|
@owner_class.new.pets.count.should == 0
|
199
199
|
end
|
200
|
-
|
200
|
+
|
201
201
|
should "return correct count if any are embedded" do
|
202
202
|
owner = @owner_class.new(:name => 'Meg')
|
203
203
|
owner.pets = [@pet_class.new, @pet_class.new]
|
@@ -207,7 +207,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
207
207
|
owner.pets.count.should == 2
|
208
208
|
end
|
209
209
|
end
|
210
|
-
|
210
|
+
|
211
211
|
context "extending the association" do
|
212
212
|
setup do
|
213
213
|
@address_class = EDoc do
|
@@ -216,27 +216,27 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
216
216
|
key :state, String
|
217
217
|
key :zip, Integer
|
218
218
|
end
|
219
|
-
|
219
|
+
|
220
220
|
@project_class = Doc do
|
221
221
|
key :name, String
|
222
222
|
end
|
223
223
|
end
|
224
|
-
|
224
|
+
|
225
225
|
should "work using a block passed to many" do
|
226
226
|
@project_class.many :addresses, :class => @address_class do
|
227
227
|
def find_all_by_state(state)
|
228
228
|
find_all { |a| a.state == state }
|
229
229
|
end
|
230
230
|
end
|
231
|
-
|
231
|
+
|
232
232
|
addr1 = @address_class.new(:address => "Gate-3 Lankershim Blvd.", :city => "Universal City", :state => "CA", :zip => "91608")
|
233
233
|
addr2 = @address_class.new(:address => "3000 W. Alameda Ave.", :city => "Burbank", :state => "CA", :zip => "91523")
|
234
234
|
addr3 = @address_class.new(:address => "111 Some Ln", :city => "Nashville", :state => "TN", :zip => "37211")
|
235
235
|
project = @project_class.create(:name => "Some Project", :addresses => [addr1, addr2, addr3])
|
236
|
-
|
236
|
+
|
237
237
|
project.addresses.find_all_by_state("CA").should == [addr1, addr2]
|
238
238
|
end
|
239
|
-
|
239
|
+
|
240
240
|
should "work using many's :extend option" do
|
241
241
|
module FindByCity
|
242
242
|
def find_by_city(city)
|
@@ -244,12 +244,12 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
244
244
|
end
|
245
245
|
end
|
246
246
|
@project_class.many :addresses, :class => @address_class, :extend => FindByCity
|
247
|
-
|
247
|
+
|
248
248
|
addr1 = @address_class.new(:address => "Gate-3 Lankershim Blvd.", :city => "Universal City", :state => "CA", :zip => "91608")
|
249
249
|
addr2 = @address_class.new(:address => "3000 W. Alameda Ave.", :city => "Burbank", :state => "CA", :zip => "91523")
|
250
250
|
addr3 = @address_class.new(:address => "111 Some Ln", :city => "Nashville", :state => "TN", :zip => "37211")
|
251
251
|
project = @project_class.create(:name => "Some Project", :addresses => [addr1, addr2, addr3])
|
252
|
-
|
252
|
+
|
253
253
|
project.addresses.find_by_city('Burbank').should == [addr2]
|
254
254
|
end
|
255
255
|
end
|
@@ -6,15 +6,15 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
6
6
|
Room.collection.remove
|
7
7
|
Message.collection.remove
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
should "default reader to empty array" do
|
11
11
|
Room.new.messages.should == []
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
should "add type key to polymorphic class base" do
|
15
15
|
Message.keys.keys.should include('_type')
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
should "allow adding to assiciation like it was an array" do
|
19
19
|
room = Room.new
|
20
20
|
room.messages << Enter.new
|
@@ -22,10 +22,10 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
22
22
|
room.messages.concat Exit.new
|
23
23
|
room.messages.size.should == 3
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
should "be able to replace the association" do
|
27
27
|
room = Room.create(:name => 'Lounge')
|
28
|
-
|
28
|
+
|
29
29
|
lambda {
|
30
30
|
room.messages = [
|
31
31
|
Enter.new(:body => 'John entered room', :position => 1),
|
@@ -33,7 +33,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
33
33
|
Exit.new(:body => 'John exited room', :position => 3)
|
34
34
|
]
|
35
35
|
}.should change { Message.count }.by(3)
|
36
|
-
|
36
|
+
|
37
37
|
room = room.reload
|
38
38
|
messages = room.messages.all :order => "position"
|
39
39
|
messages.size.should == 3
|
@@ -41,89 +41,89 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
41
41
|
messages[1].body.should == 'Heyyyoooo!'
|
42
42
|
messages[2].body.should == 'John exited room'
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
should "correctly store type when using <<, push and concat" do
|
46
46
|
room = Room.new
|
47
47
|
room.messages << Enter.new(:body => 'John entered the room', :position => 1)
|
48
48
|
room.messages.push Exit.new(:body => 'John entered the room', :position => 2)
|
49
49
|
room.messages.concat Chat.new(:body => 'Holla!' , :position => 3)
|
50
|
-
|
50
|
+
|
51
51
|
room = room.reload
|
52
52
|
messages = room.messages.all :order => "position"
|
53
53
|
messages[0]._type.should == 'Enter'
|
54
54
|
messages[1]._type.should == 'Exit'
|
55
55
|
messages[2]._type.should == 'Chat'
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
context "build" do
|
59
59
|
should "assign foreign key" do
|
60
60
|
room = Room.create
|
61
61
|
message = room.messages.build
|
62
62
|
message.room_id.should == room._id
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
should "assign _type" do
|
66
66
|
room = Room.create
|
67
67
|
message = room.messages.build
|
68
68
|
message._type.should == 'Message'
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
should "allow assigning attributes" do
|
72
72
|
room = Room.create
|
73
73
|
message = room.messages.build(:body => 'Foo!')
|
74
74
|
message.body.should == 'Foo!'
|
75
75
|
end
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
context "create" do
|
79
79
|
should "assign foreign key" do
|
80
80
|
room = Room.create
|
81
81
|
message = room.messages.create
|
82
82
|
message.room_id.should == room._id
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
should "assign _type" do
|
86
86
|
room = Room.create
|
87
87
|
message = room.messages.create
|
88
88
|
message._type.should == 'Message'
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
should "save record" do
|
92
92
|
room = Room.create
|
93
93
|
lambda {
|
94
94
|
room.messages.create
|
95
95
|
}.should change { Message.count }
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
should "allow passing attributes" do
|
99
99
|
room = Room.create
|
100
100
|
message = room.messages.create(:body => 'Foo!')
|
101
101
|
message.body.should == 'Foo!'
|
102
102
|
end
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
context "count" do
|
106
106
|
should "work scoped to association" do
|
107
107
|
room = Room.create
|
108
108
|
3.times { room.messages.create }
|
109
|
-
|
109
|
+
|
110
110
|
other_room = Room.create
|
111
111
|
2.times { other_room.messages.create }
|
112
|
-
|
112
|
+
|
113
113
|
room.messages.count.should == 3
|
114
114
|
other_room.messages.count.should == 2
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
should "work with conditions" do
|
118
118
|
room = Room.create
|
119
119
|
room.messages.create(:body => 'Foo')
|
120
120
|
room.messages.create(:body => 'Other 1')
|
121
121
|
room.messages.create(:body => 'Other 2')
|
122
|
-
|
122
|
+
|
123
123
|
room.messages.count(:body => 'Foo').should == 1
|
124
124
|
end
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
context "Finding scoped to association" do
|
128
128
|
setup do
|
129
129
|
@lounge = Room.create(:name => 'Lounge')
|
@@ -131,7 +131,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
131
131
|
@lm2 = Message.create(:body => 'I love loungin!', :position => 2)
|
132
132
|
@lounge.messages = [@lm1, @lm2]
|
133
133
|
@lounge.save
|
134
|
-
|
134
|
+
|
135
135
|
@hall = Room.create(:name => 'Hall')
|
136
136
|
@hm1 = Message.create(:body => 'Do not fall in the hall', :position => 1)
|
137
137
|
@hm3 = Message.create(:body => 'Loungin!', :position => 3)
|
@@ -139,24 +139,24 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
139
139
|
@hall.messages = [@hm1, @hm2, @hm3]
|
140
140
|
@hall.save
|
141
141
|
end
|
142
|
-
|
142
|
+
|
143
143
|
context "dynamic finders" do
|
144
144
|
should "work with single key" do
|
145
145
|
@lounge.messages.find_by_position(1).should == @lm1
|
146
146
|
@hall.messages.find_by_position(2).should == @hm2
|
147
147
|
end
|
148
|
-
|
148
|
+
|
149
149
|
should "work with multiple keys" do
|
150
150
|
@lounge.messages.find_by_body_and_position('Loungin!', 1).should == @lm1
|
151
151
|
@lounge.messages.find_by_body_and_position('Loungin!', 2).should be_nil
|
152
152
|
end
|
153
|
-
|
153
|
+
|
154
154
|
should "raise error when using !" do
|
155
155
|
lambda {
|
156
156
|
@lounge.messages.find_by_position!(222)
|
157
157
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
context "find_or_create_by" do
|
161
161
|
should "not create document if found" do
|
162
162
|
lambda {
|
@@ -175,103 +175,103 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
175
175
|
end
|
176
176
|
end
|
177
177
|
end
|
178
|
-
|
178
|
+
|
179
179
|
context "with #all" do
|
180
180
|
should "work" do
|
181
181
|
@lounge.messages.all(:order => "position").should == [@lm1, @lm2]
|
182
182
|
end
|
183
|
-
|
183
|
+
|
184
184
|
should "work with conditions" do
|
185
185
|
messages = @lounge.messages.all(:body => 'Loungin!', :order => "position")
|
186
186
|
messages.should == [@lm1]
|
187
187
|
end
|
188
|
-
|
188
|
+
|
189
189
|
should "work with order" do
|
190
190
|
messages = @lounge.messages.all(:order => 'position desc')
|
191
191
|
messages.should == [@lm2, @lm1]
|
192
192
|
end
|
193
193
|
end
|
194
|
-
|
194
|
+
|
195
195
|
context "with #first" do
|
196
196
|
should "work" do
|
197
197
|
@lounge.messages.first(:order => "position asc").should == @lm1
|
198
198
|
end
|
199
|
-
|
199
|
+
|
200
200
|
should "work with conditions" do
|
201
201
|
message = @lounge.messages.first(:body => 'I love loungin!', :order => "position asc")
|
202
202
|
message.should == @lm2
|
203
203
|
end
|
204
204
|
end
|
205
|
-
|
205
|
+
|
206
206
|
context "with #last" do
|
207
207
|
should "work" do
|
208
208
|
@lounge.messages.last(:order => "position asc").should == @lm2
|
209
209
|
end
|
210
|
-
|
210
|
+
|
211
211
|
should "work with conditions" do
|
212
212
|
message = @lounge.messages.last(:body => 'Loungin!', :order => "position asc")
|
213
213
|
message.should == @lm1
|
214
214
|
end
|
215
215
|
end
|
216
|
-
|
216
|
+
|
217
217
|
context "with one id" do
|
218
218
|
should "work for id in association" do
|
219
219
|
@lounge.messages.find(@lm2._id).should == @lm2
|
220
220
|
end
|
221
|
-
|
221
|
+
|
222
222
|
should "not work for id not in association" do
|
223
223
|
lambda {
|
224
224
|
@lounge.messages.find!(@hm2._id)
|
225
225
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
226
226
|
end
|
227
227
|
end
|
228
|
-
|
228
|
+
|
229
229
|
context "with query options/criteria" do
|
230
230
|
should "work with order on association" do
|
231
231
|
@lounge.messages.should == [@lm1, @lm2]
|
232
232
|
end
|
233
|
-
|
233
|
+
|
234
234
|
should "allow overriding the order provided to the association" do
|
235
235
|
@lounge.messages.all(:order => 'position').should == [@lm1, @lm2]
|
236
236
|
end
|
237
|
-
|
237
|
+
|
238
238
|
should "allow using conditions on association" do
|
239
239
|
@hall.latest_messages.should == [@hm3, @hm2]
|
240
240
|
end
|
241
241
|
end
|
242
|
-
|
242
|
+
|
243
243
|
context "with multiple ids" do
|
244
244
|
should "work for ids in association" do
|
245
245
|
messages = @lounge.messages.find(@lm1._id, @lm2._id)
|
246
246
|
messages.should == [@lm1, @lm2]
|
247
247
|
end
|
248
|
-
|
248
|
+
|
249
249
|
should "not work for ids not in association" do
|
250
250
|
assert_raises(MongoMapper::DocumentNotFound) do
|
251
251
|
@lounge.messages.find!(@lm1._id, @lm2._id, @hm2._id)
|
252
252
|
end
|
253
253
|
end
|
254
254
|
end
|
255
|
-
|
255
|
+
|
256
256
|
context "with #paginate" do
|
257
257
|
setup do
|
258
258
|
@messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
|
259
259
|
end
|
260
|
-
|
260
|
+
|
261
261
|
should "return total pages" do
|
262
262
|
@messages.total_pages.should == 2
|
263
263
|
end
|
264
|
-
|
264
|
+
|
265
265
|
should "return total entries" do
|
266
266
|
@messages.total_entries.should == 3
|
267
267
|
end
|
268
|
-
|
268
|
+
|
269
269
|
should "return the subject" do
|
270
270
|
@messages.should == [@hm1, @hm2]
|
271
271
|
end
|
272
272
|
end
|
273
273
|
end
|
274
|
-
|
274
|
+
|
275
275
|
context "extending the association" do
|
276
276
|
should "work using a block passed to many" do
|
277
277
|
room = Room.new(:name => "Amazing Room")
|
@@ -286,9 +286,9 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
286
286
|
room.save
|
287
287
|
room.messages.older.should == messages[3..5]
|
288
288
|
end
|
289
|
-
|
289
|
+
|
290
290
|
should "work using many's :extend option" do
|
291
|
-
|
291
|
+
|
292
292
|
room = Room.new(:name => "Amazing Room")
|
293
293
|
accounts = room.accounts = [
|
294
294
|
Bot.new(:last_logged_in => 3.weeks.ago),
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class OneEmbeddedProxyTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@post_class = Doc('Post') do
|
6
|
+
key :title, String
|
7
|
+
end
|
8
|
+
@author_class = EDoc('Author') do
|
9
|
+
key :name, String
|
10
|
+
embedded_in :post
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
should "default to nil" do
|
15
|
+
@post_class.one :author, :class => @author_class
|
16
|
+
@post_class.new.author.should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
should "be able to build" do
|
20
|
+
@post_class.one :author, :class => @author_class
|
21
|
+
|
22
|
+
post = @post_class.create
|
23
|
+
author = post.author.build(:name => "John")
|
24
|
+
post.author.should be_instance_of(@author_class)
|
25
|
+
post.author.should be_new
|
26
|
+
post.author.name.should == 'John'
|
27
|
+
post.author.should == author
|
28
|
+
post.author.post.should == post
|
29
|
+
end
|
30
|
+
|
31
|
+
should "be able to replace the association" do
|
32
|
+
@post_class.one :author, :class => @author_class
|
33
|
+
|
34
|
+
post = @post_class.new
|
35
|
+
author = @author_class.new(:name => 'Frank')
|
36
|
+
post.author = author
|
37
|
+
post.save
|
38
|
+
post.reload
|
39
|
+
|
40
|
+
post.author.should == author
|
41
|
+
post.author.nil?.should be_false
|
42
|
+
|
43
|
+
new_author = @author_class.new(:name => 'Emily')
|
44
|
+
post.author = new_author
|
45
|
+
post.author.should == new_author
|
46
|
+
end
|
47
|
+
|
48
|
+
should "not have problem loading root document if embedded one is nil" do
|
49
|
+
@post_class.one :author, :class => @author_class
|
50
|
+
post = @post_class.create
|
51
|
+
|
52
|
+
lambda {
|
53
|
+
@post_class.find(post.id)
|
54
|
+
}.should_not raise_error
|
55
|
+
end
|
56
|
+
|
57
|
+
should "have boolean method for testing presence" do
|
58
|
+
@post_class.one :author, :class => @author_class
|
59
|
+
|
60
|
+
post = @post_class.new
|
61
|
+
post.author?.should be_false
|
62
|
+
|
63
|
+
post.author = @author_class.new(:name => 'Frank')
|
64
|
+
post.author?.should be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|