Skip to content

Instantly share code, notes, and snippets.

@dwabnitz
Created November 28, 2009 19:39
Show Gist options
  • Save dwabnitz/244619 to your computer and use it in GitHub Desktop.
Save dwabnitz/244619 to your computer and use it in GitHub Desktop.
multisite environment à la doodlekit.com
# How-to set up a multi-site environment like the one in doodlekit.com
# every top-level object has a site_id
# The sites table holds all the information for each site, including the name, and what I call a 'handle'.
# The handle is used for many things, but mainly for the sub-domain.
# There is a separate field for the domain # name, so that we can support both the sub-domain, and a custom domain name at the same time.
# For every request a before_filter runs which looks something like this:
def load_site
if request.domain == DOODLEKIT_DOMAIN
sub_domain = request.subdomains.last || 'default'
@site = Site.find_by_handle(sub_domain)
else
@site = Site.find_by_domain(request.domain)
end
if @site.nil?
@site = Site.find_default
end
if session[:current_user] &&
session[:current_user].site_id != @site.id &&
!session[:current_user].is_system
session[:current_user] = nil
end
end
# Basically if the root domain name is 'doodlekit.com', then it takes the last sub-domain element and looks up that handle.
# Otherwise it looks it up by the domain name. If both of these fail, then just use the default site.
# The @site instance variable is then used to pull up features, menus, pages, layouts, etc.
# Whenever I decide to implement object caching, the Site object will definitely be the first to go in there.
# As far as DNS, all I have to do is setup everything as a CNAME to doodlekit.com. There's no Apache config or anything.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment