mongo_mapper 0.14.0 → 0.15.0

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.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/{README.rdoc → README.md} +28 -20
  4. data/examples/keys.rb +1 -1
  5. data/examples/modifiers/set.rb +1 -1
  6. data/examples/querying.rb +1 -1
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +1 -0
  10. data/lib/mongo_mapper/connection.rb +16 -38
  11. data/lib/mongo_mapper/extensions/object_id.rb +5 -1
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
  13. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  14. data/lib/mongo_mapper/plugins/document.rb +1 -1
  15. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
  16. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
  17. data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
  18. data/lib/mongo_mapper/plugins/indexes.rb +13 -6
  19. data/lib/mongo_mapper/plugins/keys.rb +1 -2
  20. data/lib/mongo_mapper/plugins/modifiers.rb +27 -10
  21. data/lib/mongo_mapper/plugins/persistence.rb +6 -2
  22. data/lib/mongo_mapper/plugins/querying.rb +9 -3
  23. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +3 -4
  24. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  25. data/lib/mongo_mapper/plugins/stats.rb +1 -3
  26. data/lib/mongo_mapper/utils.rb +2 -2
  27. data/lib/mongo_mapper/version.rb +1 -1
  28. data/spec/examples.txt +1643 -0
  29. data/spec/functional/accessible_spec.rb +1 -1
  30. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
  31. data/spec/functional/associations/belongs_to_proxy_spec.rb +4 -4
  32. data/spec/functional/associations/in_array_proxy_spec.rb +14 -14
  33. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  34. data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
  35. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
  36. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  37. data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
  38. data/spec/functional/associations/one_proxy_spec.rb +8 -8
  39. data/spec/functional/associations_spec.rb +3 -3
  40. data/spec/functional/binary_spec.rb +2 -2
  41. data/spec/functional/caching_spec.rb +15 -22
  42. data/spec/functional/callbacks_spec.rb +2 -2
  43. data/spec/functional/counter_cache_spec.rb +10 -10
  44. data/spec/functional/dirty_spec.rb +27 -10
  45. data/spec/functional/document_spec.rb +5 -5
  46. data/spec/functional/dumpable_spec.rb +1 -1
  47. data/spec/functional/embedded_document_spec.rb +5 -5
  48. data/spec/functional/identity_map_spec.rb +6 -6
  49. data/spec/functional/indexes_spec.rb +19 -18
  50. data/spec/functional/keys_spec.rb +22 -22
  51. data/spec/functional/logger_spec.rb +2 -2
  52. data/spec/functional/modifiers_spec.rb +67 -19
  53. data/spec/functional/partial_updates_spec.rb +8 -8
  54. data/spec/functional/protected_spec.rb +1 -1
  55. data/spec/functional/querying_spec.rb +48 -22
  56. data/spec/functional/safe_spec.rb +23 -27
  57. data/spec/functional/sci_spec.rb +7 -7
  58. data/spec/functional/scopes_spec.rb +1 -1
  59. data/spec/functional/static_keys_spec.rb +2 -2
  60. data/spec/functional/stats_spec.rb +28 -12
  61. data/spec/functional/validations_spec.rb +8 -16
  62. data/spec/quality_spec.rb +1 -1
  63. data/spec/spec_helper.rb +32 -8
  64. data/spec/support/matchers.rb +1 -1
  65. data/spec/unit/associations/proxy_spec.rb +1 -1
  66. data/spec/unit/clone_spec.rb +1 -1
  67. data/spec/unit/document_spec.rb +3 -3
  68. data/spec/unit/embedded_document_spec.rb +4 -5
  69. data/spec/unit/extensions_spec.rb +3 -4
  70. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  71. data/spec/unit/key_spec.rb +16 -17
  72. data/spec/unit/keys_spec.rb +7 -7
  73. data/spec/unit/mongo_mapper_spec.rb +41 -88
  74. data/spec/unit/rails_spec.rb +2 -2
  75. metadata +37 -24
  76. data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -9,7 +9,7 @@ module AssociationsSpec
