2024-05-03 03:04:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DomainConstraint
|
|
|
|
attr_reader :domain, :subdomain
|
|
|
|
|
2024-05-03 22:51:05 +00:00
|
|
|
DEFAULT_DOMAIN = nil # "yiff.rest"
|
2024-05-03 03:04:43 +00:00
|
|
|
|
|
|
|
def initialize(domain, subdomain = nil)
|
|
|
|
@domain = domain
|
|
|
|
@subdomain = subdomain
|
|
|
|
end
|
|
|
|
|
2024-05-03 22:51:05 +00:00
|
|
|
def matches?(request)
|
2024-05-06 07:47:53 +00:00
|
|
|
if Rails.env.development?
|
|
|
|
domain = request.env["websites.dev_domain"].presence || request.domain
|
|
|
|
subdomains = request.env["websites.dev_subdomains"].presence || request.subdomains
|
|
|
|
Rails.logger.info("Domain: #{@domain}; Subdomain: #{@subdomain}; Current Host: #{domain}; Current Subdomains: #{subdomains}; Matches (Domain): #{domain_matches?(request)}; Matches (Subdomain): #{subdomain_matches?(request)}")
|
|
|
|
end
|
2024-05-03 22:51:05 +00:00
|
|
|
subdomain_matches?(request) && domain_matches?(request)
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
|
|
|
|
2024-05-03 22:51:05 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def domain_matches?(request)
|
2024-05-06 07:47:53 +00:00
|
|
|
(Rails.env.development? && request.env["websites.dev_domain"] == @domain) || request.domain == @domain
|
2024-05-03 22:51:05 +00:00
|
|
|
end
|
2024-05-03 03:04:43 +00:00
|
|
|
|
2024-05-03 22:51:05 +00:00
|
|
|
def subdomain_matches?(request)
|
2024-05-06 07:47:53 +00:00
|
|
|
if Rails.env.development? && !request.env["websites.dev_subdomains"].nil?
|
|
|
|
return true if @subdomain.nil? && request.env["websites.dev_subdomains"].blank?
|
|
|
|
return true if request.env["websites.dev_subdomains"] == @subdomain
|
|
|
|
end
|
2024-05-03 23:01:03 +00:00
|
|
|
(@subdomain.nil? && request.subdomain.blank?) || request.subdomain == @subdomain
|
2024-05-03 03:04:43 +00:00
|
|
|
end
|
|
|
|
end
|