Module: SubdomainFu

Defined in:
lib/subdomain_fu/routing_extensions.rb,
lib/subdomain-fu.rb

Overview

Thanks to Jamis Buck for ideas on this stuff weblog.jamisbuck.org/2006/10/26/monkey-patching-rails-extending-routes-2 This is not yet a working part of SubdomainFu.

Defined Under Namespace

Modules: Controller, MapperExtensions, RouteExtensions, RouteSetExtensions

Constant Summary collapse

DEFAULT_TLD_SIZES =

The length of the period-split top-level domain for each environment. For example, “localhost” has a tld_size of zero, and “something.co.uk” has a tld_size of two.

To set a tld size for a given environment, just call SubdomainFu.tld_sizes = value

{:development => 0, :test => 0, :production => 1}
DEFAULT_MIRRORS =

Subdomains that are equivalent to going to the website with no subdomain at all. Defaults to “www” as the only member.

%w(www)
@@tld_sizes =
DEFAULT_TLD_SIZES.dup
@@mirrors =
DEFAULT_MIRRORS.dup
@@preferred_mirror =
nil
@@override_only_path =
false

Class Method Summary collapse

Class Method Details

.change_subdomain_of_host(subdomain, host) ⇒ Object

Changes the subdomain of the host to whatever is passed in.



84
85
86
87
88
# File 'lib/subdomain-fu.rb', line 84

def self.change_subdomain_of_host(subdomain, host)
  host = SubdomainFu.host_without_subdomain(host)
  host = "#{subdomain}.#{host}" if subdomain
  host
end

.crazy_rewrite_rule(subdomain, host) ⇒ Object

This is a black box of crazy! So I split some of the simpler logic out into the case statement above to make my brain happy!



126
127
128
129
# File 'lib/subdomain-fu.rb', line 126

def self.crazy_rewrite_rule(subdomain, host)
  (!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
    !same_subdomain?(subdomain, host)
end

.current_domain(request) ⇒ Object

Enables subdomain-fu to more completely replace DHH’s account_location plugin



141
142
143
144
145
# File 'lib/subdomain-fu.rb', line 141

def self.current_domain(request)
  domain = ""
  domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.length > 1
  domain << request.domain + request.port_string
end

.current_subdomain(request) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/subdomain-fu.rb', line 131

def self.current_subdomain(request)
  subdomain = request.subdomains(SubdomainFu.tld_size).join(".")
  if has_subdomain?(subdomain)
    subdomain
  else
    nil
  end
end

.has_subdomain?(subdomain) ⇒ Boolean

Is the current subdomain either nil or not a mirror?

Returns:

  • (Boolean)


37
38
39
# File 'lib/subdomain-fu.rb', line 37

def self.has_subdomain?(subdomain)
  subdomain != false && !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
end

.host_without_subdomain(host) ⇒ Object



64
65
66
67
# File 'lib/subdomain-fu.rb', line 64

def self.host_without_subdomain(host)
  parts = host.split('.')
  parts[-(SubdomainFu.tld_size+1)..-1].join(".")
end

.is_mirror?(subdomain) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/subdomain-fu.rb', line 41

def self.is_mirror?(subdomain)
  subdomain != false && !subdomain.blank? && SubdomainFu.mirrors.include?(subdomain)
end

.needs_rewrite?(subdomain, host) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/subdomain-fu.rb', line 101

def self.needs_rewrite?(subdomain, host)
  case subdomain
    when nil
      #rewrite when there is a preferred mirror set and there is no subdomain on the host
      return true if self.preferred_mirror && subdomain_from(host).nil?
      return false
    when false
      h = subdomain_from(host)
      #if the host has a subdomain
      if !h.nil?
        #rewrite when there is a subdomain in the host, and it is not a preferred mirror
        return true if !preferred_mirror?(h)
        #rewrite when there is a preferred mirror set and the subdomain of the host is not a mirror
        return true if self.preferred_mirror && !is_mirror?(h)
        #no rewrite if host already has mirror subdomain
        #it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
        return false if is_mirror?(h)
      end
      return self.crazy_rewrite_rule(subdomain, host)
    else
      return self.crazy_rewrite_rule(subdomain, host)
  end
end

.non_mirror_subdomain_from(host) ⇒ Object

Gets only non-mirror subdomains from the host based on the TLD size



59
60
61
62
# File 'lib/subdomain-fu.rb', line 59

def self.non_mirror_subdomain_from(host)
  sub = subdomain_from(host)
  has_subdomain?(sub) ? sub : nil
end

.override_only_path?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/subdomain-fu.rb', line 97

def self.override_only_path?
  self.override_only_path
end

.preferred_mirror?(subdomain) ⇒ Boolean

Is the subdomain a preferred mirror

Returns:

  • (Boolean)


46
47
48
# File 'lib/subdomain-fu.rb', line 46

def self.preferred_mirror?(subdomain)
  subdomain == SubdomainFu.preferred_mirror || SubdomainFu.preferred_mirror.nil?
end

.rewrite_host_for_subdomains(subdomain, host) ⇒ Object

Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/subdomain-fu.rb', line 70

def self.rewrite_host_for_subdomains(subdomain, host)
  if needs_rewrite?(subdomain, host)
    change_subdomain_of_host(subdomain || SubdomainFu.preferred_mirror, host)
  else
    if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host)) ||
        (subdomain.nil? && has_subdomain?(subdomain_from(host)))
      host
    else
      change_subdomain_of_host(SubdomainFu.preferred_mirror, host)
    end
  end
end

.same_subdomain?(subdomain, host) ⇒ Boolean

Is this subdomain equivalent to the subdomain found in this host string?

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/subdomain-fu.rb', line 91

def self.same_subdomain?(subdomain, host)
  subdomain = nil unless subdomain
  (subdomain == subdomain_from(host)) ||
    (!has_subdomain?(subdomain) && !has_subdomain?(subdomain_from(host)))
end

.subdomain_from(host) ⇒ Object

Gets the subdomain from the host based on the TLD size



51
52
53
54
55
56
# File 'lib/subdomain-fu.rb', line 51

def self.subdomain_from(host)
  return nil if host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)
  parts = host.split('.')
  sub = parts[0..-(SubdomainFu.tld_size+2)].join(".")
  sub.blank? ? nil : sub
end

.tld_sizeObject

Returns the TLD Size of the current environment.



27
28
29
# File 'lib/subdomain-fu.rb', line 27

def self.tld_size
  tld_sizes[RAILS_ENV.to_sym]
end

.tld_size=(value) ⇒ Object

Sets the TLD Size of the current environment



32
33
34
# File 'lib/subdomain-fu.rb', line 32

def self.tld_size=(value)
  tld_sizes[RAILS_ENV.to_sym] = value
end