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
@@ -27,7 +27,7 @@ module Mongoid #:nodoc:
27
27
 
28
28
  # Set the _type field on the document.
29
29
  def type(doc)
30
- doc._type = doc.class.name
30
+ doc._type = doc.class.name if Mongoid.persist_types
31
31
  end
32
32
 
33
33
  # Generates the composite key for a document.
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ class Javascript
4
+ # Constant for the file that defines all the js functions.
5
+ FUNCTIONS = File.join(File.dirname(__FILE__), "javascript", "functions.yml")
6
+
7
+ # Load the javascript functions and define a class method for each one,
8
+ # that memoizes the value.
9
+ #
10
+ # Example:
11
+ #
12
+ # <tt>Mongoid::Javascript.aggregate</tt>
13
+ YAML.load(File.read(FUNCTIONS)).each_pair do |key, function|
14
+ (class << self; self; end).class_eval <<-EOT
15
+ def #{key}
16
+ @#{key} ||= "#{function}"
17
+ end
18
+ EOT
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ aggregate:
2
+ "function(obj, prev) {
3
+ prev.count++;
4
+ }"
5
+
6
+ group:
7
+ "function(obj, prev) {
8
+ prev.group.push(obj);
9
+ }"
10
+
11
+ max:
12
+ "function(obj, prev) {
13
+ if (prev.max == 'start') {
14
+ prev.max = obj.[field];
15
+ }
16
+ if (prev.max < obj.[field]) {
17
+ prev.max = obj.[field];
18
+ }
19
+ }"
20
+
21
+ min:
22
+ "function(obj, prev) {
23
+ if (prev.min == 'start') {
24
+ prev.min = obj.[field];
25
+ }
26
+ if (prev.min > obj.[field]) {
27
+ prev.min = obj.[field];
28
+ }
29
+ }"
30
+
31
+ sum:
32
+ "function(obj, prev) {
33
+ if (prev.sum == 'start') {
34
+ prev.sum = 0;
35
+ }
36
+ prev.sum += obj.[field];
37
+ }"
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.2.7"
8
+ s.version = "1.2.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2010-02-18}
12
+ s.date = %q{2010-02-25}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
@@ -31,9 +31,11 @@ Gem::Specification.new do |s|
31
31
  "lib/mongoid/associations/has_many_related.rb",
32
32
  "lib/mongoid/associations/has_one.rb",
33
33
  "lib/mongoid/associations/has_one_related.rb",
34
+ "lib/mongoid/associations/meta_data.rb",
34
35
  "lib/mongoid/associations/options.rb",
35
36
  "lib/mongoid/associations/proxy.rb",
36
37
  "lib/mongoid/attributes.rb",
38
+ "lib/mongoid/caching.rb",
37
39
  "lib/mongoid/callbacks.rb",
38
40
  "lib/mongoid/collection.rb",
39
41
  "lib/mongoid/collections/cyclic_iterator.rb",
@@ -94,6 +96,8 @@ Gem::Specification.new do |s|
94
96
  "lib/mongoid/finders.rb",
95
97
  "lib/mongoid/identity.rb",
96
98
  "lib/mongoid/indexes.rb",
99
+ "lib/mongoid/javascript.rb",
100
+ "lib/mongoid/javascript/functions.yml",
97
101
  "lib/mongoid/matchers.rb",
98
102
  "lib/mongoid/matchers/all.rb",
99
103
  "lib/mongoid/matchers/default.rb",
@@ -150,9 +154,11 @@ Gem::Specification.new do |s|
150
154
  "spec/unit/mongoid/associations/has_many_spec.rb",
151
155
  "spec/unit/mongoid/associations/has_one_related_spec.rb",
152
156
  "spec/unit/mongoid/associations/has_one_spec.rb",
157
+ "spec/unit/mongoid/associations/meta_data_spec.rb",
153
158
  "spec/unit/mongoid/associations/options_spec.rb",
154
159
  "spec/unit/mongoid/associations_spec.rb",
155
160
  "spec/unit/mongoid/attributes_spec.rb",
161
+ "spec/unit/mongoid/caching_spec.rb",
156
162
  "spec/unit/mongoid/callbacks_spec.rb",
157
163
  "spec/unit/mongoid/collection_spec.rb",
158
164
  "spec/unit/mongoid/collections/cyclic_iterator_spec.rb",
@@ -206,6 +212,7 @@ Gem::Specification.new do |s|
206
212
  "spec/unit/mongoid/finders_spec.rb",
207
213
  "spec/unit/mongoid/identity_spec.rb",
208
214
  "spec/unit/mongoid/indexes_spec.rb",
215
+ "spec/unit/mongoid/javascript_spec.rb",
209
216
  "spec/unit/mongoid/matchers/all_spec.rb",
210
217
  "spec/unit/mongoid/matchers/default_spec.rb",
211
218
  "spec/unit/mongoid/matchers/exists_spec.rb",
@@ -267,9 +274,11 @@ Gem::Specification.new do |s|
267
274
  "spec/unit/mongoid/associations/has_many_spec.rb",
268
275
  "spec/unit/mongoid/associations/has_one_related_spec.rb",
269
276
  "spec/unit/mongoid/associations/has_one_spec.rb",
277
+ "spec/unit/mongoid/associations/meta_data_spec.rb",
270
278
  "spec/unit/mongoid/associations/options_spec.rb",
271
279
  "spec/unit/mongoid/associations_spec.rb",
272
280
  "spec/unit/mongoid/attributes_spec.rb",
281
+ "spec/unit/mongoid/caching_spec.rb",
273
282
  "spec/unit/mongoid/callbacks_spec.rb",
274
283
  "spec/unit/mongoid/collection_spec.rb",
275
284
  "spec/unit/mongoid/collections/cyclic_iterator_spec.rb",
@@ -323,6 +332,7 @@ Gem::Specification.new do |s|
323
332
  "spec/unit/mongoid/finders_spec.rb",
324
333
  "spec/unit/mongoid/identity_spec.rb",
325
334
  "spec/unit/mongoid/indexes_spec.rb",
335
+ "spec/unit/mongoid/javascript_spec.rb",
326
336
  "spec/unit/mongoid/matchers/all_spec.rb",
327
337
  "spec/unit/mongoid/matchers/default_spec.rb",
328
338
  "spec/unit/mongoid/matchers/exists_spec.rb",
@@ -12,9 +12,9 @@ describe Mongoid::Attributes do
12
12
  Person.delete_all
13
13
  end
14
14
 
15
- it "the field should not exist" do
15
+ it "the field should exist with a nil value" do
16
16
  from_db = Person.find(@person.id)
17
- from_db.attributes.has_key?(:score).should be_false
17
+ from_db.attributes.has_key?(:score).should be_true
18
18
  end
19
19
 
20
20
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Mongoid::Commands do
4
4
 
5
5
  before do
6
- @person = Person.new(:title => "Sir", :ssn => "6969696")
6
+ @person = Person.new(:title => "Sir", :ssn => "6969696", :pets => true)
7
7
  end
8
8
 
9
9
  after do
@@ -188,9 +188,11 @@ describe Mongoid::Commands do
188
188
  end
189
189
 
190
190
  it "saves the attributes" do
191
- @person.update_attributes(:ssn => "555-55-1235", :title => "Blah")
191
+ @person.update_attributes(:ssn => "555-55-1235", :pets => false, :title => nil)
192
192
  @from_db = Person.find(@person.id)
193
- @from_db.title.should == "Blah"
193
+ @from_db.ssn.should == "555-55-1235"
194
+ @from_db.pets.should == false
195
+ @from_db.title.should be_nil
194
196
  end
195
197
 
196
198
  end
@@ -50,6 +50,33 @@ describe Mongoid::Criteria do
50
50
  end