9
9
 
10
10
  many :posts, :class_name => 'AssociationsSpec::AwesomePost', :foreign_key => :creator_id
11
11
  end
12
- AwesomeUser.collection.remove
12
+ AwesomeUser.collection.drop
13
13
 
14
14
  class AwesomeTag
15
15
  include MongoMapper::EmbeddedDocument
@@ -29,8 +29,8 @@ module AssociationsSpec
29
29
  many :tags, :class_name => 'AssociationsSpec::AwesomeTag', :foreign_key => :post_id
30
30
  end
31
31
 
32
- AwesomeUser.collection.remove
33
- AwesomePost.collection.remove
32
+ AwesomeUser.collection.drop
33
+ AwesomePost.collection.drop
34
34
 
35
35
  user = AwesomeUser.create
36
36
  tag1 = AwesomeTag.new(:name => 'awesome')
@@ -10,7 +10,7 @@ describe "Binary" do
10
10
  doc.save
11
11
 
12
12
  doc = doc.reload
13
- doc.contents.to_s.should == BSON::Binary.new('010101').to_s
13
+ doc.contents.data.should == BSON::Binary.new('010101').data
14
14
  end
15
15
 
16
16
  context "Saving a document with a blank binary value" do
@@ -21,7 +21,7 @@ describe "Binary" do
21
21
  end
22
22
 
23
23
  it "not fail" do
24
- expect { @document.new(:file => nil).save }.to_not raise_error
24
+ lambda { @document.new(:file => nil).save }.should_not raise_error
25
25
  end
26
26
  end
27
27
  end
@@ -6,16 +6,16 @@ describe "Caching" do
6
6
  extend MongoMapper::Plugins
7
7
  plugin MongoMapper::Plugins::Caching
8
8
  end
9
- allow(@klass).to receive(:name).and_return('Post')
10
- allow_any_instance_of(@klass).to receive(:persisted?).and_return(true)
11
- allow_any_instance_of(@klass).to receive(:[]).and_return(nil)
12
- allow_any_instance_of(@klass).to receive(:[]=).and_return(nil)
9
+ @klass.stub(:name).and_return('Post')
10
+ @klass.any_instance.stub(:persisted?).and_return(true)
11
+ @klass.any_instance.stub(:[]).and_return(nil)
12
+ @klass.any_instance.stub(:[]=).and_return(nil)
13
13
  end
14
14
 
15
15
  context "new" do
16
16
  before do
17
17
  @doc = @klass.new
18
- allow(@doc).to receive(:persisted?).and_return(false)
18
+ @doc.stub(:persisted?).and_return(false)
19
19
  end
20
20
 
21
21
  it "should be class/new" do
@@ -23,11 +23,9 @@ describe "Caching" do
23
23
  end
24
24
 
25
25
  it "should work with suffix" do
26
- @doc.cache_key(:foo).
27
- should == 'Post/new/foo'
26
+ @doc.cache_key(:foo).should == 'Post/new/foo'
28
27
 
29
- @doc.cache_key(:foo, :bar).
30
- should == 'Post/new/foo/bar'
28
+ @doc.cache_key(:foo, :bar).should == 'Post/new/foo/bar'
31
29
  end
32
30
  end
33
31
 
@@ -35,14 +33,14 @@ describe "Caching" do
35
33
  before do
36
34
  @object_id = BSON::ObjectId.new
37
35
  @doc = @klass.new
38
- allow(@doc).to receive(:persisted).and_return(true)
39
- allow(@doc).to receive(:id).and_return(@object_id)
36
+ @doc.stub(:persisted?).and_return(true)
37
+ @doc.stub(:id).and_return(@object_id)
40
38
  end
41
39
 
42
40
  context "with updated_at" do
43
41
  before do
