domain_allow.rb 767 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: domain_allows
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. #
  11. class DomainAllow < ApplicationRecord
  12. include DomainNormalizable
  13. validates :domain, presence: true, uniqueness: true
  14. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  15. class << self
  16. def allowed?(domain)
  17. !rule_for(domain).nil?
  18. end
  19. def rule_for(domain)
  20. return if domain.blank?
  21. uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') }
  22. find_by(domain: uri.normalized_host)
  23. end
  24. end
  25. end