diff --git a/app/models/concerns/account/attribution_domains.rb b/app/models/concerns/account/attribution_domains.rb index 22bf27cc5d..ba5c092e6c 100644 --- a/app/models/concerns/account/attribution_domains.rb +++ b/app/models/concerns/account/attribution_domains.rb @@ -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 diff --git a/app/models/concerns/domain_normalizable.rb b/app/models/concerns/domain_normalizable.rb index 6571a40c54..2746280f0c 100644 --- a/app/models/concerns/domain_normalizable.rb +++ b/app/models/concerns/domain_normalizable.rb @@ -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 diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 74a494517a..72d2d78e41 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -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 diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index c4ab8b9ed2..c1b961517f 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -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 diff --git a/app/models/preview_card_provider.rb b/app/models/preview_card_provider.rb index 9d77d45e22..5c052de1e6 100644 --- a/app/models/preview_card_provider.rb +++ b/app/models/preview_card_provider.rb @@ -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