Module: SiteScope

Defined in:
lib/site_scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/site_scope.rb', line 3

def self.included(base)
  base.send :helper_method, :current_site
end

Instance Method Details

#current_siteObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/site_scope.rb', line 7

def current_site
  return @current_site unless @current_site.nil?
  # For testing we won't have a request.host so we're going to use a class 
  # variable (VhostExtension.HOST) in those cases.
  host ||= VhostExtension.HOST || request.host
  # Remove the 'www.' from the site so we don't have to always include a www. 
  # in addition to the regular domain name.
  host.gsub!(/^www\./, '')
  @current_site ||= Hostname.find_by_domain(host).try(:site) || Hostname.find_by_domain('*').try(:site) || begin Hostname.find(:first, :conditions => ["hostname LIKE ?", "%#{host}%"]) rescue raise "No site found to match #{host}." unless @current_site end
  Thread.current[:current_site_id] = @current_site.id
  @current_site
end