Sha256: 989090c473d3a5b49f1969a806d0f247d505b6417f2a1e2ae49f08646ba952bb

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:

  # This module handles the behaviour for setting up document created at and
  # updated at timestamps.
  module Timestamps
    extend ActiveSupport::Concern

    included do
      field :created_at, :type => Time
      field :updated_at, :type => Time

      set_callback :create, :before, :set_created_at
      set_callback :save, :before, :set_updated_at

      class_inheritable_accessor :record_timestamps, :instance_writer => false
      self.record_timestamps = true
    end

    # Update the created_at field on the Document to the current time. This is
    # only called on create.
    #
    # @example Set the created at time.
    #   person.set_created_at
    def set_created_at
      self.created_at = Time.now.utc if !created_at
    end

    # Update the updated_at field on the Document to the current time.
    # This is only called on create and on save.
    #
    # @example Set the updated at time.
    #   person.set_updated_at
    def set_updated_at
      self.updated_at = Time.now.utc
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-2.0.0.rc.5 lib/mongoid/timestamps.rb
mongoid-2.0.0.rc.4 lib/mongoid/timestamps.rb
mongoid-2.0.0.rc.3 lib/mongoid/timestamps.rb
mongoid-2.0.0.rc.2 lib/mongoid/timestamps.rb
mongoid-2.0.0.rc.1 lib/mongoid/timestamps.rb