Created
November 25, 2012 11:35
-
-
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
This file contains hidden or 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
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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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 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