email_domain_block.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: email_domain_blocks
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. # parent_id :bigint(8)
  11. #
  12. class EmailDomainBlock < ApplicationRecord
  13. include DomainNormalizable
  14. belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
  15. has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
  16. validates :domain, presence: true, uniqueness: true, domain: true
  17. def with_dns_records=(val)
  18. @with_dns_records = ActiveModel::Type::Boolean.new.cast(val)
  19. end
  20. def with_dns_records?
  21. @with_dns_records
  22. end
  23. alias with_dns_records with_dns_records?
  24. def self.block?(email)
  25. _, domain = email.split('@', 2)
  26. return true if domain.nil?
  27. begin
  28. domain = TagManager.instance.normalize_domain(domain)
  29. rescue Addressable::URI::InvalidURIError
  30. return true
  31. end
  32. where(domain: domain).exists?
  33. end
  34. end