Skip to content

Commit

Permalink
Blank string now returns nil for ObjectId#to_mongo. Makes using it wi…
Browse files Browse the repository at this point in the history
…th <select> tags and blank options easier. Fixes #140.
  • Loading branch information
jnunemaker committed Dec 31, 2009
1 parent 63c0c7f commit a2474f2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mongo_mapper/support.rb
Expand Up @@ -120,7 +120,7 @@ def self.from_mongo(value)

class ObjectId
def self.to_mongo(value)
if value.nil?
if value.blank?
nil
elsif value.is_a?(Mongo::ObjectID)
value
Expand Down Expand Up @@ -165,7 +165,7 @@ def asc; MongoMapper::OrderOperator.new(self, 'asc') end
def desc; MongoMapper::OrderOperator.new(self, 'desc') end
end

class Time
class Time
def self.to_mongo(value)
if value.nil? || value == ''
nil
Expand Down
4 changes: 4 additions & 0 deletions test/unit/test_support.rb
Expand Up @@ -167,6 +167,10 @@ class SupportTest < Test::Unit::TestCase
ObjectId.to_mongo(nil).should be_nil
end

should "return nil if blank string" do
ObjectId.to_mongo('').should be_nil
end

should "return value if object id" do
id = Mongo::ObjectID.new
ObjectId.to_mongo(id).should be(id)
Expand Down

0 comments on commit a2474f2

Please sign in to comment.