diff --git a/lib/mongo_mapper.rb b/lib/mongo_mapper.rb index 87753abd5..feab900b4 100644 --- a/lib/mongo_mapper.rb +++ b/lib/mongo_mapper.rb @@ -20,23 +20,28 @@ def initialize(document) end end + # @api public def self.connection @@connection ||= Mongo::Connection.new end - + + # @api public def self.connection=(new_connection) @@connection = new_connection end + # @api public def self.logger connection.logger end - + + # @api public def self.database=(name) @@database = nil @@database_name = name end - + + # @api public def self.database if @@database_name.blank? raise 'You forgot to set the default database name: MongoMapper.database = "foobar"' @@ -45,14 +50,17 @@ def self.database @@database ||= MongoMapper.connection.db(@@database_name) end + # @api private def self.ensured_indexes @@ensured_indexes ||= [] end + # @api private def self.ensure_index(klass, keys, options={}) ensured_indexes << {:klass => klass, :keys => keys, :options => options} end + # @api public def self.ensure_indexes! ensured_indexes.each do |index| unique = index[:options].delete(:unique) @@ -60,6 +68,7 @@ def self.ensure_indexes! end end + # @api private module Finders def dynamic_find(finder, args) attributes = {} @@ -83,6 +92,16 @@ def dynamic_find(finder, args) end end end + + # @api private + def self.use_time_zone? + Time.respond_to?(:zone) && Time.zone ? true : false + end + + # @api private + def self.time_class + use_time_zone? ? Time.zone : Time + end end require 'mongo_mapper/support' diff --git a/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb b/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb index 55ab0e972..94267886c 100644 --- a/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +++ b/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb @@ -4,7 +4,7 @@ class BelongsToPolymorphicProxy < Proxy def replace(doc) if doc doc.save if doc.new? - id, type = doc.id, doc.class.name + id, type = doc._id, doc.class.name end @owner.send("#{@association.foreign_key}=", id) diff --git a/lib/mongo_mapper/associations/belongs_to_proxy.rb b/lib/mongo_mapper/associations/belongs_to_proxy.rb index dca435ec5..0a3cae161 100644 --- a/lib/mongo_mapper/associations/belongs_to_proxy.rb +++ b/lib/mongo_mapper/associations/belongs_to_proxy.rb @@ -4,7 +4,7 @@ class BelongsToProxy < Proxy def replace(doc) if doc doc.save if doc.new? - id = doc.id + id = doc._id end @owner.send("#{@association.foreign_key}=", id) diff --git a/lib/mongo_mapper/associations/many_documents_as_proxy.rb b/lib/mongo_mapper/associations/many_documents_as_proxy.rb index 19dd8ff4d..7f464e1af 100644 --- a/lib/mongo_mapper/associations/many_documents_as_proxy.rb +++ b/lib/mongo_mapper/associations/many_documents_as_proxy.rb @@ -3,13 +3,13 @@ module Associations class ManyDocumentsAsProxy < ManyDocumentsProxy protected def scoped_conditions - {as_type_name => @owner.class.name, as_id_name => @owner.id} + {as_type_name => @owner.class.name, as_id_name => @owner._id} end def apply_scope(doc) ensure_owner_saved doc.send("#{as_type_name}=", @owner.class.name) - doc.send("#{as_id_name}=", @owner.id) + doc.send("#{as_id_name}=", @owner._id) doc end diff --git a/lib/mongo_mapper/associations/many_documents_proxy.rb b/lib/mongo_mapper/associations/many_documents_proxy.rb index dbe13d37c..bdeb44c22 100644 --- a/lib/mongo_mapper/associations/many_documents_proxy.rb +++ b/lib/mongo_mapper/associations/many_documents_proxy.rb @@ -98,7 +98,7 @@ def method_missing(method, *args) protected def scoped_conditions - {self.foreign_key => @owner.id} + {self.foreign_key => @owner._id} end def scoped_options(options) @@ -115,7 +115,7 @@ def ensure_owner_saved def apply_scope(doc) ensure_owner_saved - doc.send("#{self.foreign_key}=", @owner.id) + doc.send("#{self.foreign_key}=", @owner._id) doc end diff --git a/lib/mongo_mapper/associations/many_embedded_proxy.rb b/lib/mongo_mapper/associations/many_embedded_proxy.rb index 8e9cd5f39..6b90ff8ba 100644 --- a/lib/mongo_mapper/associations/many_embedded_proxy.rb +++ b/lib/mongo_mapper/associations/many_embedded_proxy.rb @@ -13,9 +13,10 @@ def build(attributes={}) doc end + # TODO: test that both string and oid version work def find(id) load_target - @target.detect { |item| item.id == id } + @target.detect { |item| item.id == id || item._id == id } end def <<(*docs) diff --git a/lib/mongo_mapper/document.rb b/lib/mongo_mapper/document.rb index 531155f3a..5d3a3e50d 100644 --- a/lib/mongo_mapper/document.rb +++ b/lib/mongo_mapper/document.rb @@ -442,12 +442,12 @@ def save! def destroy return false if frozen? - self.class.delete(id) unless new? + self.class.delete(_id) unless new? freeze end def reload - self.class.find(id) + self.class.find(_id) end private @@ -463,7 +463,7 @@ def create def assign_id if read_attribute(:_id).blank? - write_attribute(:_id, Mongo::ObjectID.new.to_s) + write_attribute :_id, Mongo::ObjectID.new end end diff --git a/lib/mongo_mapper/embedded_document.rb b/lib/mongo_mapper/embedded_document.rb index 6e4896ebe..7d6ee1e3e 100644 --- a/lib/mongo_mapper/embedded_document.rb +++ b/lib/mongo_mapper/embedded_document.rb @@ -16,7 +16,7 @@ def self.included(model) extend Validations::Macros - key :_id, String + key :_id, Mongo::ObjectID attr_accessor :_root_document end end @@ -61,6 +61,11 @@ def key(*args) key end + + def object_id_key?(name) + key = keys[name.to_s] + key && key.type == Mongo::ObjectID + end def embeddable? !self.ancestors.include?(Document) @@ -193,7 +198,7 @@ def initialize(attrs={}) if self.class.embeddable? if read_attribute(:_id).blank? - write_attribute :_id, Mongo::ObjectID.new.to_s + write_attribute :_id, Mongo::ObjectID.new @new_document = true else @new_document = false @@ -275,15 +280,20 @@ def []=(name, value) end def ==(other) - other.is_a?(self.class) && id == other.id + other.is_a?(self.class) && _id == other._id end def id - read_attribute(:_id) + read_attribute(:_id).to_s end def id=(value) @using_custom_id = true + + if self.class.object_id_key?(:_id) + value = value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value + end + write_attribute :_id, value end diff --git a/lib/mongo_mapper/finder_options.rb b/lib/mongo_mapper/finder_options.rb index 5a163b40d..f865278a2 100644 --- a/lib/mongo_mapper/finder_options.rb +++ b/lib/mongo_mapper/finder_options.rb @@ -8,17 +8,7 @@ module MongoMapper # useful for understanding how MongoMapper handles the parsing of finder # conditions and options. # - # @private - class FinderOperator - def initialize(field, operator) - @field, @operator = field, operator - end - - def to_criteria(value) - {@field => {@operator => value}} - end - end - + # @private class FinderOptions OptionKeys = [:fields, :select, :skip, :offset, :limit, :sort, :order] @@ -72,10 +62,16 @@ def to_mongo_criteria(conditions, parent_key=nil) conditions.each_pair do |field, value| field = normalized_field(field) + + if @model.object_id_key?(field) && value.is_a?(String) + value = Mongo::ObjectID.from_string(value) + end + if field.is_a?(FinderOperator) criteria.merge!(field.to_criteria(value)) next end + case value when Array operator_present = field.to_s =~ /^\$/ @@ -128,4 +124,14 @@ def to_mongo_sort_piece(str) [field, direction] end end + + class FinderOperator + def initialize(field, operator) + @field, @operator = field, operator + end + + def to_criteria(value) + {@field => {@operator => value}} + end + end end diff --git a/lib/mongo_mapper/support.rb b/lib/mongo_mapper/support.rb index e94125314..e0ed6aa6c 100644 --- a/lib/mongo_mapper/support.rb +++ b/lib/mongo_mapper/support.rb @@ -138,33 +138,21 @@ class Symbol end end -class Time +class Time def self.to_mongo(value) if value.nil? || value == '' nil else - to_utc_time(value) + time = MongoMapper.time_class.parse(value.to_s) + time && time.utc end end def self.from_mongo(value) - if Time.respond_to?(:zone) && Time.zone && value.present? + if MongoMapper.use_time_zone? && value.present? value.in_time_zone(Time.zone) else value end end - - def self.to_utc_time(value) - to_local_time(value).try(:utc) - end - - # make sure we have a time and that it is local - def self.to_local_time(value) - if Time.respond_to?(:zone) && Time.zone - Time.zone.parse(value.to_s) - else - Time.parse(value.to_s) - end - end end \ No newline at end of file diff --git a/lib/mongo_mapper/validations.rb b/lib/mongo_mapper/validations.rb index 2e9c38dc4..ab3459590 100644 --- a/lib/mongo_mapper/validations.rb +++ b/lib/mongo_mapper/validations.rb @@ -15,7 +15,7 @@ def valid?(instance) return true if allow_blank && value.blank? base_conditions = case_sensitive ? {self.attribute => value} : {} doc = instance.class.first(base_conditions.merge(scope_conditions(instance)).merge(where_conditions(instance))) - doc.nil? || instance.id == doc.id + doc.nil? || instance._id == doc._id end def message(instance) diff --git a/test/functional/associations/test_belongs_to_polymorphic_proxy.rb b/test/functional/associations/test_belongs_to_polymorphic_proxy.rb index c9d506fb7..181cf50d9 100644 --- a/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +++ b/test/functional/associations/test_belongs_to_polymorphic_proxy.rb @@ -19,11 +19,11 @@ def setup status.target = project status.save.should be_true - from_db = Status.find(status.id) - from_db.target.nil?.should be_false - from_db.target_id.should == project.id - from_db.target_type.should == "Project" - from_db.target.name.should == "mongomapper" + status = status.reload + status.target.nil?.should be_false + status.target_id.should == project._id + status.target_type.should == "Project" + status.target.name.should == "mongomapper" end should "unset the association" do @@ -32,11 +32,11 @@ def setup status.target = project status.save.should be_true - from_db = Status.find(status.id) - from_db.target = nil - from_db.target_type.nil?.should be_true - from_db.target_id.nil?.should be_true - from_db.target.nil?.should be_true + status = status.reload + status.target = nil + status.target_type.nil?.should be_true + status.target_id.nil?.should be_true + status.target.nil?.should be_true end context "association id set but document not found" do diff --git a/test/functional/associations/test_belongs_to_proxy.rb b/test/functional/associations/test_belongs_to_proxy.rb index 6db21d76e..b32794b77 100644 --- a/test/functional/associations/test_belongs_to_proxy.rb +++ b/test/functional/associations/test_belongs_to_proxy.rb @@ -42,6 +42,7 @@ def setup end should "return nil if id set but document not found" do - @comment_class.new(:name => 'Foo', :post_id => '1234').post.nil?.should be_true + id = Mongo::ObjectID.new + @comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true end end \ No newline at end of file diff --git a/test/functional/associations/test_many_documents_as_proxy.rb b/test/functional/associations/test_many_documents_as_proxy.rb index bea083232..06edfb7cf 100644 --- a/test/functional/associations/test_many_documents_as_proxy.rb +++ b/test/functional/associations/test_many_documents_as_proxy.rb @@ -35,19 +35,19 @@ def setup PostComment.new(:body => 'baz') ] }.should change { PostComment.count }.by(3) - - from_db = Post.find(post.id) - from_db.comments.size.should == 3 - from_db.comments[0].body.should == 'foo' - from_db.comments[1].body.should == 'bar' - from_db.comments[2].body.should == 'baz' + + post = post.reload + post.comments.size.should == 3 + post.comments[0].body.should == 'foo' + post.comments[1].body.should == 'bar' + post.comments[2].body.should == 'baz' end context "build" do should "assign foreign key" do post = Post.new comment = post.comments.build - comment.commentable_id.should == post.id + comment.commentable_id.should == post._id end should "assign _type" do @@ -67,7 +67,7 @@ def setup should "assign foreign key" do post = Post.new comment = post.comments.create - comment.commentable_id.should == post.id + comment.commentable_id.should == post._id end should "assign _type" do @@ -166,25 +166,25 @@ def setup context "with one id" do should "work for id in association" do - @post.comments.find(@comment2.id).should == @comment2 + @post.comments.find(@comment2._id).should == @comment2 end should "not work for id not in association" do lambda { - @post.comments.find!(@comment5.id) + @post.comments.find!(@comment5._id) }.should raise_error(MongoMapper::DocumentNotFound) end end context "with multiple ids" do should "work for ids in association" do - posts = @post.comments.find!(@comment1.id, @comment2.id) + posts = @post.comments.find!(@comment1._id, @comment2._id) posts.should == [@comment1, @comment2] end should "not work for ids not in association" do lambda { - @post.comments.find!(@comment1.id, @comment2.id, @comment4.id) + @post.comments.find!(@comment1._id, @comment2._id, @comment4._id) }.should raise_error(MongoMapper::DocumentNotFound) end end diff --git a/test/functional/associations/test_many_embedded_polymorphic_proxy.rb b/test/functional/associations/test_many_embedded_polymorphic_proxy.rb index 91d30311e..bb712d534 100644 --- a/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +++ b/test/functional/associations/test_many_embedded_polymorphic_proxy.rb @@ -24,9 +24,9 @@ def setup catalog.medias = [Video.new("file" => "video.mpg", "length" => 3600)] catalog.save.should be_true - from_db = Catalog.find(catalog.id) - from_db.medias.size.should == 1 - from_db.medias[0].file.should == "video.mpg" + catalog = catalog.reload + catalog.medias.size.should == 1 + catalog.medias[0].file.should == "video.mpg" end should "store different associations" do @@ -38,15 +38,15 @@ def setup ] catalog.save.should be_true - from_db = Catalog.find(catalog.id) - from_db.medias.size.should == 3 - from_db.medias[0].file.should == "video.mpg" - from_db.medias[0].length.should == 3600 - from_db.medias[1].file.should == "music.mp3" - from_db.medias[1].bitrate.should == "128kbps" - from_db.medias[2].file.should == "image.png" - from_db.medias[2].width.should == 800 - from_db.medias[2].height.should == 600 + catalog = catalog.reload + catalog.medias.size.should == 3 + catalog.medias[0].file.should == "video.mpg" + catalog.medias[0].length.should == 3600 + catalog.medias[1].file.should == "music.mp3" + catalog.medias[1].bitrate.should == "128kbps" + catalog.medias[2].file.should == "image.png" + catalog.medias[2].width.should == 800 + catalog.medias[2].height.should == 600 end context "With modularized models" do @@ -75,16 +75,16 @@ def setup fleet.transports[2].year.should == 2008 fleet.save.should be_true - from_db = TrModels::Fleet.find(fleet.id) - from_db.transports.size.should == 3 - from_db.transports[0].license_plate.should == "GGG123" - from_db.transports[0].icu.should be_true - from_db.transports[1].license_plate.should == "ABC123" - from_db.transports[1].model.should == "VW Golf" - from_db.transports[1].year.should == 2001 - from_db.transports[2].license_plate.should == "DEF123" - from_db.transports[2].model.should == "Honda Accord" - from_db.transports[2].year.should == 2008 + fleet = fleet.reload + fleet.transports.size.should == 3 + fleet.transports[0].license_plate.should == "GGG123" + fleet.transports[0].icu.should be_true + fleet.transports[1].license_plate.should == "ABC123" + fleet.transports[1].model.should == "VW Golf" + fleet.transports[1].year.should == 2001 + fleet.transports[2].license_plate.should == "DEF123" + fleet.transports[2].model.should == "Honda Accord" + fleet.transports[2].year.should == 2008 end should "default reader to empty array" do @@ -104,9 +104,9 @@ def setup fleet.transports = [TrModels::Car.new("license_plate" => "DCU2013", "model" => "Honda Civic")] fleet.save.should be_true - from_db = TrModels::Fleet.find(fleet.id) - from_db.transports.size.should == 1 - from_db.transports[0].license_plate.should == "DCU2013" + fleet = fleet.reload + fleet.transports.size.should == 1 + fleet.transports[0].license_plate.should == "DCU2013" end should "store different associations" do @@ -118,15 +118,15 @@ def setup ] fleet.save.should be_true - from_db = TrModels::Fleet.find(fleet.id) - from_db.transports.size.should == 3 - from_db.transports[0].license_plate.should == "ABC1223" - from_db.transports[0].model.should == "Honda Civic" - from_db.transports[0].year.should == 2003 - from_db.transports[1].license_plate.should == "XYZ9090" - from_db.transports[1].max_passengers.should == 51 - from_db.transports[2].license_plate.should == "HDD3030" - from_db.transports[2].icu.should == true + fleet = fleet.reload + fleet.transports.size.should == 3 + fleet.transports[0].license_plate.should == "ABC1223" + fleet.transports[0].model.should == "Honda Civic" + fleet.transports[0].year.should == 2003 + fleet.transports[1].license_plate.should == "XYZ9090" + fleet.transports[1].max_passengers.should == 51 + fleet.transports[2].license_plate.should == "HDD3030" + fleet.transports[2].icu.should == true end end diff --git a/test/functional/associations/test_many_embedded_proxy.rb b/test/functional/associations/test_many_embedded_proxy.rb index 35cf85696..0b23092e6 100644 --- a/test/functional/associations/test_many_embedded_proxy.rb +++ b/test/functional/associations/test_many_embedded_proxy.rb @@ -26,10 +26,10 @@ def setup project.addresses << chi project.save - from_db = Project.find(project.id) - from_db.addresses.size.should == 2 - from_db.addresses[0].should == sb - from_db.addresses[1].should == chi + project = project.reload + project.addresses.size.should == 2 + project.addresses[0].should == sb + project.addresses[1].should == chi end should "allow embedding arbitrarily deep" do @@ -47,10 +47,10 @@ def setup doc = @document.new(:person => meg) doc.save - from_db = @document.find(doc.id) - from_db.person.name.should == 'Meg' - from_db.person.child.name.should == 'Steve' - from_db.person.child.child.name.should == 'Linda' + doc = doc.reload + doc.person.name.should == 'Meg' + doc.person.child.name.should == 'Steve' + doc.person.child.child.name.should == 'Linda' end should "allow assignment of 'many' embedded documents using a hash" do @@ -70,12 +70,12 @@ def setup pet_lover.pets[1].species.should == "Siberian Husky" pet_lover.save.should be_true - from_db = RealPerson.find(pet_lover.id) - from_db.name.should == "Mr. Pet Lover" - from_db.pets[0].name.should == "Jimmy" - from_db.pets[0].species.should == "Cocker Spainel" - from_db.pets[1].name.should == "Sasha" - from_db.pets[1].species.should == "Siberian Husky" + pet_lover = pet_lover.reload + pet_lover.name.should == "Mr. Pet Lover" + pet_lover.pets[0].name.should == "Jimmy" + pet_lover.pets[0].species.should == "Cocker Spainel" + pet_lover.pets[1].name.should == "Sasha" + pet_lover.pets[1].species.should == "Siberian Husky" end context "embedding many embedded documents" do @@ -101,13 +101,13 @@ def setup doc.people << meg doc.save - from_db = @document.find(doc.id) - from_db.people.first.name.should == "Meg" - from_db.people.first.pets.should_not == [] - from_db.people.first.pets.first.name.should == "Sparky" - from_db.people.first.pets.first.species.should == "Dog" - from_db.people.first.pets[1].name.should == "Koda" - from_db.people.first.pets[1].species.should == "Dog" + doc = doc.reload + doc.people.first.name.should == "Meg" + doc.people.first.pets.should_not == [] + doc.people.first.pets.first.name.should == "Sparky" + doc.people.first.pets.first.species.should == "Dog" + doc.people.first.pets[1].name.should == "Koda" + doc.people.first.pets[1].species.should == "Dog" end should "create a reference to the root document for all embedded documents before save" do @@ -139,7 +139,7 @@ def setup should "allow finding by id" do sparky = Pet.new(:name => "Sparky", :species => "Dog") meg = Person.new(:name => "Meg", :pets => [sparky]) - meg.pets.find(sparky.id).should == sparky + meg.pets.find(sparky._id).should == sparky end context "extending the association" do diff --git a/test/functional/associations/test_many_polymorphic_proxy.rb b/test/functional/associations/test_many_polymorphic_proxy.rb index 395c6a62a..c06690c86 100644 --- a/test/functional/associations/test_many_polymorphic_proxy.rb +++ b/test/functional/associations/test_many_polymorphic_proxy.rb @@ -34,8 +34,8 @@ def setup ] }.should change { Message.count }.by(3) - from_db = Room.find(room.id) - messages = from_db.messages.all :order => "position" + room = room.reload + messages = room.messages.all :order => "position" messages.size.should == 3 messages[0].body.should == 'John entered room' messages[1].body.should == 'Heyyyoooo!' @@ -48,8 +48,8 @@ def setup room.messages.push Exit.new(:body => 'John entered the room', :position => 2) room.messages.concat Chat.new(:body => 'Holla!' , :position => 3) - from_db = Room.find(room.id) - messages = from_db.messages.all :order => "position" + room = room.reload + messages = room.messages.all :order => "position" messages[0]._type.should == 'Enter' messages[1]._type.should == 'Exit' messages[2]._type.should == 'Chat' @@ -59,7 +59,7 @@ def setup should "assign foreign key" do room = Room.create message = room.messages.build - message.room_id.should == room.id + message.room_id.should == room._id end should "assign _type" do @@ -79,7 +79,7 @@ def setup should "assign foreign key" do room = Room.create message = room.messages.create - message.room_id.should == room.id + message.room_id.should == room._id end should "assign _type" do @@ -254,12 +254,12 @@ def setup context "with one id" do should "work for id in association" do - @lounge.messages.find(@lm2.id).should == @lm2 + @lounge.messages.find(@lm2._id).should == @lm2 end should "not work for id not in association" do lambda { - @lounge.messages.find!(@hm2.id) + @lounge.messages.find!(@hm2._id) }.should raise_error(MongoMapper::DocumentNotFound) end end @@ -280,13 +280,13 @@ def setup context "with multiple ids" do should "work for ids in association" do - messages = @lounge.messages.find(@lm1.id, @lm2.id) + messages = @lounge.messages.find(@lm1._id, @lm2._id) messages.should == [@lm1, @lm2] end should "not work for ids not in association" do lambda { - @lounge.messages.find!(@lm1.id, @lm2.id, @hm2.id) + @lounge.messages.find!(@lm1._id, @lm2._id, @hm2._id) }.should raise_error(MongoMapper::DocumentNotFound) end end diff --git a/test/functional/associations/test_many_proxy.rb b/test/functional/associations/test_many_proxy.rb index 580a5d482..e2110c2cf 100644 --- a/test/functional/associations/test_many_proxy.rb +++ b/test/functional/associations/test_many_proxy.rb @@ -25,9 +25,9 @@ def setup project.statuses = [Status.new("name" => "ready")] project.save.should be_true - from_db = Project.find(project.id) - from_db.statuses.size.should == 1 - from_db.statuses[0].name.should == "ready" + project = project.reload + project.statuses.size.should == 1 + project.statuses[0].name.should == "ready" end should "correctly assign foreign key when using <<, push and concat" do @@ -36,17 +36,17 @@ def setup project.statuses.push Status.new(:name => 'push') project.statuses.concat Status.new(:name => 'concat') - from_db = Project.find(project.id) - from_db.statuses[0].project_id.should == project.id - from_db.statuses[1].project_id.should == project.id - from_db.statuses[2].project_id.should == project.id + project = project.reload + project.statuses[0].project_id.should == project._id + project.statuses[1].project_id.should == project._id + project.statuses[2].project_id.should == project._id end context "build" do should "assign foreign key" do project = Project.create status = project.statuses.build - status.project_id.should == project.id + status.project_id.should == project._id end should "allow assigning attributes" do @@ -60,7 +60,7 @@ def setup should "assign foreign key" do project = Project.create status = project.statuses.create(:name => 'Foo!') - status.project_id.should == project.id + status.project_id.should == project._id end should "save record" do @@ -81,7 +81,7 @@ def setup should "assign foreign key" do project = Project.create status = project.statuses.create!(:name => 'Foo!') - status.project_id.should == project.id + status.project_id.should == project._id end should "save record" do @@ -105,7 +105,6 @@ def setup end end - context "count" do should "work scoped to association" do project = Project.create @@ -316,25 +315,25 @@ def setup context "with one id" do should "work for id in association" do - @project1.statuses.find(@complete.id).should == @complete + @project1.statuses.find(@complete._id).should == @complete end should "not work for id not in association" do lambda { - @project1.statuses.find!(@archived.id) + @project1.statuses.find!(@archived._id) }.should raise_error(MongoMapper::DocumentNotFound) end end context "with multiple ids" do should "work for ids in association" do - statuses = @project1.statuses.find(@brand_new.id, @complete.id) + statuses = @project1.statuses.find(@brand_new._id, @complete._id) statuses.should == [@brand_new, @complete] end should "not work for ids not in association" do lambda { - @project1.statuses.find!(@brand_new.id, @complete.id, @archived.id) + @project1.statuses.find!(@brand_new._id, @complete._id, @archived._id) }.should raise_error(MongoMapper::DocumentNotFound) end end diff --git a/test/functional/test_associations.rb b/test/functional/test_associations.rb index 29ad8ee9c..e6c30ba0d 100644 --- a/test/functional/test_associations.rb +++ b/test/functional/test_associations.rb @@ -14,7 +14,7 @@ class AwesomeTag include MongoMapper::EmbeddedDocument key :name, String - key :post_id, String + key :post_id, Mongo::ObjectID belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser' end @@ -22,7 +22,7 @@ class AwesomeTag class AwesomePost include MongoMapper::Document - key :creator_id, String + key :creator_id, Mongo::ObjectID belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser' many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id @@ -38,7 +38,7 @@ class AwesomePost post2 = AwesomePost.create(:creator => user, :tags => [tag2]) user.posts.should == [post1, post2] - post1_from_db = AwesomePost.find(post1.id) - post1_from_db.tags.should == [tag1] + post1 = post1.reload + post1.tags.should == [tag1] end end \ No newline at end of file diff --git a/test/functional/test_binary.rb b/test/functional/test_binary.rb index b1b51742d..ce1451f7f 100644 --- a/test/functional/test_binary.rb +++ b/test/functional/test_binary.rb @@ -12,7 +12,7 @@ class BinaryTest < Test::Unit::TestCase doc = klass.new(:contents => '010101') doc.save - doc = klass.find(doc.id) + doc = doc.reload doc.contents.to_s.should == ByteBuffer.new('010101').to_s end end \ No newline at end of file diff --git a/test/functional/test_dirty.rb b/test/functional/test_dirty.rb index 566ae273f..c66459c06 100644 --- a/test/functional/test_dirty.rb +++ b/test/functional/test_dirty.rb @@ -55,17 +55,17 @@ def setup should "not happen when loading from database" do doc = @document.create(:phrase => 'Foo') - from_db = @document.find(doc.id) - from_db.changed?.should be_false + doc = doc.reload + doc.changed?.should be_false end should "happen if changed after loading from database" do doc = @document.create(:phrase => 'Foo') - from_db = @document.find(doc.id) - from_db.changed?.should be_false - from_db.phrase = 'Bar' - from_db.changed?.should be_true + doc = doc.reload + doc.changed?.should be_false + doc.phrase = 'Bar' + doc.changed?.should be_true end end diff --git a/test/functional/test_document.rb b/test/functional/test_document.rb index 75efd8297..95685c4b4 100644 --- a/test/functional/test_document.rb +++ b/test/functional/test_document.rb @@ -37,7 +37,7 @@ def setup context "Loading a document from the database with keys that are not defined" do setup do - @id = Mongo::ObjectID.new.to_s + @id = Mongo::ObjectID.new @document.collection.insert({ :_id => @id, :first_name => 'John', @@ -80,7 +80,7 @@ def setup doc.tags = %w(foo bar) doc.save doc.tags.should == %w(foo bar) - @document.find(doc.id).tags.should == %w(foo bar) + doc.reload.tags.should == %w(foo bar) end should "work with assignment then <<" do @@ -102,7 +102,7 @@ def setup doc.tags << "bar" doc.save doc.tags.should == %w(foo bar) - @document.find(doc.id).tags.should == %w(foo bar) + doc.reload.tags.should == %w(foo bar) end end @@ -135,7 +135,7 @@ def setup doc.foo = {:baz => 'bar'} doc.save - doc = @document.find(doc.id) + doc = doc.reload doc.foo[:baz].should == 'bar' doc.foo['baz'].should == 'bar' end @@ -156,12 +156,11 @@ def setup doc = @document.new doc.save - from_db = @document.find(doc.id) - from_db.window.should == WindowSize.new(600, 480) + doc = doc.reload + doc.window.should == WindowSize.new(600, 480) end end - context "Creating a single document" do setup do @doc_instance = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @@ -174,6 +173,8 @@ def setup should "automatically set id" do @doc_instance.id.should_not be_nil @doc_instance.id.size.should == 24 + @doc_instance.id.should be_instance_of(String) + @doc_instance._id.should be_instance_of(Mongo::ObjectID) end should "no longer be new?" do @@ -226,7 +227,7 @@ def setup context "Updating a document" do setup do doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) - @doc_instance = @document.update(doc.id, {:age => 40}) + @doc_instance = @document.update(doc._id, {:age => 40}) end should "update attributes provided" do @@ -246,8 +247,8 @@ def setup should "raise error when updating single doc if not provided id and attributes" do doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) lambda { @document.update }.should raise_error(ArgumentError) - lambda { @document.update(doc.id) }.should raise_error(ArgumentError) - lambda { @document.update(doc.id, [1]) }.should raise_error(ArgumentError) + lambda { @document.update(doc._id) }.should raise_error(ArgumentError) + lambda { @document.update(doc._id, [1]) }.should raise_error(ArgumentError) end context "Updating multiple documents" do @@ -256,8 +257,8 @@ def setup @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) @doc_instances = @document.update({ - @doc1.id => {:age => 30}, - @doc2.id => {:age => 30}, + @doc1._id => {:age => 30}, + @doc2._id => {:age => 30}, }) end @@ -272,8 +273,8 @@ def setup end should "update the documents" do - @document.find(@doc1.id).age.should == 30 - @document.find(@doc2.id).age.should == 30 + @document.find(@doc1._id).age.should == 30 + @document.find(@doc2._id).age.should == 30 end end @@ -298,7 +299,7 @@ def setup context "with a single id" do should "work" do - @document.find(@doc1.id).should == @doc1 + @document.find(@doc1._id).should == @doc1 end should "return nil if document not found with find" do @@ -314,15 +315,15 @@ def setup context "with multiple id's" do should "work as arguments" do - @document.find(@doc1.id, @doc2.id).should == [@doc1, @doc2] + @document.find(@doc1._id, @doc2._id).should == [@doc1, @doc2] end should "work as array" do - @document.find([@doc1.id, @doc2.id]).should == [@doc1, @doc2] + @document.find([@doc1._id, @doc2._id]).should == [@doc1, @doc2] end should "return array if array only has one element" do - @document.find([@doc1.id]).should == [@doc1] + @document.find([@doc1._id]).should == [@doc1] end end @@ -442,8 +443,8 @@ def setup end should "be able to find by id" do - @document.find_by_id(@doc1.id).should == @doc1 - @document.find_by_id(@doc2.id).should == @doc2 + @document.find_by_id(@doc1._id).should == @doc1 + @document.find_by_id(@doc2._id).should == @doc2 end should "return nil if document not found" do @@ -455,7 +456,7 @@ def setup setup do @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) - @document.delete(@doc1.id) + @document.delete(@doc1._id) end should "remove document from collection" do @@ -463,7 +464,7 @@ def setup end should "not remove other documents" do - @document.find(@doc2.id).should_not be(nil) + @document.find(@doc2._id).should_not be(nil) end end @@ -472,7 +473,7 @@ def setup @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'}) - @document.delete(@doc1.id, @doc2.id) + @document.delete(@doc1._id, @doc2._id) @document.count.should == 1 end @@ -481,7 +482,7 @@ def setup @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'}) - @document.delete([@doc1.id, @doc2.id]) + @document.delete([@doc1._id, @doc2._id]) @document.count.should == 1 end @@ -514,7 +515,7 @@ def setup setup do @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) - @document.destroy(@doc1.id) + @document.destroy(@doc1._id) end should "remove document from collection" do @@ -522,7 +523,7 @@ def setup end should "not remove other documents" do - @document.find(@doc2.id).should_not be(nil) + @document.find(@doc2._id).should_not be(nil) end end @@ -531,8 +532,8 @@ def setup @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'}) - @document.destroy(@doc1.id, @doc2.id) - + @document.destroy(@doc1._id, @doc2._id) + @document.count.should == 1 end @@ -540,7 +541,7 @@ def setup @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'}) @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'}) @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'}) - @document.destroy([@doc1.id, @doc2.id]) + @document.destroy([@doc1._id, @doc2._id]) @document.count.should == 1 end @@ -594,7 +595,7 @@ class ::Thing context "many" do context "=> destroy" do setup do - Property.key :thing_id, String + Property.key :thing_id, Mongo::ObjectID Property.belongs_to :thing, :dependent => :destroy Thing.many :properties, :dependent => :destroy @@ -617,7 +618,7 @@ class ::Thing context "=> delete_all" do setup do - Property.key :thing_id, String + Property.key :thing_id, Mongo::ObjectID Property.belongs_to :thing Thing.has_many :properties, :dependent => :delete_all @@ -640,7 +641,7 @@ class ::Thing context "=> nullify" do setup do - Property.key :thing_id, String + Property.key :thing_id, Mongo::ObjectID Property.belongs_to :thing Thing.has_many :properties, :dependent => :nullify @@ -665,7 +666,7 @@ class ::Thing context "belongs_to" do context "=> destroy" do setup do - Property.key :thing_id, String + Property.key :thing_id, Mongo::ObjectID Property.belongs_to :thing, :dependent => :destroy Thing.has_many :properties @@ -785,25 +786,24 @@ class ::Thing end should "update attributes in the database" do - from_db = @document.find(@doc.id) - from_db.should == @doc - from_db.first_name.should == 'John' - from_db.age.should == 27 + doc = @doc.reload + doc.should == @doc + doc.first_name.should == 'John' + doc.age.should == 27 end should "allow to add custom attributes to the document" do @doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male', :tags => [1, "2"]) @doc.save - from_db = @document.find(@doc.id) - from_db.gender.should == 'male' - from_db.tags.should == [1, "2"] + doc = @doc.reload + doc.gender.should == 'male' + doc.tags.should == [1, "2"] end should "allow to use custom methods to assign properties" do - person = RealPerson.new(:realname => "David") + person = RealPerson.new(:realname => 'David') person.save - from_db = RealPerson.find(person.id) - from_db.name.should == "David" + person.reload.name.should == 'David' end context "with key of type date" do @@ -833,17 +833,16 @@ class ::Thing end should "update attributes in the database" do - from_db = @document.find(@doc.id) - from_db.first_name.should == 'Johnny' - from_db.age.should == 30 + doc = @doc.reload + doc.first_name.should == 'Johnny' + doc.age.should == 30 end should "allow updating custom attributes" do @doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male') @doc.gender = 'Male' @doc.save - from_db = @document.find(@doc.id) - from_db.gender.should == 'Male' + @doc.reload.gender.should == 'Male' end end @@ -868,16 +867,15 @@ class ::Thing end should "update attributes in the database" do - from_db = @document.find(@doc.id) - from_db.should == @doc - from_db.first_name.should == 'Johnny' - from_db.age.should == 30 + doc = @doc.reload + doc.should == @doc + doc.first_name.should == 'Johnny' + doc.age.should == 30 end should "allow updating custom attributes" do @doc.update_attributes(:gender => 'mALe') - from_db = @document.find(@doc.id) - from_db.gender.should == 'mALe' + @doc.reload.gender.should == 'mALe' end end @@ -897,9 +895,9 @@ class ::Thing end should "update attributes in the database" do - from_db = @document.find(@doc.id) - from_db.first_name.should == 'Johnny' - from_db.age.should == 30 + doc = @doc.reload + doc.first_name.should == 'Johnny' + doc.age.should == 30 end end @@ -1031,14 +1029,14 @@ class ::DocGrandSon < ::DocSon; end steph = DocDaughter.create(:name => 'Steph') lambda { - DocSon.find!(steph.id) + DocSon.find!(steph._id) }.should raise_error(MongoMapper::DocumentNotFound) end should "not raise error for find with parent" do john = DocSon.create(:name => 'John') - DocParent.find!(john.id).should == john + DocParent.find!(john._id).should == john end should "count scoped to class" do @@ -1073,7 +1071,7 @@ class ::DocGrandSon < ::DocSon; end steph = DocDaughter.create(:name => 'Steph') lambda { - DocSon.destroy(steph.id) + DocSon.destroy(steph._id) }.should raise_error(MongoMapper::DocumentNotFound) end @@ -1082,7 +1080,7 @@ class ::DocGrandSon < ::DocSon; end steph = DocDaughter.create(:name => 'Steph') lambda { - DocSon.delete(steph.id) + DocSon.delete(steph._id) }.should_not change { DocParent.count } end @@ -1142,9 +1140,9 @@ class ::DocGrandSon < ::DocSon; end @document.update(doc._id, { :first_name => 'Johnny' }) end - from_db = @document.find(doc.id) - from_db.created_at.should == old_created_at - from_db.updated_at.should_not == old_updated_at + doc = doc.reload + doc.created_at.should == old_created_at + doc.updated_at.should_not == old_updated_at end end @@ -1174,7 +1172,7 @@ class ::DocGrandSon < ::DocSon; end context "reload" do setup do @doc_instance_1 = @document.create({:first_name => 'Ryan', :last_name => 'Koopmans', :age => '37'}) - @doc_instance_2 = @document.update(@doc_instance_1.id, {:age => '39'}) + @doc_instance_2 = @document.update(@doc_instance_1._id, {:age => '39'}) end should "load fresh information from the database" do diff --git a/test/functional/test_embedded_document.rb b/test/functional/test_embedded_document.rb index 980c4ab80..543fc44ed 100644 --- a/test/functional/test_embedded_document.rb +++ b/test/functional/test_embedded_document.rb @@ -28,9 +28,9 @@ def setup @doc.foo.city.should == 'South Bend' @doc.foo.state.should == 'IN' - from_db = @document.find(@doc.id) - from_db.foo.city.should == 'South Bend' - from_db.foo.state.should == 'IN' + doc = @doc.reload + doc.foo.city.should == 'South Bend' + doc.foo.state.should == 'IN' end end @@ -58,8 +58,9 @@ def setup address = Address.new(:city => 'South Bend', :state => 'IN') doc = @document.new(:foo => address) doc.save - read_doc = @document.find(doc.id) - read_doc.foo.new?.should == false + + doc = doc.reload + doc.foo.new?.should == false end end @@ -71,15 +72,16 @@ def setup person.pets << pet pet.save - doc = RealPerson.find(person.id) - doc.pets.first.should == pet + person = person.reload + person.pets.first.should == pet end should "save new keys" do person = RealPerson.new person[:new_attribute] = 'foobar' person.save - from_db = RealPerson.find(person.id) + + person = person.reload person.new_attribute.should == 'foobar' end end @@ -92,14 +94,13 @@ def setup person.pets << pet pet.save - doc = RealPerson.find(person.id) - pet = doc.pets.first + person = person.reload + pet = person.pets.first pet.update_attributes :name => 'koda' - doc = RealPerson.find(person.id) - embedded = doc.pets.first - embedded.id.should == pet.id - embedded.name.should == 'koda' + person = person.reload + person.pets.first._id.should == pet._id + person.pets.first.name.should == 'koda' end end end diff --git a/test/functional/test_validations.rb b/test/functional/test_validations.rb index 38e002257..62ad582ca 100644 --- a/test/functional/test_validations.rb +++ b/test/functional/test_validations.rb @@ -93,7 +93,7 @@ class ValidationsTest < Test::Unit::TestCase should "not update document" do @doc.name = nil @doc.save - @document.find(@doc.id).name.should == 'John Nunemaker' + @doc.reload.name.should == 'John Nunemaker' end should "populate document's errors" do diff --git a/test/models.rb b/test/models.rb index 586696459..6704a7330 100644 --- a/test/models.rb +++ b/test/models.rb @@ -40,7 +40,7 @@ class PostComment key :username, String, :default => 'Anonymous' key :body, String - key :commentable_id, String + key :commentable_id, Mongo::ObjectID key :commentable_type, String belongs_to :commentable, :polymorphic => true @@ -62,7 +62,7 @@ class Message key :body, String key :position, Integer key :_type, String - key :room_id, String + key :room_id, Mongo::ObjectID belongs_to :room end @@ -81,7 +81,7 @@ class Account include MongoMapper::Document key :_type, String - key :room_id, String + key :room_id, Mongo::ObjectID key :last_logged_in, Time belongs_to :room @@ -145,7 +145,7 @@ def find_all_by_state(state) class Collaborator include MongoMapper::Document - key :project_id, String + key :project_id, Mongo::ObjectID key :name, String belongs_to :project end @@ -153,8 +153,8 @@ class Collaborator class Status include MongoMapper::Document - key :project_id, String - key :target_id, String + key :project_id, Mongo::ObjectID + key :target_id, Mongo::ObjectID key :target_type, String key :name, String, :required => true key :position, Integer @@ -166,7 +166,7 @@ class Status class RealPerson include MongoMapper::Document - key :room_id, String + key :room_id, Mongo::ObjectID key :name, String belongs_to :room diff --git a/test/unit/test_embedded_document.rb b/test/unit/test_embedded_document.rb index 19e64076e..7b75dc42e 100644 --- a/test/unit/test_embedded_document.rb +++ b/test/unit/test_embedded_document.rb @@ -292,6 +292,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase should "have to_param that is id" do doc = @document.new doc.to_param.should == doc.id + doc.to_param.should be_instance_of(String) end should "have access to class logger" do diff --git a/test/unit/test_finder_options.rb b/test/unit/test_finder_options.rb index 7813ce3fe..5ce0671fa 100644 --- a/test/unit/test_finder_options.rb +++ b/test/unit/test_finder_options.rb @@ -57,9 +57,28 @@ class FinderOptionsTest < Test::Unit::TestCase end should "convert id to _id" do - FinderOptions.new(Room, :id => '1').criteria.should == { - :_id => '1' - } + id = Mongo::ObjectID.new + FinderOptions.new(Room, :id => id).criteria.should == {:_id => id} + end + + should "make sure that _id's are object ids" do + id = Mongo::ObjectID.new + FinderOptions.new(Room, :_id => id.to_s).criteria.should == {:_id => id} + end + + should "work fine with _id's that are object ids" do + id = Mongo::ObjectID.new + FinderOptions.new(Room, :_id => id).criteria.should == {:_id => id} + end + + should "make sure other object id typed keys get converted" do + id = Mongo::ObjectID.new + FinderOptions.new(Message, :room_id => id.to_s).criteria.should == {:room_id => id} + end + + should "work fine with object ids for object id typed keys" do + id = Mongo::ObjectID.new + FinderOptions.new(Message, :room_id => id).criteria.should == {:room_id => id} end should "use $in for arrays" do diff --git a/test/unit/test_mongomapper.rb b/test/unit/test_mongo_mapper.rb similarity index 56% rename from test/unit/test_mongomapper.rb rename to test/unit/test_mongo_mapper.rb index 04c9971ea..ae21a57d7 100644 --- a/test/unit/test_mongomapper.rb +++ b/test/unit/test_mongo_mapper.rb @@ -25,4 +25,28 @@ class MongoMapperTest < Test::Unit::TestCase MongoMapper::DocumentNotFound }.should_not raise_error end + + context "use_time_zone?" do + should "be true if Time.zone set" do + Time.zone = 'Hawaii' + MongoMapper.use_time_zone?.should be_true + Time.zone = nil + end + + should "be false if Time.zone not set" do + MongoMapper.use_time_zone?.should be_false + end + end + + context "time_class" do + should "be Time.zone if using time zones" do + Time.zone = 'Hawaii' + MongoMapper.time_class.should == Time.zone + Time.zone = nil + end + + should "be Time if not using time zones" do + MongoMapper.time_class.should == Time + end + end end