Created
January 3, 2010 03:13
-
-
Save amasses/267792 to your computer and use it in GitHub Desktop.
Issues with Mongo and Timezones (with Mongoid or MongoMapper gems)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defaults: &defaults | |
adapter: mongo | |
host: localhost | |
test: | |
<<: *defaults | |
database: accounting | |
development: | |
<<: *defaults | |
database: accounting | |
# username: development | |
# password: test | |
production: | |
<<: *defaults | |
host: db.mongohq.com | |
username: amasses | |
password: ********** | |
database: accounting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HelpFile | |
include Mongoid::Document | |
field :title, :type => String | |
field :excerpt, :type => String | |
field :content, :type => String | |
field :is_published, :type => Boolean | |
field :publish_date, :type => Time | |
before_save :set_publish_date, :ensure_excerpt | |
key :title | |
validates_presence_of :title, :content | |
protected | |
def ensure_excerpt | |
self.excerpt = "#{self.content[0..200]}..." if self.excerpt.nil? | |
end | |
# Update the updated_at field on the Document to the current time. | |
# This is only called on create and on save. | |
def set_publish_date | |
self.publish_date = Time.now.utc | |
end | |
def tag_list | |
[] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe HelpFile do | |
before(:each) do | |
@valid_attributes = { | |
:title => "this is a sample help file", | |
:content => "Monkeys shouldn't wear pajamas, but they look damn funny when they do!", | |
:is_published => false, | |
:publish_date => Time.now.utc | |
} | |
end | |
it "should create a new instance given valid attributes" do | |
HelpFile.create!(@valid_attributes) | |
end | |
it "should retrieve the date and be within 1 mintue" do | |
h = HelpFile.create!(@valid_attributes) | |
h.publish_date.should be_close(Time.now.utc, 1.minute) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> x = HelpFile.create(:title => "Test help file", :content => "Hello world!", :is_published => true, :publish_date => Time.now.utc) | |
=> #<HelpFile _id: test-help-file, title: Test help file, excerpt: Hello world!..., is_published: true, content: Hello world!, publish_date: 28/02/2010> | |
>> x.publish_date | |
ArgumentError: invalid date | |
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/date.rb:1573:in `new_by_frags' | |
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/date.rb:1618:in `parse' | |
from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/active_support/values/time_zone.rb:246:in `parse' | |
from /Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/extensions/time/conversions.rb:12:in `get' | |
from /Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/field.rb:35:in `get' | |
from /Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/attributes.rb:32:in `read_attribute' | |
from /Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/document.rb:122:in `publish_date' | |
from (irb):2 | |
>> Time.zone | |
=> #<ActiveSupport::TimeZone:0x10332f150 @utc_offset=36000, @name="Canberra", @tzinfo=nil> | |
>> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> use accounting | |
switched to db accounting | |
> db.help_files.find() | |
{ "_id" : "test-help-file", "title" : "Test help file", "excerpt" : "Hello world!...", "is_published" : true, "content" : "Hello world!", "publish_date" : "Mon Mar 01 2010 00:00:00 GMT+1100 (EST)" } | |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File.open(File.join(RAILS_ROOT, 'config/database.mongo.yml'), 'r') do |f| | |
@settings = YAML.load(f)[RAILS_ENV] | |
end | |
connection = Mongo::Connection.new(@settings["host"]) | |
Mongoid.database = connection.db(@settings["database"]) | |
if @settings["username"] | |
Mongoid.database.authenticate(@settings["username"], @settings["password"]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Broc:accounting-app Broc$ spec spec/models/help_file_spec.rb | |
.F | |
1) | |
ArgumentError in 'HelpFile should retrieve the date and be within 1 mintue' | |
invalid date | |
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.3/lib/active_support/values/time_zone.rb:246:in `parse' | |
/Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/extensions/time/conversions.rb:12:in `get' | |
/Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/field.rb:35:in `get' | |
/Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/attributes.rb:32:in `read_attribute' | |
/Library/Ruby/Gems/1.8/gems/mongoid-0.10.6/lib/mongoid/document.rb:122:in `publish_date' | |
./spec/models/help_file_spec.rb:20: | |
Finished in 0.459463 seconds | |
2 examples, 1 failure | |
Broc:accounting-app Broc$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this still an issue for you?