balaur (owner)

Fork Of

gist: 45826 by robbyru... handle subdomain-based acco...

Revisions

  • 478419 Sun Jan 11 14:40:14 -0800 2009
gist: 58697 Download_button fork
public
Public Clone URL: git://gist.github.com/58697.git
lib/subdomain_accounts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# Inspired by
# http://dev.rubyonrails.org/svn/rails/plugins/account_location/lib/account_location.rb
#
module SubdomainAccounts
  def self.included( controller )
    controller.helper_method(:account_domain, :account_subdomain, :account_url, :current_account, :default_account_subdomain, :default_account_url)
  end
  
  protected
    
    # TODO: need to handle www as well
    def default_account_subdomain
      ''
    end
    
    def account_url( account_subdomain = default_account_subdomain, use_ssl = request.ssl? )
      http_protocol(use_ssl) + account_host(account_subdomain)
    end
    
    def account_host( subdomain )
      account_host = ''
      account_host << subdomain + '.'
      account_host << account_domain
    end
 
    def account_domain
      account_domain = ''
      account_domain << request.domain + request.port_string
    end
 
    def account_subdomain
      request.subdomains.first || ''
    end
    
    def default_account_url( use_ssl = request.ssl? )
      http_protocol(use_ssl) + account_domain
    end
          
    def current_account
      Account.find_by_subdomain(account_subdomain)
    end
    
    def http_protocol( use_ssl = request.ssl? )
      (use_ssl ? "https://" : "http://")
    end
end