51
51
  end
52
52
 
53
+ describe "#in" do
54
+
55
+ context "when searching nil values" do
56
+
57
+ before do
58
+ @person = Person.create(:title => nil)
59
+ end
60
+
61
+ it "returns the correct document" do
62
+ from_db = Person.criteria.in(:title => [ true, false, nil ]).first
63
+ from_db.should == @person
64
+ end
65
+ end
66
+
67
+ context "when searching false values" do
68
+
69
+ before do
70
+ @person = Person.create(:terms => false)
71
+ end
72
+
73
+ it "returns the correct document" do
74
+ from_db = Person.criteria.in(:terms => [ true, false, nil ]).first
75
+ from_db.should == @person
76
+ end
77
+ end
78
+ end
79
+
53
80
  describe "#max" do
54
81
 
55
82
  context "without results" do
@@ -3,4 +3,5 @@ class Game
3
3
  field :high_score, :default => 500
4
4
  field :score, :type => Integer, :default => 0
5
5
  belongs_to_related :person
6
- end
6
+ enslave and cache
7
+ end
@@ -12,4 +12,4 @@ class Post
12
12
  where(:created_at => { "$lt" => 30.days.ago })
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -11,7 +11,11 @@ describe Mongoid::Associations::HasManyRelated do
11
11
  end
12
12
 
13
13
  let(:options) do
14
- Mongoid::Associations::Options.new(:name => :posts, :extend => block)
14
+ Mongoid::Associations::Options.new(
15
+ :name => :posts,
16
+ :foreign_key => "person_id",
17
+ :extend => block
18
+ )
15
19
  end
16
20
 
17
21
  describe "#<<" do
@@ -0,0 +1,88 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Associations::MetaData do
4
+
5
+ before do
6
+ @extension = lambda { "Test" }
7
+ end
8
+
9
+ let(:association) do
10
+ Mongoid::Associations::HasManyRelated
11
+ end
12
+
13
+ let(:options) do
14
+ Mongoid::Associations::Options.new(
15
+ :name => :games,
16
+ :extend => @extension,
17
+ :foreign_key => "person_id",
18
+ :inverse_of => :people
19
+ )
20
+ end
21
+
22
+ let(:metadata) do
23
+ Mongoid::Associations::MetaData.new(association, options)
24
+ end
25
+
26
+ describe "#extension" do
27
+
28
+ it "delegates to the options" do
29
+ metadata.extension.should == @extension
30
+ end
31
+ end
32
+
33
+ describe "#foreign_key" do
34
+
35
+ it "delegates to the options" do
36
+ metadata.foreign_key.should == "person_id"
37
+ end
38
+ end
39
+
40
+ describe "#inverse_of" do
41
+
42
+ it "delegates to the options" do
43
+ metadata.inverse_of.should == :people
44
+ end
45
+ end
46
+
47
+ describe "#klass" do
48
+
49
+ it "returns the associated klass" do
50
+ metadata.klass.should == Game
51
+ end
52
+ end
53
+
54
+ describe "#macro" do
55
+
56
+ it "returns the association macro" do
57
+ metadata.macro.should == :has_many_related
58
+ end
59
+ end
60
+
61
+ describe "#name" do
62
+
63
+ it "delegates to the options" do
64
+ metadata.name.should == "games"
65
+ end
66
+ end
67
+
68
+ describe "#options" do
69
+
70
+ it "returns the association options" do
71
+ metadata.options.should == options
72
+ end
73
+ end
74
+
75
+ describe "#polymorphic" do
76
+
77
+ it "delegates to the options" do
78
+ metadata.polymorphic.should be_false
79
+ end
80
+ end
81
+
82
+ describe "#association" do
83
+
84
+ it "returns the association type" do
85
+ metadata.association.should == Mongoid::Associations::HasManyRelated
86
+ end
87
+ end
88
+ end
@@ -75,15 +75,29 @@ describe Mongoid::Associations::Options do
75
75
 
