Sha256: e3b854c1f12da699981747cd8b96ed8c5ba82f382c2548e7387c84be1e85fb62

Contents?: true

Size: 636 Bytes

Versions: 2

Compression:

Stored size: 636 Bytes

Contents

require "mongoid/archivable/version"

module Mongoid
  module Archivable

    extend ActiveSupport::Concern

    included do
      self.const_set("Archive", Class.new)
      self.const_get("Archive").send(:include, ::Mongoid::Document).class_eval do
        include Mongoid::Attributes::Dynamic
        field :archived_at, type: Time
        field :original_id, type: String
      end

      before_destroy :archive
    end

    private

    def archive
      self.class.const_get("Archive").create(attributes.except("_id")) do |doc|
        doc.original_id = self.id
        doc.archived_at = Time.now.utc
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-archivable-1.0.0 lib/mongoid/archivable.rb
mongoid-archivable-0.1.0 lib/mongoid/archivable.rb