Sha256: 9901855976cf41b8aaecdb3a47d3ace8e80ce7d4e18c68c86369c48fbdf7f9db

Contents?: true

Size: 728 Bytes

Versions: 7

Compression:

Stored size: 728 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

7 entries across 7 versions & 1 rubygems

Version Path
ohm-contrib-1.2 lib/ohm/timestamps.rb
ohm-contrib-1.1.0 lib/ohm/timestamps.rb
ohm-contrib-1.0.1 lib/ohm/timestamps.rb
ohm-contrib-1.0.0 lib/ohm/timestamps.rb
ohm-contrib-1.0.0.rc5 lib/ohm/timestamps.rb
ohm-contrib-1.0.0.rc4 lib/ohm/timestamps.rb
ohm-contrib-1.0.0.rc3 lib/ohm/timestamps.rb