44
42
  time = Time.utc(2010, 6, 20, 8, 10, 7)
45
- allow(@doc).to receive(:[]).with(:updated_at).and_return(time)
43
+ @doc.stub(:[]).with(:updated_at).and_return(time)
46
44
  end
47
45
 
48
46
  it "should be class/id-timestamp" do
@@ -50,11 +48,9 @@ describe "Caching" do
50
48
  end
51
49
 
52
50
  it "should work with suffix" do
53
- @doc.cache_key(:foo).
54
- should == "Post/#{@object_id}-20100620081007/foo"
51
+ @doc.cache_key(:foo).should == "Post/#{@object_id}-20100620081007/foo"
55
52
 
56
- @doc.cache_key(:foo, :bar).
57
- should == "Post/#{@object_id}-20100620081007/foo/bar"
53
+ @doc.cache_key(:foo, :bar).should == "Post/#{@object_id}-20100620081007/foo/bar"
58
54
  end
59
55
  end
60
56
 
@@ -64,12 +60,9 @@ describe "Caching" do
64
60
  end
65
61
 
66
62
  it "should work with suffix" do
67
- @doc.cache_key(:foo).
68
- should == "Post/#{@object_id}/foo"
69
-
70
- @doc.cache_key(:foo, :bar, :baz).
71
- should == "Post/#{@object_id}/foo/bar/baz"
63
+ @doc.cache_key(:foo).should == "Post/#{@object_id}/foo"
64
+ @doc.cache_key(:foo, :bar, :baz).should == "Post/#{@object_id}/foo/bar/baz"
72
65
  end
73
66
  end
74
67
  end
75
- end
68
+ end
@@ -148,11 +148,11 @@ describe "Callbacks" do
148
148
  @root_class.define_callbacks :after_publish
149
149
  @root_class.after_save { |d| d.run_callbacks(:after_publish) }
150
150
 
151
- expect {
151
+ lambda {
152
152
  child = @child_class.new(:name => 'Child')
153
153
  root = @root_class.create(:name => 'Parent', :children => [child])
154
154
  child.history.should_not include(:after_publish)
155
- }.to_not raise_error
155
+ }.should_not raise_error
156
156
  end
157
157
  end
158
158
 
@@ -171,42 +171,42 @@ describe MongoMapper::Plugins::CounterCache do
171
171
  end
172
172
 
173
173
  it "should update the counter cache on save" do
174
- expect {
174
+ lambda {
175
175
  @comment.save!
176
176
  @article.reload
177
- }.to change(@article, :commentable_count).by(1)
177
+ }.should change(@article, :commentable_count).by(1)
178
178
  end
179
179
 
180
180
  it "should increment with a second object" do
181
181
  @comment.save!
182
182
 
183
- expect {
183
+ lambda {
184
184
  second_comment = CounterCacheFixtureModels::Comment.new
185
185
  second_comment.commentable = @article
186
186
  second_comment.save!
187
187
  @article.reload
188
- }.to change(@article, :commentable_count).by(1)
188
+ }.should change(@article, :commentable_count).by(1)
189
189
  end
190
190
 
191
191
  it "should decrement the counter cache on destroy" do
192
192
  @comment.save!
193
193
 
194
- expect {
194
+ lambda {
195
195
  @comment.destroy
196
196
  @article.reload
197
- }.to change(@article, :commentable_count).by(-1)
197
+ }.should change(@article, :commentable_count).by(-1)
198
198
  end
199
199
 
200
200
  it "should increment with a different type of object" do
201
201
  @comment.save!
202
202
 
203
- expect {
203
+ lambda {
204
204
  second_comment = CounterCacheFixtureModels::Comment.new
205
205
  second_comment.commentable = @article
206
206
  second_comment.save!
207
207
 
208
208
  @article.reload
209
- }.to change(@article, :commentable_count).by(1)
209
+ }.should change(@article, :commentable_count).by(1)
210
210
  end
211
211
 
212
212
  describe "without a counter cache field" do
@@ -226,9 +226,9 @@ describe MongoMapper::Plugins::CounterCache do
226
226
  it "should raise at save (runtime) if there is no counter cache field" do
227
227
  @comment.commentable = @obj
228
228
 
229
- expect {
229
+ lambda {
230
230
  @comment.save!
231
- }.to raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError)
231
+ }.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError)
232
232
  end
233
233
  end
234
234
  end
@@ -9,6 +9,13 @@ describe "Dirty" do
9
9
  end
10
10
 
11
11
  context "marking changes" do
12
+ it "should have the changed? methods" do
13
+ obj = @document.new
14
+ obj.should respond_to(:changed?)
15
+ obj.should respond_to(:phrase_changed?)
16
+ obj.should respond_to(:paragraph_changed?)
17
+ end
18
+
12
19
  it "should not happen if there are none" do
13
20
  doc = @document.new
14
21
  doc.phrase_changed?.should be_falsey
@@ -48,8 +55,8 @@ describe "Dirty" do
48
55
 
49
56
  it "should not happen when loading from database" do
50
57
  doc = @document.create(:phrase => 'Foo')
51
- expect_any_instance_of(@document).to receive(:attribute_will_change!).never
52
- expect_any_instance_of(@document).to receive(:attribute_changed?).never
58
+ @document.any_instance.should_receive(:attribute_will_change!).never
59
+ @document.any_instance.should_receive(:attribute_changed?).never
53
60
  doc = @document.find(doc.id)
54
61
  doc.changed?.should be_falsey
55
62
  end
@@ -220,19 +227,25 @@ describe "Dirty" do
220
227
  doc.a = "d"
221
228
  doc.a_change.should == ["b","d"]
222
229
  end
223
- it "should reset changes when set back to the original value" do
224
- doc = @document.create(:a=>"b")
225
- doc.a = "c"
226
- doc.a = "b"
227
- doc.changed?.should be_falsey
228
- end
230
+
231
+ # TODO: ? Is this consistent with how ActiveRecord now works with changes?
232
+ # it "should reset changes when set back to the original value" do
233
+ # doc = @document.create(:a=>"b")
234
+ # doc.a = "c"
235
+ # doc.a = "b"
236
+ # doc.changed?.should be_falsey
237
+ # end
229
238
  end
230
239
 
231
240
  context "reset_attribute!" do
232
241
  it "should reset the attribute back to the previous value" do
233
242
  doc = @document.create(:a=>"b")
234
243
  doc.a = "c"
235
- doc.reset_a!
244
+ if doc.respond_to?(:restore_a!)
245
+ doc.restore_a!
246
+ else
247
+ doc.reset_a!
248
+ end
236
249
  doc.changed?.should be_falsey
237
250
  doc.a.should == "b"
238
251
  end
@@ -241,7 +254,11 @@ describe "Dirty" do
241
254
  doc.a = "c"
242
255
  doc.a = "d"
243
256
  doc.a = "e"
244
- doc.reset_a!
257
+ if doc.respond_to?(:restore_a!)
258
+ doc.restore_a!
259
+ else
260
+ doc.reset_a!
261
+ end
245
262
  doc.changed?.should be_falsey
246
263
  doc.a.should == "b"
247
264
  end
@@ -240,8 +240,8 @@ describe "Document" do
240
240
  end
241
241
 
242
242
  it "should reset many associations" do
243
- expect(@instance.foos).to receive(:reset).at_least(1).times
244
- expect(@instance.bars).to receive(:reset).at_least(1).times
243
+ @instance.foos.should_receive(:reset).at_least(1).times
244
+ @instance.bars.should_receive(:reset).at_least(1).times
245
245
  @instance.reload
246
246
  end
247
247
 
@@ -271,7 +271,7 @@ describe "Document" do
271
271
 
272
272
  it "should raise DocumentNotFound if not found" do
273
273
  @instance.destroy
274
- expect { @instance.reload }.to raise_error(MongoMapper::DocumentNotFound)
274
+ lambda { @instance.reload }.should raise_error(MongoMapper::DocumentNotFound)
275
275
  end
276
276
 
277
277
  it "should clear keys that were removed from the database" do
@@ -283,7 +283,7 @@ describe "Document" do
283
283
  context "database has keys not defined in model" do
284
284
  before do
285
285
  @id = BSON::ObjectId.new
286
- @document.collection.insert({
286
+ @document.collection.insert_one({
287
287
  :_id => @id,
288
288
  :first_name => 'John',
289
289
  :last_name => 'Nunemaker',
@@ -304,7 +304,7 @@ describe "Document" do
304
304
  end
305
305
 
306
306
  it "should not walk ObjectSpace when creating a model" do
307
- expect(ObjectSpace).to receive(:each_object).never
307
+ ObjectSpace.should_receive(:each_object).never
308
308
  Doc()
309
309
  end
310
310
  end
@@ -7,7 +7,7 @@ describe "Documents with the Dumpable plugin" do
7
7
  let(:store) { ActiveSupport::Cache::MemoryStore.new(:size => 1.megabyte) }
8
8
 
9
9
  it "should be able to be marshalled" do
10
- expect { Marshal.dump(answer) }.to_not raise_error
10
+ lambda { Marshal.dump(answer) }.should_not raise_error
11
11
  end
12
12
 
13
13
  it "should be able to be unmarshalled" do
@@ -227,7 +227,7 @@ describe "EmbeddedDocument" do
227
227
  person.pets << pet
228
228
  pet.should be_new
229
229
 
230
- expect(person).to receive(:save!)
230
+ person.should_receive(:save!)
231
231
  pet.save!
232
232
  end
233
233
 
@@ -272,8 +272,8 @@ describe "EmbeddedDocument" do
272
272
  pet = person.pets.first
273
273
 
274
274
  attributes = {:name => 'koda'}
275
- expect(pet).to receive(:attributes=).with(attributes)
276
- expect(pet).to receive(:save!)
275
+ pet.should_receive(:attributes=).with(attributes)
276
+ pet.should_receive(:save!)
277
277
  pet.update_attributes!(attributes)
278
278
  end
279
279
 
@@ -288,8 +288,8 @@ describe "EmbeddedDocument" do
288
288
  end
289
289
 
290
290
  it "should not fail if the source document contains nils in the embedded document list" do
291
- @klass.collection.insert(:pets => [nil, {:name => "Sasha"}])
292
- expect { @klass.all.first.pets }.to_not raise_error
291
+ @klass.collection.insert_one(:pets => [nil, {:name => "Sasha"}])
292
+ lambda { @klass.all.first.pets }.should_not raise_error
293
293
  @klass.all.first.pets.tap do |pets|
294
294
  pets.length.should == 1
295
295
  pets[0].name.should == "Sasha"
@@ -14,12 +14,12 @@ describe "IdentityMap" do
14
14
  end
15
15
 
16
16
  def expect_no_queries
17
- expect_any_instance_of(Mongo::Collection).to receive(:find_one).never
18
- expect_any_instance_of(Mongo::Collection).to receive(:find).never
17
+ Mongo::Collection.any_instance.should_receive(:find_one).never
18
+ Mongo::Collection.any_instance.should_receive(:find).never
19
19
  end
20
20
 
21
21
  def expects_one_query
22
- expect_any_instance_of(Mongo::Collection).to receive(:find_one).once.and_return({})
22
+ Mongo::Collection.any_instance.should_receive(:find).once.and_call_original
23
23
  end
24
24
 
25
25
  def clear_identity_map
@@ -185,11 +185,11 @@ describe "IdentityMap" do
185
185
  key :created_at, Time
186
186
  end
187
187
 
188
- expect do
188
+ lambda do
189
189
  loaded = person_with_time_class.load({'_id' => @id, 'name' => 'Frank', 'created_at' => '2014-12-07T20:36:45.529-08:00'}, true)
190
190
  loaded.should be_present
191
191
  loaded.created_at.should be_an_instance_of(Time)
192
- end.to_not raise_error
192
+ end.should_not raise_error
193
193
  end
194
194
  end
195
195
 
@@ -420,7 +420,7 @@ describe "IdentityMap" do
420
420
  belongs_to :parent, :class_name => 'Item'
421
421
  one :blog, :class_name => 'Blog', :foreign_key => 'parent_id'
422
422
  end
423
- Item.collection.remove
423
+ Item.collection.drop
424
424
 
425
425
  class ::Blog < ::Item; end
426
426
 
@@ -13,22 +13,25 @@ describe "Indexing" do
13
13
 
14
14
  context "against a known collection" do
15
15
  before do
16
- allow(@document).to receive(:collection).and_return(double(:name => :foo))
16
+ @document.stub(:collection).and_return(double(:name => :foo, :indexes => double()))
17
17
  end
18
- [:create_index, :ensure_index].each do |method|
19
- it "should delegate #{method} to collection" do
20
- expect(@document.collection).to receive(method).with(:arg, {})
21
- @document.send(method, :arg)
22
- end
18
+
19
+ it "should delegate create_index to collection#create_one" do
20
+ @document.collection.indexes.should_receive(:create_one).with({:arg => 1}, {})
21
+ @document.create_index(:arg)
23
22
  end
24
23
 
24
+ it "should delegate ensure_index to collection#create_one" do
25
+ @document.collection.indexes.should_receive(:create_one).with({:arg => 1}, {})
26
+ @document.create_index(:arg)
27
+ end
25
28
  it "should delegate drop_index to collection" do
26
- expect(@document.collection).to receive(:drop_index).with(:arg)
29
+ @document.collection.indexes.should_receive(:drop_one).with(:arg)
27
30
  @document.drop_index(:arg)
28
31
  end
29
32
 
30
33
  it "should delegate drop_indexes to collection" do
31
- expect(@document.collection).to receive(:drop_indexes)
34
+ @document.collection.indexes.should_receive(:drop_all)
32
35
  @document.drop_indexes
33
36
  end
34
37
  end
@@ -48,6 +51,11 @@ describe "Indexing" do
48
51
  @document.should have_index('last_name_1')
49
52
  end
50
53
 
54
+ it "should allow specifying as a hash" do
55
+ @document.ensure_index({last_name: -1})
56
+ @document.should have_index('last_name_-1')
57
+ end
58
+
51
59
  it "should allow creating unique index for a key" do
52
60
  @document.ensure_index :first_name, :unique => true
53
61
  @document.should have_index('fn_1')
@@ -55,18 +63,11 @@ describe "Indexing" do
55
63
 
56
64
  it "should allow creating index on multiple keys" do
57
65
  @document.ensure_index [[:first_name, 1], [:last_name, -1]]
58
-
59
- # order is different for different versions of ruby so instead of
60
- # just checking have_index('first_name_1_last_name_-1') I'm checking
61
- # the values of the indexes to make sure the index creation was successful
62
- @document.collection.index_information.detect do |index|
63
- keys = index[0]
64
- keys.include?('fn_1') && keys.include?('last_name_-1')
65
- end.should_not be_nil
66
+ @document.should have_index('fn_1_last_name_-1')
66
67
  end
67
68
 
68
69
  it "should work with :index shortcut when defining key" do
69
- silence_stderr { @document.key :father, String, :index => true }
70
+ suppress_stderr { @document.key :father, String, :index => true }
70
71
  @document.should have_index('father_1')
71
72
  end
72
- end
73
+ end