Sha256: f9a062a2f45eb6ee8f19d10883b8f3ac370cd78ea5a90331cf7637e30e074b5f

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "mongoid/timestamps/created/short"

module Mongoid
  module Timestamps
    # This module handles the behavior for setting up document created at
    # timestamp.
    module Created
      extend ActiveSupport::Concern

      included do
        include Mongoid::Timestamps::Timeless

        field :created_at, type: Time
        set_callback :create, :before, :set_created_at
      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
        if able_to_set_created_at?
          time = Time.configured.now
          self.updated_at = time if is_a?(Updated) && !updated_at_changed?
          self.created_at = time
        end
        clear_timeless_option
      end

      # Is the created timestamp able to be set?
      #
      # @return [ true, false ] If the timestamp can be set.
      def able_to_set_created_at?
        !frozen? && !timeless? && !created_at
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-8.0.10 lib/mongoid/timestamps/created.rb
mongoid-8.1.10 lib/mongoid/timestamps/created.rb
mongoid-8.1.9 lib/mongoid/timestamps/created.rb
mongoid-8.0.9 lib/mongoid/timestamps/created.rb