mongoid 0.11.5 → 0.11.6

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,3 +1,21 @@
1
+ === 0.11.6
2
+ - Allow namespaced documents to default with:
3
+ "namespace_modelname"
4
+
5
+ - Fixed indexing of _type field to only happen on root
6
+ classes.
7
+
8
+ - Fixed creation of empty collections for embedded documents.
9
+
10
+ - Document.store_in now properly resets the collection
11
+ if the collection had already been accessed.
12
+
13
+ - Document.find(nil) now raises
14
+ Mongoid::Errors::InvalidOptions
15
+
16
+ === 0.11.5
17
+ - Removed dependency on mongo_ext, since latest version
18
+ was breaking on various operating systems.
1
19
  === 0.11.4
2
20
  - Fixed issue with dynamic fields: checking whether
3
21
  the document responded to the attribute's method
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.11.5
1
+ 0.11.6
@@ -15,14 +15,15 @@ module Mongoid #:nodoc:
15
15
  class_inheritable_accessor :defaults, :fields
16
16
 
17
17
  # The same collection is used for the entire class hierarchy.
18
- cattr_accessor :_collection, :collection_name, :embedded, :primary_key
18
+ cattr_accessor :_collection, :collection_name, :embedded, :primary_key, :indexed
19
19
 
20
20
  # Set the initial values. Defaults and fields get set to a
21
21
  # +HashWithIndifferentAccess+ while the collection name will get set to
22
22
  # the demodulized class.
23
+ self.collection_name = self.to_s.underscore.tableize.gsub("/", "_")
23
24
  self.defaults = {}.with_indifferent_access
24
25
  self.fields = {}.with_indifferent_access
25
- self.collection_name ||= self.to_s.demodulize.tableize
26
+ self.indexed = false
26
27
 
27
28
  attr_accessor :association_name, :_parent
28
29
  attr_reader :attributes, :new_record
@@ -32,12 +33,19 @@ module Mongoid #:nodoc:
32
33
  # Define all the callbacks that are accepted by the document.
33
34
  define_callbacks :before_create, :before_destroy, :before_save, :before_update, :before_validation
34
35
  define_callbacks :after_create, :after_destroy, :after_save, :after_update, :after_validation
35
-
36
- index :_type
37
36
  end
38
37
  end
39
38
 
40
39
  module ClassMethods
40
+ # Add the default indexes to the root document if they do not already
41
+ # exist. Currently this is only _type.
42
+ def add_indexes
43
+ unless indexed
44
+ self._collection.create_index(:_type, false)
45
+ self.indexed = true
46
+ end
47
+ end
48
+
41
49
  # Returns the collection associated with this +Document+. If the
42
50
  # document is embedded, there will be no collection associated
43
51
  # with it.
@@ -46,6 +54,7 @@ module Mongoid #:nodoc:
46
54
  def collection
47
55
  raise Errors::InvalidCollection.new(self) if embedded?
48
56
  self._collection ||= Mongoid.database.collection(self.collection_name)
57
+ add_indexes; self._collection
49
58
  end
50
59
 
51
60
  # return true if the +Document+ is embedded in another +Documnet+.
@@ -106,6 +115,7 @@ module Mongoid #:nodoc:
106
115
  # Macro for setting the collection name to store in.
107
116
  def store_in(name)
108
117
  self.collection_name = name.to_s
118
+ self._collection = Mongoid.database.collection(name.to_s)
109
119
  end
110
120
 
111
121
  # Returns all types to query for when using this class as the base.
@@ -13,7 +13,7 @@ module Mongoid #:nodoc
13
13
  end
14
14
  end
15
15
 
16
- # Raised when invalid options are passed into a constructor.
16
+ # Raised when invalid options are passed into a constructor or method.
17
17
  class InvalidOptions < RuntimeError; end
18
18
 
19
19
  # Raised when the database connection has not been set up.
@@ -9,7 +9,7 @@ module Mongoid #:nodoc:
9
9
  #
10
10
  # <tt>Person.all(:conditions => { :attribute => "value" })</tt>
11
11
  def all(*args)
12
- find(*args)
12
+ find(:all, *args)
13
13
  end
14
14
 
15
15
  # Returns a count of matching records in the database based on the
@@ -43,6 +43,7 @@ module Mongoid #:nodoc:
43
43
  #
44
44
  # <tt>Person.find(Mongo::ObjectID.new.to_s)</tt>
45
45
  def find(*args)
46
+ raise Errors::InvalidOptions.new("Calling Document#find with nil is invalid") if args[0].nil?
46
47
  type = args.delete_at(0) if args[0].is_a?(Symbol)
