2017-10-04 16:16:10 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: email_domain_blocks
|
|
|
|
#
|
2017-11-12 17:18:50 +02:00
|
|
|
# id :bigint not null, primary key
|
2017-11-14 21:37:17 +02:00
|
|
|
# domain :string default(""), not null
|
2017-10-04 16:16:10 +03:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
|
|
|
class EmailDomainBlock < ApplicationRecord
|
2017-11-14 21:37:17 +02:00
|
|
|
before_validation :normalize_domain
|
|
|
|
|
|
|
|
validates :domain, presence: true, uniqueness: true
|
|
|
|
|
2017-10-04 16:16:10 +03:00
|
|
|
def self.block?(email)
|
2017-11-14 21:37:17 +02:00
|
|
|
_, domain = email.split('@', 2)
|
|
|
|
|
|
|
|
return true if domain.nil?
|
|
|
|
|
|
|
|
begin
|
|
|
|
domain = TagManager.instance.normalize_domain(domain)
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-10-04 16:16:10 +03:00
|
|
|
where(domain: domain).exists?
|
|
|
|
end
|
2017-11-14 21:37:17 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def normalize_domain
|
|
|
|
self.domain = TagManager.instance.normalize_domain(domain)
|
|
|
|
end
|
2017-10-04 16:16:10 +03:00
|
|
|
end
|