You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
812 B

  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