From d4ed7e466c41f19e5f9352700c76e7ffc4d28119 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 12 Mar 2024 09:09:11 -0400 Subject: [PATCH] Extract `by_domain_length` scope in `DomainNormalizable` concern (#29517) --- app/models/concerns/domain_normalizable.rb | 12 ++++++++++++ app/models/domain_block.rb | 2 +- app/models/email_domain_block.rb | 2 +- app/models/preview_card_provider.rb | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/domain_normalizable.rb b/app/models/concerns/domain_normalizable.rb index 8e244c1d87c..76f91c5b640 100644 --- a/app/models/concerns/domain_normalizable.rb +++ b/app/models/concerns/domain_normalizable.rb @@ -5,6 +5,18 @@ module DomainNormalizable included do 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 private diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index e310918e9b4..b5d1f2e0796 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -70,7 +70,7 @@ class DomainBlock < ApplicationRecord segments = uri.normalized_host.split('.') 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 nil end diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index 40be59420ae..f3a86eae8f3 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -56,7 +56,7 @@ class EmailDomainBlock < ApplicationRecord end 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.any? end diff --git a/app/models/preview_card_provider.rb b/app/models/preview_card_provider.rb index 8ba24331bba..756707e3f1a 100644 --- a/app/models/preview_card_provider.rb +++ b/app/models/preview_card_provider.rb @@ -54,6 +54,6 @@ class PreviewCardProvider < ApplicationRecord def self.matching_domain(domain) 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