Module: Ohm::Timestamps

Defined in:
lib/ohm/timestamps.rb

Overview

Provides created_at / updated_at timestamps.

Examples:


class Post < Ohm::Model
  include Ohm::Timestamps
end

post = Post.create
post.created_at.to_i == Time.now.utc.to_i
# => true

post = Post[post.id]
post.save
post.updated_at.to_i == Time.now.utc.to_i
# => true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



21
22
23
24
# File 'lib/ohm/timestamps.rb', line 21

def self.included(model)
  model.attribute :created_at, DataTypes::Type::Timestamp
  model.attribute :updated_at, DataTypes::Type::Timestamp
end

Instance Method Details

#saveObject



26
27
28
29
30
31
# File 'lib/ohm/timestamps.rb', line 26

def save
  self.created_at = Time.now.utc.to_i if new?
  self.updated_at = Time.now.utc.to_i

  super
end