app/models/jobshop/mailman.rb in jobshop-0.0.163 vs app/models/jobshop/mailman.rb in jobshop-0.0.167

- old
+ new

@@ -1,11 +1,33 @@ # frozen_string_literals: true +# +Jobshop::Mailman+ +# +# A Mailman is responsible for handling incoming email and dispatching it to its +# approrpriate handler. If no handler is found, the email is rejected. module Jobshop class Mailman < ApplicationRecord self.primary_keys = %i[ organization_id address ] belongs_to :organization, inverse_of: :mailmen validates :address, presence: true + + class << self + def handler_for(message) + to = Mail::Address.new(message.to.first) + + mailbox = to.local.split("+").first + address = "#{mailbox}@#{to.domain}" + mailman = find_by(address: address) + + klass = if mailman + mailman.handler_type.constantize + else + Mailroom::NullHandler + end + + klass.new(message, mailman) + end + end end end