mongo_mapper 0.12.0 → 0.13.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.rdoc +35 -13
- data/bin/mmconsole +1 -1
- data/lib/mongo_mapper.rb +4 -0
- data/lib/mongo_mapper/connection.rb +17 -6
- data/lib/mongo_mapper/document.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +4 -1
- data/lib/mongo_mapper/extensions/binary.rb +1 -1
- data/lib/mongo_mapper/extensions/boolean.rb +20 -23
- data/lib/mongo_mapper/extensions/date.rb +3 -3
- data/lib/mongo_mapper/extensions/integer.rb +5 -1
- data/lib/mongo_mapper/extensions/kernel.rb +2 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
- data/lib/mongo_mapper/extensions/string.rb +2 -2
- data/lib/mongo_mapper/extensions/time.rb +7 -5
- data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
- data/lib/mongo_mapper/plugins.rb +1 -1
- data/lib/mongo_mapper/plugins/associations.rb +11 -5
- data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
- data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
- data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
- data/lib/mongo_mapper/plugins/clone.rb +4 -2
- data/lib/mongo_mapper/plugins/dirty.rb +22 -21
- data/lib/mongo_mapper/plugins/document.rb +4 -4
- data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
- data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
- data/lib/mongo_mapper/plugins/keys.rb +133 -54
- data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
- data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
- data/lib/mongo_mapper/plugins/persistence.rb +15 -5
- data/lib/mongo_mapper/plugins/querying.rb +15 -40
- data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
- data/lib/mongo_mapper/plugins/rails.rb +22 -2
- data/lib/mongo_mapper/plugins/safe.rb +8 -5
- data/lib/mongo_mapper/plugins/sci.rb +26 -4
- data/lib/mongo_mapper/plugins/scopes.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/utils.rb +12 -0
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
- data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
- data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
- data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
- data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
- data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
- data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
- data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
- data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
- data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
- data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
- data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
- data/spec/functional/associations_spec.rb +48 -0
- data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
- data/spec/functional/caching_spec.rb +75 -0
- data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
- data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
- data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
- data/spec/functional/dumpable_spec.rb +24 -0
- data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
- data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
- data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
- data/spec/functional/extensions_spec.rb +16 -0
- data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
- data/spec/functional/indexes_spec.rb +48 -0
- data/spec/functional/keys_spec.rb +224 -0
- data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
- data/spec/functional/modifiers_spec.rb +550 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +1003 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/safe_spec.rb +163 -0
- data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
- data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
- data/spec/functional/timestamps_spec.rb +97 -0
- data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
- data/spec/functional/userstamps_spec.rb +46 -0
- data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
- data/spec/spec_helper.rb +81 -0
- data/spec/support/matchers.rb +24 -0
- data/{test → spec/support}/models.rb +1 -6
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
- data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
- data/spec/unit/config_generator_spec.rb +24 -0
- data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
- data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
- data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
- data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
- data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
- data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
- data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/model_generator_spec.rb +47 -0
- data/spec/unit/mongo_mapper_spec.rb +184 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
- data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
- data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
- data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
- metadata +199 -179
- data/test/_NOTE_ON_TESTING +0 -1
- data/test/functional/test_associations.rb +0 -46
- data/test/functional/test_caching.rb +0 -77
- data/test/functional/test_indexes.rb +0 -50
- data/test/functional/test_modifiers.rb +0 -537
- data/test/functional/test_pagination.rb +0 -91
- data/test/functional/test_protected.rb +0 -201
- data/test/functional/test_querying.rb +0 -935
- data/test/functional/test_safe.rb +0 -76
- data/test/functional/test_timestamps.rb +0 -62
- data/test/functional/test_userstamps.rb +0 -44
- data/test/support/railtie.rb +0 -4
- data/test/support/railtie/autoloaded.rb +0 -2
- data/test/support/railtie/not_autoloaded.rb +0 -3
- data/test/support/railtie/parent.rb +0 -3
- data/test/test_active_model_lint.rb +0 -18
- data/test/test_helper.rb +0 -93
- data/test/unit/associations/test_base.rb +0 -146
- data/test/unit/associations/test_belongs_to_association.rb +0 -29
- data/test/unit/associations/test_many_association.rb +0 -63
- data/test/unit/associations/test_one_association.rb +0 -47
- data/test/unit/serializers/test_json_serializer.rb +0 -216
- data/test/unit/serializers/test_xml_serializer.rb +0 -196
- data/test/unit/test_identity_map_middleware.rb +0 -132
- data/test/unit/test_keys.rb +0 -65
- data/test/unit/test_mongo_mapper.rb +0 -157
- data/test/unit/test_pagination.rb +0 -11
- data/test/unit/test_rails_compatibility.rb +0 -38
- data/test/unit/test_serialization.rb +0 -166
@@ -1,29 +1,29 @@
|
|
1
|
-
require '
|
2
|
-
require 'models'
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/models'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
describe "OneAsProxy" do
|
5
|
+
before do
|
6
6
|
@post_class = Doc('Post')
|
7
7
|
@author_class = Doc('Author')
|
8
8
|
end
|
9
9
|
|
10
|
-
should
|
10
|
+
it "should default to nil" do
|
11
11
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
12
12
|
@post_class.new.author.nil?.should be_true
|
13
13
|
end
|
14
14
|
|
15
|
-
should
|
15
|
+
it "should return nil instead of a proxy" do
|
16
16
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
17
17
|
nil.should === @post_class.new.author
|
18
18
|
end
|
19
19
|
|
20
|
-
should
|
20
|
+
it "should not define any keys" do
|
21
21
|
count = @post_class.keys.length
|
22
22
|
@post_class.one :author, :class => @author_class
|
23
23
|
@post_class.keys.length.should == count
|
24
24
|
end
|
25
25
|
|
26
|
-
should
|
26
|
+
it "should allow assignment of associated document using a hash" do
|
27
27
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
28
28
|
|
29
29
|
post = @post_class.new('author' => { 'name' => 'Frank' })
|
@@ -35,7 +35,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
35
35
|
post.author.name.should == 'Frank'
|
36
36
|
end
|
37
37
|
|
38
|
-
should
|
38
|
+
it "should only return a document with the correct type" do
|
39
39
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
40
40
|
post = @post_class.create
|
41
41
|
|
@@ -48,13 +48,13 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
48
48
|
|
49
49
|
context "replacing the association" do
|
50
50
|
context "with an object of the class" do
|
51
|
-
|
51
|
+
before do
|
52
52
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
53
53
|
@post = @post_class.new
|
54
54
|
@author = @author_class.new(:name => 'Frank')
|
55
55
|
end
|
56
56
|
|
57
|
-
should
|
57
|
+
it "should work" do
|
58
58
|
@post.author = @author
|
59
59
|
@post.reload
|
60
60
|
|
@@ -66,7 +66,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
66
66
|
@post.author.should == new_author
|
67
67
|
end
|
68
68
|
|
69
|
-
should
|
69
|
+
it "should generate a new proxy instead of modifying the existing one" do
|
70
70
|
@post.author = @author
|
71
71
|
@post.reload
|
72
72
|
|
@@ -82,24 +82,24 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
82
82
|
original_author.name.should == 'Frank'
|
83
83
|
end
|
84
84
|
|
85
|
-
should
|
85
|
+
it "should assign foreign key" do
|
86
86
|
@post.author = @author
|
87
87
|
@post.author.authorable_id.should == @post.id
|
88
88
|
end
|
89
89
|
|
90
|
-
should
|
90
|
+
it "should assign _type" do
|
91
91
|
@post.author = @author
|
92
92
|
@post.author.authorable_type.should == "Post"
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
96
|
context "with a Hash" do
|
97
|
-
|
97
|
+
before do
|
98
98
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
99
99
|
@post = @post_class.new
|
100
100
|
end
|
101
101
|
|
102
|
-
should
|
102
|
+
it "should convert to an object of the class and work" do
|
103
103
|
@post.author = {'name' => 'Frank'}
|
104
104
|
@post.reload
|
105
105
|
|
@@ -110,12 +110,12 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
110
110
|
@post.author.name.should == 'Emily'
|
111
111
|
end
|
112
112
|
|
113
|
-
should
|
113
|
+
it "should assign foreign key" do
|
114
114
|
@post.author = {'name' => 'Frank'}
|
115
115
|
@post.author.authorable_id.should == @post.id
|
116
116
|
end
|
117
117
|
|
118
|
-
should
|
118
|
+
it "should assign _type" do
|
119
119
|
@post.author = {'name' => 'Frank'}
|
120
120
|
@post.author.authorable_type.should == "Post"
|
121
121
|
end
|
@@ -123,7 +123,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
123
123
|
|
124
124
|
context "with :dependent" do
|
125
125
|
context "=> delete" do
|
126
|
-
|
126
|
+
before do
|
127
127
|
@post_class.one :author, :as => :authorable, :class => @author_class, :dependent => :delete
|
128
128
|
|
129
129
|
@post = @post_class.create
|
@@ -131,24 +131,24 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
131
131
|
@post.author = @author
|
132
132
|
end
|
133
133
|
|
134
|
-
should
|
135
|
-
@author_class.any_instance.
|
134
|
+
it "should call delete on the existing document" do
|
135
|
+
@author_class.any_instance.should_receive(:delete).once
|
136
136
|
@post.author = @author_class.new
|
137
137
|
end
|
138
138
|
|
139
|
-
should
|
139
|
+
it "should remove the existing document from the database" do
|
140
140
|
@post.author = @author_class.new
|
141
141
|
lambda { @author.reload }.should raise_error(MongoMapper::DocumentNotFound)
|
142
142
|
end
|
143
143
|
|
144
|
-
should
|
145
|
-
@author_class.any_instance.
|
144
|
+
it "should do nothing if it's the same document" do
|
145
|
+
@author_class.any_instance.should_receive(:delete).never
|
146
146
|
@post.author = @author
|
147
147
|
end
|
148
148
|
end
|
149
149
|
|
150
150
|
context "=> destory" do
|
151
|
-
|
151
|
+
before do
|
152
152
|
@post_class.one :author, :as => :authorable, :class => @author_class, :dependent => :destroy
|
153
153
|
|
154
154
|
@post = @post_class.create
|
@@ -156,24 +156,24 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
156
156
|
@post.author = @author
|
157
157
|
end
|
158
158
|
|
159
|
-
should
|
160
|
-
@author_class.any_instance.
|
159
|
+
it "should call destroy the existing document" do
|
160
|
+
@author_class.any_instance.should_receive(:destroy).once
|
161
161
|
@post.author = @author_class.new
|
162
162
|
end
|
163
163
|
|
164
|
-
should
|
164
|
+
it "should remove the existing document from the database" do
|
165
165
|
@post.author = @author_class.new
|
166
166
|
lambda { @author.reload }.should raise_error(MongoMapper::DocumentNotFound)
|
167
167
|
end
|
168
168
|
|
169
|
-
should
|
170
|
-
@author_class.any_instance.
|
169
|
+
it "should do nothing if it's the same document" do
|
170
|
+
@author_class.any_instance.should_receive(:destroy).never
|
171
171
|
@post.author = @author
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
175
175
|
context "=> nullify" do
|
176
|
-
|
176
|
+
before do
|
177
177
|
@post_class.one :author, :as => :authorable, :class => @author_class, :dependent => :nullify
|
178
178
|
|
179
179
|
@post = @post_class.create
|
@@ -181,7 +181,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
181
181
|
@post.author = @author
|
182
182
|
end
|
183
183
|
|
184
|
-
should
|
184
|
+
it "should nullify the existing document" do
|
185
185
|
@author.reload
|
186
186
|
@author.authorable_id.should == @post.id
|
187
187
|
|
@@ -191,13 +191,13 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
191
191
|
@author.authorable_id.should be_nil
|
192
192
|
end
|
193
193
|
|
194
|
-
should
|
194
|
+
it "should work when it's the same document" do
|
195
195
|
old_author = @post.author
|
196
196
|
@post.author = @author
|
197
197
|
old_author.should == @post.author
|
198
198
|
end
|
199
199
|
|
200
|
-
should
|
200
|
+
it "should nullify _type" do
|
201
201
|
@post.author = @author_class.new
|
202
202
|
@author.reload
|
203
203
|
@author.authorable_type.should be_nil
|
@@ -205,7 +205,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
205
205
|
end
|
206
206
|
|
207
207
|
context "unspecified" do
|
208
|
-
|
208
|
+
before do
|
209
209
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
210
210
|
|
211
211
|
@post = @post_class.create
|
@@ -213,7 +213,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
213
213
|
@post.author = @author
|
214
214
|
end
|
215
215
|
|
216
|
-
should
|
216
|
+
it "should nullify the existing document" do
|
217
217
|
@author.reload
|
218
218
|
@author.authorable_id.should == @post.id
|
219
219
|
|
@@ -223,7 +223,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
223
223
|
@author.authorable_id.should be_nil
|
224
224
|
end
|
225
225
|
|
226
|
-
should
|
226
|
+
it "should nullify _type" do
|
227
227
|
@post.author = @author_class.new
|
228
228
|
@author.reload
|
229
229
|
@author.authorable_type.should be_nil
|
@@ -232,7 +232,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
232
232
|
end
|
233
233
|
|
234
234
|
context "with nil" do
|
235
|
-
|
235
|
+
before do
|
236
236
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
237
237
|
|
238
238
|
@post = @post_class.new
|
@@ -240,18 +240,18 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
240
240
|
@post.author = @author
|
241
241
|
end
|
242
242
|
|
243
|
-
should
|
243
|
+
it "should nullify the existing document" do
|
244
244
|
@post.author = nil
|
245
245
|
@author.reload
|
246
246
|
@author.authorable_id.should be_nil
|
247
247
|
end
|
248
248
|
|
249
|
-
should
|
249
|
+
it "should set the target to nil" do
|
250
250
|
@post.author = nil
|
251
251
|
@post.author.should == nil
|
252
252
|
end
|
253
253
|
|
254
|
-
should
|
254
|
+
it "should nullify _type" do
|
255
255
|
@post.author = nil
|
256
256
|
@author.reload
|
257
257
|
@author.authorable_type.should be_nil
|
@@ -259,7 +259,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
262
|
-
should
|
262
|
+
it "should have boolean method for testing presence" do
|
263
263
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
264
264
|
|
265
265
|
post = @post_class.new
|
@@ -269,7 +269,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
269
269
|
post.author?.should be_true
|
270
270
|
end
|
271
271
|
|
272
|
-
should
|
272
|
+
it "should work with criteria" do
|
273
273
|
@post_class.one :primary_author, :as => :authorable, :class => @author_class, :primary => true
|
274
274
|
@post_class.one :author, :as => :authorable, :class => @author_class, :primary => false
|
275
275
|
|
@@ -281,7 +281,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
281
281
|
post.primary_author.should == primary
|
282
282
|
end
|
283
283
|
|
284
|
-
should
|
284
|
+
it "should unset the association" do
|
285
285
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
286
286
|
post = @post_class.create
|
287
287
|
author = @author_class.create
|
@@ -293,7 +293,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
293
293
|
|
294
294
|
context "destroying parent with :dependent" do
|
295
295
|
context "=> destroy" do
|
296
|
-
|
296
|
+
before do
|
297
297
|
@post_class.one :author, :as => :authorable, :class => @author_class, :dependent => :destroy
|
298
298
|
|
299
299
|
@post = @post_class.create
|
@@ -301,12 +301,12 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
301
301
|
@post.author = @author
|
302
302
|
end
|
303
303
|
|
304
|
-
|
305
|
-
@author_class.any_instance.
|
304
|
+
it "should should call destroy on the associated documents" do
|
305
|
+
@author_class.any_instance.should_receive(:destroy).once
|
306
306
|
@post.destroy
|
307
307
|
end
|
308
308
|
|
309
|
-
|
309
|
+
it "should should remove the associated documents" do
|
310
310
|
@author_class.count.should == 1
|
311
311
|
@post.destroy
|
312
312
|
@post.author.should == nil
|
@@ -315,7 +315,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
315
315
|
end
|
316
316
|
|
317
317
|
context "=> delete" do
|
318
|
-
|
318
|
+
before do
|
319
319
|
@post_class.one :author, :as => :authorable, :class => @author_class, :dependent => :delete
|
320
320
|
|
321
321
|
@post = @post_class.create
|
@@ -323,12 +323,12 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
323
323
|
@post.author = @author
|
324
324
|
end
|
325
325
|
|
326
|
-
|
327
|
-
@author_class.any_instance.
|
326
|
+
it "should should call delete the associated documents" do
|
327
|
+
@author_class.any_instance.should_receive(:delete).once
|
328
328
|
@post.destroy
|
329
329
|
end
|
330
330
|
|
331
|
-
should
|
331
|
+
it "should remove the associated documents" do
|
332
332
|
@author_class.count.should == 1
|
333
333
|
@post.destroy
|
334
334
|
@post.author.should == nil
|
@@ -337,7 +337,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
337
337
|
end
|
338
338
|
|
339
339
|
context "=> nullify" do
|
340
|
-
|
340
|
+
before do
|
341
341
|
@post_class.one :author, :as => :authorable, :class => @author_class, :dependent => :nullify
|
342
342
|
|
343
343
|
@post = @post_class.create
|
@@ -345,7 +345,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
345
345
|
@post.author = @author
|
346
346
|
end
|
347
347
|
|
348
|
-
|
348
|
+
it "should should nullify the relationship but not destroy the associated document" do
|
349
349
|
@author_class.count.should == 1
|
350
350
|
@post.destroy
|
351
351
|
@post.author.should == nil
|
@@ -355,14 +355,14 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
355
355
|
@author.authorable_id.should == nil
|
356
356
|
end
|
357
357
|
|
358
|
-
should
|
358
|
+
it "should nullify _type" do
|
359
359
|
@post.destroy
|
360
360
|
@author.authorable_type.should be_nil
|
361
361
|
end
|
362
362
|
end
|
363
363
|
|
364
364
|
context "unspecified" do
|
365
|
-
|
365
|
+
before do
|
366
366
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
367
367
|
|
368
368
|
@post = @post_class.create
|
@@ -370,7 +370,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
370
370
|
@post.author = @author
|
371
371
|
end
|
372
372
|
|
373
|
-
|
373
|
+
it "should should nullify the relationship but not destroy the associated document" do
|
374
374
|
@author_class.count.should == 1
|
375
375
|
@post.destroy
|
376
376
|
@post.author.should == nil
|
@@ -380,7 +380,7 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
380
380
|
@author.authorable_id.should == nil
|
381
381
|
end
|
382
382
|
|
383
|
-
should
|
383
|
+
it "should nullify _type" do
|
384
384
|
@post.destroy
|
385
385
|
@author.authorable_type.should be_nil
|
386
386
|
end
|
@@ -388,12 +388,12 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
388
388
|
end
|
389
389
|
|
390
390
|
context "#build" do
|
391
|
-
|
391
|
+
before do
|
392
392
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
393
393
|
@post = @post_class.create
|
394
394
|
end
|
395
395
|
|
396
|
-
should
|
396
|
+
it "should be able to build with attributes" do
|
397
397
|
author = @post.build_author(:name => 'John')
|
398
398
|
@post.author.should be_instance_of(@author_class)
|
399
399
|
@post.author.should be_new
|
@@ -401,24 +401,24 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
401
401
|
@post.author.should == author
|
402
402
|
end
|
403
403
|
|
404
|
-
should
|
404
|
+
it "should assign foreign key" do
|
405
405
|
@post.build_author
|
406
406
|
@post.author.authorable_id.should == @post.id
|
407
407
|
end
|
408
408
|
|
409
|
-
should
|
409
|
+
it "should assign _type" do
|
410
410
|
@post.build_author
|
411
411
|
@post.author.authorable_type.should == 'Post'
|
412
412
|
end
|
413
413
|
end
|
414
414
|
|
415
415
|
context "#create" do
|
416
|
-
|
416
|
+
before do
|
417
417
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
418
418
|
@post = @post_class.create
|
419
419
|
end
|
420
420
|
|
421
|
-
should
|
421
|
+
it "should be able to create" do
|
422
422
|
author = @post.create_author(:name => 'John')
|
423
423
|
@post.author.should be_instance_of(@author_class)
|
424
424
|
@post.author.should_not be_new
|
@@ -426,31 +426,29 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
426
426
|
@post.author.should == author
|
427
427
|
end
|
428
428
|
|
429
|
-
should
|
429
|
+
it "should assign foreign key" do
|
430
430
|
@post.create_author
|
431
431
|
@post.author.authorable_id.should == @post.id
|
432
432
|
end
|
433
433
|
|
434
|
-
should
|
434
|
+
it "should assign _type" do
|
435
435
|
@post.create_author
|
436
436
|
@post.author.authorable_type.should == 'Post'
|
437
437
|
end
|
438
438
|
end
|
439
439
|
|
440
440
|
context "#create!" do
|
441
|
-
|
441
|
+
before do
|
442
442
|
@author_class.key :name, String, :required => true
|
443
443
|
@post_class.one :author, :as => :authorable, :class => @author_class
|
444
444
|
@post = @post_class.create
|
445
445
|
end
|
446
446
|
|
447
|
-
should
|
448
|
-
|
449
|
-
@post.create_author!
|
450
|
-
end
|
447
|
+
it "should raise exception if invalid" do
|
448
|
+
expect { @post.create_author! }.to raise_error(MongoMapper::DocumentNotValid)
|
451
449
|
end
|
452
450
|
|
453
|
-
should
|
451
|
+
it "should work if valid" do
|
454
452
|
author = @post.create_author!(:name => 'John')
|
455
453
|
@post.author.should be_instance_of(@author_class)
|
456
454
|
@post.author.should_not be_new
|
@@ -458,31 +456,31 @@ class OneAsProxyTest < Test::Unit::TestCase
|
|
458
456
|
@post.author.should == author
|
459
457
|
end
|
460
458
|
|
461
|
-
should
|
459
|
+
it "should assign foreign key if valid" do
|
462
460
|
@post.create_author!(:name => 'John')
|
463
461
|
@post.author.authorable_id.should == @post.id
|
464
462
|
end
|
465
463
|
|
466
|
-
should
|
464
|
+
it "should assign _type if valid" do
|
467
465
|
@post.create_author!(:name => 'John')
|
468
466
|
@post.author.authorable_type.should == 'Post'
|
469
467
|
end
|
470
468
|
end
|
471
469
|
|
472
470
|
context "namespaced foreign keys" do
|
473
|
-
|
471
|
+
before do
|
474
472
|
News::Paper.one :article, :as => 'articleable', :class_name => 'News::Article'
|
475
473
|
News::Article.belongs_to :articleable, :polymorphic => true
|
476
474
|
|
477
475
|
@paper = News::Paper.create
|
478
476
|
end
|
479
477
|
|
480
|
-
should
|
478
|
+
it "should work" do
|
481
479
|
@paper.create_article
|
482
480
|
@paper.article.class.should == News::Article
|
483
481
|
end
|
484
482
|
|
485
|
-
should
|
483
|
+
it "should properly infer the foreign key" do
|
486
484
|
article = @paper.create_article
|
487
485
|
article.should respond_to(:articleable_id)
|
488
486
|
article.articleable_id.should == @paper.id
|