Skip to content

Commit

Permalink
Time.to_mongo now returns nil if value is nil or empty string. Fixes m…
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Oct 11, 2009
1 parent d939c0a commit b50a1f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/mongo_mapper/support.rb
Expand Up @@ -131,7 +131,11 @@ def self.from_mongo(value)

class Time
def self.to_mongo(value)
to_utc_time(value)
if value.nil? || value == ''
nil
else
to_utc_time(value)
end
end

def self.from_mongo(value)
Expand Down
23 changes: 21 additions & 2 deletions test/unit/test_support.rb
Expand Up @@ -225,9 +225,12 @@ class SupportTest < Test::Unit::TestCase
Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
end

should "be nil if blank string" do
Time.to_mongo('').should be_nil
end

should "not be nil if nil" do
# Time.parse actually returns like right now
Time.to_mongo(nil).should_not be_nil
Time.to_mongo(nil).should be_nil
end
end

Expand All @@ -244,6 +247,12 @@ class SupportTest < Test::Unit::TestCase
Time.zone = nil
end

should "be nil if blank string" do
Time.zone = 'Hawaii'
Time.to_mongo('').should be_nil
Time.zone = nil
end

should "be nil if nil" do
Time.zone = 'Hawaii'
Time.to_mongo(nil).should be_nil
Expand All @@ -256,6 +265,10 @@ class SupportTest < Test::Unit::TestCase
time = Time.now
Time.from_mongo(time).should == time
end

should "be nil if nil" do
Time.from_mongo(nil).should be_nil
end
end

context "Time#from_mongo with Time.zone" do
Expand All @@ -268,5 +281,11 @@ class SupportTest < Test::Unit::TestCase

Time.zone = nil
end

should "be nil if nil" do
Time.zone = 'Hawaii'
Time.from_mongo(nil).should be_nil
Time.zone = nil
end
end
end

0 comments on commit b50a1f7

Please sign in to comment.