aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/action_mailroom/inbound_email/incineratable.rb
blob: 83ccec89ba471a7093613791ec30c98d12626cde (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 ActionMailroom::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
      ActionMailroom::InboundEmail::IncinerationJob.schedule(self)
    end
end