47
48
  criteria = Criteria.translate(self, *args)
48
49
  case type
@@ -61,7 +62,7 @@ module Mongoid #:nodoc:
61
62
  #
62
63
  # <tt>Person.first(:conditions => { :attribute => "value" })</tt>
63
64
  def first(*args)
64
- find(*args).one
65
+ find(:first, *args)
65
66
  end
66
67
 
67
68
  # Find the last +Document+ given the conditions.
@@ -72,7 +73,7 @@ module Mongoid #:nodoc:
72
73
  #
73
74
  # <tt>Person.last(:conditions => { :attribute => "value" })</tt>
74
75
  def last(*args)
75
- find(*args).last
76
+ find(:last, *args)
76
77
  end
77
78
 
78
79
  # Will execute a +Criteria+ based on the +DynamicFinder+ that gets
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.11.5"
8
+ s.version = "0.11.6"
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-01-04}
12
+ s.date = %q{2010-01-05}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -2,6 +2,11 @@ require "rubygems"
2
2
  require "ruby-prof"
3
3
  require "mongoid"
4
4
 
5
+ connection = Mongo::Connection.new
6
+ Mongoid.database = connection.db("mongoid_perf_test")
7
+
8
+ Mongoid.database.collection("people").drop
9
+
5
10
  class Person
6
11
  include Mongoid::Document
7
12
  include Mongoid::Timestamps
@@ -28,7 +33,7 @@ class Address
28
33
  field :city
29
34
  field :state
30
35
  field :post_code
31
- field :type
36
+ field :address_type
32
37
  belongs_to :person, :inverse_of => :address
33
38
  end
34
39
 
@@ -37,15 +42,10 @@ class Phone
37
42
  include Mongoid::Timestamps
38
43
  field :country_code, :type => Integer
39
44
  field :number
40
- field :type
45
+ field :phone_type
41
46
  belongs_to :person, :inverse_of => :phones
42
47
  end
43
48
 
44
- connection = Mongo::Connection.new
45
- Mongoid.database = connection.db("mongoid_perf_test")
46
-
47
- Mongoid.database.collection("people").drop
48
-
49
49
  RubyProf.start
50
50
 
51
51
  puts "Starting benchmark..."
@@ -71,4 +71,4 @@ result = RubyProf.stop
71
71
  printer = RubyProf::FlatPrinter.new(result)
72
72
  printer.print(STDOUT, 0)
73
73
 
74
- Mongoid.database.collection("people").drop
74
+ # Mongoid.database.collection("people").drop
@@ -16,6 +16,10 @@ describe Mongoid::Document do
16
16
 
17
17
  end
18
18
 
19
+ context "on a namespaced document" do
20
+ Medical::Patient.collection.name.should == "medical_patients"
21
+ end
22
+
19
23
  end
20
24
 
21
25
  describe "#new" do
@@ -230,6 +234,10 @@ describe Mongoid::Document do
230
234
  @owner.save
231
235
  end
232
236
 
237
+ after do
238
+ PetOwner.delete_all
239
+ end
240
+
233
241
  it "is a single object and not an array" do
234
242
  @from_db = PetOwner.find(@owner.id)
235
243
  @from_db.address.should == @address
@@ -332,6 +340,20 @@ describe Mongoid::Document do
332
340
 
333
341
  end
334
342
 
343
+ context ".store_in" do
344
+
345
+ after do
346
+ Canvas.store_in(:canvases)
347
+ end
348
+
349
+ it "switches the database collection" do
350
+ Canvas.collection.name.should == "canvases"
351
+ Canvas.store_in(:browsers)
352
+ Canvas.collection.name.should == "browsers"
353
+ end
354
+
355
+ end
356
+
335
357
  context "when has many exists through a has one" do
336
358
 
337
359
  before do
@@ -474,22 +496,22 @@ describe Mongoid::Document do
474
496
  end
475
497
 
476
498
  it "handles comparisons with todays date"do
477
- people = Person.select.where("this.dob < new Date()")
499
+ people = Person.where("this.dob < new Date()")
478
500
  people.first.should == @person
479
501
  end
480
502
 
481
503
  it "handles conparisons with a date range" do
482
- people = Person.select.where("new Date(1976, 10, 31) < this.dob && this.dob < new Date()")
504
+ people = Person.where("new Date(1976, 10, 31) < this.dob && this.dob < new Date()")
483
505
  people.first.should == @person
484
506
  end
485
507
 
486
508
  it "handles false comparisons in a date range" do
