Sha256: bc20085890fa8f24473a27bdbdef716a2e11d3c882aee8686b48b5c47c1a4259
Contents?: true
Size: 1.9 KB
Versions: 5
Compression:
Stored size: 1.9 KB
Contents
module ActsAsArchive module Base module Destroy def self.included(base) unless base.included_modules.include?(InstanceMethods) base.class_eval do alias_method :destroy_without_callbacks!, :destroy_without_callbacks class <<self alias_method :delete_all!, :delete_all end end base.send :extend, ClassMethods base.send :include, InstanceMethods end end module ClassMethods def copy_to_archive(conditions, import=false) add_conditions!(where = '', conditions) insert_cols = column_names.clone select_cols = column_names.clone if insert_cols.include?('deleted_at') unless import select_cols[select_cols.index('deleted_at')] = "'#{Time.now.utc.to_s(:db)}'" end else insert_cols << 'deleted_at' select_cols << "'#{Time.now.utc.to_s(:db)}'" end connection.execute(%{ INSERT INTO archived_#{table_name} (#{insert_cols.join(', ')}) SELECT #{select_cols.join(', ')} FROM #{table_name} #{where} }) connection.execute("DELETE FROM #{table_name} #{where}") end def delete_all(conditions=nil) copy_to_archive(conditions) end end module InstanceMethods def destroy_without_callbacks unless new_record? self.class.copy_to_archive("#{self.class.primary_key} = #{id}") end freeze end def destroy! transaction { destroy_with_callbacks! } end def destroy_with_callbacks! return false if callback(:before_destroy) == false result = destroy_without_callbacks! callback(:after_destroy) result end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems