mongoid 1.2.7 → 1.2.8

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 (39) hide show
  1. data/VERSION +1 -1
  2. data/lib/mongoid.rb +2 -1
  3. data/lib/mongoid/associations.rb +18 -14
  4. data/lib/mongoid/associations/has_many_related.rb +1 -1
  5. data/lib/mongoid/associations/meta_data.rb +28 -0
  6. data/lib/mongoid/associations/options.rb +1 -6
  7. data/lib/mongoid/attributes.rb +1 -1
  8. data/lib/mongoid/caching.rb +41 -0
  9. data/lib/mongoid/collection.rb +1 -0
  10. data/lib/mongoid/components.rb +1 -0
  11. data/lib/mongoid/config.rb +2 -0
  12. data/lib/mongoid/contexts/mongo.rb +8 -13
  13. data/lib/mongoid/criteria.rb +7 -2
  14. data/lib/mongoid/criterion/inclusion.rb +1 -0
  15. data/lib/mongoid/criterion/optional.rb +10 -2
  16. data/lib/mongoid/finders.rb +5 -23
  17. data/lib/mongoid/identity.rb +1 -1
  18. data/lib/mongoid/javascript.rb +21 -0
  19. data/lib/mongoid/javascript/functions.yml +37 -0
  20. data/mongoid.gemspec +12 -2
  21. data/spec/integration/mongoid/attributes_spec.rb +2 -2
  22. data/spec/integration/mongoid/commands_spec.rb +5 -3
  23. data/spec/integration/mongoid/criteria_spec.rb +27 -0
  24. data/spec/models/game.rb +2 -1
  25. data/spec/models/post.rb +1 -1
  26. data/spec/unit/mongoid/associations/has_many_related_spec.rb +5 -1
  27. data/spec/unit/mongoid/associations/meta_data_spec.rb +88 -0
  28. data/spec/unit/mongoid/associations/options_spec.rb +20 -19
  29. data/spec/unit/mongoid/associations_spec.rb +41 -7
  30. data/spec/unit/mongoid/caching_spec.rb +63 -0
  31. data/spec/unit/mongoid/collection_spec.rb +20 -4
  32. data/spec/unit/mongoid/config_spec.rb +7 -0
  33. data/spec/unit/mongoid/contexts/mongo_spec.rb +51 -12
  34. data/spec/unit/mongoid/criteria_spec.rb +23 -1
  35. data/spec/unit/mongoid/criterion/inclusion_spec.rb +8 -0
  36. data/spec/unit/mongoid/criterion/optional_spec.rb +0 -10
  37. data/spec/unit/mongoid/identity_spec.rb +24 -3
  38. data/spec/unit/mongoid/javascript_spec.rb +48 -0
  39. metadata +12 -2
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Caching do
4
+
5
+ before do
6
+ @klass = Class.new do
7
+ include Mongoid::Caching
8
+ end
9
+ end
10
+
11
+ describe ".cache" do
12
+
13
+ before do
14
+ @klass.cache
15
+ end
16
+
17
+ it "sets the cached boolean on the class" do
18
+ @klass.cached.should be_true
19
+ end
20
+
21
+ end
22
+
23
+ describe ".cached" do
24
+
25
+ it "defaults to false" do
26
+ @klass.cached.should be_false
27
+ end
28
+ end
29
+
30
+ describe ".cached?" do
31
+
32
+ context "when the class is cached" do
33
+
34
+ before do
35
+ @klass.cache
36
+ end
37
+
38
+ it "returns true" do
39
+ @klass.should be_cached
40
+ end
41
+ end
42
+
43
+ context "when the class is not cached" do
44
+
45
+ it "returns false" do
46
+ @klass.should_not be_cached
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ describe "#cached?" do
53
+
54
+ before do
55
+ @klass.cache
56
+ @doc = @klass.new
57
+ end
58
+
59
+ it "returns the class cached? value" do
60
+ @doc.should be_cached
61
+ end
62
+ end
63
+ end
@@ -42,12 +42,12 @@ describe Mongoid::Collection do
42
42
 
43
43
  describe "#directed" do
44
44
 
45
- before do
46
- slaves.expects(:empty?).returns(false)
47
- end
48
-
49
45
  context "when an enslave option is not passed" do
50
46
 
47
+ before do
48
+ slaves.expects(:empty?).returns(false)
49
+ end
50
+
51
51
  before do
52
52
  Person.enslave
53
53
  end
@@ -63,11 +63,27 @@ describe Mongoid::Collection do
63
63
 
64
64
  context "when an enslave option is passed" do
65
65
 
66
+ before do
67
+ slaves.expects(:empty?).returns(false)
68
+ end
69
+
66
70
  it "overwrites the default" do
67
71
  collection.directed(:enslave => true).should == slaves
68
72
  end
