Skip to content

Commit

Permalink
spec and code to use serialized values for changes, to correctly pers…
Browse files Browse the repository at this point in the history
…ist Date and DateTime fields to mongodb
  • Loading branch information
Derick Bailey committed Jul 5, 2011
1 parent 978ad66 commit b9d43f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mongoid/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Dirty #:nodoc:
def changes
{}.tap do |hash|
changed.each do |name|
change = attribute_change(name)
change = [changed_attributes[name], attributes[name]] if attribute_changed?(name)
hash[name] = change if change[0] != change[1]
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/functional/mongoid/date_time_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

describe "Date/Time attributes" do

context "when a DateTime attribute is updated and persisted" do
let(:user) do
user = User.create! :last_login => 2.days.ago
user.last_login = DateTime.now
user
end

it "should be read for persistance as a UTC Time" do
user.changes["last_login"].last.class.should == Time
end

it "should persist with no exceptions thrown" do
user.save!
end
end

context "when a Date attribute is persisted" do
let(:user) do
user = User.create! :account_expires => 2.years.from_now
user.account_expires = "2/2/2002".to_date
user
end

it "should be read for persistance as a UTC Time" do
user.changes["account_expires"].last.class.should == Time
end

it "should persist with no exceptions thrown" do
user.save!
end
end

end
2 changes: 2 additions & 0 deletions spec/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class User
include Mongoid::Document

field :name
field :last_login, :type => DateTime
field :account_expires, :type => Date

references_one :account, :foreign_key => :creator_id
references_many :posts, :foreign_key => :author_id
Expand Down

0 comments on commit b9d43f0

Please sign in to comment.