Add domain_variants helper to DomainNormalizable concern (#38539)

This commit is contained in:
Matt Jankowski 2026-04-17 11:36:09 -04:00 committed by GitHub
parent 05a1c170c2
commit b15d234ccb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 9 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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