Sha256: c0b76b5b6b2cb34cb714a8cbcc7ae0462f959a4d0bef6fb400e0cb34033b54b7

Contents?: true

Size: 985 Bytes

Versions: 16

Compression:

Stored size: 985 Bytes

Contents

module ActsAsArchive
  module Base
    module Restore

      def self.included(base)
        unless base.included_modules.include?(InstanceMethods)
          base.send :extend, ClassMethods
          base.send :include, InstanceMethods
        end
      end

      module ClassMethods
        
        def copy_from_archive(conditions)
          add_conditions!(where = '', conditions)
          col_names = column_names - [ 'deleted_at' ]
          col_names.map! { |col| connection.quote_column_name(col) }
          connection.execute(%{
            INSERT INTO #{table_name} (#{col_names.join(', ')})
              SELECT #{col_names.join(', ')}
              FROM archived_#{table_name}
              #{where}
          })
          connection.execute("DELETE FROM archived_#{table_name} #{where}")
        end
        
        def restore_all(conditions=nil)
          copy_from_archive(conditions)
        end
      end

      module InstanceMethods
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 6 rubygems

Version Path
brianjlandau-acts_as_archive-0.2.8 lib/acts_as_archive/base/restore.rb
brianjlandau-acts_as_archive-0.2.7 lib/acts_as_archive/base/restore.rb
brianjlandau-acts_as_archive-0.2.6 lib/acts_as_archive/base/restore.rb
micahwedemeyer-acts_as_archive-0.3.0 lib/acts_as_archive/base/restore.rb
sideshowbandana-acts_as_archive-0.2.7 lib/acts_as_archive/base/restore.rb
sideshowbandana-acts_as_archive-0.2.6 lib/acts_as_archive/base/restore.rb
foolabs-acts_as_archive-0.2.12 lib/acts_as_archive/base/restore.rb
gravis-acts_as_archive-0.2.11 lib/acts_as_archive/base/restore.rb
gravis-acts_as_archive-0.2.10 lib/acts_as_archive/base/restore.rb
gravis-acts_as_archive-0.2.7 lib/acts_as_archive/base/restore.rb
gravis-acts_as_archive-0.2.9 lib/acts_as_archive/base/restore.rb
gravis-acts_as_archive-0.2.8 lib/acts_as_archive/base/restore.rb
gravis-acts_as_archive-0.2.6 lib/acts_as_archive/base/restore.rb
acts_as_archive-0.2.5 lib/acts_as_archive/base/restore.rb
acts_as_archive-0.2.4 lib/acts_as_archive/base/restore.rb
acts_as_archive-0.2.3 lib/acts_as_archive/base/restore.rb