Sha256: c6e7b309d2a581adce00d85579d166b520dc2a82580ad56ad4de67ce013d845b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module PostfixAdmin
  class Alias < ApplicationRecord
    self.table_name = :alias
    self.primary_key = :address

    validate on: :create do |a|
      domain = a.rel_domain
      if domain.aliases.zero? || a.mailbox
      elsif domain.rel_aliases.pure.count >= domain.aliases
        message = "already has the maximum number of aliases " \
                  "(maximum is #{domain.aliases} aliases)"
        a.errors.add(:domain, message)
      end
    end

    validates :address, presence: true, uniqueness: { case_sensitive: false },
                        format: { with: RE_EMAIL_LIKE_WITH_ANCHORS,
                                  message: "must be a valid email address" }
    validates :goto, presence: true

    belongs_to :rel_domain, class_name: "Domain", foreign_key: :domain
    belongs_to :mailbox, foreign_key: :address, optional: true

    scope :pure, -> { joins("LEFT OUTER JOIN mailbox ON alias.address = mailbox.username").where("mailbox.username" => nil) }

    attribute :local_part, :string

    before_validation do |a|
      unless a.address
        a.address = "#{a.local_part}@#{a.domain}" unless a.local_part.empty?
      end
    end

    def mailbox?
      !!mailbox
    end

    def pure_alias?
      !mailbox
    end

    def gotos
      goto.split(",")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postfix_admin-0.3.0 lib/postfix_admin/alias.rb