Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ActiveRecord-style touch

  • Loading branch information...
commit 6043249939e250c7e4a9f09795f5e9438b7abcd4 1 parent c2614d7
@bryckbost bryckbost authored
View
1  lib/mongo_mapper.rb
@@ -56,6 +56,7 @@ module Plugins
autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
autoload :Validations, 'mongo_mapper/plugins/validations'
+ autoload :Touch, 'mongo_mapper/plugins/touch'
module Associations
autoload :Base, 'mongo_mapper/plugins/associations/base'
View
1  lib/mongo_mapper/document.rb
@@ -29,6 +29,7 @@ module Document
include Plugins::Serialization
include Plugins::Timestamps
include Plugins::Userstamps
+ include Plugins::Touch
include Plugins::Validations
include Plugins::EmbeddedCallbacks
include Plugins::Callbacks # for now callbacks needs to be after validations
View
13 lib/mongo_mapper/plugins/touch.rb
@@ -0,0 +1,13 @@
+module MongoMapper
+ module Plugins
+ module Touch
+ extend ActiveSupport::Concern
+
+ def touch(key = :updated_at)
+ raise "InvalidKey" unless self.key_names.include?(key.to_s)
+ self.set(key => Time.now.utc)
+ true
+ end
+ end
+ end
+end
View
21 test/functional/test_touch.rb
@@ -0,0 +1,21 @@
+require 'test_helper'
+
+class TouchTest < Test::Unit::TestCase
+ context "touch" do
+ def setup
+ @document = Doc { timestamps! }
+ end
+
+ should "update the updated_at timestamp" do
+ doc = @document.create
+ old_updated_at = doc.updated_at
+
+ Timecop.freeze(Time.now + 1.day) do
+ doc.touch
+ end
+
+ doc.reload
+ doc.updated_at.should_not == old_updated_at
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.