aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2018-09-28 11:01:49 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2018-09-28 11:01:49 -0700
commit456d79b853aaec14f14c4c4deaa04cfb66c5b357 (patch)
tree89fb2d165eb28bd57b286f83cf3e7e89a9e9c37d /app
parentcfb927ce754199e93740e53aee4e6b40acb396c5 (diff)
downloadrails-456d79b853aaec14f14c4c4deaa04cfb66c5b357.tar.gz
rails-456d79b853aaec14f14c4c4deaa04cfb66c5b357.tar.bz2
rails-456d79b853aaec14f14c4c4deaa04cfb66c5b357.zip
Extract MessageId concern
Diffstat (limited to 'app')
-rw-r--r--app/controllers/action_mailroom/inbound_emails_controller.rb2
-rw-r--r--app/models/action_mailroom/inbound_email.rb26
-rw-r--r--app/models/action_mailroom/inbound_email/message_id.rb25
3 files changed, 29 insertions, 24 deletions
diff --git a/app/controllers/action_mailroom/inbound_emails_controller.rb b/app/controllers/action_mailroom/inbound_emails_controller.rb
index 0723bd39c3..57e0530ac6 100644
--- a/app/controllers/action_mailroom/inbound_emails_controller.rb
+++ b/app/controllers/action_mailroom/inbound_emails_controller.rb
@@ -6,7 +6,7 @@ class ActionMailroom::InboundEmailsController < ActionController::Base
before_action :require_rfc822_message, only: :create
def create
- ActionMailroom::InboundEmail.create_from_raw_email!(params[:message])
+ ActionMailroom::InboundEmail.create_and_extract_message_id!(params[:message])
head :created
end
diff --git a/app/models/action_mailroom/inbound_email.rb b/app/models/action_mailroom/inbound_email.rb
index 1f086b24bf..cf7370d543 100644
--- a/app/models/action_mailroom/inbound_email.rb
+++ b/app/models/action_mailroom/inbound_email.rb
@@ -3,28 +3,13 @@ require "mail"
class ActionMailroom::InboundEmail < ActiveRecord::Base
self.table_name = "action_mailroom_inbound_emails"
- include Incineratable, Routable
+ include Incineratable, MessageId, Routable
has_one_attached :raw_email
enum status: %i[ pending processing delivered failed bounced ]
- before_save :generate_missing_message_id
-
- class << self
- def create_from_raw_email!(raw_email, **options)
- create! raw_email: raw_email, message_id: extract_message_id(raw_email), **options
- end
-
- def mail_from_source(source)
- Mail.new(Mail::Utilities.binary_unsafe_to_crlf(source.to_s))
- end
-
- private
- def extract_message_id(raw_email)
- mail_from_source(raw_email.read).message_id
- rescue => e
- # FIXME: Add logging with "Couldn't extract Message ID, so will generating a new random ID instead"
- end
+ def self.mail_from_source(source)
+ Mail.new Mail::Utilities.binary_unsafe_to_crlf(source.to_s)
end
def mail
@@ -34,9 +19,4 @@ class ActionMailroom::InboundEmail < ActiveRecord::Base
def source
@source ||= raw_email.download
end
-
- private
- def generate_missing_message_id
- self.message_id ||= Mail::MessageIdField.new.message_id
- end
end
diff --git a/app/models/action_mailroom/inbound_email/message_id.rb b/app/models/action_mailroom/inbound_email/message_id.rb
new file mode 100644
index 0000000000..6bdda8c1c7
--- /dev/null
+++ b/app/models/action_mailroom/inbound_email/message_id.rb
@@ -0,0 +1,25 @@
+module ActionMailroom::InboundEmail::MessageId
+ extend ActiveSupport::Concern
+
+ included do
+ before_save :generate_missing_message_id
+ end
+
+ module ClassMethods
+ def create_and_extract_message_id!(raw_email, **options)
+ create! raw_email: raw_email, message_id: extract_message_id(raw_email), **options
+ end
+
+ private
+ def extract_message_id(raw_email)
+ mail_from_source(raw_email.read).message_id
+ rescue => e
+ # FIXME: Add logging with "Couldn't extract Message ID, so will generating a new random ID instead"
+ end
+ end
+
+ private
+ def generate_missing_message_id
+ self.message_id ||= Mail::MessageIdField.new.message_id
+ end
+end