Sha256: 8ebcdd7ceca94503381338699acd1c12f43dde67ff31c202968dc1932e0b3bd0

Contents?: true

Size: 729 Bytes

Versions: 1

Compression:

Stored size: 729 Bytes

Contents

module PoBox
  module Emailable
    extend ActiveSupport::Concern

    included do
      has_many :inboxes, as: :emailable, class_name: "PoBox::Inbox", dependent: :destroy

      after_create_commit :generate_inbox

      accepts_nested_attributes_for :inboxes, allow_destroy: true

      private

      def generate_inbox
        return if inboxes.any?

        inbox_address = if email.present?
          "#{email.split("@").first}##{SecureRandom.hex(2)}"
        else
          SecureRandom.hex(8)
        end

        inbox = inboxes.new
        loop do
          inbox.address = inbox_address
          break unless PoBox::Inbox.find_by(address: inbox.address).present?
        end

        save
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
po_box-0.1.2 app/models/concerns/po_box/emailable.rb