Last active
February 26, 2016 19:42
-
-
Save StephenRoos/65642685c5381058473f to your computer and use it in GitHub Desktop.
Subdomain Logic for TMJ
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 ApplicationController < ActionController::Base | |
before_filter :enforce_government | |
helper_method :current_government | |
def enforce_government | |
if current_user | |
# if the current user belongs to a government, then | |
# make sure that they are using that government's subdomain. | |
# otherwise, make sure they are using the default subdomain... | |
if current_user.government.present? | |
redirect_to_government! unless current_government == current_user.government | |
else | |
redirect_to_default! unless default_subdomain? | |
end | |
else | |
# if there is no logged-in user, we only need to make sure | |
# that the current_subdomain is valid. if not, redirect to | |
# the default domain... | |
redirect_to_default! unless valid_subdomain? | |
end | |
end | |
# redirects the user to their government's subdomain (if they have one) | |
def redirect_to_government! | |
redirect_to_subdomain!(current_user.government.subdomain) if current_user && current_user.government.present? | |
end | |
# redirects the user to the default subdomain ('www')... | |
def redirect_to_default! | |
redirect_to_subdomain!('www') | |
end | |
# redirects the user to the specified subdomain. this | |
# redirect will preserve incoming request parameters... | |
def redirect_to_subdomain!(subdomain) | |
host = request.host.gsub(current_subdomain, subdomain) | |
protocol = request.protocol | |
redirect_to url_for(params.merge(host: host, protocol: protocol)) | |
end | |
# return the subdomain of the current request. | |
# we have two different domain patterns, one for production, | |
# and one for all other environments: | |
# Production: (www|<gov subdomain>).careerarc.com (ex.: houston.careerarc.com) | |
# Other: (www|<gov subdomain>).<env>.careerarc.com (ex.: houston.staging.careerarc.com) | |
# so we need to look at a different part of the request subdomain | |
# depending on whether or not we're in production... | |
def current_subdomain | |
subdomain = Rails.env.production? ? request.subdomain : request.subdomain(2) | |
subdomain.to_s.downcase | |
end | |
# returns a Government model matching the current_subdomain if | |
# the current_subdomain is a valid government subdomain (otherwise nil)... | |
def current_government | |
@current_government ||= Government.find(subdomain: current_subdomain) if government_subdomain? | |
end | |
# returns TRUE if the current_subdomain is the default subdomain ('www') | |
def default_subdomain? | |
current_subdomain == 'www' | |
end | |
# returns TRUE if the current_subdomain is either the default subdomain ('www') | |
# or a valid government subdomain (i.e. 'houston')... | |
def valid_subdomain? | |
valid_subdomains.include? current_subdomain | |
end | |
# inverse of valid_subdomain? | |
def invalid_subdomain? | |
!valid_subdomain? | |
end | |
# returns the list of valid subdomains, including the default subdomain ('www'). | |
def valid_subdomains | |
@subdomains ||= begin | |
subdomains = government_subdomains | |
subdomains += %w(www cms api) | |
end | |
end | |
# returns TRUE if the current_subdomain is a government subdomain... | |
def government_subdomain? | |
government_subdomains.include? current_subdomain | |
end | |
# returns the list of valid government subdomains. this method will be called | |
# frequently, and it does a non-trivial database query, but the list of government | |
# subdomains does not change frequently, so we can cache it... | |
def government_subdomains | |
@government_subdomains ||= Rails.cache.fetch("cag.government_subdomains", expires_in: 30.minutes) do | |
Government.pluck(:subdomain).map(&:downcase) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment