Extract by_domain_length
scope in DomainNormalizable
concern (#29517)
This commit is contained in:
parent
5b60d4b696
commit
d4ed7e466c
4 changed files with 15 additions and 3 deletions
|
@ -5,6 +5,18 @@ module DomainNormalizable
|
||||||
|
|
||||||
included do
|
included do
|
||||||
before_validation :normalize_domain
|
before_validation :normalize_domain
|
||||||
|
|
||||||
|
scope :by_domain_length, -> { order(domain_char_length.desc) }
|
||||||
|
end
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def domain_char_length
|
||||||
|
Arel.sql(
|
||||||
|
<<~SQL.squish
|
||||||
|
CHAR_LENGTH(domain)
|
||||||
|
SQL
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -70,7 +70,7 @@ class DomainBlock < ApplicationRecord
|
||||||
segments = uri.normalized_host.split('.')
|
segments = uri.normalized_host.split('.')
|
||||||
variants = segments.map.with_index { |_, i| segments[i..].join('.') }
|
variants = segments.map.with_index { |_, i| segments[i..].join('.') }
|
||||||
|
|
||||||
where(domain: variants).order(Arel.sql('char_length(domain) desc')).first
|
where(domain: variants).by_domain_length.first
|
||||||
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
|
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,7 +56,7 @@ class EmailDomainBlock < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def blocking?(allow_with_approval: false)
|
def blocking?(allow_with_approval: false)
|
||||||
blocks = EmailDomainBlock.where(domain: domains_with_variants, allow_with_approval: allow_with_approval).order(Arel.sql('char_length(domain) desc'))
|
blocks = EmailDomainBlock.where(domain: domains_with_variants, allow_with_approval: allow_with_approval).by_domain_length
|
||||||
blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
|
blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
|
||||||
blocks.any?
|
blocks.any?
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,6 +54,6 @@ class PreviewCardProvider < ApplicationRecord
|
||||||
|
|
||||||
def self.matching_domain(domain)
|
def self.matching_domain(domain)
|
||||||
segments = domain.split('.')
|
segments = domain.split('.')
|
||||||
where(domain: segments.map.with_index { |_, i| segments[i..].join('.') }).order(Arel.sql('char_length(domain) desc')).first
|
where(domain: segments.map.with_index { |_, i| segments[i..].join('.') }).by_domain_length.first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue