Websites/config/routes/domain_constraint.rb

36 lines
1.3 KiB
Ruby
Raw Normal View History

2024-05-03 03:04:43 +00:00
# frozen_string_literal: true
class DomainConstraint
attr_reader :domain, :subdomain
DEFAULT_DOMAIN = nil # "yiff.rest"
2024-05-03 03:04:43 +00:00
def initialize(domain, subdomain = nil)
@domain = domain
@subdomain = subdomain
end
def matches?(request)
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
subdomain_matches?(request) && domain_matches?(request)
2024-05-03 03:04:43 +00:00
end
private
def domain_matches?(request)
(Rails.env.development? && request.env["websites.dev_domain"] == @domain) || request.domain == @domain
end
2024-05-03 03:04:43 +00:00
def subdomain_matches?(request)
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