Skip to content

Commit

Permalink
Fix Embedded Document touch
Browse files Browse the repository at this point in the history
  • Loading branch information
ersatzryan committed Mar 13, 2012
1 parent b92bbd3 commit c99ae0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/mongo_mapper/plugins/touch.rb
Expand Up @@ -5,9 +5,14 @@ module Touch

def touch(key = :updated_at)
raise ArgumentError, "Invalid key named #{key}" unless self.key_names.include?(key.to_s)
self.set(key => Time.now.utc)
if self.class.embeddable?
self.write_attribute(key, Time.now.utc)
self._parent_document.touch
else
self.set(key => Time.now.utc)
end
true
end
end
end
end
end
13 changes: 11 additions & 2 deletions test/functional/test_touch.rb
Expand Up @@ -22,16 +22,25 @@ class TouchTest < Test::Unit::TestCase

context "embedded document" do
should "update the updated_at timestamp" do
emdoc = Doc { timestamps! }.create
Doc = Doc("Document") { timestamps!}
Emdoc = EDoc("EmbeddedDocument") { timestamps! }
Doc.has_many :emdocs, :class => Emdoc

doc = Doc.create
emdoc = Emdoc.new
doc.emdocs << emdoc
doc.save

old_updated_at = emdoc.updated_at
document_old_updated_at = doc.updated_at

Timecop.freeze(Time.now + 1.day) do
emdoc.touch
end

emdoc.reload
doc.reload
emdoc.updated_at.should_not == old_updated_at
doc.updated_at.should_not == document_old_updated_at
end
end

Expand Down

0 comments on commit c99ae0f

Please sign in to comment.