domain_allow.rb 812 B

12345678910111213141516171819202122232425262728293031323334
  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. include DomainMaterializable
  14. validates :domain, presence: true, uniqueness: true, domain: true
  15. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  16. class << self
  17. def allowed?(domain)
  18. !rule_for(domain).nil?
  19. end
  20. def rule_for(domain)
  21. return if domain.blank?
  22. uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') }
  23. find_by(domain: uri.normalized_host)
  24. end
  25. end
  26. end