From fc8d2bc55ff4082de43f61259bc7113f6b467117 Mon Sep 17 00:00:00 2001 From: Michael Bleigh Date: Wed, 18 Jun 2008 13:39:26 -0500 Subject: [PATCH] SubdomainFu#current_subdomain will no longer recognize mirrors (will return nil instead). Included specs for this. --- lib/subdomain-fu.rb | 11 ++++++++++- spec/subdomain_fu_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/subdomain-fu.rb b/lib/subdomain-fu.rb index 8c8e0de..4b4acb2 100644 --- a/lib/subdomain-fu.rb +++ b/lib/subdomain-fu.rb @@ -70,6 +70,15 @@ def self.same_subdomain?(subdomain, host) result end + def self.current_subdomain(request) + subdomain = request.subdomains(SubdomainFu.tld_size).join(".") + if has_subdomain?(subdomain) + subdomain + else + nil + end + end + module Controller def self.included(controller) controller.helper_method(:current_subdomain) @@ -78,7 +87,7 @@ def self.included(controller) protected def current_subdomain - request.subdomains(SubdomainFu.tld_size).join(".") + SubdomainFu.current_subdomain(request) end end end \ No newline at end of file diff --git a/spec/subdomain_fu_spec.rb b/spec/subdomain_fu_spec.rb index 4989675..70c9ca8 100644 --- a/spec/subdomain_fu_spec.rb +++ b/spec/subdomain_fu_spec.rb @@ -98,6 +98,28 @@ end end + describe "#current_subdomain" do + it "should return the current subdomain if there is one" do + request = mock("request", :subdomains => ["awesome"]) + SubdomainFu.current_subdomain(request).should == "awesome" + end + + it "should return nil if there's no subdomain" do + request = mock("request", :subdomains => []) + SubdomainFu.current_subdomain(request).should be_nil + end + + it "should return nil if the current subdomain is a mirror" do + request = mock("request", :subdomains => ["www"]) + SubdomainFu.current_subdomain(request).should be_nil + end + + it "should return the whole thing (including a .) if there's multiple subdomains" do + request = mock("request", :subdomains => ["awesome","rad"]) + SubdomainFu.current_subdomain(request).should == "awesome.rad" + end + end + describe "#same_subdomain?" do it { SubdomainFu.same_subdomain?("www","www.localhost").should be_true } it { SubdomainFu.same_subdomain?("www","localhost").should be_true }