Permalink
Browse files

Fix Embedded Document touch

  • Loading branch information...
ersatzryan committed Mar 13, 2012
1 parent b92bbd3 commit c99ae0f8543fdebd2c8a792e3f90892506826a80
Showing with 18 additions and 4 deletions.
  1. +7 −2 lib/mongo_mapper/plugins/touch.rb
  2. +11 −2 test/functional/test_touch.rb
@@ -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
@@ -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

0 comments on commit c99ae0f

Please sign in to comment.