487
- people = Person.select.where("new Date(2005, 10, 31) < this.dob && this.dob < new Date()")
509
+ people = Person.where("new Date(2005, 10, 31) < this.dob && this.dob < new Date()")
488
510
  people.should be_empty
489
511
  end
490
512
 
491
513
  it "handles comparisons with date objects"do
492
- people = Person.select.where(:dob => { "$lt" => Date.today.midnight })
514
+ people = Person.where(:dob => { "$lt" => Date.today.midnight })
493
515
  people.first.should == @person
494
516
  end
495
517
 
@@ -13,6 +13,9 @@ Mongoid.database = connection.db("mongoid_test")
13
13
 
14
14
  Spec::Runner.configure do |config|
15
15
  config.mock_with :mocha
16
+ config.after :suite do
17
+ Mongoid.database.collections.each(&:drop)
18
+ end
16
19
  end
17
20
 
18
21
  class MixedDrink
@@ -255,3 +258,17 @@ end
255
258
  class Circle < Shape
256
259
  field :radius, :type => Integer, :default => 0
257
260
  end
261
+
262
+ # Namespacing test models:
263
+ module Medical
264
+ class Patient
265
+ include Mongoid::Document
266
+ field :name
267
+ has_many :prescriptions, :class_name => "Medical::Prescription"
268
+ end
269
+
270
+ class Prescription
271
+ include Mongoid::Document
272
+ field :name
273
+ end
274
+ end
@@ -35,6 +35,22 @@ describe Mongoid::Associations do
35
35
 
36
36
  end
37
37
 
38
+ context "when child and parent are namespaced" do
39
+
40
+ before do
41
+ @patient = Medical::Patient.new(:name => "Ridley")
42
+ @prescription = Medical::Prescription.new(:name => "Zoloft")
43
+ @patient.prescriptions << @prescription
44
+ @second = @patient.prescriptions.build(:name => "Codeine")
45
+ end
46
+
47
+ it "sets the correct association classes" do
48
+ @patient.prescriptions.first.should == @prescription
49
+ @patient.prescriptions.last.should == @second
50
+ end
51
+
52
+ end
53
+
38
54
  context "when setting a parent" do
39
55
 
40
56
  context "when the child is one level deep" do
@@ -3,12 +3,14 @@ require "spec_helper"
3
3
  describe Mongoid::Document do
4
4
 
5
5
  before do
6
- @collection = stub(:name => "people")
7
- @canvas_collection = stub(:name => "canvases")
8
6
  @database = mock
9
7
  Mongoid.stubs(:database).returns(@database)
8
+ @collection = stub(:name => "people")
9
+ @canvas_collection = stub(:name => "canvases")
10
10
  @database.stubs(:collection).with("people").returns(@collection)
11
11
  @database.stubs(:collection).with("canvases").returns(@canvas_collection)
12
+ @collection.stubs(:create_index).with(:_type, false)
13
+ @canvas_collection.stubs(:create_index).with(:_type, false)
12
14
  end
13
15
 
14
16
  after do
@@ -705,11 +707,9 @@ describe Mongoid::Document do
705
707
 
706
708
  context "on a parent class" do
707
709
 
708
- before do
710
+ it "sets the collection name and collection for the document" do
711
+ @database.expects(:collection).with("population").returns(@collection)
709
712
  Patient.store_in :population
710
- end
711
-
712
- it "sets the collection name for the document" do
713
713
  Patient.collection_name.should == "population"
714
714
  end
715
715
 
@@ -717,15 +717,13 @@ describe Mongoid::Document do
717
717
 
718
718
  context "on a subclass" do
719
719
 
720
- before do
721
- Firefox.store_in :browsers
722
- end
723
-
724
720
  after do
725
721
  Firefox.store_in :canvases
726
722
  end
727
723
 
728
724
  it "changes the collection name for the entire hierarchy" do
725
+ @database.expects(:collection).with("browsers").returns(@collection)
726
+ Firefox.store_in :browsers
729
727
  Canvas.collection_name.should == "browsers"
730
728
  end
731
729
 
@@ -95,6 +95,14 @@ describe Mongoid::Finders do
95
95
 
96
96
  end
97
97
 
98
+ context "when nil passed in" do
99
+
100
+ it "raises an error" do
101
+ lambda { Person.find(nil) }.should raise_error
102
+ end
103
+
104
+ end
105
+
98
106
  context "when finding first" do
99
107
 
100
108
  it "delegates to criteria" do
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: 0.11.5
4
+ version: 0.11.6
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-01-04 00:00:00 -05:00
12
+ date: 2010-01-05 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency