Module: ColdStorage::Model

Defined in:
lib/cold_storage/model.rb

Overview

Extended onto ActiveRecord::Base, so every model can declare itself archivable.

Instance Method Summary collapse

Instance Method Details

#archivable(**options) ⇒ ColdStorage::Policy

Declares which rows of this model move to the archive database.

class Payroll < ApplicationRecord
archivable after: 18.months, every: 1.month
end

Options:

after:                Duration. Rows older than this are archivable.
                    (alias: older_than:)
on:                   Column the age is measured on.
                    Default: :created_at, or the deleted column when
                    deleted: true.
deleted:              true to archive soft-deleted rows only.
deleted_column:       Column holding the soft-delete timestamp.
                    Default: ColdStorage.config.deleted_column.
on_destroy:           true to copy a row to the archive whenever it is
                    really deleted (destroy/destroy_all), so hard
                    deletes are kept too. (alias: hard_delete:)
every:                Duration. Minimum time between two runs.
scope:                Symbol, proc or relation narrowing the selection.
cascade:              has_many/has_one association names to archive
                    together with their parent.
batch_size:           Rows moved per round trip.
delete_after_archive: false to copy without deleting the source rows.
delete_method:        :delete_all (default) or :destroy_all.

Returns:



34
35
36
37
38
39
40
41
# File 'lib/cold_storage/model.rb', line 34

def archivable(**options)
  include ColdStorage::Archivable unless include?(ColdStorage::Archivable)

  self.archiving_policy = Policy.new(self, **options)
  install_archive_on_destroy! if archiving_policy.on_destroy?
  Registry.register(self)
  archiving_policy
end

#archivable?Boolean

Every model answers this, so callers never have to rescue NoMethodError.

Returns:

  • (Boolean)


44
45
46
# File 'lib/cold_storage/model.rb', line 44

def archivable?
  false
end