Module: ArchiveAble

Extended by:
ActiveSupport::Concern
Defined in:
lib/app/models/concerns/archive_able.rb

Overview

A module that allows for trash/recycling of an object it will be deleted in the app, but remain in the database until permanently deleted

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/app/models/concerns/archive_able.rb', line 10

def self.included(base)
  base.class_eval do
    # Fields
    field :archived, type: Mongoid::Boolean, default: false
    field :archived_at, type: Time
    field :archived_by_name, type: String
    field :archived_by_email, type: String
    field :restored_at, type: Time
    field :restored_by_name, type: String
    field :restored_by_email, type: String
    # Relationships
    belongs_to :archived_by, polymorphic: true, optional: true, inverse_of: nil
    belongs_to :restored_by, polymorphic: true, optional: true, inverse_of: nil
  end
end

Instance Method Details

#archive(actor) ⇒ Object

This method is abstract.

Archive the object

Parameters:

  • actor (Member|User)
    • the thing performing the action



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/app/models/concerns/archive_able.rb', line 56

def archive(actor)
  params = if actor.is_a? Cron::Job
             { archived: true,
               archived_at: Time.now.utc,
               archived_by_name: "System - #{actor.class.name}" }
           else
             { archived: true,
               archived_by: actor,
               archived_at: Time.now.utc,
               archived_by_name: actor.name,
               archived_by_email: actor.email }
           end
  params[:name] = "#{name}-(archived at #{Time.now.utc})" if respond_to?(:name=)
  update params
end

#days_to_deletionObject



84
85
86
87
88
# File 'lib/app/models/concerns/archive_able.rb', line 84

def days_to_deletion
  archived? ? ((archived_at + retention_days.days - Time.now.utc) / 86_400).to_i :  0
rescue StandardError
  archived? ? retention_days : 0
end

#display_archived_byObject

This method is abstract.

Display who archived this object

Returns String.

Returns:

  • String



28
29
30
31
32
33
34
35
36
# File 'lib/app/models/concerns/archive_able.rb', line 28

def display_archived_by
  if archived_by.present?
    archived_by.display_name
  elsif archived_by_email.present?
    "#{archived_by_name} (#{archived_by_email})"
  else
    archived_by_name
  end
end

#display_restored_byObject

This method is abstract.

Display who restored this object

Returns String.

Returns:

  • String



40
41
42
43
44
45
46
47
48
# File 'lib/app/models/concerns/archive_able.rb', line 40

def display_restored_by
  if restored_by.present?
    restored_by.display_name
  elsif restored_by_email.present?
    "#{restored_by_name} (#{restored_by_email})"
  else
    restored_by_name
  end
end

#restore(actor) ⇒ Object

This method is abstract.

recycle the object

Parameters:

  • actor (Member|User)
    • the actor performing the action



74
75
76
77
78
79
80
81
82
# File 'lib/app/models/concerns/archive_able.rb', line 74

def restore(actor)
  params = { archived: false,
             restored_by: actor,
             restored_at: Time.now.utc,
             restored_by_name: actor.name,
             restored_by_email: actor.email }
  params[:name] = name.split('-(archived at').first if respond_to?(:name=)
  update params
end

#retention_daysObject



90
91
92
# File 'lib/app/models/concerns/archive_able.rb', line 90

def retention_days
  30
end

#unarchived?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/app/models/concerns/archive_able.rb', line 50

def unarchived?
  !archived?
end