diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-11-05 16:36:21 -0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2018-11-05 16:36:21 -0800 |
commit | 874446cd72f9edac60deb8dcd91cf2f019b5347c (patch) | |
tree | 6f91b3f33f8e557e46b0986f1275a30506bc1832 | |
parent | 152a442b1902050265ddcef56f5506b3bfbb12e4 (diff) | |
download | rails-874446cd72f9edac60deb8dcd91cf2f019b5347c.tar.gz rails-874446cd72f9edac60deb8dcd91cf2f019b5347c.tar.bz2 rails-874446cd72f9edac60deb8dcd91cf2f019b5347c.zip |
Extract Mail-bound methods into mail_ext for future upstream work
-rw-r--r-- | app/models/action_mailbox/inbound_email.rb | 6 | ||||
-rw-r--r-- | app/models/action_mailbox/inbound_email/message_id.rb | 2 | ||||
-rw-r--r-- | lib/action_mailbox.rb | 1 | ||||
-rw-r--r-- | lib/action_mailbox/mail_ext.rb | 4 | ||||
-rw-r--r-- | lib/action_mailbox/mail_ext/from_source.rb | 5 | ||||
-rw-r--r-- | lib/action_mailbox/mail_ext/recipients.rb | 5 | ||||
-rw-r--r-- | lib/action_mailbox/router/route.rb | 8 |
7 files changed, 19 insertions, 12 deletions
diff --git a/app/models/action_mailbox/inbound_email.rb b/app/models/action_mailbox/inbound_email.rb index 7d1a36b705..ea564a254e 100644 --- a/app/models/action_mailbox/inbound_email.rb +++ b/app/models/action_mailbox/inbound_email.rb @@ -8,12 +8,8 @@ class ActionMailbox::InboundEmail < ActiveRecord::Base has_one_attached :raw_email enum status: %i[ pending processing delivered failed bounced ] - def self.mail_from_source(source) - Mail.new Mail::Utilities.binary_unsafe_to_crlf(source.to_s) - end - def mail - @mail ||= self.class.mail_from_source(source) + @mail ||= Mail.from_source(source) end def source diff --git a/app/models/action_mailbox/inbound_email/message_id.rb b/app/models/action_mailbox/inbound_email/message_id.rb index 5cfcadaba1..70d39d1e33 100644 --- a/app/models/action_mailbox/inbound_email/message_id.rb +++ b/app/models/action_mailbox/inbound_email/message_id.rb @@ -14,7 +14,7 @@ module ActionMailbox::InboundEmail::MessageId private def extract_message_id(source) - mail_from_source(source).message_id + Mail.from_source(source).message_id rescue => e # FIXME: Add logging with "Couldn't extract Message ID, so will generating a new random ID instead" end diff --git a/lib/action_mailbox.rb b/lib/action_mailbox.rb index fbc8122d9d..417a1a7431 100644 --- a/lib/action_mailbox.rb +++ b/lib/action_mailbox.rb @@ -1,4 +1,5 @@ require "action_mailbox/engine" +require "action_mailbox/mail_ext" module ActionMailbox extend ActiveSupport::Autoload diff --git a/lib/action_mailbox/mail_ext.rb b/lib/action_mailbox/mail_ext.rb new file mode 100644 index 0000000000..abdaae4f9e --- /dev/null +++ b/lib/action_mailbox/mail_ext.rb @@ -0,0 +1,4 @@ +require "mail" + +# The hope is to upstream most of these basic additions to the Mail gem's Mail object. But until then, here they lay! +Dir["#{File.expand_path(File.dirname(__FILE__))}/mail_ext/*"].each { |path| require "action_mailbox/mail_ext/#{File.basename(path)}" } diff --git a/lib/action_mailbox/mail_ext/from_source.rb b/lib/action_mailbox/mail_ext/from_source.rb new file mode 100644 index 0000000000..ddc3cbe385 --- /dev/null +++ b/lib/action_mailbox/mail_ext/from_source.rb @@ -0,0 +1,5 @@ +module Mail + def self.from_source(source) + Mail.new Mail::Utilities.binary_unsafe_to_crlf(source.to_s) + end +end diff --git a/lib/action_mailbox/mail_ext/recipients.rb b/lib/action_mailbox/mail_ext/recipients.rb new file mode 100644 index 0000000000..58561c7f01 --- /dev/null +++ b/lib/action_mailbox/mail_ext/recipients.rb @@ -0,0 +1,5 @@ +class Mail::Message + def recipients + Array(to) + Array(cc) + Array(bcc) + end +end diff --git a/lib/action_mailbox/router/route.rb b/lib/action_mailbox/router/route.rb index 34cc684381..24be8d4804 100644 --- a/lib/action_mailbox/router/route.rb +++ b/lib/action_mailbox/router/route.rb @@ -10,9 +10,9 @@ class ActionMailbox::Router::Route def match?(inbound_email) case address when String - recipients_from(inbound_email.mail).any? { |recipient| address.casecmp?(recipient) } + inbound_email.mail.recipients.any? { |recipient| address.casecmp?(recipient) } when Regexp - recipients_from(inbound_email.mail).any? { |recipient| address.match?(recipient) } + inbound_email.mail.recipients.any? { |recipient| address.match?(recipient) } when Proc address.call(inbound_email) else @@ -30,8 +30,4 @@ class ActionMailbox::Router::Route raise ArgumentError, "Expected a String, Regexp, Proc, or matchable, got #{address.inspect}" end end - - def recipients_from(mail) - Array(mail.to) + Array(mail.cc) + Array(mail.bcc) - end end |