Sha256: 55ab6d05defe63b4f69283119d7f03a7861e3f2d38cd942e33d3dcea4c5faf21

Contents?: true

Size: 1.12 KB

Versions: 50

Compression:

Stored size: 1.12 KB

Contents

module MongoModel
  # MongoModel automatically timestamps create and update operations if the document has properties
  # named created_at/created_on or updated_at/updated_on.
  module Timestamps #:nodoc:
    extend ActiveSupport::Concern
    
    included do
      before_save   :set_update_timestamps
      before_create :set_create_timestamps
    end
    
    module ClassMethods
      # Defines timestamp properties created_at and updated_at.
      # When the document is created or updated, these properties will be respectively updated.
      def timestamps!
        property :created_at, Time
        property :updated_at, Time
      end
    end
  
  private
    def set_update_timestamps
      write_attribute(:updated_at, Time.now) if properties.include?(:updated_at)
      write_attribute(:updated_on, Time.now) if properties.include?(:updated_on)
    end
    
    def set_create_timestamps
      write_attribute(:created_at, Time.now) if properties.include?(:created_at) && !query_attribute(:created_at)
      write_attribute(:created_on, Time.now) if properties.include?(:created_on) && !query_attribute(:created_on)
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
mongomodel-0.5.5 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.5.4 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.5.3 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.5.2 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.5.1 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.5.0 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.9 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.8 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.7 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.6 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.5 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.4 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.3 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.2 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.1 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.4.0 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.3.6 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.3.5 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.3.4 lib/mongomodel/concerns/timestamps.rb
mongomodel-0.3.3 lib/mongomodel/concerns/timestamps.rb