Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created August 27, 2011 18:08
Show Gist options
  • Save scottwater/1175676 to your computer and use it in GitHub Desktop.
Save scottwater/1175676 to your computer and use it in GitHub Desktop.
Subdomains and Domains in Rails 3.0
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
constraints(Subdomain) do
get "/" => "public_sites#show_subdomain"
end
constraints(Domain) do
get "/" => "public_sites#show_domain"
end
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
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