Add domain_variants helper to DomainNormalizable concern (#38539)
This commit is contained in:
parent
05a1c170c2
commit
b15d234ccb
@ -12,8 +12,7 @@ module Account::AttributionDomains
|
||||
end
|
||||
|
||||
def can_be_attributed_from?(domain)
|
||||
segments = domain.split('.')
|
||||
variants = segments.map.with_index { |_, i| segments[i..].join('.') }.to_set
|
||||
variants = self.class.domain_variants(domain).to_set
|
||||
self[:attribution_domains].to_set.intersect?(variants)
|
||||
end
|
||||
end
|
||||
|
||||
@ -17,6 +17,11 @@ module DomainNormalizable
|
||||
SQL
|
||||
)
|
||||
end
|
||||
|
||||
def domain_variants(domain)
|
||||
segments = domain.to_s.split('.')
|
||||
Array.new(segments.size) { |i| segments[i..].join('.') }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@ -69,9 +69,7 @@ class DomainBlock < ApplicationRecord
|
||||
return if domain.blank?
|
||||
|
||||
uri = Addressable::URI.new.tap { |u| u.host = domain.strip.delete('/') }
|
||||
segments = uri.normalized_host.split('.')
|
||||
variants = segments.map.with_index { |_, i| segments[i..].join('.') }
|
||||
|
||||
variants = domain_variants(uri.normalized_host)
|
||||
where(domain: variants).by_domain_length.first
|
||||
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
|
||||
nil
|
||||
|
||||
@ -67,9 +67,7 @@ class EmailDomainBlock < ApplicationRecord
|
||||
@uris.flat_map do |uri|
|
||||
next if uri.nil?
|
||||
|
||||
segments = uri.normalized_host.split('.')
|
||||
|
||||
segments.map.with_index { |_, i| segments[i..].join('.') }
|
||||
self.class.module_parent.domain_variants(uri.normalized_host)
|
||||
end.uniq
|
||||
end
|
||||
|
||||
|
||||
@ -36,7 +36,6 @@ class PreviewCardProvider < ApplicationRecord
|
||||
scope :not_trendable, -> { where(trendable: false) }
|
||||
|
||||
def self.matching_domain(domain)
|
||||
segments = domain.split('.')
|
||||
where(domain: segments.map.with_index { |_, i| segments[i..].join('.') }).by_domain_length.first
|
||||
where(domain: domain_variants(domain)).by_domain_length.first
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user