From 193ca979ce5d48f9b102257692ac75bc781e9291 Mon Sep 17 00:00:00 2001 From: kelvinst Date: Sat, 24 Sep 2016 18:14:56 -0300 Subject: [PATCH] Allowing to exclude subdomains by a regular expression --- lib/apartment/elevators/subdomain.rb | 12 +++++++++++- spec/unit/elevators/subdomain_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/apartment/elevators/subdomain.rb b/lib/apartment/elevators/subdomain.rb index b68aa55e..9e3ecc3e 100644 --- a/lib/apartment/elevators/subdomain.rb +++ b/lib/apartment/elevators/subdomain.rb @@ -14,12 +14,22 @@ def self.excluded_subdomains=(arg) @excluded_subdomains = arg end + def self.excluded_subdomain?(subdomain) + excluded_subdomains.any? do |excluded_subdomain| + if excluded_subdomain.is_a? Regexp + subdomain =~ excluded_subdomain + else + subdomain == excluded_subdomain + end + end + end + def parse_tenant_name(request) request_subdomain = subdomain(request.host) # If the domain acquired is set to be excluded, set the tenant to whatever is currently # next in line in the schema search path. - tenant = if self.class.excluded_subdomains.include?(request_subdomain) + tenant = if self.class.excluded_subdomain?(request_subdomain) nil else request_subdomain diff --git a/spec/unit/elevators/subdomain_spec.rb b/spec/unit/elevators/subdomain_spec.rb index b8655a2b..c3ef7026 100644 --- a/spec/unit/elevators/subdomain_spec.rb +++ b/spec/unit/elevators/subdomain_spec.rb @@ -53,4 +53,28 @@ described_class.excluded_subdomains = nil end end + + describe ".excluded_subdomain?" do + it "must ignore any item of the list" do + described_class.excluded_subdomains = %w{foo bar cereal} + + expect(described_class.excluded_subdomain?("foo")).to eq(true) + expect(described_class.excluded_subdomain?("bar")).to eq(true) + expect(described_class.excluded_subdomain?("cereal")).to eq(true) + expect(described_class.excluded_subdomain?("flakes")).to eq(false) + + described_class.excluded_subdomains = nil + end + + it "must ignore regexp's on the list that match" do + described_class.excluded_subdomains = [/foo/, /bar/, /cereal-\d+/] + + expect(described_class.excluded_subdomain?("food")).to eq(true) + expect(described_class.excluded_subdomain?("bareatric")).to eq(true) + expect(described_class.excluded_subdomain?("cereal")).to eq(false) + expect(described_class.excluded_subdomain?("cereal-1985")).to eq(true) + + described_class.excluded_subdomains = nil + end + end end