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, 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}
- @@tld_sizes =
DEFAULT_TLD_SIZES.dup
- @@mirrors =
%w(www)
- @@preferred_mirror =
nil
Class Method Summary collapse
-
.change_subdomain_of_host(subdomain, host) ⇒ Object
Changes the subdomain of the host to whatever is passed in.
- .current_subdomain(request) ⇒ Object
-
.has_subdomain?(subdomain) ⇒ Boolean
Is the current subdomain either nil or a mirror?.
- .host_without_subdomain(host) ⇒ Object
- .needs_rewrite?(subdomain, host) ⇒ Boolean
-
.rewrite_host_for_subdomains(subdomain, host) ⇒ Object
Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other).
-
.same_subdomain?(subdomain, host) ⇒ Boolean
Is this subdomain equivalent to the subdomain found in this host string?.
-
.subdomain_from(host) ⇒ Object
Gets the subdomain from the host based on the TLD size.
-
.tld_size ⇒ Object
Returns the TLD Size of the current environment.
-
.tld_size=(value) ⇒ Object
Sets the TLD Size of the current environment.
Class Method Details
.change_subdomain_of_host(subdomain, host) ⇒ Object
Changes the subdomain of the host to whatever is passed in.
64 65 66 67 68 |
# File 'lib/subdomain-fu.rb', line 64 def self.change_subdomain_of_host(subdomain, host) host = SubdomainFu.host_without_subdomain(host) host = "#{subdomain}.#{host}" if subdomain host end |
.current_subdomain(request) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/subdomain-fu.rb', line 83 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 a mirror?
33 34 35 |
# File 'lib/subdomain-fu.rb', line 33 def self.has_subdomain?(subdomain) !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain) end |
.host_without_subdomain(host) ⇒ Object
45 46 47 48 |
# File 'lib/subdomain-fu.rb', line 45 def self.host_without_subdomain(host) parts = host.split('.') parts[-(SubdomainFu.tld_size+1)..-1].join(".") end |
.needs_rewrite?(subdomain, host) ⇒ Boolean
77 78 79 80 81 |
# File 'lib/subdomain-fu.rb', line 77 def self.needs_rewrite?(subdomain, host) subdomain = nil if subdomain.blank? (!has_subdomain?(subdomain) && subdomain != SubdomainFu.preferred_mirror && SubdomainFu.preferred_mirror != nil) || !same_subdomain?(subdomain, host) end |
.rewrite_host_for_subdomains(subdomain, host) ⇒ Object
Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/subdomain-fu.rb', line 51 def self.rewrite_host_for_subdomains(subdomain, host) unless needs_rewrite?(subdomain, host) if has_subdomain?(subdomain) || (subdomain_from(host) == SubdomainFu.preferred_mirror) || (!has_subdomain?(subdomain) && SubdomainFu.preferred_mirror == nil) host else change_subdomain_of_host(SubdomainFu.preferred_mirror, host) end else change_subdomain_of_host(subdomain || SubdomainFu.preferred_mirror, host) end end |
.same_subdomain?(subdomain, host) ⇒ Boolean
Is this subdomain equivalent to the subdomain found in this host string?
71 72 73 74 75 |
# File 'lib/subdomain-fu.rb', line 71 def self.same_subdomain?(subdomain, host) subdomain = nil unless subdomain (subdomain == SubdomainFu.subdomain_from(host)) || (!SubdomainFu.has_subdomain?(subdomain) && !SubdomainFu.has_subdomain?(SubdomainFu.subdomain_from(host))) end |
.subdomain_from(host) ⇒ Object
Gets the subdomain from the host based on the TLD size
38 39 40 41 42 43 |
# File 'lib/subdomain-fu.rb', line 38 def self.subdomain_from(host) return nil unless host parts = host.split('.') sub = parts[0..-(SubdomainFu.tld_size+2)].join(".") sub.blank? ? nil : sub end |
.tld_size ⇒ Object
Returns the TLD Size of the current environment.
23 24 25 |
# File 'lib/subdomain-fu.rb', line 23 def self.tld_size tld_sizes[RAILS_ENV.to_sym] end |
.tld_size=(value) ⇒ Object
Sets the TLD Size of the current environment
28 29 30 |
# File 'lib/subdomain-fu.rb', line 28 def self.tld_size=(value) tld_sizes[RAILS_ENV.to_sym] = value end |