aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/action_mailbox/inbound_email/incineratable.rb
blob: 6ba73c0c6d53523537b32dbe3a9a062066e99a4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module ActionMailbox::InboundEmail::Incineratable
  extend ActiveSupport::Concern

  # TODO: Extract into framework configuration
  INCINERATABLE_AFTER = 30.days

  included do
    before_update :remember_to_incinerate_later
    after_update_commit :incinerate_later, if: :need_to_incinerate_later?
  end

  def incinerate
    Incineration.new(self).run
  end

  private
    # TODO: Use enum change tracking once merged into Active Support
    def remember_to_incinerate_later
      if status_changed? && (delivered? || failed?)
        @incinerate_later = true
      end
    end

    def need_to_incinerate_later?
      @incinerate_later
    end

    def incinerate_later
      ActionMailbox::InboundEmail::IncinerationJob.schedule(self)
    end
end