mongoid 0.10.6 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/Rakefile +3 -3
  2. data/VERSION +1 -1
  3. data/lib/mongoid.rb +2 -2
  4. data/lib/mongoid/associations.rb +1 -1
  5. data/lib/mongoid/associations/belongs_to.rb +1 -1
  6. data/lib/mongoid/associations/has_many.rb +8 -6
  7. data/lib/mongoid/associations/has_one.rb +6 -5
  8. data/lib/mongoid/commands.rb +94 -41
  9. data/lib/mongoid/commands/delete_all.rb +2 -3
  10. data/lib/mongoid/commands/deletion.rb +1 -1
  11. data/lib/mongoid/commands/destroy_all.rb +2 -0
  12. data/lib/mongoid/commands/save.rb +1 -1
  13. data/lib/mongoid/criteria.rb +1 -1
  14. data/lib/mongoid/document.rb +43 -32
  15. data/lib/mongoid/extensions.rb +3 -3
  16. data/lib/mongoid/extensions/hash/assimilation.rb +2 -3
  17. data/lib/mongoid/extensions/string/inflections.rb +1 -0
  18. data/mongoid.gemspec +13 -9
  19. data/perf/benchmark.rb +8 -4
  20. data/spec/integration/mongoid/commands_spec.rb +103 -0
  21. data/spec/integration/mongoid/document_spec.rb +13 -1
  22. data/spec/integration/mongoid/inheritance_spec.rb +105 -0
  23. data/spec/spec_helper.rb +24 -2
  24. data/spec/unit/mongoid/associations/belongs_to_spec.rb +3 -3
  25. data/spec/unit/mongoid/associations/has_many_spec.rb +92 -31
  26. data/spec/unit/mongoid/associations/has_one_spec.rb +57 -4
  27. data/spec/unit/mongoid/associations_spec.rb +4 -4
  28. data/spec/unit/mongoid/attributes_spec.rb +2 -2
  29. data/spec/unit/mongoid/commands/delete_all_spec.rb +4 -3
  30. data/spec/unit/mongoid/commands/delete_spec.rb +1 -1
  31. data/spec/unit/mongoid/commands/destroy_all_spec.rb +3 -2
  32. data/spec/unit/mongoid/commands/destroy_spec.rb +1 -1
  33. data/spec/unit/mongoid/commands/save_spec.rb +3 -3
  34. data/spec/unit/mongoid/commands_spec.rb +6 -6
  35. data/spec/unit/mongoid/criteria_spec.rb +45 -36
  36. data/spec/unit/mongoid/document_spec.rb +198 -29
  37. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
  38. data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +33 -8
  39. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -2
  40. data/spec/unit/mongoid/finders_spec.rb +1 -1
  41. data/spec/unit/mongoid/timestamps_spec.rb +1 -1
  42. metadata +9 -5
@@ -18,7 +18,7 @@ require "mongoid/extensions/string/inflections"
18
18
  require "mongoid/extensions/symbol/inflections"
19
19
  require "mongoid/extensions/time/conversions"
20
20
 
21
- class Array #:nodoc:
21
+ class Array #:nodoc
22
22
  include Mongoid::Extensions::Array::Accessors
23
23
  include Mongoid::Extensions::Array::Assimilation
24
24
  include Mongoid::Extensions::Array::Conversions
@@ -44,8 +44,8 @@ end
44
44
  class Hash #:nodoc
45
45
  include Mongoid::Extensions::Hash::Accessors
46
46
  include Mongoid::Extensions::Hash::Assimilation
47
- extend Mongoid::Extensions::Hash::Conversions
48
47
  include Mongoid::Extensions::Hash::CriteriaHelpers
48
+ extend Mongoid::Extensions::Hash::Conversions
49
49
  end
50
50
 
51
51
  class Integer #:nodoc
@@ -57,8 +57,8 @@ class Object #:nodoc:
57
57
  end
58
58
 
59
59
  class String #:nodoc
60
- extend Mongoid::Extensions::String::Conversions
61
60
  include Mongoid::Extensions::String::Inflections
61
+ extend Mongoid::Extensions::String::Conversions
62
62
  end
63
63
 
64
64
  class Symbol #:nodoc
@@ -17,9 +17,8 @@ module Mongoid #:nodoc:
17
17
  # <tt>{:first_name => "Hank", :last_name => "Moody"}.assimilate(name, options)</tt>
18
18
  #
19
19
  # Returns: The child +Document+.
20
- def assimilate(parent, options)
21
- klass = options.klass
22
- child = klass.instantiate(self)
20
+ def assimilate(parent, options, type = nil)
21
+ child = type ? type.instantiate(self.merge(:_type => type.name)) : options.klass.instantiate(self)
23
22
  child.assimilate(parent, options)
