Sha256: 00a0c4d9865bfa81525e4d3e74a2950df472f2c33cca799c0905729920a323ce
Contents?: true
Size: 1.02 KB
Versions: 1
Compression:
Stored size: 1.02 KB
Contents
require 'active_support/concern' module Mongoid module Archivable extend ActiveSupport::Concern module Restoration # Restores the archived document to its former glory. def restore document = self.class.to_s.split('::').first # Turns User::Archive into User. self.class.const_get(document).create(attributes.except("_id", "original_id", "archived_at")) do |doc| doc.id = self.original_id end end end included do self.const_set("Archive", Class.new) self.const_get("Archive").class_eval do include Mongoid::Document include Mongoid::Attributes::Dynamic include Mongoid::Archivable::Restoration 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mongoid-archivable-1.1.0 | lib/mongoid/archivable.rb |