Sha256: 2a0e92317fbfe75253018e81a8ef1b8e3d2fd0a048293e21aed5209941a6bb2c

Contents?: true

Size: 1.22 KB

Versions: 27

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require "mongoid/timestamps/updated/short"

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

      included do
        include Mongoid::Timestamps::Timeless

        field :updated_at, type: Time
        set_callback :create, :before, :set_updated_at
        set_callback :update, :before, :set_updated_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
        if able_to_set_updated_at?
          self.updated_at = Time.now.utc unless updated_at_changed?
        end

        clear_timeless_option
      end

      # Is the updated timestamp able to be set?
      #
      # @example Can the timestamp be set?
      #   document.able_to_set_updated_at?
      #
      # @return [ true, false ] If the timestamp can be set.
      #
      # @since 2.4.0
      def able_to_set_updated_at?
        !frozen? && !timeless? && (new_record? || changed?)
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/timestamps/updated.rb
mongoid-7.3.4 lib/mongoid/timestamps/updated.rb
mongoid-7.1.11 lib/mongoid/timestamps/updated.rb
mongoid-7.2.6 lib/mongoid/timestamps/updated.rb
mongoid-7.3.3 lib/mongoid/timestamps/updated.rb
mongoid-7.3.2 lib/mongoid/timestamps/updated.rb
mongoid-7.2.5 lib/mongoid/timestamps/updated.rb
mongoid-7.1.10 lib/mongoid/timestamps/updated.rb
mongoid-7.1.9 lib/mongoid/timestamps/updated.rb
mongoid-7.2.4 lib/mongoid/timestamps/updated.rb
mongoid-7.3.1 lib/mongoid/timestamps/updated.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/timestamps/updated.rb
mongoid-7.3.0 lib/mongoid/timestamps/updated.rb
mongoid-7.2.3 lib/mongoid/timestamps/updated.rb
mongoid-7.1.8 lib/mongoid/timestamps/updated.rb
mongoid-7.2.2 lib/mongoid/timestamps/updated.rb
mongoid-7.2.1 lib/mongoid/timestamps/updated.rb
mongoid-7.1.7 lib/mongoid/timestamps/updated.rb
mongoid-7.2.0 lib/mongoid/timestamps/updated.rb
mongoid-7.1.6 lib/mongoid/timestamps/updated.rb