24
23
  end
25
24
  end
@@ -7,6 +7,7 @@ module Mongoid #:nodoc:
7
7
  ActiveSupport::Inflector.inflections do |inflect|
8
8
  inflect.singular("address", "address")
9
9
  inflect.singular("addresses", "address")
10
+ inflect.irregular("canvas", "canvases")
10
11
  end
11
12
 
12
13
  REVERSALS = {
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.10.6"
8
+ s.version = "0.11.0"
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{2009-12-29}
12
+ s.date = %q{2010-01-02}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -73,9 +73,11 @@ Gem::Specification.new do |s|
73
73
  "mongoid.gemspec",
74
74
  "perf/benchmark.rb",
75
75
  "spec/integration/mongoid/associations_spec.rb",
76
+ "spec/integration/mongoid/commands_spec.rb",
76
77
  "spec/integration/mongoid/criteria_spec.rb",
77
78
  "spec/integration/mongoid/document_spec.rb",
78
79
  "spec/integration/mongoid/finders_spec.rb",
80
+ "spec/integration/mongoid/inheritance_spec.rb",
79
81
  "spec/spec.opts",
80
82
  "spec/spec_helper.rb",
81
83
  "spec/unit/mongoid/associations/belongs_to_related_spec.rb",
@@ -123,16 +125,18 @@ Gem::Specification.new do |s|
123
125
  "spec/unit/mongoid/versioning_spec.rb",
124
126
  "spec/unit/mongoid_spec.rb"
125
127
  ]
