class Apartment::Elevators::Subdomain
Assumes that database name should match subdomain
Provides a rack based db switching solution based on subdomains
def self.excluded_subdomains
def self.excluded_subdomains @@excluded_subdomains ||= [] end
def self.excluded_subdomains=(arg)
def self.excluded_subdomains=(arg) @@excluded_subdomains = arg end
def named_host?(host)
def named_host?(host) !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) end
def parse_database_name(request)
def parse_database_name(request) request_subdomain = subdomain(request.host) # If the domain acquired is set to be exlcluded, set the database to whatever is currently # next in line in the schema search path. database = if self.class.excluded_subdomains.include?(request_subdomain) nil else request_subdomain end database.present? && database || nil end
def subdomain(host)
def subdomain(host) subdomains(host).first end
def subdomains(host, tld_length = 1)
def subdomains(host, tld_length = 1) return [] unless named_host?(host) host.split('.')[0..-(tld_length + 2)] end