Skip to content

Instantly share code, notes, and snippets.

@matenia
Created November 25, 2012 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matenia/4143196 to your computer and use it in GitHub Desktop.
Save matenia/4143196 to your computer and use it in GitHub Desktop.
Attempt at extracting subdomain mapping to a simple class for use with a whitelabel solution
require 'subdomain_mapper'
class ApplicationController < ActionController::Base
before_filter :check_whitelabel
def check_whitelabel
sd = SubdomainMapper.new(request.subdomain)
prepend_view_path(sd.whitelabel_view_path)
end
end
# scoped by env which makes dev easier
development: &base
# folder name which is located within app/whitelabels
subdomains:
shopping:
# mapped subdomains
values:
- shopping
- shopping.dev
test:
<<: *base
production:
<<: *base
class SubdomainMapper
# maps the requested subdomain to one in the config file
def initialize(key)
@subdomain = key
end
def reserved_subdomains
APP_CONFIG['subdomains']
end
def with_key
# we need to always return an array
reserved_subdomains.find { |k,v| v['values'].include?(subdomain_key) } || []
end
def whitelabel_view_path
"app/whitelabels/#{with_key.first}/views" if with_key.present?
end
def subdomain_key
@subdomain
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment