Module: Koi::Model::Archivable

Extended by:
ActiveSupport::Concern
Included in:
Admin::User
Defined in:
app/models/concerns/koi/model/archivable.rb

Overview

Add support for archiving using an ‘:archived_at` column. Adds scopes for including/excluding collection elements based on whether they have an archived_at date set or not.

Usage: Include this module in your model and add the ‘archived_at` column via a migration.

Examples:

Model.all # default scope, excludes archived
Model.archived # only returns archived records
Model.with_archived # returns all records

Instance Method Summary collapse

Instance Method Details

#archiveObject

Mark a record as archived. It will no longer appear in default queries.



45
46
47
# File 'app/models/concerns/koi/model/archivable.rb', line 45

def archive
  self.archived_at = Time.current
end

#archive!Object

Archive a record immediately, without validation.



50
51
52
53
54
# File 'app/models/concerns/koi/model/archivable.rb', line 50

def archive!
  archive
  save!(validate: false) if persisted?
  self
end

#archivedObject

Returns true iff the record has been archived.



31
32
33
# File 'app/models/concerns/koi/model/archivable.rb', line 31

def archived
  archived_at.present?
end

#archived=(archived) ⇒ Object

Update archived status based on given boolean value.



36
37
38
39
40
41
42
# File 'app/models/concerns/koi/model/archivable.rb', line 36

def archived=(archived)
  if ActiveRecord::Type::Boolean.new.cast(archived)
    archive
  else
    restore
  end
end

#restoreObject

Mark a record as no longer archived. It will appear in default queries.



57
58
59
# File 'app/models/concerns/koi/model/archivable.rb', line 57

def restore
  self.archived_at = nil
end

#restore!Object

Restore a record immediately, without validation.



62
63
64
65
66
# File 'app/models/concerns/koi/model/archivable.rb', line 62

def restore!
  restore
  save!(validate: false) if persisted?
  self
end