69
73
  end
70
74
 
75
+ context "when cached option is passed" do
76
+
77
+ let(:options) do
78
+ { :cache => true }
79
+ end
80
+
81
+ it "removed the cache option" do
82
+ collection.directed(options).should == master
83
+ options[:cache].should be_nil
84
+ end
85
+ end
86
+
71
87
  end
72
88
 
73
89
  describe "#find" do
@@ -39,6 +39,13 @@ describe Mongoid::Config do
39
39
  end
40
40
  end
41
41
 
42
+ describe "#persist_types" do
43
+
44
+ it "defaults to true" do
45
+ config.persist_types.should == true
46
+ end
47
+ end
48
+
42
49
  describe "#persist_in_safe_mode=" do
43
50
 
44
51
  context "when setting to true" do
@@ -73,7 +73,13 @@ describe Mongoid::Contexts::Mongo do
73
73
  before do
74
74
  @cursor = stub(:count => 500)
75
75
  @collection = mock
76
- @klass = stub(:collection => @collection, :hereditary => false, :instantiate => @person)
76
+ @klass = stub(
77
+ :collection => @collection,
78
+ :hereditary => false,
79
+ :instantiate => @person,
80
+ :enslaved? => false,
81
+ :cached? => false
82
+ )
77
83
  @criteria = Mongoid::Criteria.new(@klass)
78
84
  @criteria.where(selector).skip(20)
79
85
  @context = Mongoid::Contexts::Mongo.new(@criteria)
@@ -151,27 +157,60 @@ describe Mongoid::Contexts::Mongo do
151
157
  let(:selector) { { :field => "value" } }
152
158
  let(:options) { { :skip => 20 } }
153
159
  let(:klass) { Person }
160
+ let(:criteria) { Mongoid::Criteria.new(klass) }
161
+ let(:context) { Mongoid::Contexts::Mongo.new(criteria) }
154
162
 
155
163
  before do
156
- @criteria = Mongoid::Criteria.new(klass)
157
- @criteria.where(selector).skip(20)
158
- @context = Mongoid::Contexts::Mongo.new(@criteria)
164
+ criteria.where(selector).skip(20)
159
165
  end
160
166
 
161
167
  it "sets the selector" do
162
- @context.selector.should == @criteria.selector
168
+ context.selector.should == criteria.selector
163
169
  end
164
170
 
165
171
  it "sets the options" do
166
- @context.options.should == options
172
+ context.options.should == options
167
173
  end
168
174
 
169
175
  it "sets the klass" do
170
- @context.klass.should == klass
176
+ context.klass.should == klass
177
+ end
178
+
179
+ context "when persisting type" do
180
+
181
+ it "set the selector to query across the _type when it is hereditary" do
182
+ context.selector[:_type].should == {'$in' => Person._types}
183
+ end
184
+
185
+ end
186
+
187
+ context "when not persisting type" do
188
+
189
+ before do
190
+ Mongoid.persist_types = false
191
+ end
192
+
193
+ after do
194
+ Mongoid.persist_types = true
195
+ end
196
+
197
+ it "does not add the type to the selector" do
198
+ context.selector[:_type].should be_nil
199
+ end
200
+
171
201
  end
172
202
 
173
- it "set the selector to query across the _type of the Criteria's klass when it is hereditary" do
174
- @context.selector[:_type].should == {'$in' => Person._types}
203
+ context "enslaved and cached classes" do
204
+
205
+ let(:klass) { Game }
206
+
207
+ it "enslaves the criteria" do
208
+ context.criteria.should be_enslaved
209
+ end
210
+
211
+ it "caches the criteria" do
212
+ context.criteria.should be_cached
213
+ end
175
214
  end
176
215
  end
177
216
 
@@ -236,7 +275,7 @@ describe Mongoid::Contexts::Mongo do
236
275
  describe "#max" do
237
276
 
238
277
  before do
239
- @reduce = Mongoid::Contexts::Mongo::MAX_REDUCE.gsub("[field]", "age")
278
+ @reduce = Mongoid::Javascript.max.gsub("[field]", "age")
240
279
  @collection = mock
241
280
  Person.expects(:collection).returns(@collection)
242
281
  @criteria = Mongoid::Criteria.new(Person)
@@ -259,7 +298,7 @@ describe Mongoid::Contexts::Mongo do
259
298
  describe "#min" do
260
299
 
261
300
  before do
262
- @reduce = Mongoid::Contexts::Mongo::MIN_REDUCE.gsub("[field]", "age")
301
+ @reduce = Mongoid::Javascript.min.gsub("[field]", "age")
263
302
  @collection = mock
264
303
  Person.expects(:collection).returns(@collection)
265
304
  @criteria = Mongoid::Criteria.new(Person)
