Permalink
Browse files

Mingo::Timestamps provides `created_at`, `updated_at`

  • Loading branch information...
1 parent 8f7588c commit eda78018c3423f23fd6d10ffac82dcf7fd91738f @mislav committed Apr 26, 2011
Showing with 22 additions and 0 deletions.
  1. +1 −0 lib/mingo.rb
  2. +21 −0 lib/mingo/timestamps.rb
View
@@ -22,6 +22,7 @@ class Mingo < Hashie::Dash
autoload :Persistence, 'mingo/persistence'
autoload :Callbacks, 'mingo/callbacks'
autoload :Changes, 'mingo/changes'
+ autoload :Timestamps, 'mingo/timestamps'
class << self
attr_writer :db, :collection
View
@@ -0,0 +1,21 @@
+class Mingo
+ module Timestamps
+ def self.included(base)
+ base.before_update :touch_updated_timestamp
+ end
+
+ def created_at
+ @created_at ||= self.id && self.id.generation_time
+ end
+
+ def updated_at
+ self[:updated_at] || created_at
+ end
+
+ protected
+
+ def touch_updated_timestamp
+ self[:updated_at] = Time.now if changed?
+ end
+ end
+end

0 comments on commit eda7801

Please sign in to comment.