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
- #archive(actor) ⇒ Object abstract
- #days_to_deletion ⇒ Object
- #delete_able? ⇒ Boolean
-
#display_archived_by ⇒ Object
abstract
String.
-
#display_restored_by ⇒ Object
abstract
String.
- #edit_able? ⇒ Boolean
- #restore(actor) ⇒ Object abstract
- #restore_able? ⇒ Boolean
- #retention_days ⇒ Object
- #unarchived? ⇒ Boolean
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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/app/models/concerns/archive_able.rb', line 68 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_deletion ⇒ Object
96 97 98 99 100 |
# File 'lib/app/models/concerns/archive_able.rb', line 96 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 |
#delete_able? ⇒ Boolean
62 63 64 |
# File 'lib/app/models/concerns/archive_able.rb', line 62 def delete_able? super && unarchived? end |
#display_archived_by ⇒ Object
This method is abstract.
Display who archived this object
Returns String.
32 33 34 35 36 37 38 39 40 |
# File 'lib/app/models/concerns/archive_able.rb', line 32 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_by ⇒ Object
This method is abstract.
Display who restored this object
Returns String.
44 45 46 47 48 49 50 51 52 |
# File 'lib/app/models/concerns/archive_able.rb', line 44 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 |
#edit_able? ⇒ Boolean
58 59 60 |
# File 'lib/app/models/concerns/archive_able.rb', line 58 def edit_able? super && unarchived? end |
#restore(actor) ⇒ Object
This method is abstract.
recycle the object
86 87 88 89 90 91 92 93 94 |
# File 'lib/app/models/concerns/archive_able.rb', line 86 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 |
#restore_able? ⇒ Boolean
26 27 28 |
# File 'lib/app/models/concerns/archive_able.rb', line 26 def restore_able? archived? end |
#retention_days ⇒ Object
102 103 104 |
# File 'lib/app/models/concerns/archive_able.rb', line 102 def retention_days 30 end |
#unarchived? ⇒ Boolean
54 55 56 |
# File 'lib/app/models/concerns/archive_able.rb', line 54 def unarchived? !archived? end |