Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit eda78018c3423f23fd6d10ffac82dcf7fd91738f 1 parent 8f7588c
Mislav Marohnić authored
Showing with 22 additions and 0 deletions.
  1. +1 −0  lib/mingo.rb
  2. +21 −0 lib/mingo/timestamps.rb
1  lib/mingo.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
21 lib/mingo/timestamps.rb
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
Please sign in to comment.
Something went wrong with that request. Please try again.