Sha256: 7cc197d1f198c35221fea4c62761240bfda1efc6bdba39f8107602bcdb2b0bbf

Contents?: true

Size: 727 Bytes

Versions: 4

Compression:

Stored size: 727 Bytes

Contents

require File.expand_path("../datatypes", __FILE__)

module Ohm
  # Provides created_at / updated_at timestamps.
  #
  # @example
  #
  #   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
  module Timestamps
    def self.included(model)
      model.attribute :created_at, DataTypes::Type::Timestamp
      model.attribute :updated_at, DataTypes::Type::Timestamp
    end

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

      super
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ohm-contrib-2.0.0.alpha5 lib/ohm/timestamps.rb
ohm-contrib-2.0.0.alpha4 lib/ohm/timestamps.rb
ohm-contrib-2.0.0.alpha3 lib/ohm/timestamps.rb
ohm-contrib-2.0.0.alpha2 lib/ohm/timestamps.rb