Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/apartment/elevators/subdomain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/elevators/subdomain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use after:

after { 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