76
76
  describe "#foreign_key" do
77
77
 
78
- before do
79
- @attributes = { :name => :game }
80
- @options = Mongoid::Associations::Options.new(@attributes)
81
- end
78
+ context "when no custom key defined" do
79
+
80
+ before do
81
+ @attributes = { :name => :posts }
82
+ @options = Mongoid::Associations::Options.new(@attributes)
83
+ end
82
84
 
83
- it "returns the association foreign_key" do
84
- @options.foreign_key.should == "game_id"
85
+ it "returns the association foreign_key" do
86
+ @options.foreign_key.should == "post_id"
87
+ end
85
88
  end
86
89
 
90
+ context "when a custom key is defined" do
91
+
92
+ before do
93
+ @attributes = { :name => :posts, :foreign_key => "blog_post_id" }
94
+ @options = Mongoid::Associations::Options.new(@attributes)
95
+ end
96
+
97
+ it "returns the custom foreign_key" do
98
+ @options.foreign_key.should == "blog_post_id"
99
+ end
100
+ end
87
101
  end
88
102
 
89
103
  describe "#inverse_of" do
@@ -146,19 +160,6 @@ describe Mongoid::Associations::Options do
146
160
 
147
161
  end
148
162
 
149
- describe "#parent_key" do
150
-
151
- before do
152
- @attributes = { :parent_key => :person_id }
153
- @options = Mongoid::Associations::Options.new(@attributes)
154
- end
155
-
156
- it "returns the parent's foreign key value" do
157
- @options.parent_key.should == :person_id
158
- end
159
-
160
- end
161
-
162
163
  describe "#polymorphic" do
163
164
 
164
165
  context "when attribute provided" do
@@ -94,6 +94,17 @@ describe Mongoid::Associations do
94
94
 
95
95
  end
96
96
 
97
+ describe "#associations" do
98
+
99
+ before do
100
+ @person = Person.new
101
+ end
102
+
103
+ it "is a hash with name keys and meta data values" do
104
+ @person.associations["addresses"].should be_a_kind_of(Mongoid::Associations::MetaData)
105
+ end
106
+ end
107
+
97
108
  describe ".belongs_to" do
98
109
 
99
110
  it "creates a reader for the association" do
@@ -429,6 +440,10 @@ describe Mongoid::Associations do
429
440
  @game.should respond_to(:person)
430
441
  end
431
442
 
443
+ it "defaults the foreign_key option to the name_id" do
444
+ @game.associations["person"].foreign_key.should == "person_id"
445
+ end
446
+
432
447
  context "when document is root level" do
433
448
 
434
449
  it "puts an index on the foreign key" do
@@ -494,15 +509,34 @@ describe Mongoid::Associations do
494
509
 
495
510
  context "when associations exist" do
496
511
 
497
- before do
498
- @related = stub(:id => "100", :person= => true)
499
- @person = Person.new
500
- @person.posts = [@related]
512
+ context "when the document is a new record" do
513
+
514
+ before do
515
+ @related = stub(:id => "100", :person= => true)
516
+ @person = Person.new
517
+ @person.posts = [@related]
518
+ end
519
+
520
+ it "saves each association" do
521
+ @related.expects(:save).returns(@related)
522
+ @person.update_associations(:posts)
523
+ end
524
+
501
525
  end
502
526
 
503
- it "saves each association" do
504
- @related.expects(:save).returns(@related)
505
- @person.update_associations(:posts)
527
+ context "when the document is not new" do
528
+
529
+ before do
530
+ @related = stub(:id => "100", :person= => true)
531
+ @person = Person.new
532
+ @person.instance_variable_set(:@new_record, false)
533
+ @person.posts = [@related]
534
+ end
535
+
536
+ it "does not save each association" do
537
+ @person.update_associations(:posts)
538
+ end
539
+
506
540
  end
507
541
 
508
542
  end