Created
August 27, 2011 18:08
-
-
Save scottwater/1175676 to your computer and use it in GitHub Desktop.
Subdomains and Domains in Rails 3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Domain | |
def self.matches?(request) | |
# ROOT_DOMAIN is a configurable value so we don't catch skip www.kickofflabs.com | |
request.host.downcase != KickoffLabs::ROOT_DOMAIN | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constraints(Subdomain) do | |
get "/" => "public_sites#show_subdomain" | |
end | |
constraints(Domain) do | |
get "/" => "public_sites#show_domain" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'Custom Domains' do | |
context 'Marketing Site' do | |
it "should route #{KickoffLabs::ROOT_DOMAIN} to the home controller" do | |
{:get => "http://#{KickoffLabs::ROOT_DOMAIN}"}.should route_to(:controller => 'info', :action => 'home') | |
end | |
end | |
context 'Subdomains' do | |
it "should route sub.example.org to the public_sites#show" do | |
{:get => 'http://sub.example.org'}.should route_to(:controller => 'public_sites', :action => 'show_subdomain') | |
end | |
end | |
context 'Domains' do | |
it "should route customdomain.com to the public_sites#show_domain" do | |
{:get => 'http://customdomain.com'}.should route_to(:controller => 'public_sites', :action => 'show_domain') | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Subdomain | |
extend UrlHelper | |
def self.matches?(request) | |
# want to make sure we only catch our own (KickoffLabs domain) and ignore our own www. | |
request.domain.downcase == clean_root_domain && request.subdomain.present? && request.subdomain != "www" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment