aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/action_mailbox/inbound_email/incineratable.rb
blob: 8a82b87a99d114b78a77d0ebe39cc9d6188a89b9 (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: :incinerating_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? || bounced? || failed?)
        @incinerating_later = true
      end
    end

    def incinerating_later?
      @incinerating_later ||= false
    end

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