# frozen_string_literal: 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 belongs_to :organization, -> { readonly }, 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