126
- s.homepage = %q{http://github.com/durran/mongoid}
128
+ s.homepage = %q{http://mongoid.org}
127
129
  s.rdoc_options = ["--charset=UTF-8"]
128
130
  s.require_paths = ["lib"]
129
131
  s.rubygems_version = %q{1.3.5}
130
132
  s.summary = %q{ODM framework for MongoDB}
131
133
  s.test_files = [
132
134
  "spec/integration/mongoid/associations_spec.rb",
135
+ "spec/integration/mongoid/commands_spec.rb",
133
136
  "spec/integration/mongoid/criteria_spec.rb",
134
137
  "spec/integration/mongoid/document_spec.rb",
135
138
  "spec/integration/mongoid/finders_spec.rb",
139
+ "spec/integration/mongoid/inheritance_spec.rb",
136
140
  "spec/spec_helper.rb",
137
141
  "spec/unit/mongoid/associations/belongs_to_related_spec.rb",
138
142
  "spec/unit/mongoid/associations/belongs_to_spec.rb",
@@ -186,16 +190,16 @@ Gem::Specification.new do |s|
186
190
 
187
191
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
188
192
  s.add_runtime_dependency(%q<activesupport>, [">= 2.2.2"])
189
- s.add_runtime_dependency(%q<mongo>, [">= 0.18.1"])
190
- s.add_runtime_dependency(%q<mongo_ext>, [">= 0.18.1"])
193
+ s.add_runtime_dependency(%q<mongo>, [">= 0.18.2"])
194
+ s.add_runtime_dependency(%q<mongo_ext>, [">= 0.18.2"])
191
195
  s.add_runtime_dependency(%q<durran-validatable>, [">= 1.8.4"])
192
196
  s.add_runtime_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
193
197
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
194
198
  s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
195
199
  else
196
200
  s.add_dependency(%q<activesupport>, [">= 2.2.2"])
197
- s.add_dependency(%q<mongo>, [">= 0.18.1"])
198
- s.add_dependency(%q<mongo_ext>, [">= 0.18.1"])
201
+ s.add_dependency(%q<mongo>, [">= 0.18.2"])
202
+ s.add_dependency(%q<mongo_ext>, [">= 0.18.2"])
199
203
  s.add_dependency(%q<durran-validatable>, [">= 1.8.4"])
200
204
  s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
201
205
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
@@ -203,8 +207,8 @@ Gem::Specification.new do |s|
203
207
  end
204
208
  else
205
209
  s.add_dependency(%q<activesupport>, [">= 2.2.2"])
206
- s.add_dependency(%q<mongo>, [">= 0.18.1"])
207
- s.add_dependency(%q<mongo_ext>, [">= 0.18.1"])
210
+ s.add_dependency(%q<mongo>, [">= 0.18.2"])
211
+ s.add_dependency(%q<mongo_ext>, [">= 0.18.2"])
208
212
  s.add_dependency(%q<durran-validatable>, [">= 1.8.4"])
209
213
  s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
210
214
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
@@ -2,7 +2,8 @@ require "rubygems"
2
2
  require "ruby-prof"
3
3
  require "mongoid"
4
4
 
5
- class Person < Mongoid::Document
5
+ class Person
6
+ include Mongoid::Document
6
7
  include Mongoid::Timestamps
7
8
  include Mongoid::Versioning
8
9
  field :birth_date, :type => Date
@@ -11,7 +12,8 @@ class Person < Mongoid::Document
11
12
  has_many :phones
12
13
  end
13
14
 
14
- class Name < Mongoid::Document
15
+ class Name
16
+ include Mongoid::Document
15
17
  include Mongoid::Timestamps
16
18
  field :given
17
19
  field :family
@@ -19,7 +21,8 @@ class Name < Mongoid::Document
19
21
  belongs_to :person, :inverse_of => :name
20
22
  end
21
23
 
22
- class Address < Mongoid::Document
24
+ class Address
25
+ include Mongoid::Document
23
26
  include Mongoid::Timestamps
24
27
  field :street
25
28
  field :city
@@ -29,7 +32,8 @@ class Address < Mongoid::Document
29
32
  belongs_to :person, :inverse_of => :address
30
33
  end
31
34
 
32
- class Phone < Mongoid::Document
35
+ class Phone
36
+ include Mongoid::Document
33
37
  include Mongoid::Timestamps
34
38
  field :country_code, :type => Integer
35
39
  field :number
@@ -0,0 +1,103 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Commands do
4
+
5
+ before do
6
+ @person = Person.new(:title => "Sir")
7
+ end
8
+
9
+ after do
10
+ @person.delete
11
+ end
12
+
13
+ describe "#delete" do
14
+
15
+ before do
16
+ @person.save
17
+ end
18
+
19
+ it "deletes the document" do
20
+ @person.delete
21
+ lambda { Person.find(@person.id) }.should raise_error
22
+ end
23
+
24
+ it "returns true" do
25
+ @person.delete.should be_true
26
+ end
27
+
28
+ end
29
+
30
+ describe "#destroy" do
31
+
32
+ before do
33
+ @person.save
34
+ end
35
+
36
+ it "deletes the document" do
37
+ @person.destroy
38
+ lambda { Person.find(@person.id) }.should raise_error
39
+ end
40
+
41
+ it "returns true" do
42
+ @person.destroy.should be_true
43
+ end
44
+
45
+ end
46
+
47
+ describe "#save" do
48
+
49
+ context "when validation passes" do
50
+
51
+ it "returns true" do
52
+ @person.save.should be_true
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ describe "#update_attributes" do
60
+
61
+ context "when validation passes" do
62
+
63
+ it "returns true" do
64
+ @person.update_attributes(:title => "Blah").should be_true
65
+ end
66
+
67
+ it "saves the attributes" do
68
+ @person.update_attributes(:title => "Blah")
69
+ @from_db = Person.find(@person.id)
70
+ @from_db.title.should == "Blah"
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ describe ".create" do
78
+
79
+ it "saves and returns the document" do
80
+ person = Person.create(:title => "Sensei")
81
+ person.should be_a_kind_of(Person)
82
+ person.should_not be_a_new_record
83
+ end
84
+
85
+ end
86
+
87
+ describe ".delete_all" do
88
+
89
+ it "returns true" do
90
+ Person.delete_all.should be_true
91
+ end
92
+
93
+ end
94
+
95
+ describe ".destroy_all" do
96
+
97
+ it "returns true" do
98
+ Person.destroy_all.should be_true
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -6,6 +6,18 @@ describe Mongoid::Document do
6
6
  Mongoid.database.collection(:people).drop
7
7
  end
8
8
 
9
+ describe ".collection" do
10
+
11
+ context "on a subclass of a root document" do
12
+
13
+ it "returns the root document collection" do
14
+ Browser.collection.should == Canvas.collection
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
9
21
  describe "#new" do
10
22
 
11
23
  it "gets a new or current database connection" do
@@ -313,7 +325,7 @@ describe Mongoid::Document do
313
325
  end
314
326
 
315
327
  it "returns false" do
316
- @comment.save.should_not be_valid
328
+ @comment.should_not be_valid
317
329
  end
318
330
 
319
331
  end
@@ -0,0 +1,105 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Document do
4
+
5
+ context "when document is a subclass of a root class" do
6
+
7
+ before do
8
+ Browser.delete_all
9
+ @browser = Browser.create(:version => 3, :name => "Test")
10
+ end
11
+
12
+ it "saves in the same collection as the root" do
13
+ collection = Mongoid.database.collection("canvases")
14
+ attributes = collection.find({ :name => "Test"}, {}).first
15
+ attributes["version"].should == 3
16
+ attributes["name"].should == "Test"
17
+ attributes["_type"].should == "Browser"
18
+ attributes["_id"].should == @browser.id
19
+ end
20
+
21
+ end
22
+
23
+ context "when document is a subclass of a subclass" do
24
+
25
+ before do
26
+ Firefox.delete_all
27
+ @firefox = Firefox.create(:version => 2, :name => "Testy")
28
+ end
29
+
30
+ it "saves in the same collection as the root" do
31
+ collection = Mongoid.database.collection("canvases")
32
+ attributes = collection.find({ :name => "Testy"}, {}).first
33
+ attributes["version"].should == 2
34
+ attributes["name"].should == "Testy"
35
+ attributes["_type"].should == "Firefox"
36
+ attributes["_id"].should == @firefox.id
37
+ end
38
+
39
+ end
40
+
41
+ context "when document has associations" do
42
+
43
+ before do
44
+ Firefox.delete_all
45
+ @firefox = Firefox.new(:name => "firefox")
46
+ @writer = HtmlWriter.new(:speed => 100)
47
+ @circle = Circle.new(:radius => 50)
48
+ @square = Square.new(:width => 300, :height => 150)
49
+ @firefox.writer = @writer
50
+ @firefox.shapes << [ @circle, @square ]
51
+ @firefox.save
52
+ end
53
+
54
+ after do
55
+ Firefox.delete_all
56
+ end
57
+
58
+ it "properly saves a has one subclass" do
59
+ from_db = Firefox.find(@firefox.id)
60
+ from_db.should be_a_kind_of(Firefox)
61
+ from_db.writer.should be_a_kind_of(HtmlWriter)
62
+ from_db.writer.should == @writer
63
+ end
64
+
65
+ it "properly saves a has many subclass" do
66
+ from_db = Firefox.find(@firefox.id)
67
+ from_db.shapes.first.should == @circle
68
+ from_db.shapes.first.should be_a_kind_of(Circle)
69
+ from_db.shapes.last.should == @square
70
+ from_db.shapes.last.should be_a_kind_of(Square)
71
+ end
72
+
73
+ it "properly sets up the belongs to" do
74
+ from_db = Firefox.find(@firefox.id)
75
+ circle = from_db.shapes.first
76
+ circle.should == @circle
77
+ circle.canvas.should == @firefox
78
+ end
79
+
80
+ end
81
+
82
+ context "deleting subclasses" do
83
+
84
+ before do
85
+ @firefox = Firefox.create(:name => "firefox")
86
+ @browser = Browser.create(:name => "browser")
87
+ @canvas = Canvas.create(:name => "canvas")
88
+ end
89
+
90
+ after do
91
+ Firefox.delete_all
92
+ Browser.delete_all
93
+ Canvas.delete_all
94
+ end
95
+
96
+ it "deletes from the parent class collection" do
97
+ @firefox.delete
98
+ Firefox.count.should == 0
99
+ Browser.count.should == 1
100
+ Canvas.count.should == 1
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -181,7 +181,7 @@ end
181
181
 
182
182
  class Patient
183
183
  include Mongoid::Document
184
- collection_name "population"
184
+ store_in :inpatient
185
185
  end
186
186
 
187
187
  if RUBY_VERSION == '1.8.6'
@@ -201,8 +201,9 @@ end
201
201
  # Inhertiance test models:
202
202
  class Canvas
203
203
  include Mongoid::Document
204
- field :size, :type => Integer, :default => 0
204
+ field :name
205
205
  has_many :shapes
206
+ has_one :writer
206
207
 
207
208
  def render
208
209
  shapes.each { |shape| render }
@@ -210,18 +211,39 @@ class Canvas
210
211
  end
211
212
 
212
213
  class Browser < Canvas
214
+ field :version, :type => Integer
213
215
  def render; end
214
216
  end
215
217
 
216
218
  class Firefox < Browser
219
+ field :user_agent
217
220
  def render; end
218
221
  end
219
222
 
223
+ class Writer
224
+ include Mongoid::Document
225
+ field :speed, :type => Integer, :default => 0
226
+
227
+ belongs_to :canvas, :inverse_of => :writer
228
+
229
+ def write; end
230
+ end
231
+
232
+ class HtmlWriter < Writer
233
+ def write; end
234
+ end
235
+
236
+ class PdfWriter < Writer
237
+ def write; end
238
+ end
239
+
220
240
  class Shape
221
241
  include Mongoid::Document
222
242
  field :x, :type => Integer, :default => 0
223
243
  field :y, :type => Integer, :default => 0
224
244
 
245
+ belongs_to :canvas, :inverse_of => :shapes
246
+
225
247
  def render; end
226
248
  end
227
249