1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add incineration by default

This commit is contained in:
David Heinemeier Hansson 2018-09-19 15:20:04 -07:00
parent c6cae759ef
commit 3c8fc4e9ae
5 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,11 @@
class ActionMailroom::InboundEmail::IncinerationJob < ApplicationJob
queue_as :action_mailroom_incineration
def self.schedule(inbound_email)
set(wait: ActionMailroom::InboundEmail::Incineratable::INCINERATABLE_AFTER).perform_later(inbound_email)
end
def perform(inbound_email)
inbound_email.incinerate
end
end

View file

@ -1,6 +1,8 @@
require "mail"
class ActionMailroom::InboundEmail < ActiveRecord::Base
include Incineratable
self.table_name = "action_mailroom_inbound_emails"
has_one_attached :raw_email

View file

@ -0,0 +1,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

View file

@ -0,0 +1,18 @@
class ActionMailroom::InboundEmail::Incineratable::Incineration
def initialize(inbound_email)
@inbound_email = inbound_email
end
def run
@inbound_email.destroy if due? && processed?
end
private
def due?
@inbound_email.updated_at < ActionMailroom::InboundEmail::Incineratable::INCINERATABLE_AFTER.ago.end_of_day
end
def processed?
@inbound_email.delivered? || @inbound_email.failed?
end
end

View file

@ -0,0 +1,14 @@
require_relative '../../test_helper'
class ActionMailroom::InboundEmail::IncinerationTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
test "incinerate emails 30 days after they have been processed" do
freeze_time
assert_enqueued_with job: ActionMailroom::InboundEmail::IncinerationJob, at: 30.days.from_now do
inbound_email = create_inbound_email("welcome.eml")
inbound_email.delivered!
end
end
end