domain_allow.rb 841 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 Paginable
  13. include DomainNormalizable
  14. include DomainMaterializable
  15. validates :domain, presence: true, uniqueness: true, domain: true
  16. def to_log_human_identifier
  17. domain
  18. end
  19. class << self
  20. def allowed?(domain)
  21. !rule_for(domain).nil?
  22. end
  23. def allowed_domains
  24. select(:domain)
  25. end
  26. def rule_for(domain)
  27. return if domain.blank?
  28. uri = Addressable::URI.new.tap { |u| u.host = domain.delete('/') }
  29. find_by(domain: uri.normalized_host)
  30. end
  31. end
  32. end