2018-12-26 16:38:42 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DomainNormalizable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2020-06-01 11:47:20 +10:00
|
|
|
before_validation :normalize_domain
|
2024-03-13 00:09:11 +11:00
|
|
|
|
|
|
|
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
|
2018-12-26 16:38:42 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def normalize_domain
|
2019-02-03 13:59:51 +11:00
|
|
|
self.domain = TagManager.instance.normalize_domain(domain&.strip)
|
2022-11-14 18:07:14 +11:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
errors.add(:domain, :invalid)
|
2018-12-26 16:38:42 +11:00
|
|
|
end
|
|
|
|
end
|