@@ -371,7 +410,7 @@ describe Mongoid::Contexts::Mongo do
371
410
  context "when klass not provided" do
372
411
 
373
412
  before do
374
- @reduce = Mongoid::Contexts::Mongo::SUM_REDUCE.gsub("[field]", "age")
413
+ @reduce = Mongoid::Javascript.sum.gsub("[field]", "age")
375
414
  @collection = mock
376
415
  @criteria = Mongoid::Criteria.new(Person)
377
416
  @context = Mongoid::Contexts::Mongo.new(@criteria)
@@ -318,7 +318,12 @@ describe Mongoid::Criteria do
318
318
 
319
319
  before do
320
320
  Person.expects(:collection).returns(@collection)
321
- @collection.expects(:find).with({ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir" }, {}).returns(@cursor)
321
+ @collection.expects(:find).with(
322
+ { :_type => { "$in" => ["Doctor", "Person"] },
323
+ :title => "Sir"
324
+ },
325
+ { :cache => true }
326
+ ).returns(@cursor)
322
327
  @cursor.expects(:each).yields(@person)
323
328
  @criteria.cache
324
329
  @criteria.each do |doc|
@@ -387,6 +392,7 @@ describe Mongoid::Criteria do
387
392
  it "sets the klass to the given class" do
388
393
  criteria.klass.should == Person
389
394
  end
395
+
390
396
  end
391
397
 
392
398
  describe "#last" do
@@ -704,6 +710,22 @@ describe Mongoid::Criteria do
704
710
 
705
711
  end
706
712
 
713
+ context "when Person, :conditions => {:id => id}" do
714
+
715
+ before do
716
+ @criteria = Mongoid::Criteria.translate(Person, :conditions => { :id => "1234e567" })
717
+ end
718
+
719
+ it "returns a criteria with a selector from the conditions" do
720
+ @criteria.selector.should == { :_id => "1234e567" }
721
+ end
722
+
723
+ it "returns a criteria with klass Person" do
724
+ @criteria.klass.should == Person
725
+ end
726
+
727
+ end
728
+
707
729
  context "when :all, :conditions => {}" do
708
730
 
709
731
  before do
@@ -66,6 +66,14 @@ describe Mongoid::Criterion::Inclusion do
66
66
  }
67
67
  end
68
68
 
69
+ it "#any_in is aliased to #in" do
70
+ @criteria.any_in(:title => ["title1", "title2"], :text => ["test"])
71
+ @criteria.selector.should ==
72
+ {
73
+ :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
74
+ }
75
+ end
76
+
69
77
  it "returns self" do
70
78
  @criteria.in(:title => ["title1"]).should == @criteria
71
79
  end
@@ -30,16 +30,6 @@ describe Mongoid::Criterion::Optional do
30
30
  it "returns true" do
31
31
  @criteria.cached?.should be_true
32
32
  end
33
-
34
- it "removes cache from the options" do
35
- @criteria.cached?
36
- @criteria.options[:cache].should be_nil
37
- end
38
-
39
- it "sets the cache instance variable" do
40
- @criteria.cached?
41
- @criteria.instance_variable_get(:@cached).should be_true
42
- end
43
33
  end
44
34
 
45
35
  context "when the criteria has no cache option" do
@@ -6,9 +6,30 @@ describe Mongoid::Identity do
6
6
 
7
7
  let(:name) { Name.new }
8
8
 
9
- it "sets the document _type to the class name" do
10
- Mongoid::Identity.create(name)
11
- name._type.should == "Name"
9
+ context "when persisting types" do
10
+
11
+ it "sets the document _type to the class name" do
12
+ Mongoid::Identity.create(name)
13
+ name._type.should == "Name"
14
+ end
15
+
16
+ end
17
+
18
+ context "when not persisting types" do
19
+
20
+ before do
21
+ Mongoid.persist_types = false
22
+ @name = Name.new
23
+ end
24
+
25
+ after do
26
+ Mongoid.persist_types = true
27
+ end
28
+
29
+ it "does not set the type" do
30
+ @name._type.should be_nil
31
+ end
32
+
12
33
  end
13
34
 
14
35
  it "returns the document" do
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Javascript do
4
+
5
+ let(:js) do
6
+ Mongoid::Javascript
7
+ end
8
+
9
+ describe ".aggregate" do
10
+
11
+ it "returns the aggregate function" do
12
+ js.aggregate.should == "function(obj, prev) { prev.count++; }"
13
+ end
14
+ end
15
+
16
+ describe ".group" do
17
+
18
+ it "returns the group function" do
19
+ js.group.should == "function(obj, prev) { prev.group.push(obj); }"
20
+ end
21
+ end
22
+
23
+ describe ".max" do
24
+
25
+ it "returns the max function" do
26
+ js.max.should == "function(obj, prev) { if (prev.max == 'start') { " +
27
+ "prev.max = obj.[field]; } if (prev.max < obj.[field]) { prev.max = obj.[field];" +
28
+ " } }"
29
+ end
30
+ end
31
+
32
+ describe ".min" do
33
+
34
+ it "returns the min function" do
35
+ js.min.should == "function(obj, prev) { if (prev.min == 'start') { " +
36
+ "prev.min = obj.[field]; } if (prev.min > obj.[field]) { prev.min = obj.[field];" +
37
+ " } }"
38
+ end
39
+ end
40
+
41
+ describe ".sum" do
42
+
43
+ it "returns the sum function" do
44
+ js.sum.should == "function(obj, prev) { if (prev.sum == 'start') { prev.sum = 0; " +
45
+ "} prev.sum += obj.[field]; }"
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.7
4
+ version: 1.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-18 00:00:00 -05:00
12
+ date: 2010-02-25 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -97,9 +97,11 @@ files:
97
97
  - lib/mongoid/associations/has_many_related.rb
98
98
  - lib/mongoid/associations/has_one.rb
99
99
  - lib/mongoid/associations/has_one_related.rb
100
+ - lib/mongoid/associations/meta_data.rb
100
101
  - lib/mongoid/associations/options.rb
101
102
  - lib/mongoid/associations/proxy.rb
102
103
  - lib/mongoid/attributes.rb
104
+ - lib/mongoid/caching.rb
103
105
  - lib/mongoid/callbacks.rb
104
106
  - lib/mongoid/collection.rb
105
107
  - lib/mongoid/collections/cyclic_iterator.rb
@@ -160,6 +162,8 @@ files:
160
162
  - lib/mongoid/finders.rb
161
163
  - lib/mongoid/identity.rb
162
164
  - lib/mongoid/indexes.rb
165
+ - lib/mongoid/javascript.rb
166
+ - lib/mongoid/javascript/functions.yml
163
167
  - lib/mongoid/matchers.rb
164
168
  - lib/mongoid/matchers/all.rb
165
169
  - lib/mongoid/matchers/default.rb
@@ -216,9 +220,11 @@ files:
216
220
  - spec/unit/mongoid/associations/has_many_spec.rb
217
221
  - spec/unit/mongoid/associations/has_one_related_spec.rb
218
222
  - spec/unit/mongoid/associations/has_one_spec.rb
223
+ - spec/unit/mongoid/associations/meta_data_spec.rb
219
224
  - spec/unit/mongoid/associations/options_spec.rb
220
225
  - spec/unit/mongoid/associations_spec.rb
221
226
  - spec/unit/mongoid/attributes_spec.rb
227
+ - spec/unit/mongoid/caching_spec.rb
222
228
  - spec/unit/mongoid/callbacks_spec.rb
223
229
  - spec/unit/mongoid/collection_spec.rb
224
230
  - spec/unit/mongoid/collections/cyclic_iterator_spec.rb
@@ -272,6 +278,7 @@ files:
272
278
  - spec/unit/mongoid/finders_spec.rb
273
279
  - spec/unit/mongoid/identity_spec.rb
274
280
  - spec/unit/mongoid/indexes_spec.rb
281
+ - spec/unit/mongoid/javascript_spec.rb
275
282
  - spec/unit/mongoid/matchers/all_spec.rb
276
283
  - spec/unit/mongoid/matchers/default_spec.rb
277
284
  - spec/unit/mongoid/matchers/exists_spec.rb
@@ -355,9 +362,11 @@ test_files:
355
362
  - spec/unit/mongoid/associations/has_many_spec.rb
356
363
  - spec/unit/mongoid/associations/has_one_related_spec.rb
357
364
  - spec/unit/mongoid/associations/has_one_spec.rb
365
+ - spec/unit/mongoid/associations/meta_data_spec.rb
358
366
  - spec/unit/mongoid/associations/options_spec.rb
359
367
  - spec/unit/mongoid/associations_spec.rb
360
368
  - spec/unit/mongoid/attributes_spec.rb
369
+ - spec/unit/mongoid/caching_spec.rb
361
370
  - spec/unit/mongoid/callbacks_spec.rb
362
371
  - spec/unit/mongoid/collection_spec.rb
363
372
  - spec/unit/mongoid/collections/cyclic_iterator_spec.rb
@@ -411,6 +420,7 @@ test_files:
411
420
  - spec/unit/mongoid/finders_spec.rb
412
421
  - spec/unit/mongoid/identity_spec.rb
413
422
  - spec/unit/mongoid/indexes_spec.rb
423
+ - spec/unit/mongoid/javascript_spec.rb
414
424
  - spec/unit/mongoid/matchers/all_spec.rb
415
425
  - spec/unit/mongoid/matchers/default_spec.rb
416
426
  - spec/unit/mongoid/matchers/exists_spec.rb